Skip to content

Commit

Permalink
Fix git-init in case master is not the default branch
Browse files Browse the repository at this point in the history
This fix will patch the `git-init` such that if user doesn't provides revision then instead of taking `master` as the default branch we are doing the `symbolic-link` to the HEAD branch. Also added e2e tests for it.

Signed-off-by: vinamra28 <[email protected]>
  • Loading branch information
vinamra28 committed Jun 23, 2020
1 parent a7ce319 commit 6eff8c4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 10 deletions.
5 changes: 1 addition & 4 deletions pkg/apis/resource/v1alpha1/git/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ func NewResource(name, gitImage string, r *resource.PipelineResource) (*Resource
gitResource.NOProxy = param.Value
}
}
// default revision to master if nothing is provided
if gitResource.Revision == "" {
gitResource.Revision = "master"
}

return &gitResource, nil
}

Expand Down
44 changes: 41 additions & 3 deletions pkg/apis/resource/v1alpha1/git/git_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestNewGitResource_Valid(t *testing.T) {
Name: "test-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "master",
Revision: "",
Refspec: "",
GitImage: "override-with-git:latest",
Submodules: true,
Expand All @@ -96,7 +96,7 @@ func TestNewGitResource_Valid(t *testing.T) {
Name: "test-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "master",
Revision: "",
Refspec: "refs/changes/22/222134",
GitImage: "override-with-git:latest",
Submodules: true,
Expand All @@ -117,7 +117,7 @@ func TestNewGitResource_Valid(t *testing.T) {
Name: "test-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "master",
Revision: "",
Refspec: "",
GitImage: "override-with-git:latest",
Submodules: true,
Expand Down Expand Up @@ -675,6 +675,44 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
{Name: "NO_PROXY", Value: "no-proxy.git.com"},
},
},
}, {
desc: "Without Refspec and without revision",
gitResource: &git.Resource{
Name: "git-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "",
Refspec: "",
GitImage: "override-with-git:latest",
Submodules: false,
Depth: 1,
SSLVerify: true,
HTTPProxy: "http-proxy.git.com",
HTTPSProxy: "https-proxy.git.com",
NOProxy: "no-proxy.git.com",
},
want: corev1.Container{
Name: "git-source-git-resource-twkr2",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"",
"-path",
"/test/test",
"-submodules=false",
},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "git-resource"},
{Name: "HOME", Value: pipeline.HomeDir},
{Name: "HTTP_PROXY", Value: "http-proxy.git.com"},
{Name: "HTTPS_PROXY", Value: "https-proxy.git.com"},
{Name: "NO_PROXY", Value: "no-proxy.git.com"},
},
},
}} {
t.Run(tc.desc, func(t *testing.T) {
ts := v1beta1.TaskSpec{}
Expand Down
9 changes: 6 additions & 3 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
return err
}

if spec.Revision == "" {
spec.Revision = "master"
}
if spec.Path != "" {
if _, err := run(logger, "", "init", spec.Path); err != nil {
return err
Expand All @@ -85,6 +82,12 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
logger.Warnf("Failed to set http.sslVerify in git config: %s", err)
return err
}
if spec.Revision == "" {
spec.Revision = "HEAD"
if _, err := run(logger, "", "symbolic-ref", spec.Revision, "refs/remotes/origin/HEAD"); err != nil {
return err
}
}

fetchArgs := []string{"fetch"}
if spec.Submodules {
Expand Down
57 changes: 57 additions & 0 deletions test/git_checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,63 @@ func TestGitPipelineRunFail_HTTPS_PROXY(t *testing.T) {
}
}

// TestGitPipelineRunWithNonMasterBranch is an integration test that will verify the source code is either fetched or pulled
// successfully under different revision inputs (default branch, branch)
// This test will run on spring-petclinic repository which does not contain a master branch as the default branch
func TestGitPipelineRunWithNonMasterBranch(t *testing.T) {
t.Parallel()

revisions := []string{"", "main"}

for _, revision := range revisions {

t.Run(revision, func(t *testing.T) {
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)

t.Logf("Creating Git PipelineResource %s", gitSourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResourceSpringPetClinic(revision, "", "true", "", "", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}

t.Logf("Creating Task %s", gitTestTaskName)
if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}

t.Logf("Creating Pipeline %s", gitTestPipelineName)
if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}

t.Logf("Creating PipelineRun %s", gitTestPipelineRunName)
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}

if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", gitTestPipelineRunName, err)
t.Fatalf("PipelineRun execution failed")
}
})
}
}

// getGitPipelineResourceSpringPetClinic will help to clone the spring-petclinic repository which does not contains master branch
func getGitPipelineResourceSpringPetClinic(revision, refspec, sslverify, httpproxy, httpsproxy, noproxy string) *v1alpha1.PipelineResource {
return tb.PipelineResource(gitSourceResourceName, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("Url", "https:/spring-projects/spring-petclinic"),
tb.PipelineResourceSpecParam("Revision", revision),
tb.PipelineResourceSpecParam("Refspec", refspec),
tb.PipelineResourceSpecParam("sslVerify", sslverify),
tb.PipelineResourceSpecParam("httpProxy", httpproxy),
tb.PipelineResourceSpecParam("httpsProxy", httpsproxy),
tb.PipelineResourceSpecParam("noProxy", noproxy),
))
}

func getGitPipelineResource(revision, refspec, sslverify, httpproxy, httpsproxy, noproxy string) *v1alpha1.PipelineResource {
return tb.PipelineResource(gitSourceResourceName, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
Expand Down
57 changes: 57 additions & 0 deletions test/v1alpha1/git_checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,63 @@ func TestGitPipelineRunFail_HTTPS_PROXY(t *testing.T) {
}
}

// TestGitPipelineRunWithNonMasterBranch is an integration test that will verify the source code is either fetched or pulled
// successfully under different revision inputs (default branch, branch)
// This test will run on spring-petclinic repository which does not contain a master branch as the default branch
func TestGitPipelineRunWithNonMasterBranch(t *testing.T) {
t.Parallel()

revisions := []string{"", "main"}

for _, revision := range revisions {

t.Run(revision, func(t *testing.T) {
c, namespace := setup(t)
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf)
defer tearDown(t, c, namespace)

t.Logf("Creating Git PipelineResource %s", gitSourceResourceName)
if _, err := c.PipelineResourceClient.Create(getGitPipelineResourceSpringPetClinic(revision, "", "true", "", "", "")); err != nil {
t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err)
}

t.Logf("Creating Task %s", gitTestTaskName)
if _, err := c.TaskClient.Create(getGitCheckTask()); err != nil {
t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err)
}

t.Logf("Creating Pipeline %s", gitTestPipelineName)
if _, err := c.PipelineClient.Create(getGitCheckPipeline()); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err)
}

t.Logf("Creating PipelineRun %s", gitTestPipelineRunName)
if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun()); err != nil {
t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err)
}

if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil {
t.Errorf("Error waiting for PipelineRun %s to finish: %s", gitTestPipelineRunName, err)
t.Fatalf("PipelineRun execution failed")
}
})
}
}

// getGitPipelineResourceSpringPetClinic will help to clone the spring-petclinic repository which does not contains master branch
func getGitPipelineResourceSpringPetClinic(revision, refspec, sslverify, httpproxy, httpsproxy, noproxy string) *v1alpha1.PipelineResource {
return tb.PipelineResource(gitSourceResourceName, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("Url", "https:/spring-projects/spring-petclinic"),
tb.PipelineResourceSpecParam("Revision", revision),
tb.PipelineResourceSpecParam("Refspec", refspec),
tb.PipelineResourceSpecParam("sslVerify", sslverify),
tb.PipelineResourceSpecParam("httpProxy", httpproxy),
tb.PipelineResourceSpecParam("httpsProxy", httpsproxy),
tb.PipelineResourceSpecParam("noProxy", noproxy),
))
}

func getGitPipelineResource(revision, refspec, sslverify, httpproxy, httpsproxy, noproxy string) *v1alpha1.PipelineResource {
return tb.PipelineResource(gitSourceResourceName, tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypeGit,
Expand Down

0 comments on commit 6eff8c4

Please sign in to comment.