|
| 1 | +package resourcemerge |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + batchv1 "k8s.io/api/batch/v1" |
| 7 | + "k8s.io/apimachinery/pkg/api/equality" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + pointer "k8s.io/utils/pointer" |
| 10 | +) |
| 11 | + |
| 12 | +func TestEnsureJob(t *testing.T) { |
| 13 | + labelSelector := metav1.LabelSelector{} |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + existing batchv1.Job |
| 17 | + required batchv1.Job |
| 18 | + |
| 19 | + expectedModified bool |
| 20 | + expectedPanic bool |
| 21 | + expected batchv1.Job |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "different backofflimit count", |
| 25 | + existing: batchv1.Job{ |
| 26 | + Spec: batchv1.JobSpec{ |
| 27 | + BackoffLimit: pointer.Int32Ptr(2)}}, |
| 28 | + required: batchv1.Job{ |
| 29 | + Spec: batchv1.JobSpec{ |
| 30 | + BackoffLimit: pointer.Int32Ptr(3)}}, |
| 31 | + |
| 32 | + expectedModified: true, |
| 33 | + expected: batchv1.Job{ |
| 34 | + Spec: batchv1.JobSpec{ |
| 35 | + BackoffLimit: pointer.Int32Ptr(3)}}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "same backofflimit count", |
| 39 | + existing: batchv1.Job{ |
| 40 | + Spec: batchv1.JobSpec{ |
| 41 | + BackoffLimit: pointer.Int32Ptr(2)}}, |
| 42 | + required: batchv1.Job{ |
| 43 | + Spec: batchv1.JobSpec{ |
| 44 | + BackoffLimit: pointer.Int32Ptr(2)}}, |
| 45 | + |
| 46 | + expectedModified: false, |
| 47 | + expected: batchv1.Job{ |
| 48 | + Spec: batchv1.JobSpec{ |
| 49 | + BackoffLimit: pointer.Int32Ptr(2)}}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "required-Selector not nil", |
| 53 | + existing: batchv1.Job{ |
| 54 | + Spec: batchv1.JobSpec{ |
| 55 | + Selector: &labelSelector, |
| 56 | + ManualSelector: pointer.BoolPtr(false)}}, |
| 57 | + required: batchv1.Job{ |
| 58 | + Spec: batchv1.JobSpec{ |
| 59 | + Selector: &labelSelector, |
| 60 | + ManualSelector: pointer.BoolPtr(true)}}, |
| 61 | + |
| 62 | + expectedPanic: true, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, test := range tests { |
| 67 | + t.Run(test.name, func(t *testing.T) { |
| 68 | + defer func() { |
| 69 | + switch r := recover(); r { |
| 70 | + case nil: |
| 71 | + if test.expectedPanic { |
| 72 | + t.Errorf(test.name + " should have panicked!") |
| 73 | + } |
| 74 | + default: |
| 75 | + if !test.expectedPanic { |
| 76 | + panic(r) |
| 77 | + } |
| 78 | + } |
| 79 | + }() |
| 80 | + modified := pointer.BoolPtr(false) |
| 81 | + EnsureJob(modified, &test.existing, test.required) |
| 82 | + if *modified != test.expectedModified { |
| 83 | + t.Errorf("mismatch modified got: %v want: %v", *modified, test.expectedModified) |
| 84 | + } |
| 85 | + |
| 86 | + if !equality.Semantic.DeepEqual(test.existing, test.expected) { |
| 87 | + t.Errorf("mismatch Job got: %v want: %v", test.existing, test.expected) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
0 commit comments