From 478ecc7dfc60dd91b8a4fc356cbfcae55bf8ea74 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Thu, 10 Sep 2020 13:47:56 -0400 Subject: [PATCH] Clean up e2e Git PipelineResource tests - don't use test builders - collapse various tests to table-driven tests - remove unused/unnecessary params - copy v1beta1 test to v1alpha1 --- test/git_checkout_test.go | 536 +++++++++++------------------ test/multiarch_utils.go | 3 - test/v1alpha1/git_checkout_test.go | 501 +++++++++++---------------- 3 files changed, 387 insertions(+), 653 deletions(-) diff --git a/test/git_checkout_test.go b/test/git_checkout_test.go index c7dedb6a0bc..8179433f0d7 100644 --- a/test/git_checkout_test.go +++ b/test/git_checkout_test.go @@ -22,10 +22,8 @@ import ( "strings" "testing" - tb "github.com/tektoncd/pipeline/internal/builder/v1beta1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - resources "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -38,40 +36,123 @@ const ( gitTestPipelineRunName = "git-check-pipeline-run" ) -// TestGitPipelineRun is an integration test that will verify the source code is either fetched or pulled -// successfully under different revision inputs (branch, commitid, tag, ref) +// TestGitPipelineRun is an integration test that will verify the source code +// is either fetched or pulled successfully under different resource +// parameters. func TestGitPipelineRun(t *testing.T) { - SkipIfExcluded(t) - - t.Parallel() - - revisions := []string{"master", "c15aced0e5aaee6456fbe6f7a7e95e0b5b3b2b2f", "release-0.1", "v0.1.0", "refs/pull/347/head"} - - for _, revision := range revisions { - - t.Run(revision, func(t *testing.T) { + for _, tc := range []struct { + name string + repo string + revision string + refspec string + sslVerify string + }{{ + name: "tekton @ master", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + }, { + name: "tekton @ commit", + repo: "https://github.com/tektoncd/pipeline", + revision: "c15aced0e5aaee6456fbe6f7a7e95e0b5b3b2b2f", + }, { + name: "tekton @ release", + repo: "https://github.com/tektoncd/pipeline", + revision: "release-0.1", + }, { + name: "tekton @ tag", + repo: "https://github.com/tektoncd/pipeline", + revision: "v0.1.0", + }, { + name: "tekton @ PR ref", + repo: "https://github.com/tektoncd/pipeline", + revision: "refs/pull/347/head", + }, { + name: "tekton @ master with refspec", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + refspec: "refs/tags/v0.1.0:refs/tags/v0.1.0 refs/heads/master:refs/heads/master", + }, { + name: "tekton @ commit with PR refspec", + repo: "https://github.com/tektoncd/pipeline", + revision: "968d5d37a61bfb85426c885dc1090c1cc4b33436", + refspec: "refs/pull/1009/head", + }, { + name: "tekton @ master with PR refspec", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + refspec: "refs/pull/1009/head:refs/heads/master", + }, { + name: "tekton @ master with sslverify=false", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + sslVerify: "false", + }, { + name: "non-master repo with default revision", + repo: "https://github.com/spring-projects/spring-petclinic", + revision: "", + }, { + name: "non-master repo with main revision", + repo: "https://github.com/spring-projects/spring-petclinic", + revision: "main", + }} { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() 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(getGitPipelineResource(revision, "", "true", "", "", "")); err != nil { + if _, err := c.PipelineResourceClient.Create(&v1alpha1.PipelineResource{ + ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, + Spec: v1alpha1.PipelineResourceSpec{ + Type: v1alpha1.PipelineResourceTypeGit, + Params: []v1alpha1.ResourceParam{ + {Name: "Url", Value: tc.repo}, + {Name: "Revision", Value: tc.revision}, + {Name: "Refspec", Value: tc.refspec}, + {Name: "sslVerify", Value: tc.sslVerify}, + }, + }, + }); 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 := c.PipelineRunClient.Create(&v1beta1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, + Spec: v1beta1.PipelineRunSpec{ + Resources: []v1beta1.PipelineResourceBinding{{ + Name: "git-repo", + ResourceRef: &v1beta1.PipelineResourceRef{Name: gitSourceResourceName}, + }}, + PipelineSpec: &v1beta1.PipelineSpec{ + Resources: []v1beta1.PipelineDeclaredResource{{ + Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, + }}, + Tasks: []v1beta1.PipelineTask{{ + Name: "git-check", + TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: &v1beta1.TaskSpec{ + Resources: &v1beta1.TaskResources{ + Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ + Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, + }}}, + }, + Steps: []v1beta1.Step{{Container: corev1.Container{ + Image: "alpine/git", + Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, + }}}, + }}, + Resources: &v1beta1.PipelineTaskResources{ + Inputs: []v1beta1.PipelineTaskInputResource{{ + Name: "gitsource", + Resource: "git-repo", + }}, + }, + }}, + }, + }, + }); err != nil { + t.Fatalf("Failed to create PipelineRun %q: %s", gitTestPipelineRunName, err) } if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil { @@ -82,347 +163,116 @@ func TestGitPipelineRun(t *testing.T) { } } -// Test revision fetching with refspec specified -func TestGitPipelineRunWithRefspec(t *testing.T) { - SkipIfExcluded(t) - - t.Parallel() - +// TestGitPipelineRunFail is a test to ensure that the code extraction from +// github fails as expected when an invalid revision or https proxy is passed +// on the pipelineresource. +func TestGitPipelineRunFail(t *testing.T) { for _, tc := range []struct { - description string - revision string - refspec string + name string + revision string + httpsproxy string }{{ - description: "Fetch refs/tags/v0.1.0 alongside master and checkout the master branch", - revision: "master", - refspec: "refs/tags/v0.1.0:refs/tags/v0.1.0 refs/heads/master:refs/heads/master", - }, { - description: "Checkout specific revision from refs/pull/1009/head's commit chain", - revision: "968d5d37a61bfb85426c885dc1090c1cc4b33436", - refspec: "refs/pull/1009/head", + name: "invalid revision", + revision: "Idontexistrabbitmonkeydonkey", }, { - description: "Fetch refs/pull/1009/head into a named master branch and then check it out", - revision: "master", - refspec: "refs/pull/1009/head:refs/heads/master", + name: "invalid httpsproxy", + httpsproxy: "invalid.https.proxy.example.com", }} { - t.Run(tc.description, func(t *testing.T) { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() c, namespace := setup(t) knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf) defer tearDown(t, c, namespace) - if _, err := c.PipelineResourceClient.Create(getGitPipelineResource(tc.revision, tc.refspec, "true", "", "", "")); err != nil { + t.Logf("Creating Git PipelineResource %s", gitSourceResourceName) + if _, err := c.PipelineResourceClient.Create(&v1alpha1.PipelineResource{ + ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, + Spec: v1alpha1.PipelineResourceSpec{ + Type: v1alpha1.PipelineResourceTypeGit, + Params: []v1alpha1.ResourceParam{ + {Name: "Url", Value: "https://github.com/tektoncd/pipeline"}, + {Name: "Revision", Value: tc.revision}, + {Name: "httpsProxy", Value: tc.httpsproxy}, + }, + }, + }); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err) } - if _, err := c.TaskClient.Create(getGitCheckTask(namespace)); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err) - } - - if _, err := c.PipelineClient.Create(getGitCheckPipeline(namespace)); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err) - } - - if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun(namespace)); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err) + t.Logf("Creating PipelineRun %s", gitTestPipelineRunName) + if _, err := c.PipelineRunClient.Create(&v1beta1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, + Spec: v1beta1.PipelineRunSpec{ + Resources: []v1beta1.PipelineResourceBinding{{ + Name: "git-repo", + ResourceRef: &v1beta1.PipelineResourceRef{Name: gitSourceResourceName}, + }}, + PipelineSpec: &v1beta1.PipelineSpec{ + Resources: []v1beta1.PipelineDeclaredResource{{ + Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, + }}, + Tasks: []v1beta1.PipelineTask{{ + Name: "git-check", + TaskSpec: &v1beta1.EmbeddedTask{TaskSpec: &v1beta1.TaskSpec{ + Resources: &v1beta1.TaskResources{ + Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ + Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, + }}}, + }, + Steps: []v1beta1.Step{{Container: corev1.Container{ + Image: "alpine/git", + Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, + }}}, + }}, + Resources: &v1beta1.PipelineTaskResources{ + Inputs: []v1beta1.PipelineTaskInputResource{{ + Name: "gitsource", + Resource: "git-repo", + }}, + }, + }}, + }, + }, + }); err != nil { + t.Fatalf("Failed to create PipelineRun %q: %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") - } - - }) - } -} - -// TestGitPipelineRun_Disable_SSLVerify will verify the source code is retrieved even after disabling SSL certificates (sslVerify) -func TestGitPipelineRun_Disable_SSLVerify(t *testing.T) { - t.Parallel() - - 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(getGitPipelineResource("master", "", "false", "", "", "")); 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") - } -} - -// TestGitPipelineRunFail is a test to ensure that the code extraction from github fails as expected when -// an invalid revision is passed on the pipelineresource. -func TestGitPipelineRunFail(t *testing.T) { - t.Parallel() - - 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(getGitPipelineResource("Idontexistrabbitmonkeydonkey", "", "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 { - taskruns, err := c.TaskRunClient.List(metav1.ListOptions{}) - if err != nil { - t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err) - } - for _, tr := range taskruns.Items { - if tr.Status.PodName != "" { - p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) + taskruns, err := c.TaskRunClient.List(metav1.ListOptions{}) if err != nil { - t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) + t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err) } - - for _, stat := range p.Status.ContainerStatuses { - if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) { - if stat.State.Terminated != nil { - req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) - logContent, err := req.Do().Raw() - if err != nil { - t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) - } - // Check for failure messages from fetch and pull in the log file - if strings.Contains(strings.ToLower(string(logContent)), "couldn't find remote ref idontexistrabbitmonkeydonkey") { - t.Logf("Found exepected errors when retrieving non-existent git revision") - } else { - t.Logf("Container `%s` log File: %s", stat.Name, logContent) - t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.") - } + for _, tr := range taskruns.Items { + if tr.Status.PodName != "" { + p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) + if err != nil { + t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) } - } - } - } - } - - } else { - t.Fatalf("PipelineRun succeeded when should have failed") - } -} - -// TestGitPipelineRunFail_HTTPS_PROXY is a test to ensure that the code extraction from github fails as expected when -// an invalid HTTPS_PROXY is passed on the pipelineresource. -func TestGitPipelineRunFail_HTTPS_PROXY(t *testing.T) { - t.Parallel() - - 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(getGitPipelineResource("master", "", "true", "", "invalid.https.proxy.com", "")); 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 { - taskruns, err := c.TaskRunClient.List(metav1.ListOptions{}) - if err != nil { - t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err) - } - for _, tr := range taskruns.Items { - if tr.Status.PodName != "" { - p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) - if err != nil { - t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) - } - - for _, stat := range p.Status.ContainerStatuses { - if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) { - if stat.State.Terminated != nil { - req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) - logContent, err := req.Do().Raw() - if err != nil { - t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) - } - // Check for failure messages from fetch and pull in the log file - if strings.Contains(strings.ToLower(string(logContent)), "could not resolve proxy: invalid.https.proxy.com") { - t.Logf("Found exepected errors when using non-existent https proxy") - } else { - t.Logf("Container `%s` log File: %s", stat.Name, logContent) - t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.") + for _, stat := range p.Status.ContainerStatuses { + if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) { + if stat.State.Terminated != nil { + req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) + logContent, err := req.Do().Raw() + if err != nil { + t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) + } + // Check for failure messages from fetch and pull in the log file + if strings.Contains(strings.ToLower(string(logContent)), "couldn't find remote ref idontexistrabbitmonkeydonkey") { + t.Logf("Found exepected errors when retrieving non-existent git revision") + } else { + t.Logf("Container `%s` log File: %s", stat.Name, logContent) + t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.") + } + } } } } } - } - } - } else { - t.Fatalf("PipelineRun succeeded when should have failed") - } -} - -// 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) { - SkipIfExcluded(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") + } else { + t.Fatalf("PipelineRun succeeded when should have 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://github.com/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, - tb.PipelineResourceSpecParam("Url", "https://github.com/tektoncd/pipeline"), - 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 getGitCheckTask(namespace string) *v1beta1.Task { - return &v1beta1.Task{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestTaskName, Namespace: namespace}, - Spec: v1beta1.TaskSpec{ - Resources: &v1beta1.TaskResources{ - Inputs: []v1beta1.TaskResource{{ResourceDeclaration: v1beta1.ResourceDeclaration{ - Name: "gitsource", Type: resources.PipelineResourceTypeGit, - }}}, - }, - Steps: []v1beta1.Step{{Container: corev1.Container{ - Image: "alpine/git", - Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, - }}}, - }, - } -} - -func getGitCheckPipeline(namespace string) *v1beta1.Pipeline { - return &v1beta1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineName, Namespace: namespace}, - Spec: v1beta1.PipelineSpec{ - Resources: []v1beta1.PipelineDeclaredResource{{ - Name: "git-repo", Type: resources.PipelineResourceTypeGit, - }}, - Tasks: []v1beta1.PipelineTask{{ - Name: "git-check", - TaskRef: &v1beta1.TaskRef{Name: gitTestTaskName}, - Resources: &v1beta1.PipelineTaskResources{ - Inputs: []v1beta1.PipelineTaskInputResource{{ - Name: "gitsource", - Resource: "git-repo", - }}, - }, - }}, - }, - } - -} - -func getGitCheckPipelineRun(namespace string) *v1beta1.PipelineRun { - return &v1beta1.PipelineRun{ - ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName, Namespace: namespace}, - Spec: v1beta1.PipelineRunSpec{ - PipelineRef: &v1beta1.PipelineRef{Name: gitTestPipelineName}, - Resources: []v1beta1.PipelineResourceBinding{{ - Name: "git-repo", - ResourceRef: &v1beta1.PipelineResourceRef{Name: gitSourceResourceName}, - }}, - }, - } -} diff --git a/test/multiarch_utils.go b/test/multiarch_utils.go index 20d39be0ffb..c72b96d605b 100644 --- a/test/multiarch_utils.go +++ b/test/multiarch_utils.go @@ -110,9 +110,6 @@ func initExcludedTests() map[string]bool { "TestEntrypointRunningStepsInOrder", "TestGitPipelineRun", "TestGitPipelineRunFail", - "TestGitPipelineRunWithRefspec", - "TestGitPipelineRun_Disable_SSLVerify", - "TestGitPipelineRunWithNonMasterBranch", "TestHelmDeployPipelineRun", "TestKanikoTaskRun", "TestPipelineRun", diff --git a/test/v1alpha1/git_checkout_test.go b/test/v1alpha1/git_checkout_test.go index b28f81471d9..391156fd5d4 100644 --- a/test/v1alpha1/git_checkout_test.go +++ b/test/v1alpha1/git_checkout_test.go @@ -22,8 +22,8 @@ import ( "strings" "testing" - tb "github.com/tektoncd/pipeline/internal/builder/v1alpha1" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" knativetest "knative.dev/pkg/test" @@ -36,38 +36,123 @@ const ( gitTestPipelineRunName = "git-check-pipeline-run" ) -// TestGitPipelineRun is an integration test that will verify the source code is either fetched or pulled -// successfully under different revision inputs (branch, commitid, tag, ref) +// TestGitPipelineRun is an integration test that will verify the source code +// is either fetched or pulled successfully under different resource +// parameters. func TestGitPipelineRun(t *testing.T) { - t.Parallel() - - revisions := []string{"master", "c15aced0e5aaee6456fbe6f7a7e95e0b5b3b2b2f", "release-0.1", "v0.1.0", "refs/pull/347/head"} - - for _, revision := range revisions { - - t.Run(revision, func(t *testing.T) { + for _, tc := range []struct { + name string + repo string + revision string + refspec string + sslVerify string + }{{ + name: "tekton @ master", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + }, { + name: "tekton @ commit", + repo: "https://github.com/tektoncd/pipeline", + revision: "c15aced0e5aaee6456fbe6f7a7e95e0b5b3b2b2f", + }, { + name: "tekton @ release", + repo: "https://github.com/tektoncd/pipeline", + revision: "release-0.1", + }, { + name: "tekton @ tag", + repo: "https://github.com/tektoncd/pipeline", + revision: "v0.1.0", + }, { + name: "tekton @ PR ref", + repo: "https://github.com/tektoncd/pipeline", + revision: "refs/pull/347/head", + }, { + name: "tekton @ master with refspec", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + refspec: "refs/tags/v0.1.0:refs/tags/v0.1.0 refs/heads/master:refs/heads/master", + }, { + name: "tekton @ commit with PR refspec", + repo: "https://github.com/tektoncd/pipeline", + revision: "968d5d37a61bfb85426c885dc1090c1cc4b33436", + refspec: "refs/pull/1009/head", + }, { + name: "tekton @ master with PR refspec", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + refspec: "refs/pull/1009/head:refs/heads/master", + }, { + name: "tekton @ master with sslverify=false", + repo: "https://github.com/tektoncd/pipeline", + revision: "master", + sslVerify: "false", + }, { + name: "non-master repo with default revision", + repo: "https://github.com/spring-projects/spring-petclinic", + revision: "", + }, { + name: "non-master repo with main revision", + repo: "https://github.com/spring-projects/spring-petclinic", + revision: "main", + }} { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() 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(getGitPipelineResource(revision, "", "true", "", "", "")); err != nil { + if _, err := c.PipelineResourceClient.Create(&v1alpha1.PipelineResource{ + ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, + Spec: v1alpha1.PipelineResourceSpec{ + Type: v1alpha1.PipelineResourceTypeGit, + Params: []v1alpha1.ResourceParam{ + {Name: "Url", Value: tc.repo}, + {Name: "Revision", Value: tc.revision}, + {Name: "Refspec", Value: tc.refspec}, + {Name: "sslVerify", Value: tc.sslVerify}, + }, + }, + }); 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 := c.PipelineRunClient.Create(&v1alpha1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, + Spec: v1alpha1.PipelineRunSpec{ + Resources: []v1alpha1.PipelineResourceBinding{{ + Name: "git-repo", + ResourceRef: &v1alpha1.PipelineResourceRef{Name: gitSourceResourceName}, + }}, + PipelineSpec: &v1alpha1.PipelineSpec{ + Resources: []v1alpha1.PipelineDeclaredResource{{ + Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, + }}, + Tasks: []v1alpha1.PipelineTask{{ + Name: "git-check", + TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ + Resources: &v1beta1.TaskResources{ + Inputs: []v1alpha1.TaskResource{{ResourceDeclaration: v1alpha1.ResourceDeclaration{ + Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, + }}}, + }, + Steps: []v1alpha1.Step{{Container: corev1.Container{ + Image: "alpine/git", + Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, + }}}, + }}, + Resources: &v1alpha1.PipelineTaskResources{ + Inputs: []v1alpha1.PipelineTaskInputResource{{ + Name: "gitsource", + Resource: "git-repo", + }}, + }, + }}, + }, + }, + }); err != nil { + t.Fatalf("Failed to create PipelineRun %q: %s", gitTestPipelineRunName, err) } if err := WaitForPipelineRunState(c, gitTestPipelineRunName, timeout, PipelineRunSucceed(gitTestPipelineRunName), "PipelineRunCompleted"); err != nil { @@ -78,314 +163,116 @@ func TestGitPipelineRun(t *testing.T) { } } -// Test revision fetching with refspec specified -func TestGitPipelineRunWithRefspec(t *testing.T) { - t.Parallel() - +// TestGitPipelineRunFail is a test to ensure that the code extraction from +// github fails as expected when an invalid revision or https proxy is passed +// on the pipelineresource. +func TestGitPipelineRunFail(t *testing.T) { for _, tc := range []struct { - description string - revision string - refspec string + name string + revision string + httpsproxy string }{{ - description: "Fetch refs/tags/v0.1.0 alongside master and checkout the master branch", - revision: "master", - refspec: "refs/tags/v0.1.0:refs/tags/v0.1.0 refs/heads/master:refs/heads/master", - }, { - description: "Checkout specific revision from refs/pull/1009/head's commit chain", - revision: "968d5d37a61bfb85426c885dc1090c1cc4b33436", - refspec: "refs/pull/1009/head", + name: "invalid revision", + revision: "Idontexistrabbitmonkeydonkey", }, { - description: "Fetch refs/pull/1009/head into a named master branch and then check it out", - revision: "master", - refspec: "refs/pull/1009/head:refs/heads/master", + name: "invalid httpsproxy", + httpsproxy: "invalid.https.proxy.example.com", }} { - t.Run(tc.description, func(t *testing.T) { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() c, namespace := setup(t) knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf) defer tearDown(t, c, namespace) - if _, err := c.PipelineResourceClient.Create(getGitPipelineResource(tc.revision, tc.refspec, "true", "", "", "")); err != nil { + t.Logf("Creating Git PipelineResource %s", gitSourceResourceName) + if _, err := c.PipelineResourceClient.Create(&v1alpha1.PipelineResource{ + ObjectMeta: metav1.ObjectMeta{Name: gitSourceResourceName}, + Spec: v1alpha1.PipelineResourceSpec{ + Type: v1alpha1.PipelineResourceTypeGit, + Params: []v1alpha1.ResourceParam{ + {Name: "Url", Value: "https://github.com/tektoncd/pipeline"}, + {Name: "Revision", Value: tc.revision}, + {Name: "httpsProxy", Value: tc.httpsproxy}, + }, + }, + }); err != nil { t.Fatalf("Failed to create Pipeline Resource `%s`: %s", gitSourceResourceName, err) } - if _, err := c.TaskClient.Create(getGitCheckTask()); err != nil { - t.Fatalf("Failed to create Task `%s`: %s", gitTestTaskName, err) - } - - if _, err := c.PipelineClient.Create(getGitCheckPipeline()); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineName, err) - } - - if _, err := c.PipelineRunClient.Create(getGitCheckPipelineRun()); err != nil { - t.Fatalf("Failed to create Pipeline `%s`: %s", gitTestPipelineRunName, err) + t.Logf("Creating PipelineRun %s", gitTestPipelineRunName) + if _, err := c.PipelineRunClient.Create(&v1alpha1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{Name: gitTestPipelineRunName}, + Spec: v1alpha1.PipelineRunSpec{ + Resources: []v1alpha1.PipelineResourceBinding{{ + Name: "git-repo", + ResourceRef: &v1alpha1.PipelineResourceRef{Name: gitSourceResourceName}, + }}, + PipelineSpec: &v1alpha1.PipelineSpec{ + Resources: []v1alpha1.PipelineDeclaredResource{{ + Name: "git-repo", Type: v1alpha1.PipelineResourceTypeGit, + }}, + Tasks: []v1alpha1.PipelineTask{{ + Name: "git-check", + TaskSpec: &v1alpha1.TaskSpec{TaskSpec: v1beta1.TaskSpec{ + Resources: &v1beta1.TaskResources{ + Inputs: []v1alpha1.TaskResource{{ResourceDeclaration: v1alpha1.ResourceDeclaration{ + Name: "gitsource", Type: v1alpha1.PipelineResourceTypeGit, + }}}, + }, + Steps: []v1alpha1.Step{{Container: corev1.Container{ + Image: "alpine/git", + Args: []string{"--git-dir=/workspace/gitsource/.git", "show"}, + }}}, + }}, + Resources: &v1alpha1.PipelineTaskResources{ + Inputs: []v1alpha1.PipelineTaskInputResource{{ + Name: "gitsource", + Resource: "git-repo", + }}, + }, + }}, + }, + }, + }); err != nil { + t.Fatalf("Failed to create PipelineRun %q: %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") - } - - }) - } -} - -// TestGitPipelineRun_Disable_SSLVerify will verify the source code is retrieved even after disabling SSL certificates (sslVerify) -func TestGitPipelineRun_Disable_SSLVerify(t *testing.T) { - t.Parallel() - - 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(getGitPipelineResource("master", "", "false", "", "", "")); 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") - } -} - -// TestGitPipelineRunFail is a test to ensure that the code extraction from github fails as expected when -// an invalid revision is passed on the pipelineresource. -func TestGitPipelineRunFail(t *testing.T) { - t.Parallel() - - 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(getGitPipelineResource("Idontexistrabbitmonkeydonkey", "", "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 { - taskruns, err := c.TaskRunClient.List(metav1.ListOptions{}) - if err != nil { - t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err) - } - for _, tr := range taskruns.Items { - if tr.Status.PodName != "" { - p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) + taskruns, err := c.TaskRunClient.List(metav1.ListOptions{}) if err != nil { - t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) + t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err) } - - for _, stat := range p.Status.ContainerStatuses { - if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) { - if stat.State.Terminated != nil { - req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) - logContent, err := req.Do().Raw() - if err != nil { - t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) - } - // Check for failure messages from fetch and pull in the log file - if strings.Contains(strings.ToLower(string(logContent)), "couldn't find remote ref idontexistrabbitmonkeydonkey") { - t.Logf("Found exepected errors when retrieving non-existent git revision") - } else { - t.Logf("Container `%s` log File: %s", stat.Name, logContent) - t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.") - } + for _, tr := range taskruns.Items { + if tr.Status.PodName != "" { + p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) + if err != nil { + t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) } - } - } - } - } - - } else { - t.Fatalf("PipelineRun succeeded when should have failed") - } -} - -// TestGitPipelineRunFail_HTTPS_PROXY is a test to ensure that the code extraction from github fails as expected when -// an invalid HTTPS_PROXY is passed on the pipelineresource. -func TestGitPipelineRunFail_HTTPS_PROXY(t *testing.T) { - t.Parallel() - - 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(getGitPipelineResource("master", "", "true", "", "invalid.https.proxy.com", "")); 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 { - taskruns, err := c.TaskRunClient.List(metav1.ListOptions{}) - if err != nil { - t.Errorf("Error getting TaskRun list for PipelineRun %s %s", gitTestPipelineRunName, err) - } - for _, tr := range taskruns.Items { - if tr.Status.PodName != "" { - p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) - if err != nil { - t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) - } - for _, stat := range p.Status.ContainerStatuses { - if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) { - if stat.State.Terminated != nil { - req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) - logContent, err := req.Do().Raw() - if err != nil { - t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) - } - // Check for failure messages from fetch and pull in the log file - if strings.Contains(strings.ToLower(string(logContent)), "could not resolve proxy: invalid.https.proxy.com") { - t.Logf("Found expected errors when using non-existent https proxy") - } else { - t.Logf("Container `%s` log File: %s", stat.Name, logContent) - t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.") + for _, stat := range p.Status.ContainerStatuses { + if strings.HasPrefix(stat.Name, "step-git-source-"+gitSourceResourceName) { + if stat.State.Terminated != nil { + req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) + logContent, err := req.Do().Raw() + if err != nil { + t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) + } + // Check for failure messages from fetch and pull in the log file + if strings.Contains(strings.ToLower(string(logContent)), "couldn't find remote ref idontexistrabbitmonkeydonkey") { + t.Logf("Found exepected errors when retrieving non-existent git revision") + } else { + t.Logf("Container `%s` log File: %s", stat.Name, logContent) + t.Fatalf("The git code extraction did not fail as expected. Expected errors not found in log file.") + } + } } } } } - } - } - - } else { - t.Fatalf("PipelineRun succeeded when should have failed") - } -} - -// 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") + } else { + t.Fatalf("PipelineRun succeeded when should have 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://github.com/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, - tb.PipelineResourceSpecParam("Url", "https://github.com/tektoncd/pipeline"), - 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 getGitCheckTask() *v1alpha1.Task { - return tb.Task(gitTestTaskName, tb.TaskSpec( - tb.TaskInputs(tb.InputsResource("gitsource", v1alpha1.PipelineResourceTypeGit)), - tb.Step("alpine/git", tb.StepArgs("--git-dir=/workspace/gitsource/.git", "show")), - )) -} - -func getGitCheckPipeline() *v1alpha1.Pipeline { - return tb.Pipeline(gitTestPipelineName, tb.PipelineSpec( - tb.PipelineDeclaredResource("git-repo", "git"), - tb.PipelineTask("git-check", gitTestTaskName, - tb.PipelineTaskInputResource("gitsource", "git-repo"), - ), - )) -} - -func getGitCheckPipelineRun() *v1alpha1.PipelineRun { - return tb.PipelineRun(gitTestPipelineRunName, tb.PipelineRunSpec( - gitTestPipelineName, - tb.PipelineRunResourceBinding("git-repo", tb.PipelineResourceBindingRef(gitSourceResourceName)), - )) -}