From 3d948b051e839b894a0769d666840988a306a21a Mon Sep 17 00:00:00 2001 From: "Jorge Alberto Diaz Orozco (Akiel)" Date: Sat, 10 Feb 2024 14:07:28 +0100 Subject: [PATCH] Improve tests adding a scenario with a CronJob that doesn't need to be changed --- mutate_test.go | 41 ++++++-- testdata/non_injectable_cronjob.json | 152 +++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 10 deletions(-) create mode 100644 testdata/non_injectable_cronjob.json diff --git a/mutate_test.go b/mutate_test.go index 1ed87c7..d7ff4d5 100644 --- a/mutate_test.go +++ b/mutate_test.go @@ -8,17 +8,38 @@ import ( "testing" ) -func TestMutateValidCronjob(t *testing.T) { - b, err := os.ReadFile("testdata/injectable_cronjob.json") - assert.NoError(t, err) +func TestMutate(t *testing.T) { + tests := []struct { + name string + file string + expected string + }{ + { + name: "When the cronjob needs to be injected then the patch should be the annotation", + file: "testdata/injectable_cronjob.json", + expected: `[{"op":"add","path":"/spec/jobTemplate/spec/template/metadata/annotations","value":{"linkerd.io/inject":"disabled"}}]`, + }, + { + name: "When the cronjob does not need to be injected then the patch should be empty", + file: "testdata/non_injectable_cronjob.json", + expected: "[{}]", + }, + } - response, err := Mutate(b) - assert.NoError(t, err) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b, err := os.ReadFile(tt.file) + assert.NoError(t, err) - r := v1beta1.AdmissionReview{} - err = json.Unmarshal(response, &r) - assert.NoError(t, err, "failed to unmarshal with error %s", err) + response, err := Mutate(b) + assert.NoError(t, err) - rr := r.Response - assert.Equal(t, `[{"op":"add","path":"/spec/jobTemplate/spec/template/metadata/annotations","value":{"linkerd.io/inject":"disabled"}}]`, string(rr.Patch)) + r := v1beta1.AdmissionReview{} + err = json.Unmarshal(response, &r) + assert.NoError(t, err, "failed to unmarshal with error %s", err) + + rr := r.Response + assert.Equal(t, tt.expected, string(rr.Patch)) + }) + } } diff --git a/testdata/non_injectable_cronjob.json b/testdata/non_injectable_cronjob.json new file mode 100644 index 0000000..9f53737 --- /dev/null +++ b/testdata/non_injectable_cronjob.json @@ -0,0 +1,152 @@ +{ + "kind": "AdmissionReview", + "apiVersion": "admission.k8s.io/v1", + "request": { + "uid": "34dc2238-8365-4dab-8807-7685596da8a5", + "kind": { + "group": "batch", + "version": "v1", + "kind": "CronJob" + }, + "resource": { + "group": "batch", + "version": "v1", + "resource": "cronjobs" + }, + "requestKind": { + "group": "batch", + "version": "v1", + "kind": "CronJob" + }, + "requestResource": { + "group": "batch", + "version": "v1", + "resource": "cronjobs" + }, + "name": "testjob", + "namespace": "antivax", + "operation": "CREATE", + "userInfo": { + "username": "jorge.diaz", + "uid": "jorge.diaz", + "groups": [ + "admin", + "system:authenticated" + ] + }, + "object": { + "kind": "CronJob", + "apiVersion": "batch/v1", + "metadata": { + "name": "testjob", + "namespace": "antivax", + "creationTimestamp": null, + "annotations": { + "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"batch/v1\",\"kind\":\"CronJob\",\"metadata\":{\"annotations\":{},\"name\":\"testjob\",\"namespace\":\"antivax\"},\"spec\":{\"jobTemplate\":{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"command\":[\"/bin/sh\",\"-c\",\"date; echo Hello!\"],\"image\":\"busybox:latest\",\"imagePullPolicy\":\"IfNotPresent\",\"name\":\"testjob\"}],\"restartPolicy\":\"OnFailure\"}}}},\"schedule\":\"* * * * *\"}}\n" + }, + "managedFields": [ + { + "manager": "kubectl-client-side-apply", + "operation": "Update", + "apiVersion": "batch/v1", + "time": "2024-02-09T12:24:24Z", + "fieldsType": "FieldsV1", + "fieldsV1": { + "f:metadata": { + "f:annotations": { + ".": {}, + "f:kubectl.kubernetes.io/last-applied-configuration": {} + } + }, + "f:spec": { + "f:concurrencyPolicy": {}, + "f:failedJobsHistoryLimit": {}, + "f:jobTemplate": { + "f:spec": { + "f:template": { + "f:spec": { + "f:containers": { + "k:{\"name\":\"testjob\"}": { + ".": {}, + "f:command": {}, + "f:image": {}, + "f:imagePullPolicy": {}, + "f:name": {}, + "f:resources": {}, + "f:terminationMessagePath": {}, + "f:terminationMessagePolicy": {} + } + }, + "f:dnsPolicy": {}, + "f:restartPolicy": {}, + "f:schedulerName": {}, + "f:securityContext": {}, + "f:terminationGracePeriodSeconds": {} + } + } + } + }, + "f:schedule": {}, + "f:successfulJobsHistoryLimit": {}, + "f:suspend": {} + } + } + } + ] + }, + "spec": { + "schedule": "* * * * *", + "concurrencyPolicy": "Allow", + "suspend": false, + "jobTemplate": { + "metadata": { + "creationTimestamp": null, + "annotations": { + "linkerd.io/inject": "enabled" + } + }, + "spec": { + "template": { + "metadata": { + "creationTimestamp": null + }, + "spec": { + "containers": [ + { + "name": "testjob", + "image": "busybox:latest", + "command": [ + "/bin/sh", + "-c", + "date; echo Hello!" + ], + "resources": {}, + "terminationMessagePath": "/dev/termination-log", + "terminationMessagePolicy": "File", + "imagePullPolicy": "IfNotPresent" + } + ], + "restartPolicy": "OnFailure", + "terminationGracePeriodSeconds": 30, + "dnsPolicy": "ClusterFirst", + "securityContext": {}, + "schedulerName": "default-scheduler" + } + } + } + }, + "successfulJobsHistoryLimit": 3, + "failedJobsHistoryLimit": 1 + }, + "status": {} + }, + "oldObject": null, + "dryRun": false, + "options": { + "kind": "CreateOptions", + "apiVersion": "meta.k8s.io/v1", + "fieldManager": "kubectl-client-side-apply", + "fieldValidation": "Strict" + } + } +}