|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 17 | +) |
| 18 | + |
| 19 | +func testObj() runtime.Object { |
| 20 | + obj := &unstructured.Unstructured{} |
| 21 | + obj.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ConfigMap"}) |
| 22 | + obj.SetName("cm") |
| 23 | + obj.SetNamespace("default") |
| 24 | + return obj |
| 25 | +} |
| 26 | + |
| 27 | +func TestWaitForDelete_AlreadyGone(t *testing.T) { |
| 28 | + cl := fake.NewClientBuilder().Build() |
| 29 | + rc := &RetryClient{Client: cl} |
| 30 | + |
| 31 | + err := WaitForDelete(rc, []runtime.Object{testObj()}) |
| 32 | + require.NoError(t, err) |
| 33 | +} |
| 34 | + |
| 35 | +func TestWaitForDelete_TransientErrorThenGone(t *testing.T) { |
| 36 | + callCount := 0 |
| 37 | + cl := fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{ |
| 38 | + Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 39 | + callCount++ |
| 40 | + if callCount <= 3 { |
| 41 | + return fmt.Errorf("transient API error") |
| 42 | + } |
| 43 | + return k8serrors.NewNotFound(schema.GroupResource{Resource: "configmaps"}, "cm") |
| 44 | + }, |
| 45 | + }).Build() |
| 46 | + rc := &RetryClient{Client: cl} |
| 47 | + |
| 48 | + err := WaitForDelete(rc, []runtime.Object{testObj()}) |
| 49 | + require.NoError(t, err) |
| 50 | + assert.Greater(t, callCount, 3) |
| 51 | +} |
| 52 | + |
| 53 | +func TestWaitForDelete_StillExistsThenGone(t *testing.T) { |
| 54 | + callCount := 0 |
| 55 | + cl := fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{ |
| 56 | + Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 57 | + callCount++ |
| 58 | + if callCount <= 2 { |
| 59 | + return nil |
| 60 | + } |
| 61 | + return k8serrors.NewNotFound(schema.GroupResource{Resource: "configmaps"}, "cm") |
| 62 | + }, |
| 63 | + }).Build() |
| 64 | + rc := &RetryClient{Client: cl} |
| 65 | + |
| 66 | + err := WaitForDelete(rc, []runtime.Object{testObj()}) |
| 67 | + require.NoError(t, err) |
| 68 | + assert.Greater(t, callCount, 2) |
| 69 | +} |
| 70 | + |
| 71 | +func TestWaitForDelete_PersistentErrorTimesOut(t *testing.T) { |
| 72 | + cl := fake.NewClientBuilder().WithInterceptorFuncs(interceptor.Funcs{ |
| 73 | + Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 74 | + return fmt.Errorf("persistent API error") |
| 75 | + }, |
| 76 | + }).Build() |
| 77 | + rc := &RetryClient{Client: cl} |
| 78 | + |
| 79 | + err := WaitForDelete(rc, []runtime.Object{testObj()}) |
| 80 | + assert.Error(t, err) |
| 81 | + assert.ErrorIs(t, err, context.DeadlineExceeded) |
| 82 | +} |
0 commit comments