diff --git a/pkg/validation/projects.go b/pkg/validation/projects.go index 61e098fd5..ea4e8880d 100644 --- a/pkg/validation/projects.go +++ b/pkg/validation/projects.go @@ -21,15 +21,18 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) error { continue } - if len(gitSource.Remotes) == 0 { + switch len(gitSource.Remotes) { + case 0: errString += fmt.Sprintf("\nstarterProject %s should have at least one remote", starterProject.Name) - } else if len(gitSource.Remotes) > 1 { - errString += fmt.Sprintf("\nstarterProject %s should have one remote only", starterProject.Name) - } else if gitSource.CheckoutFrom.Remote != "" { - err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, starterProject.Name) - if err != nil { - errString += fmt.Sprintf("\n%s", err.Error()) + case 1: + if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" { + err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, starterProject.Name) + if err != nil { + errString += fmt.Sprintf("\n%s", err.Error()) + } } + default: // len(gitSource.Remotes) >= 2 + errString += fmt.Sprintf("\nstarterProject %s should have one remote only", starterProject.Name) } } @@ -56,11 +59,21 @@ func ValidateProjects(projects []v1alpha2.Project) error { continue } - if len(gitSource.Remotes) == 0 { + switch len(gitSource.Remotes) { + case 0: errString += fmt.Sprintf("\nprojects %s should have at least one remote", project.Name) - } else if len(gitSource.Remotes) > 1 || gitSource.CheckoutFrom.Remote != "" { - err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name) - if err != nil { + case 1: + if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" { + if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil { + errString += fmt.Sprintf("\n%s", err.Error()) + } + } + default: // len(gitSource.Remotes) >= 2 + if gitSource.CheckoutFrom == nil || gitSource.CheckoutFrom.Remote == "" { + errString += fmt.Sprintf("\nproject %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name) + continue + } + if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil { errString += fmt.Sprintf("\n%s", err.Error()) } } diff --git a/pkg/validation/projects_test.go b/pkg/validation/projects_test.go index 07a60cece..c32853e71 100644 --- a/pkg/validation/projects_test.go +++ b/pkg/validation/projects_test.go @@ -7,64 +7,56 @@ import ( "github.com/stretchr/testify/assert" ) -func generateDummyGitStarterProject(name, checkoutRemote string, remotes map[string]string) v1alpha2.StarterProject { +func generateDummyGitStarterProject(name string, checkoutRemote *v1alpha2.CheckoutFrom, remotes map[string]string) v1alpha2.StarterProject { return v1alpha2.StarterProject{ Name: name, ProjectSource: v1alpha2.ProjectSource{ Git: &v1alpha2.GitProjectSource{ GitLikeProjectSource: v1alpha2.GitLikeProjectSource{ - Remotes: remotes, - CheckoutFrom: &v1alpha2.CheckoutFrom{ - Remote: checkoutRemote, - }, + Remotes: remotes, + CheckoutFrom: checkoutRemote, }, }, }, } } -func generateDummyGithubStarterProject(name, checkoutRemote string, remotes map[string]string) v1alpha2.StarterProject { +func generateDummyGithubStarterProject(name string, checkoutRemote *v1alpha2.CheckoutFrom, remotes map[string]string) v1alpha2.StarterProject { return v1alpha2.StarterProject{ Name: name, ProjectSource: v1alpha2.ProjectSource{ Github: &v1alpha2.GithubProjectSource{ GitLikeProjectSource: v1alpha2.GitLikeProjectSource{ - Remotes: remotes, - CheckoutFrom: &v1alpha2.CheckoutFrom{ - Remote: checkoutRemote, - }, + Remotes: remotes, + CheckoutFrom: checkoutRemote, }, }, }, } } -func generateDummyGitProject(name, checkoutRemote string, remotes map[string]string) v1alpha2.Project { +func generateDummyGitProject(name string, checkoutRemote *v1alpha2.CheckoutFrom, remotes map[string]string) v1alpha2.Project { return v1alpha2.Project{ Name: name, ProjectSource: v1alpha2.ProjectSource{ Git: &v1alpha2.GitProjectSource{ GitLikeProjectSource: v1alpha2.GitLikeProjectSource{ - Remotes: remotes, - CheckoutFrom: &v1alpha2.CheckoutFrom{ - Remote: checkoutRemote, - }, + Remotes: remotes, + CheckoutFrom: checkoutRemote, }, }, }, } } -func generateDummyGithubProject(name, checkoutRemote string, remotes map[string]string) v1alpha2.Project { +func generateDummyGithubProject(name string, checkoutRemote *v1alpha2.CheckoutFrom, remotes map[string]string) v1alpha2.Project { return v1alpha2.Project{ Name: name, ProjectSource: v1alpha2.ProjectSource{ Github: &v1alpha2.GithubProjectSource{ GitLikeProjectSource: v1alpha2.GitLikeProjectSource{ - Remotes: remotes, - CheckoutFrom: &v1alpha2.CheckoutFrom{ - Remote: checkoutRemote, - }, + Remotes: remotes, + CheckoutFrom: checkoutRemote, }, }, }, @@ -85,35 +77,41 @@ func TestValidateStarterProjects(t *testing.T) { { name: "Valid Starter Project", starterProjects: []v1alpha2.StarterProject{ - generateDummyGitStarterProject("project1", "origin", map[string]string{"origin": "originremote"}), - generateDummyGitStarterProject("project2", "origin", map[string]string{"origin": "originremote2"}), + generateDummyGitStarterProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote"}), + generateDummyGitStarterProject("project2", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote2"}), }, }, { name: "Invalid Starter Project", starterProjects: []v1alpha2.StarterProject{ - generateDummyGithubStarterProject("project1", "origin", map[string]string{"origin": "originremote", "test": "testremote"}), + generateDummyGithubStarterProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote", "test": "testremote"}), }, wantErr: &oneRemoteErr, }, { name: "Invalid Starter Project with wrong checkout", starterProjects: []v1alpha2.StarterProject{ - generateDummyGithubStarterProject("project1", "origin", map[string]string{"test": "testremote"}), + generateDummyGithubStarterProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"test": "testremote"}), }, wantErr: &wrongCheckoutErr, }, { name: "Valid Starter Project with empty checkout remote", starterProjects: []v1alpha2.StarterProject{ - generateDummyGitStarterProject("project1", "", map[string]string{"origin": "originremote"}), + generateDummyGitStarterProject("project1", &v1alpha2.CheckoutFrom{Remote: ""}, map[string]string{"origin": "originremote"}), + }, + }, + { + name: "Valid Starter Project with no checkout remote", + starterProjects: []v1alpha2.StarterProject{ + generateDummyGitStarterProject("project1", nil, map[string]string{"origin": "originremote"}), }, }, { name: "Invalid Starter Project with empty remotes", starterProjects: []v1alpha2.StarterProject{ - generateDummyGithubStarterProject("project1", "origin", map[string]string{}), - generateDummyGithubStarterProject("project3", "origin", map[string]string{"origin": "originremote", "test": "testremote"}), + generateDummyGithubStarterProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{}), + generateDummyGithubStarterProject("project3", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote", "test": "testremote"}), }, wantErr: &atleastOneRemoteErr, }, @@ -135,6 +133,7 @@ func TestValidateProjects(t *testing.T) { wrongCheckoutErr := "unable to find the checkout remote .* in the remotes for project.*" atleastOneRemoteErr := "projects .* should have at least one remote" + missingCheckOutFromRemoteErr := "project .* has more than one remote defined, but has no checkoutfrom remote defined" tests := []struct { name string @@ -144,37 +143,44 @@ func TestValidateProjects(t *testing.T) { { name: "Valid Project", projects: []v1alpha2.Project{ - generateDummyGitProject("project1", "origin", map[string]string{"origin": "originremote"}), - generateDummyGithubProject("project2", "origin", map[string]string{"origin": "originremote"}), + generateDummyGitProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote"}), + generateDummyGithubProject("project2", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote"}), }, }, + { + name: "Invalid Project with multiple remotes but no checkoutfrom", + projects: []v1alpha2.Project{ + generateDummyGithubProject("project2", nil, map[string]string{"origin": "originremote", "test": "testremote"}), + }, + wantErr: &missingCheckOutFromRemoteErr, + }, { name: "Invalid Project with multiple remote and empty checkout remote", projects: []v1alpha2.Project{ - generateDummyGitProject("project2", "origin", map[string]string{"origin": "originremote"}), - generateDummyGithubProject("project1", "", map[string]string{"origin": "originremote", "test": "testremote"}), + generateDummyGitProject("project2", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{"origin": "originremote"}), + generateDummyGithubProject("project1", &v1alpha2.CheckoutFrom{Remote: ""}, map[string]string{"origin": "originremote", "test": "testremote"}), }, - wantErr: &wrongCheckoutErr, + wantErr: &missingCheckOutFromRemoteErr, }, { name: "Invalid Project with wrong checkout", projects: []v1alpha2.Project{ - generateDummyGithubProject("project1", "origin", map[string]string{"origin": "originremote", "test": "testremote"}), - generateDummyGitProject("project2", "origin1", map[string]string{"origin2": "originremote2"}), + generateDummyGithubProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin1"}, map[string]string{"origin": "originremote", "test": "testremote"}), + generateDummyGitProject("project2", &v1alpha2.CheckoutFrom{Remote: "origin1"}, map[string]string{"origin2": "originremote2"}), }, wantErr: &wrongCheckoutErr, }, { name: "Valid Project with empty checkout remote", projects: []v1alpha2.Project{ - generateDummyGitProject("project1", "", map[string]string{"origin": "originremote"}), + generateDummyGitProject("project1", &v1alpha2.CheckoutFrom{Remote: ""}, map[string]string{"origin": "originremote"}), }, }, { name: "Invalid Project with empty remotes", projects: []v1alpha2.Project{ - generateDummyGitProject("project1", "origin", map[string]string{}), - generateDummyGithubProject("project2", "origins", map[string]string{"origin": "originremote", "test": "testremote"}), + generateDummyGitProject("project1", &v1alpha2.CheckoutFrom{Remote: "origin"}, map[string]string{}), + generateDummyGithubProject("project2", &v1alpha2.CheckoutFrom{Remote: "origins"}, map[string]string{"origin": "originremote", "test": "testremote"}), }, wantErr: &atleastOneRemoteErr, },