Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit ff446ec

Browse files
author
Chao Xu
committed
adding a test to make sure the ignore NotFound error patch is working
1 parent a3a6130 commit ff446ec

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

pkg/registry/pod/etcd/etcd_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strings"
2121
"testing"
2222

23+
etcd "github.com/coreos/etcd/client"
24+
"golang.org/x/net/context"
2325
"k8s.io/kubernetes/pkg/api"
2426
"k8s.io/kubernetes/pkg/api/errors"
2527
etcderrors "k8s.io/kubernetes/pkg/api/errors/etcd"
@@ -30,6 +32,7 @@ import (
3032
"k8s.io/kubernetes/pkg/registry/registrytest"
3133
"k8s.io/kubernetes/pkg/runtime"
3234
"k8s.io/kubernetes/pkg/securitycontext"
35+
"k8s.io/kubernetes/pkg/storage"
3336
"k8s.io/kubernetes/pkg/storage/etcd/etcdtest"
3437
etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
3538
"k8s.io/kubernetes/pkg/util"
@@ -130,6 +133,70 @@ func TestDelete(t *testing.T) {
130133
test.TestDeleteGraceful(scheduledPod, 30)
131134
}
132135

136+
type FailDeletionStorage struct {
137+
storage.Interface
138+
Called *bool
139+
}
140+
141+
func (f FailDeletionStorage) Delete(ctx context.Context, key string, out runtime.Object) error {
142+
*f.Called = true
143+
return etcd.Error{Code: etcd.ErrorCodeKeyNotFound}
144+
}
145+
146+
func newFailDeleteStorage(t *testing.T, called *bool) (*REST, *etcdtesting.EtcdTestServer) {
147+
etcdStorage, server := registrytest.NewEtcdStorage(t, "")
148+
failDeleteStorage := FailDeletionStorage{etcdStorage, called}
149+
restOptions := generic.RESTOptions{failDeleteStorage, generic.UndecoratedStorage, 3}
150+
storage := NewStorage(restOptions, nil, nil)
151+
return storage.Pod, server
152+
}
153+
154+
func TestIgnoreDeleteNotFound(t *testing.T) {
155+
pod := validNewPod()
156+
testContext := api.WithNamespace(api.NewContext(), api.NamespaceDefault)
157+
called := false
158+
registry, server := newFailDeleteStorage(t, &called)
159+
defer server.Terminate(t)
160+
161+
// should fail if pod A is not created yet.
162+
_, err := registry.Delete(testContext, pod.Name, nil)
163+
if !errors.IsNotFound(err) {
164+
t.Errorf("Unexpected error: %v", err)
165+
}
166+
167+
// create pod
168+
_, err = registry.Create(testContext, pod)
169+
if err != nil {
170+
t.Errorf("Unexpected error: %v", err)
171+
}
172+
173+
// delete object with grace period 0, storage will return NotFound, but the
174+
// registry shouldn't get any error since we ignore the NotFound error.
175+
zero := int64(0)
176+
opt := &api.DeleteOptions{GracePeriodSeconds: &zero}
177+
obj, err := registry.Delete(testContext, pod.Name, opt)
178+
if err != nil {
179+
t.Fatalf("Unexpected error: %v", err)
180+
}
181+
182+
if !called {
183+
t.Fatalf("expect the overriding Delete method to be called")
184+
}
185+
deletedPod, ok := obj.(*api.Pod)
186+
if !ok {
187+
t.Fatalf("expect a pod is returned")
188+
}
189+
if deletedPod.DeletionTimestamp == nil {
190+
t.Errorf("expect the DeletionTimestamp to be set")
191+
}
192+
if deletedPod.DeletionGracePeriodSeconds == nil {
193+
t.Fatalf("expect the DeletionGracePeriodSeconds to be set")
194+
}
195+
if *deletedPod.DeletionGracePeriodSeconds != 0 {
196+
t.Errorf("expect the DeletionGracePeriodSeconds to be 0, got %d", *deletedPod.DeletionTimestamp)
197+
}
198+
}
199+
133200
func TestCreateSetsFields(t *testing.T) {
134201
storage, _, _, server := newStorage(t)
135202
defer server.Terminate(t)

0 commit comments

Comments
 (0)