Skip to content

Commit

Permalink
e2e: revert contrasttest
Browse files Browse the repository at this point in the history
  • Loading branch information
wirungu committed Apr 26, 2024
1 parent 0120d6f commit ffb9b52
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions e2e/internal/contrasttest/contrasttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,25 +39,38 @@ 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),
}
}

// Init patches the given resources for the test environment and makes them available to Generate and Set.
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)
Expand Down

0 comments on commit ffb9b52

Please sign in to comment.