From 88967dd63f325569cc0e11104a1445b0e7d7c6f2 Mon Sep 17 00:00:00 2001 From: dervoeti Date: Wed, 6 Nov 2024 11:36:05 +0100 Subject: [PATCH] refactor: move namespace generation from harness to case / enable usage of preferred namespaces --- pkg/test/case.go | 16 +++++++++++++--- pkg/test/case_test.go | 4 ++++ pkg/test/harness.go | 14 +------------- pkg/test/harness_test.go | 4 ---- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/pkg/test/case.go b/pkg/test/case.go index bf42429..3baee1c 100644 --- a/pkg/test/case.go +++ b/pkg/test/case.go @@ -2,6 +2,8 @@ package test import ( "context" + "crypto/sha256" + "encoding/hex" "fmt" "os" "path/filepath" @@ -11,7 +13,6 @@ import ( "testing" "time" - petname "github.com/dustinkirkland/golang-petname" "github.com/thoas/go-funk" corev1 "k8s.io/api/core/v1" eventsv1 "k8s.io/api/events/v1" @@ -415,14 +416,23 @@ func (t *Case) Run(test *testing.T, ts *report.Testsuite) { } } +// Derive the namespace to use for the test case from its name +func deriveNamespaceFromTestcaseName(testcaseName string) string { + hasher := sha256.New() + hasher.Write([]byte(testcaseName)) + hash := hex.EncodeToString(hasher.Sum(nil)) + + return fmt.Sprintf("kuttl-%s", hash[:10]) +} + func (t *Case) determineNamespace() (*namespace, error) { ns := &namespace{ Name: t.PreferredNamespace, AutoCreated: false, } - // no preferred ns, means we auto-create with petnames + // no preferred ns, means we derive the namespace from the test case name if t.PreferredNamespace == "" { - ns.Name = fmt.Sprintf("kuttl-test-%s", petname.Generate(2, "-")) + ns.Name = deriveNamespaceFromTestcaseName(t.Name) ns.AutoCreated = true } else { exist, err := t.NamespaceExists(t.PreferredNamespace) diff --git a/pkg/test/case_test.go b/pkg/test/case_test.go index 21a9a06..7e5ab6b 100644 --- a/pkg/test/case_test.go +++ b/pkg/test/case_test.go @@ -425,3 +425,7 @@ func TestGetIndexFromFile(t *testing.T) { }) } } + +func TestDetermineNamespace(t *testing.T) { + assert.Equal(t, "kuttl-c7e64f7a24", deriveNamespaceFromTestcaseName("smoke_airflow-2.9.2_openshift-false_executor-kubernetes")) +} diff --git a/pkg/test/harness.go b/pkg/test/harness.go index 7ef2ca1..8672257 100644 --- a/pkg/test/harness.go +++ b/pkg/test/harness.go @@ -3,8 +3,6 @@ package test import ( "bytes" "context" - "crypto/sha256" - "encoding/hex" "errors" "fmt" "math/rand" @@ -59,16 +57,6 @@ type Harness struct { RunLabels labels.Set } -// Calculate the namespace to use for the test case -// Get the sha256 hash the test case name -func determineNamespace(testcaseName string) string { - hasher := sha256.New() - hasher.Write([]byte(testcaseName)) - hash := hex.EncodeToString(hasher.Sum(nil)) - - return fmt.Sprintf("kuttl-%s", hash[:10]) -} - // LoadTests loads all of the tests in a given directory. func (h *Harness) LoadTests(dir string) ([]*Case, error) { dir, err := filepath.Abs(dir) @@ -95,7 +83,7 @@ func (h *Harness) LoadTests(dir string) ([]*Case, error) { Timeout: timeout, Steps: []*Step{}, Name: file.Name(), - PreferredNamespace: determineNamespace(file.Name()), + PreferredNamespace: h.TestSuite.Namespace, Dir: filepath.Join(dir, file.Name()), SkipDelete: h.TestSuite.SkipDelete, Suppress: h.TestSuite.Suppress, diff --git a/pkg/test/harness_test.go b/pkg/test/harness_test.go index 344c976..b9f3e7c 100644 --- a/pkg/test/harness_test.go +++ b/pkg/test/harness_test.go @@ -88,7 +88,3 @@ func TestAddNodeCaches(t *testing.T) { assert.Equal(t, "/var/lib/docker/data/kind-0", kindCfg.Nodes[0].ExtraMounts[0].HostPath) assert.Equal(t, "/var/lib/docker/data/kind-1", kindCfg.Nodes[1].ExtraMounts[0].HostPath) } - -func TestDetermineNamespace(t *testing.T) { - assert.Equal(t, "kuttl-c7e64f7a24", determineNamespace("smoke_airflow-2.9.2_openshift-false_executor-kubernetes")) -}