Skip to content

Commit

Permalink
fix: create namespace when running TestSuite
Browse files Browse the repository at this point in the history
Signed-off-by: chenmj11 <chenmj11@lenovo.com>
  • Loading branch information
chenmj11 committed May 10, 2024
1 parent 5449fc6 commit 2c3351c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,12 @@ func shortString(obj *corev1.ObjectReference) string {
// Run runs a test case including all of its steps.
func (t *Case) Run(test *testing.T, ts *report.Testsuite) {
setupReport := report.NewCase("setup")
ns := t.determineNamespace()
ns, err := t.determineNamespace()
if err != nil {
setupReport.Failure = report.NewFailure(err.Error(), nil)
ts.AddTestcase(setupReport)
test.Fatal(err)
}

cl, err := t.Client(false)
if err != nil {
Expand Down Expand Up @@ -388,7 +393,7 @@ func (t *Case) Run(test *testing.T, ts *report.Testsuite) {
}
}

func (t *Case) determineNamespace() *namespace {
func (t *Case) determineNamespace() (*namespace, error) {
ns := &namespace{
Name: t.PreferredNamespace,
AutoCreated: false,
Expand All @@ -397,9 +402,17 @@ func (t *Case) determineNamespace() *namespace {
if t.PreferredNamespace == "" {
ns.Name = fmt.Sprintf("kuttl-test-%s", petname.Generate(2, "-"))
ns.AutoCreated = true
} else {
exist, err := t.NamespaceExists(t.PreferredNamespace)
if err != nil {
return nil, fmt.Errorf("failed to determine existence of namespace %q: %w", t.PreferredNamespace, err)
}
if !exist {
ns.AutoCreated = true
}
}
// if we have a preferred namespace, we do NOT auto-create
return ns
// if we have a preferred namespace, and it already exists, we do NOT auto-create
return ns, nil
}

// CollectTestStepFiles collects a map of test steps and their associated files
Expand Down

0 comments on commit 2c3351c

Please sign in to comment.