From 4bdf320bef840a99e04e33a0d95f2f0b66c8a91d Mon Sep 17 00:00:00 2001 From: Michael Sauter Date: Thu, 12 Oct 2023 16:12:12 +0200 Subject: [PATCH] Run tests for start and finish task also with private cert --- docs/development.adoc | 4 +- test/e2e/common_test.go | 7 -- test/e2e/main_test.go | 10 +-- test/e2e/pipeline_run_test.go | 4 +- test/e2e/task_finish_test.go | 36 +++++++-- test/e2e/task_start_test.go | 77 +++++++++---------- .../.ods/artifacts/manifest.json | 2 +- 7 files changed, 75 insertions(+), 65 deletions(-) delete mode 100644 test/e2e/common_test.go diff --git a/docs/development.adoc b/docs/development.adoc index aa482931..24cb0f2d 100644 --- a/docs/development.adoc +++ b/docs/development.adoc @@ -22,8 +22,8 @@ As mentioned above, `make test` will run all tests. You may also run only a subs * `make test-cmd` for the packages under `cmd` * `make test-pkg` for the packages under `pkg` * `make test-internal` for the packages under `internal` +* `make test-e2e-tasks` for the task tests +* `make test-e2e-pipelineruns` for the pipeline run tests * `make test-e2e` for the task tests and the pipeline run tests -Tests can be run both without a private certificate (which is the default) or using a private certificate to test how the tasks perform when the services use a certificate which is not part of the OS default trusted certs. If you want to use a private certificate in the task tests, pass `-private-cert` to `go test`. - Images used in tasks are rebuilt automatically before executing tests. This provides the best accuracy but it can slow down testing considerably. If you did not make changes since the last test run that would affect the images, you can pass `-ods-reuse-images` to `go test`. diff --git a/test/e2e/common_test.go b/test/e2e/common_test.go deleted file mode 100644 index d4926dfa..00000000 --- a/test/e2e/common_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package e2e - -import ( - "flag" -) - -var privateCertFlag = flag.Bool("private-cert", false, "Whether to run tests using a private cert") diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go index 823775c9..a79dc1bf 100644 --- a/test/e2e/main_test.go +++ b/test/e2e/main_test.go @@ -92,9 +92,9 @@ func newTektonClient(t *testing.T) *tekton.Clientset { // then commits and pushes to Bitbucket. // The workspace will also be setup with an ODS context directory in .ods // with the given namespace. -func initBitbucketRepo(t *testing.T, k8sClient kubernetes.Interface, namespace string) ttr.WorkspaceOpt { +func initBitbucketRepo(t *testing.T, k8sClient kubernetes.Interface, namespace string, privateCert bool) ttr.WorkspaceOpt { return func(c *ttr.WorkspaceConfig) error { - _ = tasktesting.SetupBitbucketRepo(t, k8sClient, namespace, c.Dir, tasktesting.BitbucketProjectKey, false) + _ = tasktesting.SetupBitbucketRepo(t, k8sClient, namespace, c.Dir, tasktesting.BitbucketProjectKey, privateCert) return nil } } @@ -102,19 +102,19 @@ func initBitbucketRepo(t *testing.T, k8sClient kubernetes.Interface, namespace s // withBitbucketSourceWorkspace configures the task run with a workspace named // "source", mapped to the directory sourced from sourceDir. The directory is // initialised as a Git repository with an ODS context with the given namespace. -func withBitbucketSourceWorkspace(t *testing.T, sourceDir string, k8sClient kubernetes.Interface, namespace string, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt { +func withBitbucketSourceWorkspace(t *testing.T, sourceDir string, k8sClient kubernetes.Interface, namespace string, privateCert bool, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt { return ott.WithSourceWorkspace( t, sourceDir, - append([]ttr.WorkspaceOpt{initBitbucketRepo(t, k8sClient, namespace)}, opts...)..., + append([]ttr.WorkspaceOpt{initBitbucketRepo(t, k8sClient, namespace, privateCert)}, opts...)..., ) } func checkBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) { buildStatusPage, err := c.BuildStatusList(gitCommit) - buildStatus := buildStatusPage.Values[0] if err != nil { t.Fatal(err) } + buildStatus := buildStatusPage.Values[0] if buildStatus.State != wantBuildStatus { t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus) } diff --git a/test/e2e/pipeline_run_test.go b/test/e2e/pipeline_run_test.go index 746cbad3..8adadab3 100644 --- a/test/e2e/pipeline_run_test.go +++ b/test/e2e/pipeline_run_test.go @@ -57,7 +57,7 @@ func TestPipelineRun(t *testing.T) { } t.Logf("Workspace is in %s", wsDir) odsContext := tasktesting.SetupBitbucketRepo( - t, k8sClient, namespaceConfig.Name, wsDir, tasktesting.BitbucketProjectKey, *privateCertFlag, + t, k8sClient, namespaceConfig.Name, wsDir, tasktesting.BitbucketProjectKey, false, ) // The webhook URL needs to be the address of the KinD control plane on the node port. @@ -75,7 +75,7 @@ func TestPipelineRun(t *testing.T) { if err != nil { t.Fatalf("could not get Bitbucket webhook secret: %s", err) } - bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false) _, err = bitbucketClient.WebhookCreate( odsContext.Project, odsContext.Repository, diff --git a/test/e2e/task_finish_test.go b/test/e2e/task_finish_test.go index d322c81b..940aad1f 100644 --- a/test/e2e/task_finish_test.go +++ b/test/e2e/task_finish_test.go @@ -30,14 +30,14 @@ func runFinishTask(opts ...ttr.TaskRunOpt) error { func TestFinishTaskSetsBitbucketStatusToFailed(t *testing.T) { k8sClient := newK8sClient(t) if err := runFinishTask( - withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app-with-artifacts", k8sClient, namespaceConfig.Name), + withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app-with-artifacts", k8sClient, namespaceConfig.Name, false), ttr.WithStringParams(map[string]string{ "pipeline-run-name": "foo", "aggregate-tasks-status": "None", }), ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun, logs bytes.Buffer) { _, odsContext := ott.GetSourceWorkspaceContext(t, config) - bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false) checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusFailed) }), ); err != nil { @@ -53,12 +53,12 @@ func TestFinishTaskSetsBitbucketStatusToSuccessfulAndUploadsArtifactsToNexus(t * "../testdata/workspaces/hello-world-app-with-artifacts", func(c *ttr.WorkspaceConfig) error { odsContext := tasktesting.SetupBitbucketRepo( - t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, *privateCertFlag, + t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, false, ) // Pretend there is alredy a coverage report in Nexus. // This assures the safeguard is working to avoid duplicate upload. t.Log("Uploading coverage artifact to Nexus and writing manifest") - nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, false) if _, err := nexusClient.Upload( nexus.TestTemporaryRepository, pipelinectxt.ArtifactGroup(odsContext, pipelinectxt.CodeCoveragesDir), @@ -90,7 +90,7 @@ func TestFinishTaskSetsBitbucketStatusToSuccessfulAndUploadsArtifactsToNexus(t * }), ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun, logs bytes.Buffer) { _, odsContext := ott.GetSourceWorkspaceContext(t, config) - bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false) checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusSuccessful) checkArtifactsAreInNexus(t, k8sClient, odsContext, nexus.TestTemporaryRepository) @@ -104,6 +104,30 @@ func TestFinishTaskSetsBitbucketStatusToSuccessfulAndUploadsArtifactsToNexus(t * } } +func TestFinishTaskUsesPrivateCert(t *testing.T) { + k8sClient := newK8sClient(t) + nc, cleanup, err := ttr.SetupTempNamespace( + clusterConfig, + ott.InstallODSPipeline(&ott.InstallOptions{PrivateCert: true}), + ) + if err != nil { + t.Fatal(err) + } + defer cleanup() + if err := ttr.RunTask( + ttr.InNamespace(nc.Name), + ttr.UsingTask("ods-pipeline-finish"), + withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app-with-artifacts", k8sClient, nc.Name, true), + ttr.WithStringParams(map[string]string{ + "pipeline-run-name": "foo", + "aggregate-tasks-status": "Succeeded", + "artifact-target": nexus.TestTemporaryRepository, + }), + ); err != nil { + t.Fatal(err) + } +} + func TestFinishTaskStopsGracefullyWhenContextCannotBeRead(t *testing.T) { if err := runFinishTask( ott.WithSourceWorkspace(t, "../testdata/workspaces/empty"), @@ -125,7 +149,7 @@ func TestFinishTaskStopsGracefullyWhenContextCannotBeRead(t *testing.T) { func checkArtifactsAreInNexus(t *testing.T, k8sClient kubernetes.Interface, odsContext *pipelinectxt.ODSContext, targetRepository string) { - nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, false) // List of expected artifacts to have been uploaded to Nexus artifactsMap := map[string][]string{ diff --git a/test/e2e/task_start_test.go b/test/e2e/task_start_test.go index 87e35de6..030b2a1b 100644 --- a/test/e2e/task_start_test.go +++ b/test/e2e/task_start_test.go @@ -34,7 +34,7 @@ func runStartTask(opts ...ttr.TaskRunOpt) error { func TestStartTaskClonesRepoAtBranch(t *testing.T) { k8sClient := newK8sClient(t) if err := runStartTask( - withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name), + withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name, false), func(c *ttr.TaskRunConfig) error { c.Params = append(c.Params, ttr.TektonParamsFromStringParams(map[string]string{ "url": bitbucketURLForWorkspace(c.WorkspaceConfigs["source"]), @@ -48,7 +48,7 @@ func TestStartTaskClonesRepoAtBranch(t *testing.T) { wsDir, odsContext := ott.GetSourceWorkspaceContext(t, config) checkODSContext(t, wsDir, odsContext) checkFilesExist(t, wsDir, filepath.Join(pipelinectxt.ArtifactsPath, pipelinectxt.ArtifactsManifestFilename)) - bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false) checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusInProgress) }), ); err != nil { @@ -59,7 +59,7 @@ func TestStartTaskClonesRepoAtBranch(t *testing.T) { func TestStartTaskClonesRepoAtTag(t *testing.T) { k8sClient := newK8sClient(t) if err := runStartTask( - withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name), + withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name, false), func(c *ttr.TaskRunConfig) error { wsDir, odsContext := ott.GetSourceWorkspaceContext(t, c) tasktesting.UpdateBitbucketRepoWithTagOrFatal(t, odsContext, wsDir, "v1.0.0") @@ -91,7 +91,7 @@ func TestStartTaskClonesRepoAndSubrepos(t *testing.T) { // Setup sub-component subrepoContext = setupBitbucketRepoWithSubdirOrFatal(t, c, k8sClient) // Nexus artifacts - nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, false) artifactsBaseDir := filepath.Join(projectpath.Root, "test", testdataWorkspacesPath, "hello-world-app-with-artifacts", pipelinectxt.ArtifactsPath) _, err := nexusClient.Upload( nexus.TestTemporaryRepository, @@ -141,7 +141,7 @@ func TestStartTaskClonesRepoAndSubrepos(t *testing.T) { checkFileContent(t, destinationArtifactsBaseDir, xUnitFileSource, xUnitContent) checkFilesExist(t, destinationArtifactsBaseDir, pipelinectxt.ArtifactsManifestFilename) - bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag) + bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false) checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusInProgress) }), ); err != nil { @@ -194,7 +194,7 @@ func TestStartTaskClonesUsingLFS(t *testing.T) { "../testdata/workspaces/hello-world-app", func(c *ttr.WorkspaceConfig) error { odsContext := tasktesting.SetupBitbucketRepo( - t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, *privateCertFlag, + t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, false, ) tasktesting.EnableLfsOnBitbucketRepoOrFatal(t, filepath.Base(c.Dir), tasktesting.BitbucketProjectKey) lfsFilename = "lfspicture.jpg" @@ -221,40 +221,33 @@ func TestStartTaskClonesUsingLFS(t *testing.T) { } } -// func TestStartTaskUsesPrivateCert(t *testing.T) { -// k8sClient := newK8sClient(t) -// nc, cleanup, err := ttr.SetupTempNamespace( -// clusterConfig, -// ott.StartBitbucket(), -// ott.StartNexus(), -// ott.InstallODSPipeline(&ott.InstallOptions{PrivateCert: true}), -// ) -// if err != nil { -// t.Fatal(err) -// } -// defer cleanup() -// if err := runStartTask( -// withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, nc.Name), -// func(c *ttr.TaskRunConfig) error { -// c.Params = append(c.Params, ttr.TektonParamsFromStringParams(map[string]string{ -// "url": bitbucketURLForWorkspace(c.WorkspaceConfigs["source"]), -// "git-full-ref": "refs/heads/master", -// "project": tasktesting.BitbucketProjectKey, -// "pipeline-run-name": "foo", -// })...) -// return nil -// }, -// ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun, logs bytes.Buffer) { -// wsDir, odsContext := ott.GetSourceWorkspaceContext(t, config) -// checkODSContext(t, wsDir, odsContext) -// checkFilesExist(t, wsDir, filepath.Join(pipelinectxt.ArtifactsPath, pipelinectxt.ArtifactsManifestFilename)) -// bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, nc.Name, *privateCertFlag) -// checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusInProgress) -// }), -// ); err != nil { -// t.Fatal(err) -// } -// } +func TestStartTaskUsesPrivateCert(t *testing.T) { + k8sClient := newK8sClient(t) + nc, cleanup, err := ttr.SetupTempNamespace( + clusterConfig, + ott.InstallODSPipeline(&ott.InstallOptions{PrivateCert: true}), + ) + if err != nil { + t.Fatal(err) + } + defer cleanup() + if err := ttr.RunTask( + ttr.InNamespace(nc.Name), + ttr.UsingTask("ods-pipeline-start"), + withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, nc.Name, true), + func(c *ttr.TaskRunConfig) error { + c.Params = append(c.Params, ttr.TektonParamsFromStringParams(map[string]string{ + "url": bitbucketURLForWorkspace(c.WorkspaceConfigs["source"]), + "git-full-ref": "refs/heads/master", + "project": tasktesting.BitbucketProjectKey, + "pipeline-run-name": "foo", + })...) + return nil + }, + ); err != nil { + t.Fatal(err) + } +} func setupBitbucketRepoWithSubdirOrFatal(t *testing.T, c *ttr.WorkspaceConfig, k8sClient kubernetes.Interface) *pipelinectxt.ODSContext { // Setup sub-component @@ -267,7 +260,7 @@ func setupBitbucketRepoWithSubdirOrFatal(t *testing.T, c *ttr.WorkspaceConfig, k t.Fatal(err) } subCtxt := tasktesting.SetupBitbucketRepo( - t, k8sClient, namespaceConfig.Name, tempDir, tasktesting.BitbucketProjectKey, *privateCertFlag, + t, k8sClient, namespaceConfig.Name, tempDir, tasktesting.BitbucketProjectKey, false, ) err = os.RemoveAll(tempDir) if err != nil { @@ -278,7 +271,7 @@ func setupBitbucketRepoWithSubdirOrFatal(t *testing.T, c *ttr.WorkspaceConfig, k t.Fatal(err) } _ = tasktesting.SetupBitbucketRepo( - t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, *privateCertFlag, + t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, false, ) return subCtxt } diff --git a/test/testdata/workspaces/hello-world-app-with-artifacts/.ods/artifacts/manifest.json b/test/testdata/workspaces/hello-world-app-with-artifacts/.ods/artifacts/manifest.json index c08edde7..640d737c 100644 --- a/test/testdata/workspaces/hello-world-app-with-artifacts/.ods/artifacts/manifest.json +++ b/test/testdata/workspaces/hello-world-app-with-artifacts/.ods/artifacts/manifest.json @@ -1,4 +1,4 @@ { - "sourceRepository": "ods-temporary-artifacts", + "repository": "ods-temporary-artifacts", "artifacts": [] }