Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve tests adding a scenario with a CronJob that doesn't need to b…
Browse files Browse the repository at this point in the history
…e changed
jadolg committed Feb 10, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
jadolg Jorge Alberto Díaz Orozco (Akiel)
1 parent 3736318 commit a05b999
Showing 2 changed files with 184 additions and 10 deletions.
42 changes: 32 additions & 10 deletions mutate_test.go
Original file line number Diff line number Diff line change
@@ -8,17 +8,39 @@ import (
"testing"
)

func TestMutateValidCronjob(t *testing.T) {
b, err := os.ReadFile("testdata/injectable_cronjob.json")
assert.NoError(t, err)
func TestMutate(t *testing.T) {
// Table tests
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))
})
}
}
152 changes: 152 additions & 0 deletions testdata/non_injectable_cronjob.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit a05b999

Please sign in to comment.