Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit bf4936c

Browse files
author
Michael Sauter
authored
Merge pull request #734 from opendevstack/task/add-tests-with-private-cert
Run tests for start and finish task also with private cert
2 parents 163dfb6 + 4bdf320 commit bf4936c

File tree

7 files changed

+75
-65
lines changed

7 files changed

+75
-65
lines changed

docs/development.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ As mentioned above, `make test` will run all tests. You may also run only a subs
2222
* `make test-cmd` for the packages under `cmd`
2323
* `make test-pkg` for the packages under `pkg`
2424
* `make test-internal` for the packages under `internal`
25+
* `make test-e2e-tasks` for the task tests
26+
* `make test-e2e-pipelineruns` for the pipeline run tests
2527
* `make test-e2e` for the task tests and the pipeline run tests
2628

27-
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`.
28-
2929
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`.

test/e2e/common_test.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/e2e/main_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,29 @@ func newTektonClient(t *testing.T) *tekton.Clientset {
9292
// then commits and pushes to Bitbucket.
9393
// The workspace will also be setup with an ODS context directory in .ods
9494
// with the given namespace.
95-
func initBitbucketRepo(t *testing.T, k8sClient kubernetes.Interface, namespace string) ttr.WorkspaceOpt {
95+
func initBitbucketRepo(t *testing.T, k8sClient kubernetes.Interface, namespace string, privateCert bool) ttr.WorkspaceOpt {
9696
return func(c *ttr.WorkspaceConfig) error {
97-
_ = tasktesting.SetupBitbucketRepo(t, k8sClient, namespace, c.Dir, tasktesting.BitbucketProjectKey, false)
97+
_ = tasktesting.SetupBitbucketRepo(t, k8sClient, namespace, c.Dir, tasktesting.BitbucketProjectKey, privateCert)
9898
return nil
9999
}
100100
}
101101

102102
// withBitbucketSourceWorkspace configures the task run with a workspace named
103103
// "source", mapped to the directory sourced from sourceDir. The directory is
104104
// initialised as a Git repository with an ODS context with the given namespace.
105-
func withBitbucketSourceWorkspace(t *testing.T, sourceDir string, k8sClient kubernetes.Interface, namespace string, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt {
105+
func withBitbucketSourceWorkspace(t *testing.T, sourceDir string, k8sClient kubernetes.Interface, namespace string, privateCert bool, opts ...ttr.WorkspaceOpt) ttr.TaskRunOpt {
106106
return ott.WithSourceWorkspace(
107107
t, sourceDir,
108-
append([]ttr.WorkspaceOpt{initBitbucketRepo(t, k8sClient, namespace)}, opts...)...,
108+
append([]ttr.WorkspaceOpt{initBitbucketRepo(t, k8sClient, namespace, privateCert)}, opts...)...,
109109
)
110110
}
111111

112112
func checkBuildStatus(t *testing.T, c *bitbucket.Client, gitCommit, wantBuildStatus string) {
113113
buildStatusPage, err := c.BuildStatusList(gitCommit)
114-
buildStatus := buildStatusPage.Values[0]
115114
if err != nil {
116115
t.Fatal(err)
117116
}
117+
buildStatus := buildStatusPage.Values[0]
118118
if buildStatus.State != wantBuildStatus {
119119
t.Fatalf("Got: %s, want: %s", buildStatus.State, wantBuildStatus)
120120
}

test/e2e/pipeline_run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestPipelineRun(t *testing.T) {
5757
}
5858
t.Logf("Workspace is in %s", wsDir)
5959
odsContext := tasktesting.SetupBitbucketRepo(
60-
t, k8sClient, namespaceConfig.Name, wsDir, tasktesting.BitbucketProjectKey, *privateCertFlag,
60+
t, k8sClient, namespaceConfig.Name, wsDir, tasktesting.BitbucketProjectKey, false,
6161
)
6262

6363
// 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) {
7575
if err != nil {
7676
t.Fatalf("could not get Bitbucket webhook secret: %s", err)
7777
}
78-
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
78+
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
7979
_, err = bitbucketClient.WebhookCreate(
8080
odsContext.Project,
8181
odsContext.Repository,

test/e2e/task_finish_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ func runFinishTask(opts ...ttr.TaskRunOpt) error {
3030
func TestFinishTaskSetsBitbucketStatusToFailed(t *testing.T) {
3131
k8sClient := newK8sClient(t)
3232
if err := runFinishTask(
33-
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app-with-artifacts", k8sClient, namespaceConfig.Name),
33+
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app-with-artifacts", k8sClient, namespaceConfig.Name, false),
3434
ttr.WithStringParams(map[string]string{
3535
"pipeline-run-name": "foo",
3636
"aggregate-tasks-status": "None",
3737
}),
3838
ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun, logs bytes.Buffer) {
3939
_, odsContext := ott.GetSourceWorkspaceContext(t, config)
40-
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
40+
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
4141
checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusFailed)
4242
}),
4343
); err != nil {
@@ -53,12 +53,12 @@ func TestFinishTaskSetsBitbucketStatusToSuccessfulAndUploadsArtifactsToNexus(t *
5353
"../testdata/workspaces/hello-world-app-with-artifacts",
5454
func(c *ttr.WorkspaceConfig) error {
5555
odsContext := tasktesting.SetupBitbucketRepo(
56-
t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, *privateCertFlag,
56+
t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, false,
5757
)
5858
// Pretend there is alredy a coverage report in Nexus.
5959
// This assures the safeguard is working to avoid duplicate upload.
6060
t.Log("Uploading coverage artifact to Nexus and writing manifest")
61-
nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
61+
nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
6262
if _, err := nexusClient.Upload(
6363
nexus.TestTemporaryRepository,
6464
pipelinectxt.ArtifactGroup(odsContext, pipelinectxt.CodeCoveragesDir),
@@ -90,7 +90,7 @@ func TestFinishTaskSetsBitbucketStatusToSuccessfulAndUploadsArtifactsToNexus(t *
9090
}),
9191
ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun, logs bytes.Buffer) {
9292
_, odsContext := ott.GetSourceWorkspaceContext(t, config)
93-
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
93+
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
9494
checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusSuccessful)
9595
checkArtifactsAreInNexus(t, k8sClient, odsContext, nexus.TestTemporaryRepository)
9696

@@ -104,6 +104,30 @@ func TestFinishTaskSetsBitbucketStatusToSuccessfulAndUploadsArtifactsToNexus(t *
104104
}
105105
}
106106

107+
func TestFinishTaskUsesPrivateCert(t *testing.T) {
108+
k8sClient := newK8sClient(t)
109+
nc, cleanup, err := ttr.SetupTempNamespace(
110+
clusterConfig,
111+
ott.InstallODSPipeline(&ott.InstallOptions{PrivateCert: true}),
112+
)
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
defer cleanup()
117+
if err := ttr.RunTask(
118+
ttr.InNamespace(nc.Name),
119+
ttr.UsingTask("ods-pipeline-finish"),
120+
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app-with-artifacts", k8sClient, nc.Name, true),
121+
ttr.WithStringParams(map[string]string{
122+
"pipeline-run-name": "foo",
123+
"aggregate-tasks-status": "Succeeded",
124+
"artifact-target": nexus.TestTemporaryRepository,
125+
}),
126+
); err != nil {
127+
t.Fatal(err)
128+
}
129+
}
130+
107131
func TestFinishTaskStopsGracefullyWhenContextCannotBeRead(t *testing.T) {
108132
if err := runFinishTask(
109133
ott.WithSourceWorkspace(t, "../testdata/workspaces/empty"),
@@ -125,7 +149,7 @@ func TestFinishTaskStopsGracefullyWhenContextCannotBeRead(t *testing.T) {
125149

126150
func checkArtifactsAreInNexus(t *testing.T, k8sClient kubernetes.Interface, odsContext *pipelinectxt.ODSContext, targetRepository string) {
127151

128-
nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
152+
nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
129153

130154
// List of expected artifacts to have been uploaded to Nexus
131155
artifactsMap := map[string][]string{

test/e2e/task_start_test.go

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func runStartTask(opts ...ttr.TaskRunOpt) error {
3434
func TestStartTaskClonesRepoAtBranch(t *testing.T) {
3535
k8sClient := newK8sClient(t)
3636
if err := runStartTask(
37-
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name),
37+
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name, false),
3838
func(c *ttr.TaskRunConfig) error {
3939
c.Params = append(c.Params, ttr.TektonParamsFromStringParams(map[string]string{
4040
"url": bitbucketURLForWorkspace(c.WorkspaceConfigs["source"]),
@@ -48,7 +48,7 @@ func TestStartTaskClonesRepoAtBranch(t *testing.T) {
4848
wsDir, odsContext := ott.GetSourceWorkspaceContext(t, config)
4949
checkODSContext(t, wsDir, odsContext)
5050
checkFilesExist(t, wsDir, filepath.Join(pipelinectxt.ArtifactsPath, pipelinectxt.ArtifactsManifestFilename))
51-
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
51+
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
5252
checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusInProgress)
5353
}),
5454
); err != nil {
@@ -59,7 +59,7 @@ func TestStartTaskClonesRepoAtBranch(t *testing.T) {
5959
func TestStartTaskClonesRepoAtTag(t *testing.T) {
6060
k8sClient := newK8sClient(t)
6161
if err := runStartTask(
62-
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name),
62+
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, namespaceConfig.Name, false),
6363
func(c *ttr.TaskRunConfig) error {
6464
wsDir, odsContext := ott.GetSourceWorkspaceContext(t, c)
6565
tasktesting.UpdateBitbucketRepoWithTagOrFatal(t, odsContext, wsDir, "v1.0.0")
@@ -91,7 +91,7 @@ func TestStartTaskClonesRepoAndSubrepos(t *testing.T) {
9191
// Setup sub-component
9292
subrepoContext = setupBitbucketRepoWithSubdirOrFatal(t, c, k8sClient)
9393
// Nexus artifacts
94-
nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
94+
nexusClient := tasktesting.NexusClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
9595
artifactsBaseDir := filepath.Join(projectpath.Root, "test", testdataWorkspacesPath, "hello-world-app-with-artifacts", pipelinectxt.ArtifactsPath)
9696
_, err := nexusClient.Upload(
9797
nexus.TestTemporaryRepository,
@@ -141,7 +141,7 @@ func TestStartTaskClonesRepoAndSubrepos(t *testing.T) {
141141
checkFileContent(t, destinationArtifactsBaseDir, xUnitFileSource, xUnitContent)
142142
checkFilesExist(t, destinationArtifactsBaseDir, pipelinectxt.ArtifactsManifestFilename)
143143

144-
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, *privateCertFlag)
144+
bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, namespaceConfig.Name, false)
145145
checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusInProgress)
146146
}),
147147
); err != nil {
@@ -194,7 +194,7 @@ func TestStartTaskClonesUsingLFS(t *testing.T) {
194194
"../testdata/workspaces/hello-world-app",
195195
func(c *ttr.WorkspaceConfig) error {
196196
odsContext := tasktesting.SetupBitbucketRepo(
197-
t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, *privateCertFlag,
197+
t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, false,
198198
)
199199
tasktesting.EnableLfsOnBitbucketRepoOrFatal(t, filepath.Base(c.Dir), tasktesting.BitbucketProjectKey)
200200
lfsFilename = "lfspicture.jpg"
@@ -221,40 +221,33 @@ func TestStartTaskClonesUsingLFS(t *testing.T) {
221221
}
222222
}
223223

224-
// func TestStartTaskUsesPrivateCert(t *testing.T) {
225-
// k8sClient := newK8sClient(t)
226-
// nc, cleanup, err := ttr.SetupTempNamespace(
227-
// clusterConfig,
228-
// ott.StartBitbucket(),
229-
// ott.StartNexus(),
230-
// ott.InstallODSPipeline(&ott.InstallOptions{PrivateCert: true}),
231-
// )
232-
// if err != nil {
233-
// t.Fatal(err)
234-
// }
235-
// defer cleanup()
236-
// if err := runStartTask(
237-
// withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, nc.Name),
238-
// func(c *ttr.TaskRunConfig) error {
239-
// c.Params = append(c.Params, ttr.TektonParamsFromStringParams(map[string]string{
240-
// "url": bitbucketURLForWorkspace(c.WorkspaceConfigs["source"]),
241-
// "git-full-ref": "refs/heads/master",
242-
// "project": tasktesting.BitbucketProjectKey,
243-
// "pipeline-run-name": "foo",
244-
// })...)
245-
// return nil
246-
// },
247-
// ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun, logs bytes.Buffer) {
248-
// wsDir, odsContext := ott.GetSourceWorkspaceContext(t, config)
249-
// checkODSContext(t, wsDir, odsContext)
250-
// checkFilesExist(t, wsDir, filepath.Join(pipelinectxt.ArtifactsPath, pipelinectxt.ArtifactsManifestFilename))
251-
// bitbucketClient := tasktesting.BitbucketClientOrFatal(t, k8sClient, nc.Name, *privateCertFlag)
252-
// checkBuildStatus(t, bitbucketClient, odsContext.GitCommitSHA, bitbucket.BuildStatusInProgress)
253-
// }),
254-
// ); err != nil {
255-
// t.Fatal(err)
256-
// }
257-
// }
224+
func TestStartTaskUsesPrivateCert(t *testing.T) {
225+
k8sClient := newK8sClient(t)
226+
nc, cleanup, err := ttr.SetupTempNamespace(
227+
clusterConfig,
228+
ott.InstallODSPipeline(&ott.InstallOptions{PrivateCert: true}),
229+
)
230+
if err != nil {
231+
t.Fatal(err)
232+
}
233+
defer cleanup()
234+
if err := ttr.RunTask(
235+
ttr.InNamespace(nc.Name),
236+
ttr.UsingTask("ods-pipeline-start"),
237+
withBitbucketSourceWorkspace(t, "../testdata/workspaces/hello-world-app", k8sClient, nc.Name, true),
238+
func(c *ttr.TaskRunConfig) error {
239+
c.Params = append(c.Params, ttr.TektonParamsFromStringParams(map[string]string{
240+
"url": bitbucketURLForWorkspace(c.WorkspaceConfigs["source"]),
241+
"git-full-ref": "refs/heads/master",
242+
"project": tasktesting.BitbucketProjectKey,
243+
"pipeline-run-name": "foo",
244+
})...)
245+
return nil
246+
},
247+
); err != nil {
248+
t.Fatal(err)
249+
}
250+
}
258251

259252
func setupBitbucketRepoWithSubdirOrFatal(t *testing.T, c *ttr.WorkspaceConfig, k8sClient kubernetes.Interface) *pipelinectxt.ODSContext {
260253
// Setup sub-component
@@ -267,7 +260,7 @@ func setupBitbucketRepoWithSubdirOrFatal(t *testing.T, c *ttr.WorkspaceConfig, k
267260
t.Fatal(err)
268261
}
269262
subCtxt := tasktesting.SetupBitbucketRepo(
270-
t, k8sClient, namespaceConfig.Name, tempDir, tasktesting.BitbucketProjectKey, *privateCertFlag,
263+
t, k8sClient, namespaceConfig.Name, tempDir, tasktesting.BitbucketProjectKey, false,
271264
)
272265
err = os.RemoveAll(tempDir)
273266
if err != nil {
@@ -278,7 +271,7 @@ func setupBitbucketRepoWithSubdirOrFatal(t *testing.T, c *ttr.WorkspaceConfig, k
278271
t.Fatal(err)
279272
}
280273
_ = tasktesting.SetupBitbucketRepo(
281-
t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, *privateCertFlag,
274+
t, k8sClient, namespaceConfig.Name, c.Dir, tasktesting.BitbucketProjectKey, false,
282275
)
283276
return subCtxt
284277
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"sourceRepository": "ods-temporary-artifacts",
2+
"repository": "ods-temporary-artifacts",
33
"artifacts": []
44
}

0 commit comments

Comments
 (0)