diff --git a/e2e/aks-runtime/aks_runtime_test.go b/e2e/aks-runtime/aks_runtime_test.go index bd39c9ce7a..04ef2fa34f 100644 --- a/e2e/aks-runtime/aks_runtime_test.go +++ b/e2e/aks-runtime/aks_runtime_test.go @@ -25,17 +25,12 @@ import ( const testContainer = "testcontainer" -var ( - imageReplacementsFile, namespaceFile, _platformStr string - skipUndeploy bool -) - func TestAKSRuntime(t *testing.T) { require := require.New(t) workdir := t.TempDir() - f, err := os.Open(imageReplacementsFile) + f, err := os.Open(contrasttest.Flags.ImageReplacementsFile) require.NoError(err) imageReplacements, err := kuberesource.ImageReplacementsFromFile(f) require.NoError(err) @@ -59,8 +54,8 @@ func TestAKSRuntime(t *testing.T) { err = c.Apply(ctx, ns...) cancel() require.NoError(err) - if namespaceFile != "" { - require.NoError(os.WriteFile(namespaceFile, []byte(namespace), 0o644)) + if contrasttest.Flags.NamespaceFile != "" { + require.NoError(os.WriteFile(contrasttest.Flags.NamespaceFile, []byte(namespace), 0o644)) } // simple deployment that logs the kernel version and then sleeps @@ -110,7 +105,7 @@ func TestAKSRuntime(t *testing.T) { require.NoError(c.WaitFor(ctx, kubeclient.Ready, kubeclient.Deployment{}, namespace, testContainer)) t.Cleanup(func() { - if skipUndeploy { + if contrasttest.Flags.SkipUndeploy { return } @@ -134,10 +129,7 @@ func TestAKSRuntime(t *testing.T) { } func TestMain(m *testing.M) { - flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") - flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") - flag.StringVar(&_platformStr, "platform", "", "Deployment platform") - flag.BoolVar(&skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run()) diff --git a/e2e/genpolicy/genpolicy_test.go b/e2e/genpolicy/genpolicy_test.go index b35d6a28f4..d646df966a 100644 --- a/e2e/genpolicy/genpolicy_test.go +++ b/e2e/genpolicy/genpolicy_test.go @@ -24,23 +24,18 @@ import ( "github.com/stretchr/testify/require" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - // TestGenpolicy runs regression tests for generated policies. func TestGenpolicy(t *testing.T) { testCases := kuberesource.GenpolicyRegressionTests() - platform, err := platforms.FromString(platformStr) + platform, err := platforms.FromString(contrasttest.Flags.PlatformStr) require.NoError(t, err) runtimeHandler, err := manifest.RuntimeHandler(platform) require.NoError(t, err) for name, deploy := range testCases { t.Run(name, func(t *testing.T) { - ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy) + ct := contrasttest.New(t) ct.Init(t, kuberesource.PatchRuntimeHandlers([]any{deploy}, runtimeHandler)) @@ -72,10 +67,7 @@ func TestGenpolicy(t *testing.T) { } func TestMain(m *testing.M) { - flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") - flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") - flag.StringVar(&platformStr, "platform", "", "Deployment platform") - flag.BoolVar(&skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run()) diff --git a/e2e/getdents/getdents_test.go b/e2e/getdents/getdents_test.go index 1f9b489497..0463ca50d7 100644 --- a/e2e/getdents/getdents_test.go +++ b/e2e/getdents/getdents_test.go @@ -28,15 +28,10 @@ const ( getdent = "getdents-tester" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - func TestGetDEnts(t *testing.T) { - platform, err := platforms.FromString(platformStr) + platform, err := platforms.FromString(contrasttest.Flags.PlatformStr) require.NoError(t, err) - ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy) + ct := contrasttest.New(t) runtimeHandler, err := manifest.RuntimeHandler(platform) require.NoError(t, err) @@ -89,10 +84,7 @@ func TestGetDEnts(t *testing.T) { } func TestMain(m *testing.M) { - flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") - flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") - flag.StringVar(&platformStr, "platform", "", "Deployment platform") - flag.BoolVar(&skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run()) diff --git a/e2e/internal/contrasttest/contrasttest.go b/e2e/internal/contrasttest/contrasttest.go index 8b79e1547d..be250608c8 100644 --- a/e2e/internal/contrasttest/contrasttest.go +++ b/e2e/internal/contrasttest/contrasttest.go @@ -10,8 +10,8 @@ import ( "context" "crypto/rand" "crypto/x509" - "encoding/hex" "encoding/json" + "flag" "fmt" "io" "os" @@ -32,6 +32,25 @@ import ( "github.com/stretchr/testify/require" ) +// Flags contains the parsed Flags for the test. +var Flags testFlags + +// testFlags contains the flags for the test. +type testFlags struct { + PlatformStr string + ImageReplacementsFile string + NamespaceFile string + SkipUndeploy bool +} + +// RegisterFlags registers the flags that are shared between all tests. +func RegisterFlags() { + flag.StringVar(&Flags.ImageReplacementsFile, "image-replacements", "", "path to image replacements file") + flag.StringVar(&Flags.NamespaceFile, "namespace-file", "", "file to store the namespace in") + flag.StringVar(&Flags.PlatformStr, "platform", "", "Deployment platform") + flag.BoolVar(&Flags.SkipUndeploy, "skip-undeploy", false, "Skip undeploying the test namespace") +} + // ContrastTest is the Contrast test helper struct. type ContrastTest struct { // inputs, usually filled by New() @@ -50,14 +69,17 @@ type ContrastTest struct { } // New creates a new contrasttest.T object bound to the given test. -func New(t *testing.T, imageReplacements, namespaceFile string, platform platforms.Platform, skipUndeploy bool) *ContrastTest { +func New(t *testing.T) *ContrastTest { + platform, err := platforms.FromString(Flags.PlatformStr) + require.NoError(t, err) + return &ContrastTest{ - Namespace: MakeNamespace(t), + Namespace: MakeNamespace(t, Flags.NamespaceSuffix), WorkDir: t.TempDir(), - ImageReplacementsFile: imageReplacements, + ImageReplacementsFile: Flags.ImageReplacementsFile, Platform: platform, - NamespaceFile: namespaceFile, - SkipUndeploy: skipUndeploy, + NamespaceFile: Flags.NamespaceFile, + SkipUndeploy: Flags.SkipUndeploy, Kubeclient: kubeclient.NewForTest(t), } } @@ -374,14 +396,29 @@ func (ct *ContrastTest) FactorPlatformTimeout(timeout time.Duration) time.Durati } // MakeNamespace creates a namespace string using a given *testing.T. -func MakeNamespace(t *testing.T) string { - buf := make([]byte, 4) +func MakeNamespace(t *testing.T, namespaceSuffix string) string { + var namespaceParts []string + + // First part(s) are consist of all valid characters in the lower case test name. re := regexp.MustCompile("[a-z0-9-]+") + namespaceParts = append(namespaceParts, re.FindAllString(strings.ToLower(t.Name()), -1)...) + + // Append some randomness + buf := make([]byte, 4) n, err := rand.Reader.Read(buf) require.NoError(t, err) require.Equal(t, 4, n) - return strings.Join(append(re.FindAllString(strings.ToLower(t.Name()), -1), hex.EncodeToString(buf)), "-") + namespaceParts = append(namespaceParts, fmt.Sprintf("%x", buf)) + + // Below we join all parts with a dash already, the user usually provides the + // suffix with a dash, so we remove any leading dash from the suffix. + namespaceSuffix = strings.TrimPrefix(namespaceSuffix, "-") + + // Append the suffix + namespaceParts = append(namespaceParts, namespaceSuffix) + + return strings.Join(namespaceParts, "-") } func toPtr[T any](t T) *T { diff --git a/e2e/openssl/openssl_test.go b/e2e/openssl/openssl_test.go index 88bd3b6116..a941ea2412 100644 --- a/e2e/openssl/openssl_test.go +++ b/e2e/openssl/openssl_test.go @@ -35,16 +35,11 @@ const ( rootCAFile = "coordinator-root-ca.pem" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - // TestOpenSSL runs e2e tests on the example OpenSSL deployment. func TestOpenSSL(t *testing.T) { - platform, err := platforms.FromString(platformStr) + platform, err := platforms.FromString(contrasttest.Flags.PlatformStr) require.NoError(t, err) - ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy) + ct := contrasttest.New(t) runtimeHandler, err := manifest.RuntimeHandler(platform) require.NoError(t, err) @@ -245,10 +240,7 @@ func TestOpenSSL(t *testing.T) { } func TestMain(m *testing.M) { - flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") - flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") - flag.StringVar(&platformStr, "platform", "", "Deployment platform") - flag.BoolVar(&skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run()) diff --git a/e2e/policy/deterministic_test.go b/e2e/policy/deterministic_test.go index 599c6b3d4c..251c83aabb 100644 --- a/e2e/policy/deterministic_test.go +++ b/e2e/policy/deterministic_test.go @@ -20,10 +20,9 @@ import ( func TestDeterminsticPolicyGeneration(t *testing.T) { require := require.New(t) - platform, err := platforms.FromString(platformStr) + platform, err := platforms.FromString(contrasttest.Flags.PlatformStr) require.NoError(err) - skipUndeploy := true // doesn't matter, because we don't deploy - ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy) + ct := contrasttest.New(t) // create K8s resources runtimeHandler, err := manifest.RuntimeHandler(platform) diff --git a/e2e/policy/policy_test.go b/e2e/policy/policy_test.go index f58fda92ac..5aa6dcc8ee 100644 --- a/e2e/policy/policy_test.go +++ b/e2e/policy/policy_test.go @@ -36,15 +36,10 @@ const ( coordinatorPod = "coordinator-0" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - func TestPolicy(t *testing.T) { - platform, err := platforms.FromString(platformStr) + platform, err := platforms.FromString(contrasttest.Flags.PlatformStr) require.NoError(t, err) - ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, skipUndeploy) + ct := contrasttest.New(t) runtimeHandler, err := manifest.RuntimeHandler(platform) require.NoError(t, err) @@ -197,10 +192,7 @@ func TestPolicy(t *testing.T) { } func TestMain(m *testing.M) { - flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") - flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") - flag.StringVar(&platformStr, "platform", "", "Deployment platform") - flag.BoolVar(&skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run()) diff --git a/e2e/regression/regression_test.go b/e2e/regression/regression_test.go index 784c910878..d59bd469bd 100644 --- a/e2e/regression/regression_test.go +++ b/e2e/regression/regression_test.go @@ -24,23 +24,18 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - _skipUndeploy bool // just here for interoptability, ignored in this test -) - func TestRegression(t *testing.T) { yamlDir := "./e2e/regression/testdata/" files, err := os.ReadDir(yamlDir) require.NoError(t, err) - platform, err := platforms.FromString(platformStr) + platform, err := platforms.FromString(contrasttest.Flags.PlatformStr) require.NoError(t, err) runtimeHandler, err := manifest.RuntimeHandler(platform) require.NoError(t, err) - ct := contrasttest.New(t, imageReplacementsFile, namespaceFile, platform, false) + ct := contrasttest.New(t) // Initially just deploy the coordinator bundle @@ -97,12 +92,7 @@ func TestRegression(t *testing.T) { } func TestMain(m *testing.M) { - flag.StringVar(&imageReplacementsFile, "image-replacements", "", "path to image replacements file") - flag.StringVar(&namespaceFile, "namespace-file", "", "file to store the namespace in") - flag.StringVar(&platformStr, "platform", "", "Deployment platform") - - // ignored and just here for interoptability, we always undeploy to save resources - flag.BoolVar(&_skipUndeploy, "skip-undeploy", false, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run())