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

The webhook tests include boilerplate that is easy to eliminate.

In preparation for adding more tests, this commit adds helper functions.
This commit is contained in:
Oliver Gould 2024-12-12 15:39:44 -08:00 committed by GitHub
parent c16f08cd15
commit 61cc57db33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 104 deletions

View File

@ -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).
@ -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)
}
@ -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 := loadPatch(factory, t, "pod-with-debug.patch.json")
expectedPatch, err := unmarshalPatch(expectedPatchBytes)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
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)
@ -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).
@ -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)
@ -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
@ -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).
@ -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)
}
@ -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)
if err != nil {
return nil, err
}
return actualPatch, nil
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 {
t.Fatalf("Unexpected error: %s", err)
}
return b
}
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
}