Skip to content

Commit

Permalink
test/e2e: Create and clean up namespace in main_test
Browse files Browse the repository at this point in the history
- Create a new namespace at the start of the test run and wait
for it to be ready before starting
- If Teardown is done then delete it at the end of the test run

Fixes: #1652
Signed-off-by: stevenhorsman <steven@uk.ibm.com>
Co-authored-by: Da Li Liu <liudali@cn.ibm.com>
  • Loading branch information
stevenhorsman and Da Li Liu committed Jan 9, 2024
1 parent 9597192 commit 280a03e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/e2e/assessment_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/e2e-framework/klient"
"sigs.k8s.io/e2e-framework/klient/k8s"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/env"
)

const WAIT_NAMESPACE_AVAILABLE_TIMEOUT = time.Second * 120

func reverseSlice(slice []string) []string {
length := len(slice)
for i := 0; i < length/2; i++ {
Expand Down Expand Up @@ -476,3 +479,43 @@ func DeletePod(ctx context.Context, client klient.Client, pod *v1.Pod, tcDelDura
log.Infof("Pod %s has been successfully deleted within %.0fs", pod.Name, tcDelDuration.Seconds())
return nil
}

func CreateAndWaitForNamespace(ctx context.Context, client klient.Client, namespaceName string) error {
log.Infof("Creating namespace '%s'...", namespaceName)
nsObj := v1.Namespace{}
nsObj.Name = namespaceName
if err := client.Resources().Create(ctx, &nsObj); err != nil {
return err
}

log.Infof("Wait for namespace '%s' be ready", namespaceName)
if err := wait.For(conditions.New(client.Resources()).ResourceMatch(&nsObj, func(object k8s.Object) bool {
ns, ok := object.(*v1.Namespace)
if !ok {
log.Printf("Not a namespace object: %v", object)
return false
}
return ns.Status.Phase == v1.NamespaceActive
}), wait.WithTimeout(WAIT_NAMESPACE_AVAILABLE_TIMEOUT)); err != nil {
return err
}
log.Infof("Namespace '%s' is ready", namespaceName)
return nil
}

func DeleteAndWaitForNamespace(ctx context.Context, client klient.Client, namespaceName string) error {
nsObj := v1.Namespace{}
nsObj.Name = namespaceName
if err := client.Resources().Delete(ctx, &nsObj); err != nil {
return err
}
log.Infof("Deleting namespace '%s'...", nsObj.Name)
if err := wait.For(conditions.New(
client.Resources()).ResourceDeleted(&nsObj),
wait.WithInterval(5*time.Second),
wait.WithTimeout(60*time.Second)); err != nil {
return err
}
log.Infof("Namespace '%s' has been successfully deleted within 60s", nsObj.Name)
return nil
}
10 changes: 10 additions & 0 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func TestMain(m *testing.M) {
return ctx, err
}
}

if err = CreateAndWaitForNamespace(ctx, cfg.Client(), E2eNamespace); err != nil {
return ctx, err
}

return ctx, nil
})

Expand All @@ -146,6 +151,11 @@ func TestMain(m *testing.M) {
if !shouldTeardown {
return ctx, nil
}

if err = DeleteAndWaitForNamespace(ctx, cfg.Client(), E2eNamespace); err != nil {
return ctx, err
}

if shouldProvisionCluster {
if err = provisioner.DeleteCluster(ctx, cfg); err != nil {
return ctx, err
Expand Down

0 comments on commit 280a03e

Please sign in to comment.