From d7d61a2ce49150223b72a68e201601ea5039df68 Mon Sep 17 00:00:00 2001 From: Leonard Cohnen Date: Fri, 6 Dec 2024 20:24:12 +0100 Subject: [PATCH 1/2] e2e: refactor shared flags to contrasttest package --- e2e/aks-runtime/aks_runtime_test.go | 18 +++-------- e2e/genpolicy/genpolicy_test.go | 14 ++------- e2e/getdents/getdents_test.go | 14 ++------- e2e/internal/contrasttest/contrasttest.go | 31 ++++++++++++++++--- e2e/openssl/openssl_test.go | 14 ++------- e2e/policy/deterministic_test.go | 5 ++- e2e/policy/policy_test.go | 14 ++------- e2e/regression/regression_test.go | 16 ++-------- e2e/servicemesh/servicemesh_test.go | 14 ++------- .../volumestatefulset_test.go | 14 ++------- e2e/workloadsecret/workloadsecret_test.go | 14 ++------- 11 files changed, 58 insertions(+), 110 deletions(-) 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..04a25f8985 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()) From 20453a5dfe3ca082e8de60d94a09f52879cf0085 Mon Sep 17 00:00:00 2001 From: Leonard Cohnen Date: Fri, 6 Dec 2024 20:28:31 +0100 Subject: [PATCH 2/2] ci: add "ci" to e2e test namespaces --- .github/workflows/e2e.yml | 3 ++- .github/workflows/e2e_aks_runtime.yml | 3 ++- e2e/aks-runtime/aks_runtime_test.go | 2 +- e2e/internal/contrasttest/contrasttest.go | 19 ++++++++++++++----- justfile | 3 ++- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index cc94731154..f8dca9fcf8 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -79,7 +79,8 @@ jobs: --image-replacements workspace/just.containerlookup \ --namespace-file workspace/e2e.namespace \ --platform ${{ inputs.platform }} \ - --skip-undeploy="${{ inputs.skip-undeploy && 'true' || 'false' }}" + --skip-undeploy="${{ inputs.skip-undeploy && 'true' || 'false' }}" \ + --namespace-suffix="-ci" - name: Download logs if: always() run: | diff --git a/.github/workflows/e2e_aks_runtime.yml b/.github/workflows/e2e_aks_runtime.yml index 4b3d10d155..54ee539958 100644 --- a/.github/workflows/e2e_aks_runtime.yml +++ b/.github/workflows/e2e_aks_runtime.yml @@ -87,7 +87,8 @@ jobs: --image-replacements workspace/just.containerlookup \ --namespace-file workspace/e2e.namespace \ --platform AKS-CLH-SNP \ - --skip-undeploy="false" + --skip-undeploy="false" \ + --namespace-suffix="-ci" - name: Download logs if: always() run: | diff --git a/e2e/aks-runtime/aks_runtime_test.go b/e2e/aks-runtime/aks_runtime_test.go index 04ef2fa34f..6738d84926 100644 --- a/e2e/aks-runtime/aks_runtime_test.go +++ b/e2e/aks-runtime/aks_runtime_test.go @@ -34,7 +34,7 @@ func TestAKSRuntime(t *testing.T) { require.NoError(err) imageReplacements, err := kuberesource.ImageReplacementsFromFile(f) require.NoError(err) - namespace := contrasttest.MakeNamespace(t) + namespace := contrasttest.MakeNamespace(t, contrasttest.Flags.NamespaceSuffix) // Log versions kataPolicyGenV, err := az.KataPolicyGenVersion() diff --git a/e2e/internal/contrasttest/contrasttest.go b/e2e/internal/contrasttest/contrasttest.go index 04a25f8985..83bcf80e8a 100644 --- a/e2e/internal/contrasttest/contrasttest.go +++ b/e2e/internal/contrasttest/contrasttest.go @@ -10,7 +10,6 @@ import ( "context" "crypto/rand" "crypto/x509" - "encoding/hex" "encoding/json" "flag" "fmt" @@ -41,6 +40,7 @@ type testFlags struct { PlatformStr string ImageReplacementsFile string NamespaceFile string + NamespaceSuffix string SkipUndeploy bool } @@ -48,6 +48,7 @@ type testFlags struct { 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.NamespaceSuffix, "namespace-suffix", "", "suffix to append to the namespace") flag.StringVar(&Flags.PlatformStr, "platform", "", "Deployment platform") flag.BoolVar(&Flags.SkipUndeploy, "skip-undeploy", false, "Skip undeploying the test namespace") } @@ -75,7 +76,7 @@ func New(t *testing.T) *ContrastTest { require.NoError(t, err) return &ContrastTest{ - Namespace: MakeNamespace(t), + Namespace: MakeNamespace(t, Flags.NamespaceSuffix), WorkDir: t.TempDir(), ImageReplacementsFile: Flags.ImageReplacementsFile, Platform: platform, @@ -397,14 +398,22 @@ 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)) + + return strings.Join(namespaceParts, "-") + namespaceSuffix } func toPtr[T any](t T) *T { diff --git a/justfile b/justfile index 58e10e9030..58642fd541 100644 --- a/justfile +++ b/justfile @@ -72,7 +72,8 @@ e2e target=default_deploy_target platform=default_platform: soft-clean coordinat --image-replacements ./{{ workspace_dir }}/just.containerlookup \ --namespace-file ./{{ workspace_dir }}/just.namespace \ --platform {{ platform }} \ - --skip-undeploy=true + --skip-undeploy=true \ + --namespace-suffix=${namespace_suffix-} # Generate policies, apply Kubernetes manifests. deploy target=default_deploy_target cli=default_cli platform=default_platform: (runtime target platform) (apply "runtime") (populate target platform) (generate cli platform) (apply target)