|
1 | 1 | package controller |
2 | 2 |
|
3 | 3 | import ( |
4 | | - . "github.com/onsi/ginkgo/v2" |
5 | | - . "github.com/onsi/gomega" |
6 | | - "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 4 | + "reflect" |
| 5 | + "testing" |
7 | 6 |
|
8 | 7 | corev1 "k8s.io/api/core/v1" |
| 8 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
9 | 9 | ) |
10 | 10 |
|
11 | | -var _ = Describe("Helper Functions Test", func() { |
12 | | - |
13 | | - Context("identifyFinalizers", func() { |
| 11 | +func TestIdentifyFinalizers(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + inputFinalizers []string |
| 15 | + expectedForeignFinalizers []string |
| 16 | + expectedOwnFinalizer bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "no finalizers on the object at all", |
| 20 | + inputFinalizers: []string{}, |
| 21 | + expectedForeignFinalizers: []string{}, |
| 22 | + expectedOwnFinalizer: false, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "only the own finalizer on the object", |
| 26 | + inputFinalizers: []string{Finalizer}, |
| 27 | + expectedForeignFinalizers: []string{}, |
| 28 | + expectedOwnFinalizer: true, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "only other finalizers on the object", |
| 32 | + inputFinalizers: []string{"other/finalizer1", "other/finalizer2"}, |
| 33 | + expectedForeignFinalizers: []string{"other/finalizer1", "other/finalizer2"}, |
| 34 | + expectedOwnFinalizer: false, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "both own and other finalizers on the object", |
| 38 | + inputFinalizers: []string{"other/finalizer1", Finalizer, "other/finalizer2"}, |
| 39 | + expectedForeignFinalizers: []string{"other/finalizer1", "other/finalizer2"}, |
| 40 | + expectedOwnFinalizer: true, |
| 41 | + }, |
| 42 | + } |
14 | 43 |
|
15 | | - It("should work correctly with no finalizers on the object at all", func() { |
| 44 | + for _, tt := range tests { |
| 45 | + t.Run(tt.name, func(t *testing.T) { |
16 | 46 | obj := &corev1.Namespace{} |
17 | | - ff, own := identifyFinalizers(obj) |
18 | | - Expect(ff).To(BeEmpty()) |
19 | | - Expect(own).To(BeFalse()) |
20 | | - }) |
21 | 47 |
|
22 | | - It("should work correctly with only the own finalizer on the object", func() { |
23 | | - obj := &corev1.Namespace{} |
24 | | - controllerutil.AddFinalizer(obj, Finalizer) |
25 | | - ff, own := identifyFinalizers(obj) |
26 | | - Expect(ff).To(BeEmpty()) |
27 | | - Expect(own).To(BeTrue()) |
28 | | - }) |
| 48 | + // Add input finalizers to the objeXx |
| 49 | + for _, finalizer := range tt.inputFinalizers { |
| 50 | + controllerutil.AddFinalizer(obj, finalizer) |
| 51 | + } |
29 | 52 |
|
30 | | - It("should work correctly with only other finalizers on the object", func() { |
31 | | - obj := &corev1.Namespace{} |
32 | | - controllerutil.AddFinalizer(obj, "other/finalizer1") |
33 | | - controllerutil.AddFinalizer(obj, "other/finalizer2") |
34 | | - ff, own := identifyFinalizers(obj) |
35 | | - Expect(ff).To(ConsistOf("other/finalizer1", "other/finalizer2")) |
36 | | - Expect(own).To(BeFalse()) |
37 | | - }) |
| 53 | + foreignFinalizers, ownFinalizer := identifyFinalizers(obj) |
38 | 54 |
|
39 | | - It("should work correctly with both own and other finalizers on the object", func() { |
40 | | - obj := &corev1.Namespace{} |
41 | | - controllerutil.AddFinalizer(obj, "other/finalizer1") |
42 | | - controllerutil.AddFinalizer(obj, Finalizer) |
43 | | - controllerutil.AddFinalizer(obj, "other/finalizer2") |
44 | | - ff, own := identifyFinalizers(obj) |
45 | | - Expect(ff).To(ConsistOf("other/finalizer1", "other/finalizer2")) |
46 | | - Expect(own).To(BeTrue()) |
| 55 | + // Assert the results |
| 56 | + if !reflect.DeepEqual(foreignFinalizers, tt.expectedForeignFinalizers) { |
| 57 | + t.Errorf("identifyFinalizers() foreignFinalizers = %v, want %v", foreignFinalizers, tt.expectedForeignFinalizers) |
| 58 | + } |
| 59 | + if ownFinalizer != tt.expectedOwnFinalizer { |
| 60 | + t.Errorf("identifyFinalizers() ownFinalizer = %v, want %v", ownFinalizer, tt.expectedOwnFinalizer) |
| 61 | + } |
47 | 62 | }) |
48 | | - |
49 | | - }) |
50 | | - |
51 | | -}) |
| 63 | + } |
| 64 | +} |
0 commit comments