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 f169933f8c..01220f1610 100644 --- a/e2e/internal/contrasttest/contrasttest.go +++ b/e2e/internal/contrasttest/contrasttest.go @@ -12,6 +12,7 @@ import ( "crypto/x509" "encoding/hex" "encoding/json" + "flag" "fmt" "io" "os" @@ -32,6 +33,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 +70,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), WorkDir: t.TempDir(), - ImageReplacementsFile: imageReplacements, + ImageReplacementsFile: Flags.ImageReplacementsFile, Platform: platform, - NamespaceFile: namespaceFile, - SkipUndeploy: skipUndeploy, + NamespaceFile: Flags.NamespaceFile, + SkipUndeploy: Flags.SkipUndeploy, Kubeclient: kubeclient.NewForTest(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()) diff --git a/e2e/servicemesh/servicemesh_test.go b/e2e/servicemesh/servicemesh_test.go index eeea231bd5..1d43dba2e2 100644 --- a/e2e/servicemesh/servicemesh_test.go +++ b/e2e/servicemesh/servicemesh_test.go @@ -26,16 +26,11 @@ import ( "github.com/stretchr/testify/require" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - // TestIngressEgress tests that the ingress and egress proxies work as configured. func TestIngressEgress(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) @@ -153,10 +148,7 @@ func TestIngressEgress(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/volumestatefulset/volumestatefulset_test.go b/e2e/volumestatefulset/volumestatefulset_test.go index 9e4a9f3738..4a383239e3 100644 --- a/e2e/volumestatefulset/volumestatefulset_test.go +++ b/e2e/volumestatefulset/volumestatefulset_test.go @@ -21,16 +21,11 @@ import ( "github.com/stretchr/testify/require" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - // TestWorkloadSecrets tests that secrets are correctly injected into workloads. func TestVolumeStatefulSet(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) @@ -106,10 +101,7 @@ func TestVolumeStatefulSet(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", true, "skip undeploy step in the test") + contrasttest.RegisterFlags() flag.Parse() os.Exit(m.Run()) diff --git a/e2e/workloadsecret/workloadsecret_test.go b/e2e/workloadsecret/workloadsecret_test.go index 8257558f28..a368aaac24 100644 --- a/e2e/workloadsecret/workloadsecret_test.go +++ b/e2e/workloadsecret/workloadsecret_test.go @@ -24,16 +24,11 @@ import ( "github.com/stretchr/testify/require" ) -var ( - imageReplacementsFile, namespaceFile, platformStr string - skipUndeploy bool -) - // TestWorkloadSecrets tests that secrets are correctly injected into workloads. func TestWorkloadSecrets(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) @@ -138,10 +133,7 @@ func TestWorkloadSecrets(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())