Skip to content

Commit

Permalink
Merge pull request #734 from opendevstack/task/add-tests-with-private…
Browse files Browse the repository at this point in the history
…-cert

Run tests for start and finish task also with private cert
  • Loading branch information
michaelsauter authored Oct 12, 2023
2 parents 163dfb6 + 4bdf320 commit bf4936c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 65 deletions.
4 changes: 2 additions & 2 deletions docs/development.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
7 changes: 0 additions & 7 deletions test/e2e/common_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,29 @@ 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
}
}

// 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)
}
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/pipeline_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
36 changes: 30 additions & 6 deletions test/e2e/task_finish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -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)

Expand All @@ -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"),
Expand All @@ -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{
Expand Down
77 changes: 35 additions & 42 deletions test/e2e/task_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand All @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"sourceRepository": "ods-temporary-artifacts",
"repository": "ods-temporary-artifacts",
"artifacts": []
}

0 comments on commit bf4936c

Please sign in to comment.