|
| 1 | +package specifieddelete |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 12 | + |
| 13 | + appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1" |
| 14 | +) |
| 15 | + |
| 16 | +func TestPatchPodSpecifiedDelete(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + pod *corev1.Pod |
| 19 | + keepPVC bool |
| 20 | + expected *corev1.Pod |
| 21 | + }{ |
| 22 | + { |
| 23 | + pod: &corev1.Pod{ |
| 24 | + ObjectMeta: metav1.ObjectMeta{ |
| 25 | + Name: "test-pod", |
| 26 | + }, |
| 27 | + Spec: corev1.PodSpec{}, |
| 28 | + }, |
| 29 | + keepPVC: false, |
| 30 | + expected: &corev1.Pod{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Name: "test-pod", |
| 33 | + Labels: map[string]string{ |
| 34 | + appsv1alpha1.SpecifiedDeleteKey: "true", |
| 35 | + }, |
| 36 | + }, |
| 37 | + Spec: corev1.PodSpec{}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + pod: &corev1.Pod{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: "test-pod", |
| 44 | + }, |
| 45 | + Spec: corev1.PodSpec{}, |
| 46 | + }, |
| 47 | + keepPVC: true, |
| 48 | + expected: &corev1.Pod{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: "test-pod", |
| 51 | + Labels: map[string]string{ |
| 52 | + appsv1alpha1.SpecifiedDeleteKey: "true", |
| 53 | + appsv1alpha1.KeepPVCForDeletionKey: "true", |
| 54 | + }, |
| 55 | + }, |
| 56 | + Spec: corev1.PodSpec{}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for i, test := range tests { |
| 62 | + t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { |
| 63 | + cli := fake.NewClientBuilder().WithObjects(test.pod).Build() |
| 64 | + _, err := PatchPodSpecifiedDelete(cli, test.pod, test.keepPVC) |
| 65 | + assert.NoError(t, err) |
| 66 | + |
| 67 | + // patch will write result object into the given test.pod |
| 68 | + if apiequality.Semantic.DeepEqual(test.expected, test.pod) { |
| 69 | + t.Fatalf("expected %v but got %v", test.expected, test.pod) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments