use more robust project.exists function (#1719)

* use more robust exists function

* checked the error status returned by project

* running only the two tests on travis

* added more error check and made tests better

* reversed the targetted run

* removed the comment

* added comment to GetProject
This commit is contained in:
Girish Ramnani
2019-05-14 20:29:26 +05:30
committed by OpenShift Merge Robot
parent 990928dee0
commit 0237ea2ab9
3 changed files with 33 additions and 14 deletions

View File

@@ -44,6 +44,7 @@ import (
routev1 "github.com/openshift/api/route/v1"
oauthv1client "github.com/openshift/client-go/oauth/clientset/versioned/typed/oauth/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// utilities
@@ -470,6 +471,26 @@ func (c *Client) GetProjectNames() ([]string, error) {
return projectNames, nil
}
// GetProject returns project based on the name of the project.Errors related to
// project not being found or forbidden are translated to nil project for compatibility
func (c *Client) GetProject(projectName string) (*projectv1.Project, error) {
prj, err := c.projectClient.Projects().Get(projectName, metav1.GetOptions{})
if err != nil {
istatus, ok := err.(kerrors.APIStatus)
if ok {
status := istatus.Status()
if status.Reason == metav1.StatusReasonNotFound || status.Reason == metav1.StatusReasonForbidden {
return nil, nil
}
} else {
return nil, err
}
}
return prj, err
}
// CreateNewProject creates project with given projectName
func (c *Client) CreateNewProject(projectName string, wait bool) error {
projectRequest := &projectv1.ProjectRequest{

View File

@@ -69,21 +69,17 @@ func DescribeProjects(client *occlient.Client) (ProjectList, error) {
return getMachineReadableFormatForList(projects), nil
}
// Checks whether a project with the given name exists or not
// Exists Checks whether a project with the given name exists or not
// projectName is the project name to perform check for
// The first returned parameter is a bool indicating if a project with the given name already exists or not
// The second returned parameter is the error that might occurs while execution
func Exists(client *occlient.Client, projectName string) (bool, error) {
projects, err := client.GetProjectNames()
if err != nil {
return false, errors.Wrap(err, "unable to get the project list")
project, err := client.GetProject(projectName)
if err != nil || project == nil {
return false, err
}
for _, project := range projects {
if project == projectName {
return true, nil
}
}
return false, nil
return true, nil
}
func GetMachineReadableFormat(projectName string, isActive bool, apps []string) Project {

View File

@@ -260,20 +260,22 @@ func componentTests(args ...string) {
})
Context("Creating Component even in new project", func() {
var project string
JustBeforeEach(func() {
context = helper.CreateNewContext()
project = helper.RandString(10)
})
JustAfterEach(func() {
os.RemoveAll(context)
helper.DeleteProject(project)
})
It("should create component", func() {
helper.CmdShouldPass("odo", append(args, "create", "nodejs", "cmp-git", "--git", "https://github.com/openshift/nodejs-ex", "--project", "new-project", "--context", context, "--app", "testing")...)
helper.CmdShouldPass("odo", append(args, "create", "nodejs", "cmp-git", "--git", "https://github.com/openshift/nodejs-ex", "--project", project, "--context", context, "--app", "testing")...)
helper.CmdShouldPass("odo", "push", "--context", context, "-v4")
oc.SwitchProject("new-project")
oc.SwitchProject(project)
projectList := helper.CmdShouldPass("odo", "project", "list")
Expect(projectList).To(ContainSubstring("new-project"))
helper.DeleteProject("new-project")
Expect(projectList).To(ContainSubstring(project))
})
})