From c8f21e9e127d75a3f2031ed16cf6e111a0ece8c0 Mon Sep 17 00:00:00 2001 From: Armel Soro Date: Mon, 16 May 2022 15:48:32 +0200 Subject: [PATCH] Check for namespace/project existence with '{oc,kubectl} get {namespace,project} ' For some reason, checking by listing all namespaces/projects could end up returning an outdated list. --- tests/helper/helper_cli.go | 5 ++++- tests/helper/helper_kubectl.go | 6 ++++-- tests/helper/helper_oc.go | 6 ++++-- tests/integration/cmd_namespace_test.go | 12 ++++++------ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/helper/helper_cli.go b/tests/helper/helper_cli.go index 2ba3d0be787..7fcc4c00c00 100644 --- a/tests/helper/helper_cli.go +++ b/tests/helper/helper_cli.go @@ -20,7 +20,10 @@ type CliRunner interface { DeletePod(podName string, projectName string) GetAllNamespaceProjects() []string GetNamespaceProject() string - CheckNamespaceProjectExists(name string) bool + + // HasNamespaceProject returns whether the specified namespace or project exists in the cluster + HasNamespaceProject(name string) bool + GetActiveNamespace() string GetEnvsDevFileDeployment(componentName, appName, projectName string) map[string]string GetEnvRefNames(componentName, appName, projectName string) []string diff --git a/tests/helper/helper_kubectl.go b/tests/helper/helper_kubectl.go index fcb414d537e..365f092bd10 100644 --- a/tests/helper/helper_kubectl.go +++ b/tests/helper/helper_kubectl.go @@ -383,8 +383,10 @@ func (kubectl KubectlRunner) GetNamespaceProject() string { return Cmd(kubectl.path, "get", "namespace").ShouldPass().Out() } -func (kubectl KubectlRunner) CheckNamespaceProjectExists(name string) bool { - return Cmd(kubectl.path, "get", "namespace", name).ShouldPass().pass +func (kubectl KubectlRunner) HasNamespaceProject(name string) bool { + out := Cmd(kubectl.path, "get", "namespace", name, "-o", "jsonpath={.metadata.name}"). + ShouldRun().Out() + return strings.Contains(out, name) } func (kubectl KubectlRunner) GetActiveNamespace() string { diff --git a/tests/helper/helper_oc.go b/tests/helper/helper_oc.go index 32145b17f57..e75e8f8a3af 100644 --- a/tests/helper/helper_oc.go +++ b/tests/helper/helper_oc.go @@ -575,8 +575,10 @@ func (oc OcRunner) GetNamespaceProject() string { return Cmd(oc.path, "get", "project").ShouldPass().Out() } -func (oc OcRunner) CheckNamespaceProjectExists(name string) bool { - return Cmd(oc.path, "get", "project", name).ShouldPass().pass +func (oc OcRunner) HasNamespaceProject(name string) bool { + out := Cmd(oc.path, "get", "project", name, "-o", "jsonpath={.metadata.name}"). + ShouldRun().Out() + return strings.Contains(out, name) } func (oc OcRunner) GetActiveNamespace() string { diff --git a/tests/integration/cmd_namespace_test.go b/tests/integration/cmd_namespace_test.go index 283044dfeaa..28b1c570ce4 100644 --- a/tests/integration/cmd_namespace_test.go +++ b/tests/integration/cmd_namespace_test.go @@ -33,7 +33,7 @@ var _ = Describe("odo create/delete/list/set namespace/project tests", func() { defer func(ns string) { commonVar.CliRunner.DeleteNamespaceProject(ns) }(namespace) - Expect(commonVar.CliRunner.CheckNamespaceProjectExists(namespace)).To(BeTrue()) + Expect(commonVar.CliRunner.HasNamespaceProject(namespace)).To(BeTrue()) Expect(commonVar.CliRunner.GetActiveNamespace()).To(Equal(namespace)) }) @@ -55,7 +55,7 @@ var _ = Describe("odo create/delete/list/set namespace/project tests", func() { BeforeEach(func() { namespace = helper.CreateRandProject() - Expect(commonVar.CliRunner.CheckNamespaceProjectExists(namespace)).To(BeTrue()) + Expect(commonVar.CliRunner.HasNamespaceProject(namespace)).To(BeTrue()) }) checkNsDeletionFunc := func(additionalArgs []string, nsCheckerFunc func()) { @@ -73,15 +73,15 @@ var _ = Describe("odo create/delete/list/set namespace/project tests", func() { It(fmt.Sprintf("should successfully delete the %s asynchronously", commandName), func() { checkNsDeletionFunc(nil, func() { - Eventually(func() []string { - return commonVar.CliRunner.GetAllNamespaceProjects() - }, 60*time.Second).ShouldNot(ContainElement(namespace)) + Eventually(func() bool { + return commonVar.CliRunner.HasNamespaceProject(namespace) + }, 60*time.Second).Should(BeFalse()) }) }) It(fmt.Sprintf("should successfully delete the %s synchronously with --wait", commandName), func() { checkNsDeletionFunc([]string{"--wait"}, func() { - Expect(commonVar.CliRunner.GetAllNamespaceProjects()).ShouldNot(ContainElement(namespace)) + Expect(commonVar.CliRunner.HasNamespaceProject(namespace)).To(BeFalse()) }) }) })