|
| 1 | +package webhooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + "k8s.io/utils/ptr" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + |
| 12 | + "github.com/appuio/appuio-cloud-agent/skipper" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_PodRunOnceActiveDeadlineSecondsMutator_Handle(t *testing.T) { |
| 16 | + const overrideAnnotation = "appuio.io/active-deadline-seconds-override" |
| 17 | + const defaultActiveDeadlineSeconds = 60 |
| 18 | + |
| 19 | + testCases := []struct { |
| 20 | + name string |
| 21 | + |
| 22 | + subject client.Object |
| 23 | + additionalObjects []client.Object |
| 24 | + |
| 25 | + allowed bool |
| 26 | + expectedActiveDeadlineSeconds int |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "pod with restartPolicy=Always", |
| 30 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 31 | + RestartPolicy: corev1.RestartPolicyAlways, |
| 32 | + }), |
| 33 | + additionalObjects: []client.Object{ |
| 34 | + newNamespace("testns", nil, nil), |
| 35 | + }, |
| 36 | + allowed: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "pod with restartPolicy=OnFailure", |
| 40 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 41 | + RestartPolicy: corev1.RestartPolicyOnFailure, |
| 42 | + }), |
| 43 | + additionalObjects: []client.Object{ |
| 44 | + newNamespace("testns", nil, nil), |
| 45 | + }, |
| 46 | + allowed: true, |
| 47 | + expectedActiveDeadlineSeconds: defaultActiveDeadlineSeconds, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "pod with restartPolicy=Never", |
| 51 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 52 | + RestartPolicy: corev1.RestartPolicyNever, |
| 53 | + }), |
| 54 | + additionalObjects: []client.Object{ |
| 55 | + newNamespace("testns", nil, nil), |
| 56 | + }, |
| 57 | + allowed: true, |
| 58 | + expectedActiveDeadlineSeconds: defaultActiveDeadlineSeconds, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "pod in namespace with override annotation", |
| 62 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 63 | + RestartPolicy: corev1.RestartPolicyNever, |
| 64 | + }), |
| 65 | + additionalObjects: []client.Object{ |
| 66 | + newNamespace("testns", nil, map[string]string{ |
| 67 | + overrideAnnotation: "30", |
| 68 | + }), |
| 69 | + }, |
| 70 | + allowed: true, |
| 71 | + expectedActiveDeadlineSeconds: 30, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "pod with existing activeDeadlineSeconds", |
| 75 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 76 | + RestartPolicy: corev1.RestartPolicyNever, |
| 77 | + ActiveDeadlineSeconds: ptr.To(int64(77)), |
| 78 | + }), |
| 79 | + additionalObjects: []client.Object{ |
| 80 | + newNamespace("testns", nil, nil), |
| 81 | + }, |
| 82 | + allowed: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "pod in namespace with invalid override annotation", |
| 86 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 87 | + RestartPolicy: corev1.RestartPolicyNever, |
| 88 | + }), |
| 89 | + additionalObjects: []client.Object{ |
| 90 | + newNamespace("testns", nil, map[string]string{ |
| 91 | + overrideAnnotation: "invalid", |
| 92 | + }), |
| 93 | + }, |
| 94 | + allowed: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "non-existing namespace", |
| 98 | + subject: newPodWithSpec("testns", "pod1", corev1.PodSpec{ |
| 99 | + RestartPolicy: corev1.RestartPolicyNever, |
| 100 | + }), |
| 101 | + allowed: false, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range testCases { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + c, scheme, decoder := prepareClient(t, tc.additionalObjects...) |
| 110 | + |
| 111 | + subject := PodRunOnceActiveDeadlineSecondsMutator{ |
| 112 | + Decoder: decoder, |
| 113 | + Client: c, |
| 114 | + Skipper: skipper.StaticSkipper{}, |
| 115 | + |
| 116 | + OverrideAnnotation: overrideAnnotation, |
| 117 | + DefaultActiveDeadlineSeconds: defaultActiveDeadlineSeconds, |
| 118 | + } |
| 119 | + |
| 120 | + resp := subject.Handle(context.Background(), admissionRequestForObject(t, tc.subject, scheme)) |
| 121 | + t.Log("Response:", resp.Result.Reason, resp.Result.Message) |
| 122 | + require.Equal(t, tc.allowed, resp.Allowed) |
| 123 | + |
| 124 | + if tc.expectedActiveDeadlineSeconds == 0 { |
| 125 | + require.Len(t, resp.Patches, 0) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + require.Len(t, resp.Patches, 1) |
| 130 | + require.Equal(t, tc.expectedActiveDeadlineSeconds, resp.Patches[0].Value) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments