diff --git a/e2e/internal/contrasttest/contrasttest.go b/e2e/internal/contrasttest/contrasttest.go index f9c0680eb6..cb23c07027 100644 --- a/e2e/internal/contrasttest/contrasttest.go +++ b/e2e/internal/contrasttest/contrasttest.go @@ -27,11 +27,10 @@ import ( // ContrastTest is the Contrast test helper struct. type ContrastTest struct { // inputs, usually filled by New() - Namespace string - WorkDir string - AddNamespaceObjects bool - ImageReplacements map[string]string - Kubeclient *kubeclient.Kubeclient + Namespace string + WorkDir string + ImageReplacements map[string]string + Kubeclient *kubeclient.Kubeclient // outputs of contrast subcommands coordinatorPolicyHash string @@ -40,13 +39,12 @@ type ContrastTest struct { } // New creates a new contrasttest.T object bound to the given test. -func New(t *testing.T, namespace string, addNamespaceObjects bool, imageReplacements map[string]string) *ContrastTest { +func New(t *testing.T, imageReplacements map[string]string) *ContrastTest { return &ContrastTest{ - Namespace: namespace, - WorkDir: t.TempDir(), - AddNamespaceObjects: addNamespaceObjects, - ImageReplacements: imageReplacements, - Kubeclient: kubeclient.NewForTest(t), + Namespace: makeNamespace(t), + WorkDir: t.TempDir(), + ImageReplacements: imageReplacements, + Kubeclient: kubeclient.NewForTest(t), } } @@ -54,11 +52,25 @@ func New(t *testing.T, namespace string, addNamespaceObjects bool, imageReplacem func (ct *ContrastTest) Init(t *testing.T, resources []any) { require := require.New(t) + // Create namespace + namespace, err := kuberesource.ResourcesToUnstructured([]any{kuberesource.Namespace(ct.Namespace)}) + require.NoError(err) + // Creating a namespace should not take too long. + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + err = ct.Kubeclient.Apply(ctx, namespace...) + cancel() + require.NoError(err) + t.Cleanup(func() { + // Deleting the namespace may take some time due to pod cleanup, but we don't want to wait until the test times out. + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + if err := ct.Kubeclient.Delete(ctx, namespace...); err != nil { + t.Logf("Could not delete namespace %q: %v", ct.Namespace, err) + } + }) + // Prepare resources resources = kuberesource.PatchImages(resources, ct.ImageReplacements) - if ct.AddNamespaceObjects { - resources = kuberesource.AddNamespaceObjects(resources, ct.Namespace) - } resources = kuberesource.PatchNamespaces(resources, ct.Namespace) unstructuredResources, err := kuberesource.ResourcesToUnstructured(resources) require.NoError(err)