Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/helper/helper_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions tests/helper/helper_kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions tests/helper/helper_oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion tests/helper/helper_podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

. "github.com/onsi/gomega"

"github.com/redhat-developer/odo/pkg/podman"
)

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/helper/odo_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
13 changes: 12 additions & 1 deletion tests/integration/cmd_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down