Skip to content

Commit

Permalink
chore(proxy-injector): reduce test boilerplate
Browse files Browse the repository at this point in the history
The webhook tests include boilerplate that is easy to eliminate.

In preparation for adding more tests, this commit adds helper functions.
  • Loading branch information
olix0r committed Dec 12, 2024
1 parent c16f08c commit 3452c4c
Showing 1 changed file with 36 additions and 96 deletions.
132 changes: 36 additions & 96 deletions controller/proxy-injector/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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).
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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).
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 3452c4c

Please sign in to comment.