Skip to content

Commit

Permalink
refactor: move namespace generation from harness to case / enable usa…
Browse files Browse the repository at this point in the history
…ge of preferred namespaces
  • Loading branch information
dervoeti committed Nov 6, 2024
1 parent fbf9921 commit 88967dd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
16 changes: 13 additions & 3 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package test

import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/test/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
14 changes: 1 addition & 13 deletions pkg/test/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package test
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"math/rand"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions pkg/test/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

0 comments on commit 88967dd

Please sign in to comment.