Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(proxy-injector): reduce test boilerplate #13479

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 36 additions & 101 deletions controller/proxy-injector/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +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.
WithKind(fakeReq.Kind.Kind).
Expand All @@ -135,10 +123,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,20 +132,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")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
pod := fileContents(factory, t, "pod-with-debug-enabled.yaml")
fakeReq := getFakePodReq(pod)
conf := confNsEnabled().WithKind(fakeReq.Kind.Kind).WithOwnerRetriever(ownerRetrieverFake)
_, err = conf.ParseMetaAndYAML(fakeReq.Object.Raw)
Expand All @@ -172,29 +146,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,21 +172,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)
patchJSON, err := conf.GetPodPatch(true)
Expand All @@ -250,38 +204,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 +288,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 +309,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 +340,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
}
Loading