-
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 17, 2024
1 parent
5bf7e01
commit db8deb0
Showing
5 changed files
with
193 additions
and
13 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
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,174 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/defenseunicorns/zarf/src/config" | ||
"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" | ||
) | ||
|
||
// NewTestAdmissionServer creates a test server for the admission webhook. | ||
func NewTestAdmissionServer(t *testing.T, zarfState *types.ZarfState) *httptest.Server { | ||
t.Helper() | ||
server := NewAdmissionServer("0", zarfState) | ||
return httptest.NewServer(server.Handler) | ||
} | ||
|
||
// createPodAdmissionRequest creates an admission request for a pod. | ||
func createPodAdmissionRequest(t *testing.T, operation v1.Operation, pod *corev1.Pod) *v1.AdmissionRequest { | ||
t.Helper() | ||
raw, err := json.Marshal(pod) | ||
require.NoError(t, err) | ||
return &v1.AdmissionRequest{ | ||
Operation: operation, | ||
Kind: metav1.GroupVersionKind{ | ||
Group: "", | ||
Version: "v1", | ||
Kind: "Pod", | ||
}, | ||
Object: runtime.RawExtension{ | ||
Raw: raw, | ||
}, | ||
} | ||
} | ||
|
||
// sendAdmissionReview sends an admission review request to the server and returns the response. | ||
func sendAdmissionReview(t *testing.T, server *httptest.Server, endpoint string, admissionReq *v1.AdmissionRequest) *v1.AdmissionResponse { | ||
t.Helper() | ||
|
||
b, err := json.Marshal(&v1.AdmissionReview{ | ||
Request: admissionReq, | ||
}) | ||
require.NoError(t, err) | ||
|
||
req := httptest.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(b)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
rr := httptest.NewRecorder() | ||
server.Config.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 | ||
} | ||
|
||
// TestPodMutationWebhook tests the pod mutation webhook. | ||
func TestPodMutationWebhook(t *testing.T) { | ||
t.Parallel() | ||
|
||
server := NewTestAdmissionServer( | ||
t, | ||
&types.ZarfState{ | ||
RegistryInfo: types.RegistryInfo{ | ||
Address: "127.0.0.1:31999", | ||
}, | ||
}) | ||
defer server.Close() | ||
|
||
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{ | ||
Name: "test-pod-mutation", | ||
Namespace: "test-pod-mutation", | ||
Labels: map[string]string{"should-be": "mutated"}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "nginx", | ||
Image: "nginx", | ||
}, | ||
}, | ||
InitContainers: []corev1.Container{ | ||
{ | ||
Name: "busybox", | ||
Image: "busybox", | ||
}, | ||
}, | ||
EphemeralContainers: []corev1.EphemeralContainer{ | ||
{ | ||
EphemeralContainerCommon: corev1.EphemeralContainerCommon{ | ||
Name: "alpine", | ||
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{ | ||
Name: "nginx", | ||
Namespace: "nginx", | ||
Labels: map[string]string{"zarf-agent": "patched"}, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "nginx", | ||
Image: "nginx", | ||
}, | ||
}, | ||
}, | ||
}), | ||
expectedPatch: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
resp := sendAdmissionReview(t, server, "/mutate/pod", tt.admissionReq) | ||
if tt.expectedPatch != nil { | ||
expectedPatchJSON, err := json.Marshal(tt.expectedPatch) | ||
require.NoError(t, err) | ||
require.JSONEq(t, string(expectedPatchJSON), string(resp.Patch)) | ||
} else { | ||
require.Nil(t, 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