|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/go-logr/logr" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/apimachinery/pkg/util/uuid" |
| 16 | + "k8s.io/utils/ptr" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 23 | +) |
| 24 | + |
| 25 | +func TestCheckAndHandleResourceDeletion(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + ctx := context.Background() |
| 29 | + |
| 30 | + // Create a logger (for tests, we use logr.Discard() to avoid actual logging) |
| 31 | + logger := logr.Discard() |
| 32 | + |
| 33 | + // Table-driven test cases |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + deletionTimestamp *time.Time |
| 37 | + finalizerPresent bool |
| 38 | + expectFinalizer bool |
| 39 | + expectFinish bool |
| 40 | + expectError bool |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "Object not being deleted, finalizer not present", |
| 44 | + deletionTimestamp: nil, |
| 45 | + finalizerPresent: false, |
| 46 | + expectFinalizer: true, |
| 47 | + expectFinish: false, |
| 48 | + expectError: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Object not being deleted, finalizer already present", |
| 52 | + deletionTimestamp: nil, |
| 53 | + finalizerPresent: true, |
| 54 | + expectFinalizer: true, |
| 55 | + expectFinish: false, |
| 56 | + expectError: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Object being deleted, finalizer present", |
| 60 | + deletionTimestamp: ptr.To(time.Now()), |
| 61 | + finalizerPresent: true, |
| 62 | + expectFinalizer: false, |
| 63 | + expectFinish: true, |
| 64 | + expectError: false, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range tests { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + // Create a fake object |
| 71 | + object := newTestObject(t) |
| 72 | + |
| 73 | + // Set DeletionTimestamp if the test requires it |
| 74 | + if tc.deletionTimestamp != nil { |
| 75 | + object.SetDeletionTimestamp(&metav1.Time{Time: *tc.deletionTimestamp}) |
| 76 | + } |
| 77 | + |
| 78 | + // Add or remove finalizer based on the test case |
| 79 | + finalizer := "test.finalizer" |
| 80 | + if tc.finalizerPresent { |
| 81 | + controllerutil.AddFinalizer(object, finalizer) |
| 82 | + } |
| 83 | + |
| 84 | + // Create a fake client |
| 85 | + fakeClient := fake.NewClientBuilder().WithObjects(object).Build() |
| 86 | + |
| 87 | + cleanupFunc := func(ctx context.Context) error { |
| 88 | + if tc.deletionTimestamp == nil { |
| 89 | + t.Fatalf("cleanup function should not be called") |
| 90 | + } |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + // Call the function under test |
| 95 | + finish, err := CheckAndHandleObjectDeletion(ctx, fakeClient, object, finalizer, cleanupFunc, logger) |
| 96 | + |
| 97 | + // Check for errors |
| 98 | + if (err != nil) != tc.expectError { |
| 99 | + t.Errorf("Expected error: %v, got: %v", tc.expectError, err) |
| 100 | + } |
| 101 | + |
| 102 | + // Check if reconciliation should finish |
| 103 | + if finish != tc.expectFinish { |
| 104 | + t.Errorf("Expected finish: %v, got: %v", tc.expectFinish, finish) |
| 105 | + } |
| 106 | + |
| 107 | + // Check if finalizer was added/removed as expected |
| 108 | + hasFinalizer := controllerutil.ContainsFinalizer(object, finalizer) |
| 109 | + if hasFinalizer != tc.expectFinalizer { |
| 110 | + t.Errorf("Expected finalizer: %v, got: %v", tc.expectFinalizer, hasFinalizer) |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestGetObjectFromCache(t *testing.T) { |
| 117 | + t.Parallel() |
| 118 | + |
| 119 | + ctx := context.Background() |
| 120 | + |
| 121 | + // No-op logger for testing |
| 122 | + logger := logr.Discard() |
| 123 | + |
| 124 | + // Test cases |
| 125 | + tests := []struct { |
| 126 | + name string |
| 127 | + objectExists bool |
| 128 | + expectMiss bool |
| 129 | + expectError bool |
| 130 | + returnError error |
| 131 | + }{ |
| 132 | + { |
| 133 | + name: "Object exists in cache", |
| 134 | + objectExists: true, |
| 135 | + returnError: nil, |
| 136 | + expectMiss: false, |
| 137 | + expectError: false, |
| 138 | + }, |
| 139 | + { |
| 140 | + name: "Object not found in cache", |
| 141 | + objectExists: false, |
| 142 | + returnError: k8serrors.NewNotFound(newTestGroupResource(t), "test-resource"), |
| 143 | + expectMiss: true, |
| 144 | + expectError: false, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "Error while fetching from cache", |
| 148 | + objectExists: false, |
| 149 | + returnError: errors.New("some network error"), |
| 150 | + expectMiss: true, |
| 151 | + expectError: true, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + for _, tc := range tests { |
| 156 | + t.Run(tc.name, func(t *testing.T) { |
| 157 | + // Create a fake client with the expected error behavior |
| 158 | + clientBuilder := fake.NewClientBuilder() |
| 159 | + |
| 160 | + // Create a fake object |
| 161 | + object := newTestObject(t) |
| 162 | + |
| 163 | + if tc.objectExists { |
| 164 | + clientBuilder.WithObjects(object) |
| 165 | + } |
| 166 | + |
| 167 | + if tc.expectError { |
| 168 | + clientBuilder.WithInterceptorFuncs(interceptor.Funcs{ |
| 169 | + Get: func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 170 | + return tc.returnError |
| 171 | + }, |
| 172 | + }) |
| 173 | + } |
| 174 | + |
| 175 | + fakeClient := clientBuilder.Build() |
| 176 | + |
| 177 | + // Call the function under test |
| 178 | + namespacedName := types.NamespacedName{Name: "test-resource", Namespace: "default"} |
| 179 | + miss, err := GetObject(ctx, fakeClient, object, namespacedName, logger) |
| 180 | + if tc.expectError { |
| 181 | + assert.ErrorIs(t, err, tc.returnError) |
| 182 | + } else { |
| 183 | + assert.NoError(t, err) |
| 184 | + } |
| 185 | + assert.Equal(t, miss, tc.expectMiss) |
| 186 | + }) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// newTestObject returns a test unstructured.Unstructured object only to be used in tests. |
| 191 | +func newTestObject(t *testing.T) *unstructured.Unstructured { |
| 192 | + object := &unstructured.Unstructured{} |
| 193 | + object.SetNamespace("default") |
| 194 | + object.SetName("test-resource") |
| 195 | + object.SetUID(uuid.NewUUID()) |
| 196 | + object.SetGroupVersionKind(newTestGroupVersionKind(t)) |
| 197 | + return object |
| 198 | +} |
| 199 | + |
| 200 | +// newTestGroupVersionKind returns a test schema.GroupVersionKind only to be used in tests. |
| 201 | +func newTestGroupVersionKind(t *testing.T) schema.GroupVersionKind { |
| 202 | + return schema.GroupVersionKind{ |
| 203 | + Group: "test.group", |
| 204 | + Version: "v1", |
| 205 | + Kind: "TestKind", |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +// newTestGroupResource returns a test schema.GroupResource only to be used in tests. |
| 210 | +func newTestGroupResource(t *testing.T) schema.GroupResource { |
| 211 | + return schema.GroupResource{ |
| 212 | + Group: "test.group", |
| 213 | + Resource: "test-resource", |
| 214 | + } |
| 215 | +} |
0 commit comments