Fix potential namespace name collision issue with odo create/delete/list/set namespace/project tests (#7050)

* Make sure to delete the namespace generated for the test

Otherwise, it might cause name collisions
upon several subsequent runs of the same tests.

* Be more resilient when trying to delete the namespace/project after each test

Do nothing if the namespace/project no longer exists

* Be more resilient when trying to create the namespace/project before each test

Eventually check the existence of the created namespace/project.

* Rename 'helper#GetProjectName' into 'helper#GenerateProjectName'

This makes the intent clearer.

* Make sure to always delete the random namespace/project created when testing project/namespace deletion
This commit is contained in:
Armel Soro
2023-08-29 13:45:36 +02:00
committed by GitHub
parent 64fd342c89
commit 58954156e6
6 changed files with 37 additions and 15 deletions

View File

@@ -298,7 +298,7 @@ func CommonAfterEach(commonVar CommonVar) {
}
}
if commonVar.Project != "" {
if commonVar.Project != "" && commonVar.CliRunner.HasNamespaceProject(commonVar.Project) {
// delete the random project/namespace created in CommonBeforeEach
commonVar.CliRunner.DeleteNamespaceProject(commonVar.Project, false)
}
@@ -362,8 +362,8 @@ func JsonPathContentHasLen(json string, path string, len int) {
Expect(intVal).To(Equal(len), fmt.Sprintf("%q should contain exactly %d elements", path, len))
}
// GetProjectName sets projectNames based on the name of the test file name (without path and replacing _ with -), line number of current ginkgo execution, and a random string of 3 letters
func GetProjectName() string {
// GenerateProjectName generates a new projectName based on the name of the test file name (without path and replacing _ with -), line number of current ginkgo execution, and a random string of 3 letters
func GenerateProjectName() string {
//Get current test filename and remove file path, file extension and replace undescores with hyphens
currGinkgoTestFileName := strings.Replace(strings.Split(strings.Split(CurrentSpecReport().
ContainerHierarchyLocations[0].FileName, "/")[len(strings.Split(CurrentSpecReport().ContainerHierarchyLocations[0].FileName, "/"))-1], ".")[0], "_", "-", -1)

View File

@@ -167,17 +167,21 @@ func (kubectl KubectlRunner) GetServices(namespace string) string {
// CreateAndSetRandNamespaceProject create and set new project
func (kubectl KubectlRunner) CreateAndSetRandNamespaceProject() string {
projectName := GetProjectName()
projectName := GenerateProjectName()
kubectl.createAndSetRandNamespaceProject(projectName)
return projectName
}
func (kubectl KubectlRunner) createAndSetRandNamespaceProject(projectName string) string {
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
Cmd("kubectl", "create", "namespace", projectName).ShouldPass()
if kubectl.HasNamespaceProject(projectName) {
fmt.Fprintf(GinkgoWriter, "Namespace %q already exists\n", projectName)
} else {
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
Cmd("kubectl", "create", "namespace", projectName).ShouldPass()
}
Cmd("kubectl", "config", "set-context", "--current", "--namespace", projectName).ShouldPass()
session := Cmd("kubectl", "get", "namespaces").ShouldPass().Out()
Expect(session).To(ContainSubstring(projectName))
// ListNamespaceProject makes sure that project eventually appears in the list of all namespaces/projects.
kubectl.ListNamespaceProject(projectName)
kubectl.addConfigMapForCleanup(projectName) // add configmap for cleanup
return projectName
}

View File

@@ -324,7 +324,7 @@ func (oc OcRunner) VerifyResourceToBeDeleted(ri ResourceInfo) {
// CreateAndSetRandNamespaceProject create and set new project
func (oc OcRunner) CreateAndSetRandNamespaceProject() string {
projectName := GetProjectName()
projectName := GenerateProjectName()
oc.createAndSetRandNamespaceProject(projectName)
return projectName
}
@@ -337,9 +337,15 @@ func (oc OcRunner) CreateAndSetRandNamespaceProjectOfLength(i int) string {
}
func (oc OcRunner) createAndSetRandNamespaceProject(projectName string) string {
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
session := Cmd(oc.path, "new-project", projectName).ShouldPass().Out()
Expect(session).To(ContainSubstring(projectName))
if oc.HasNamespaceProject(projectName) {
fmt.Fprintf(GinkgoWriter, "Project %q already exists\n", projectName)
} else {
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
session := Cmd(oc.path, "new-project", projectName).ShouldPass().Out()
Expect(session).To(ContainSubstring(projectName))
}
// ListNamespaceProject makes sure that project eventually appears in the list of all namespaces/projects.
oc.ListNamespaceProject(projectName)
oc.addConfigMapForCleanup(projectName)
return projectName
}

View File

@@ -11,6 +11,7 @@ import (
"strings"
. "github.com/onsi/gomega"
"github.com/redhat-developer/odo/pkg/podman"
)
@@ -28,7 +29,7 @@ func GenerateAndSetContainersConf(dir string) {
if !useNamespaces {
return
}
ns := GetProjectName()
ns := GenerateProjectName()
containersConfPath := filepath.Join(dir, "containers.conf")
err := CreateFileWithContent(containersConfPath, fmt.Sprintf(`
[engine]

View File

@@ -34,7 +34,7 @@ func GetPreferenceValue(key string) string {
// CreateRandProject create new project with random name (10 letters)
// without writing to the config file (without switching project)
func CreateRandProject() string {
projectName := GetProjectName()
projectName := GenerateProjectName()
fmt.Fprintf(GinkgoWriter, "Creating a new project: %s\n", projectName)
session := Cmd("odo", "create", "project", projectName, "-w", "-v4").ShouldPass().Out()
Expect(session).To(ContainSubstring("New project created"))

View File

@@ -30,9 +30,14 @@ var _ = Describe("odo create/delete/list/set namespace/project tests", func() {
// Ref: https://github.com/redhat-developer/odo/issues/6827
var namespace string
BeforeEach(func() {
namespace = helper.GetProjectName()
namespace = helper.GenerateProjectName()
helper.Cmd("odo", "create", "namespace", namespace, "--wait").ShouldPass()
})
AfterEach(func() {
commonVar.CliRunner.DeleteNamespaceProject(namespace, false)
})
It("should list the new namespace when listing namespace", func() {
out := helper.Cmd("odo", "list", "namespace").ShouldPass().Out()
Expect(out).To(ContainSubstring(namespace))
@@ -76,6 +81,12 @@ var _ = Describe("odo create/delete/list/set namespace/project tests", func() {
Expect(commonVar.CliRunner.HasNamespaceProject(namespace)).To(BeTrue())
})
AfterEach(func() {
if commonVar.CliRunner.HasNamespaceProject(namespace) {
commonVar.CliRunner.DeleteNamespaceProject(namespace, false)
}
})
checkNsDeletionFunc := func(wait bool, nsCheckerFunc func()) {
args := []string{"delete", commandName, namespace, "--force"}
if wait {