Skip to content

Commit

Permalink
test: unit test for namespace generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dervoeti committed Nov 6, 2024
1 parent 9f1ab92 commit d294f62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 15 additions & 12 deletions pkg/test/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ 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))

truncatedFileName := testcaseName
if len(truncatedFileName) > 32 {
truncatedFileName = truncatedFileName[:32]
}
return fmt.Sprintf("kuttl-%s-%s", truncatedFileName, 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)
Expand All @@ -81,22 +95,11 @@ func (h *Harness) LoadTests(dir string) ([]*Case, error) {
continue
}

// Calculate the namespace to use for the test case
// Get the sha256 hash the test case name
hasher := sha256.New()
hasher.Write([]byte(file.Name()))
hash := hex.EncodeToString(hasher.Sum(nil))

truncatedFileName := file.Name()
if len(truncatedFileName) > 32 {
truncatedFileName = truncatedFileName[:32]
}

tests = append(tests, &Case{
Timeout: timeout,
Steps: []*Step{},
Name: file.Name(),
PreferredNamespace: fmt.Sprintf("kuttl-%s-%s", hash[:10], truncatedFileName),
PreferredNamespace: determineNamespace(file.Name()),
Dir: filepath.Join(dir, file.Name()),
SkipDelete: h.TestSuite.SkipDelete,
Suppress: h.TestSuite.Suppress,
Expand Down
4 changes: 4 additions & 0 deletions pkg/test/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@ 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-smoke_airflow-2.9.2_openshift-fa-c7e64f7a24", determineNamespace("smoke_airflow-2.9.2_openshift-false_executor-kubernetes"))
}

0 comments on commit d294f62

Please sign in to comment.