diff --git a/controller/proxy-injector/webhook_test.go b/controller/proxy-injector/webhook_test.go index 1e192876f6911..ba7df40fe5b35 100644 --- a/controller/proxy-injector/webhook_test.go +++ b/controller/proxy-injector/webhook_test.go @@ -105,22 +105,11 @@ func TestGetPodPatch(t *testing.T) { }, } - expectedPatchBytes, err := factory.FileContents("pod.patch.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - expectedPatch, err := unmarshalPatch(expectedPatchBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - + _, expectedPatch := loadPatch(factory, t, "pod.patch.json") for _, testCase := range testCases { testCase := testCase // pin t.Run(testCase.filename, func(t *testing.T) { - pod, err := factory.FileContents(testCase.filename) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + pod := fileContents(factory, t, testCase.filename) fakeReq := getFakePodReq(pod) fullConf := testCase.conf. @@ -135,10 +124,7 @@ func TestGetPodPatch(t *testing.T) { if err != nil { t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err) } - actualPatch, err := unmarshalPatch(patchJSON) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + actualPatch := unmarshalPatch(t, patchJSON) if diff := deep.Equal(expectedPatch, actualPatch); diff != nil { t.Fatalf("The actual patch didn't match what was expected.\n%+v", diff) } @@ -147,17 +133,9 @@ func TestGetPodPatch(t *testing.T) { }) t.Run("by checking annotations with debug", func(t *testing.T) { - expectedPatchBytes, err := factory.FileContents("pod-with-debug.patch.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - - expectedPatch, err := unmarshalPatch(expectedPatchBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + _, expectedPatch := loadPatch(factory, t, "pod-with-debug.patch.json") - pod, err := factory.FileContents("pod-with-debug-enabled.yaml") + pod := fileContents(factory, t, "pod-with-debug-enabled.yaml") if err != nil { t.Fatalf("Unexpected error: %s", err) } @@ -172,29 +150,16 @@ func TestGetPodPatch(t *testing.T) { if err != nil { t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err) } - actualPatch, err := unmarshalPatch(patchJSON) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + actualPatch := unmarshalPatch(t, patchJSON) if diff := deep.Equal(expectedPatch, actualPatch); diff != nil { t.Fatalf("The actual patch didn't match what was expected.\n%+v", diff) } }) t.Run("by checking pod inherits config annotations from namespace", func(t *testing.T) { - expectedPatchBytes, err := factory.FileContents("pod-with-ns-annotations.patch.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - expectedPatch, err := unmarshalPatch(expectedPatchBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + _, expectedPatch := loadPatch(factory, t, "pod-with-ns-annotations.patch.json") - pod, err := factory.FileContents("pod-inject-enabled.yaml") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + pod := fileContents(factory, t, "pod-inject-enabled.yaml") fakeReq := getFakePodReq(pod) conf := confNsWithConfigAnnotations(). WithKind(fakeReq.Kind.Kind). @@ -211,20 +176,14 @@ func TestGetPodPatch(t *testing.T) { if err != nil { t.Fatalf("Unexpected PatchForAdmissionRequest error: %s", err) } - actualPatch, err := unmarshalPatch(patchJSON) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + actualPatch := unmarshalPatch(t, patchJSON) if diff := deep.Equal(expectedPatch, actualPatch); diff != nil { t.Fatalf("The actual patch didn't match what was expected.\n+%v", diff) } }) t.Run("by checking container spec", func(t *testing.T) { - deployment, err := factory.FileContents("deployment-with-injected-proxy.yaml") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + deployment := fileContents(factory, t, "deployment-with-injected-proxy.yaml") fakeReq := getFakePodReq(deployment) conf := confNsDisabled().WithKind(fakeReq.Kind.Kind) @@ -250,38 +209,10 @@ func TestGetAnnotationPatch(t *testing.T) { t.Fatalf("Unexpected error: %s", err) } t.Run("by checking patch annotations", func(t *testing.T) { - servicePatchBytes, err := factory.FileContents("annotation.patch.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - servicePatch, err := unmarshalPatch(servicePatchBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - podPatchBytes, err := factory.FileContents("annotation.patch.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - podPatch, err := unmarshalPatch(podPatchBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - filteredServiceBytes, err := factory.FileContents("filtered-service-opaque-ports.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - filteredServicePatch, err := unmarshalPatch(filteredServiceBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - filteredPodBytes, err := factory.FileContents("filtered-pod-opaque-ports.json") - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } - filteredPodPatch, err := unmarshalPatch(filteredPodBytes) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + servicePatchBytes, servicePatch := loadPatch(factory, t, "annotation.patch.json") + podPatchBytes, podPatch := loadPatch(factory, t, "annotation.patch.json") + filteredServiceBytes, filteredServicePatch := loadPatch(factory, t, "filtered-service-opaque-ports.json") + filteredPodBytes, filteredPodPatch := loadPatch(factory, t, "filtered-pod-opaque-ports.json") var testCases = []struct { name string filename string @@ -362,10 +293,7 @@ func TestGetAnnotationPatch(t *testing.T) { for _, testCase := range testCases { testCase := testCase // pin t.Run(testCase.name, func(t *testing.T) { - service, err := factory.FileContents(testCase.filename) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + service := fileContents(factory, t, testCase.filename) fakeReq := getFakeServiceReq(service) fullConf := testCase.conf. WithKind(fakeReq.Kind.Kind). @@ -386,10 +314,7 @@ func TestGetAnnotationPatch(t *testing.T) { if len(testCase.expectedPatchBytes) == 0 { return } - actualPatch, err := unmarshalPatch(patchJSON) - if err != nil { - t.Fatalf("Unexpected error: %s", err) - } + actualPatch := unmarshalPatch(t, patchJSON) if diff := deep.Equal(testCase.expectedPatch, actualPatch); diff != nil { t.Fatalf("The actual patch didn't match what was expected.\n%+v", diff) } @@ -420,12 +345,27 @@ func ownerRetrieverFake(p *corev1.Pod) (string, string, error) { return pkgK8s.Deployment, "owner-deployment", nil } -func unmarshalPatch(patchJSON []byte) (unmarshalledPatch, error) { - var actualPatch unmarshalledPatch - err := json.Unmarshal(patchJSON, &actualPatch) +func loadPatch(factory *fake.Factory, t *testing.T, name string) ([]byte, unmarshalledPatch) { + t.Helper() + bytes := fileContents(factory, t, name) + patch := unmarshalPatch(t, bytes) + return bytes, patch +} + +func fileContents(factory *fake.Factory, t *testing.T, name string) []byte { + t.Helper() + b, err := factory.FileContents(name) if err != nil { - return nil, err + t.Fatalf("Unexpected error: %s", err) } + return b +} - return actualPatch, nil +func unmarshalPatch(t *testing.T, patchJSON []byte) unmarshalledPatch { + t.Helper() + var actualPatch unmarshalledPatch + if err := json.Unmarshal(patchJSON, &actualPatch); err != nil { + t.Fatalf("Unexpected error: %s", err) + } + return actualPatch }