-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Rodriguez
committed
May 20, 2024
1 parent
5bf7e01
commit 6dbbb7d
Showing
5 changed files
with
219 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package hooks | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/defenseunicorns/zarf/src/config" | ||
"github.com/defenseunicorns/zarf/src/internal/agent/http/admission" | ||
"github.com/defenseunicorns/zarf/src/internal/agent/operations" | ||
"github.com/defenseunicorns/zarf/src/types" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// createPodAdmissionRequest creates an admission request for a pod. | ||
func createPodAdmissionRequest(t *testing.T, op v1.Operation, pod *corev1.Pod) *v1.AdmissionRequest { | ||
t.Helper() | ||
raw, err := json.Marshal(pod) | ||
require.NoError(t, err) | ||
return &v1.AdmissionRequest{ | ||
Operation: op, | ||
Object: runtime.RawExtension{ | ||
Raw: raw, | ||
}, | ||
} | ||
} | ||
|
||
// TestPodMutationWebhook tests the pod mutation webhook. | ||
func TestPodMutationWebhook(t *testing.T) { | ||
t.Parallel() | ||
|
||
handler := admission.NewHandler().Serve(NewPodMutationHook(&types.ZarfState{ | ||
RegistryInfo: types.RegistryInfo{ | ||
Address: "127.0.0.1:31999", | ||
}, | ||
})) | ||
|
||
tests := []struct { | ||
name string | ||
admissionReq *v1.AdmissionRequest | ||
expectedPatch []operations.PatchOperation | ||
}{ | ||
{ | ||
name: "pod with label should be mutated", | ||
admissionReq: createPodAdmissionRequest(t, v1.Create, &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{"should-be": "mutated"}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{Image: "nginx"}}, | ||
InitContainers: []corev1.Container{{Image: "busybox"}}, | ||
EphemeralContainers: []corev1.EphemeralContainer{ | ||
{ | ||
EphemeralContainerCommon: corev1.EphemeralContainerCommon{ | ||
Image: "alpine", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
expectedPatch: []operations.PatchOperation{ | ||
operations.ReplacePatchOperation( | ||
"/spec/imagePullSecrets", | ||
[]corev1.LocalObjectReference{{Name: config.ZarfImagePullSecretName}}, | ||
), | ||
operations.ReplacePatchOperation( | ||
"/spec/initContainers/0/image", | ||
"127.0.0.1:31999/library/busybox:latest-zarf-2140033595", | ||
), | ||
operations.ReplacePatchOperation( | ||
"/spec/ephemeralContainers/0/image", | ||
"127.0.0.1:31999/library/alpine:latest-zarf-1117969859", | ||
), | ||
operations.ReplacePatchOperation( | ||
"/spec/containers/0/image", | ||
"127.0.0.1:31999/library/nginx:latest-zarf-3793515731", | ||
), | ||
operations.ReplacePatchOperation( | ||
"/metadata/labels/zarf-agent", | ||
"patched", | ||
), | ||
}, | ||
}, | ||
{ | ||
name: "pod with zarf-agent patched label", | ||
admissionReq: createPodAdmissionRequest(t, v1.Create, &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{"zarf-agent": "patched"}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{Image: "nginx"}}, | ||
}, | ||
}), | ||
expectedPatch: nil, | ||
}, | ||
{ | ||
name: "pod with no labels", | ||
admissionReq: createPodAdmissionRequest(t, v1.Create, &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: nil, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{{Image: "nginx"}}, | ||
}, | ||
}), | ||
expectedPatch: []operations.PatchOperation{ | ||
operations.ReplacePatchOperation( | ||
"/spec/imagePullSecrets", | ||
[]corev1.LocalObjectReference{{Name: config.ZarfImagePullSecretName}}, | ||
), | ||
operations.ReplacePatchOperation( | ||
"/spec/containers/0/image", | ||
"127.0.0.1:31999/library/nginx:latest-zarf-3793515731", | ||
), | ||
operations.AddPatchOperation( | ||
"/metadata/labels", | ||
map[string]string{"zarf-agent": "patched"}, | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
resp := sendAdmissionRequest(t, tt.admissionReq, handler) | ||
if tt.expectedPatch != nil { | ||
expectedPatchJSON, err := json.Marshal(tt.expectedPatch) | ||
require.NoError(t, err) | ||
require.JSONEq(t, string(expectedPatchJSON), string(resp.Patch)) | ||
} else { | ||
require.Empty(t, string(resp.Patch)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package hooks | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/admission/v1" | ||
) | ||
|
||
// sendAdmissionRequest sends an admission request to the handler and returns the response. | ||
func sendAdmissionRequest(t *testing.T, admissionReq *v1.AdmissionRequest, handler http.HandlerFunc) *v1.AdmissionResponse { | ||
t.Helper() | ||
|
||
b, err := json.Marshal(&v1.AdmissionReview{ | ||
Request: admissionReq, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Note: The URL ("/test") doesn't matter here because we are directly invoking the handler. | ||
// The handler processes the request based on the HTTP method and body content, not the URL path. | ||
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(b)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
rr := httptest.NewRecorder() | ||
handler.ServeHTTP(rr, req) | ||
|
||
require.Equal(t, http.StatusOK, rr.Code) | ||
|
||
var admissionReview v1.AdmissionReview | ||
err = json.NewDecoder(rr.Body).Decode(&admissionReview) | ||
require.NoError(t, err) | ||
|
||
resp := admissionReview.Response | ||
require.NotNil(t, resp) | ||
require.True(t, resp.Allowed) | ||
|
||
return resp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters