Skip to content

Commit e7ab514

Browse files
authored
Git resolver tests for authentication with token (#689)
1 parent 2bef033 commit e7ab514

File tree

5 files changed

+46
-53
lines changed

5 files changed

+46
-53
lines changed

pkg/oc/oc.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func CreateSecretForGitResolver(secretData string) {
127127
cmd.MustSucceed("oc", "create", "secret", "generic", "github-auth-secret", "--from-literal", "github-auth-key="+secretData, "-n", "openshift-pipelines")
128128
}
129129

130+
func CreateSecretInNamespace(secretData, secretName, namespace string) {
131+
cmd.MustSucceed("oc", "create", "secret", "generic", secretName, "--from-literal", "private-repo-token="+secretData, "-n", namespace)
132+
}
133+
130134
func CreateSecretForWebhook(tokenSecretData, webhookSecretData, namespace string) {
131135
cmd.MustSucceed("oc", "create", "secret", "generic", "gitlab-webhook-config", "--from-literal", "provider.token="+tokenSecretData, "--from-literal", "webhook.secret="+webhookSecretData, "-n", namespace)
132136
}
@@ -168,18 +172,4 @@ func CopySecret(secretName string, sourceNamespace string, destNamespace string)
168172
cmdOutput := cmd.MustSucceed("bash", "-c", fmt.Sprintf(`echo '%s' | jq 'del(.metadata["namespace", "creationTimestamp", "resourceVersion", "selfLink", "uid", "annotations"]) | .data |= with_entries(if .key == "github-auth-key" then .key = "token" else . end)'`, secretJson)).Stdout()
169173
cmd.MustSucceed("bash", "-c", fmt.Sprintf(`echo '%s' | kubectl apply -n %s -f -`, cmdOutput, destNamespace))
170174
log.Printf("Successfully copied secret %s from %s to %s", secretName, sourceNamespace, destNamespace)
171-
}
172-
173-
func CopySecretWithNewName(secretName string, sourceNamespace string, destNamespace string, newSecretName string) {
174-
secretJson := cmd.MustSucceed("oc", "get", "secret", secretName, "-n", sourceNamespace, "-o", "json").Stdout()
175-
cmdOutput := cmd.MustSucceed("bash", "-c", fmt.Sprintf(`echo '%s' | jq 'del(.metadata["namespace", "creationTimestamp", "resourceVersion", "selfLink", "uid", "annotations"]) | .metadata.name = "%s"'`, secretJson, newSecretName)).Stdout()
176-
cmd.MustSucceed("bash", "-c", fmt.Sprintf(`echo '%s' | kubectl apply -n %s -f -`, cmdOutput, destNamespace))
177-
log.Printf("Successfully copied secret %s from %s to %s as %s", secretName, sourceNamespace, destNamespace, newSecretName)
178-
}
179-
180-
func PatchSecretTokenKey(secretName string, namespace string, tokenKey string) {
181-
secretJson := cmd.MustSucceed("oc", "get", "secret", secretName, "-n", namespace, "-o", "json").Stdout()
182-
cmdOutput := cmd.MustSucceed("bash", "-c", fmt.Sprintf(`echo '%s' | jq '.data |= with_entries(if .key == "github-auth-key" or .key == "token" then .key = "%s" else . end)'`, secretJson, tokenKey)).Stdout()
183-
cmd.MustSucceed("bash", "-c", fmt.Sprintf(`echo '%s' | kubectl apply -n %s -f -`, cmdOutput, namespace))
184-
log.Printf("Successfully patched secret %s in namespace %s to set token key to %s", secretName, namespace, tokenKey)
185-
}
175+
}

specs/pipelines/git-resolvers.spec

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,24 @@ Steps:
2020
|----|-----------------------------------|------------|
2121
|1 |git-resolver-pipelinerun |successful |
2222

23-
## Test the functionality of git resolvers with authentication: PIPELINES-24-TC02
23+
## Test the functionality of git resolvers with authentication and token: PIPELINES-24-TC02
2424
Tags: e2e
2525
Component: Resolvers
2626
Level: Integration
2727
Type: Functional
2828
Importance: High
2929

3030
Steps:
31+
* Create secret "private-repo-auth-secret" in autogenerated namespace with GitHub token
3132
* Create
32-
|S.NO|resource_dir |
33-
|----|-----------------------------------------------------------------|
34-
|1 |testdata/resolvers/pipelineruns/git-resolver-pipelinerun-private.yaml |
35-
* Verify pipelinerun
36-
|S.NO|pipeline_run_name |status |
37-
|----|-----------------------------------|------------|
38-
|1 |git-resolver-pipelinerun-private |successful |
39-
40-
## Test the functionality of git resolvers authentication with token: PIPELINES-24-TC03
41-
Tags: e2e
42-
Component: Resolvers
43-
Level: Integration
44-
Type: Functional
45-
Importance: High
46-
47-
Steps:
48-
* Copy secret "github-auth-secret" from "openshift-pipelines" namespace to autogenerated namespace as "private-repo-auth-secret"
49-
* Patch secret "private-repo-auth-secret" in autogenerated namespace to set token to "private-repo-token"
50-
* Create
51-
|S.NO|resource_dir |
52-
|----|-----------------------------------------------------------------|
53-
|1 |testdata/resolvers/pipelineruns/git-resolver-pipelinerun-private-token-auth.yaml |
33+
|S.NO|resource_dir |
34+
|----|---------------------------------------------------------------------------------|
35+
|1 |testdata/resolvers/pipelineruns/git-resolver-pipelinerun-private.yaml |
36+
|2 |testdata/resolvers/pipelineruns/git-resolver-pipelinerun-private-token-auth.yaml |
37+
|3 |testdata/resolvers/pipelineruns/git-resolver-pipelinerun-private-url.yaml |
5438
* Verify pipelinerun
5539
|S.NO|pipeline_run_name |status |
5640
|----|-----------------------------------|------------|
57-
|1 |git-resolver-pipelinerun-private-token-auth |successful |
41+
|1 |git-resolver-pipelinerun-private |successful |
42+
|2 |git-resolver-pipelinerun-private-token-auth |successful |
43+
|3 |git-resolver-pipelinerun-private-url |successful |

steps/cli/oc.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ var _ = gauge.Step("Configure GitHub token for git resolver in TektonConfig", fu
230230
}
231231
})
232232

233+
var _ = gauge.Step("Create secret <secretName> in autogenerated namespace with GitHub token", func(secretName string) {
234+
if os.Getenv("GITHUB_TOKEN") == "" {
235+
log.Printf("Token for authorization to the GitHub repository was not exported as a system variable")
236+
} else {
237+
secretData := os.Getenv("GITHUB_TOKEN")
238+
oc.CreateSecretInNamespace(secretData, secretName, store.Namespace())
239+
}
240+
})
241+
233242
var _ = gauge.Step("Configure the bundles resolver", func() {
234243
patch_data := "{\"spec\":{\"pipeline\":{\"bundles-resolver-config\":{\"default-kind\":\"task\", \"defaut-service-account\":\"pipelines\"}}}}"
235244
oc.UpdateTektonConfig(patch_data)
@@ -285,15 +294,3 @@ var _ = gauge.Step("Copy secret <secretName> from <sourceNamespace> namespace to
285294
testsuit.T.Fail(fmt.Errorf("secret %s doesn't exist in namespace %s", secretName, sourceNamespace))
286295
}
287296
})
288-
289-
var _ = gauge.Step("Copy secret <secretName> from <sourceNamespace> namespace to autogenerated namespace as <newSecretName>", func(secretName string, sourceNamespace string, newSecretName string) {
290-
if oc.SecretExists(secretName, sourceNamespace) {
291-
oc.CopySecretWithNewName(secretName, sourceNamespace, store.Namespace(), newSecretName)
292-
} else {
293-
testsuit.T.Fail(fmt.Errorf("secret %s doesn't exist in namespace %s", secretName, sourceNamespace))
294-
}
295-
})
296-
297-
var _ = gauge.Step("Patch secret <secretName> in autogenerated namespace to set token to <tokenKey>", func(secretName string, tokenKey string) {
298-
oc.PatchSecretTokenKey(secretName, store.Namespace(), tokenKey)
299-
})

testdata/resolvers/pipelineruns/git-resolver-pipelinerun-private-token-auth.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ spec:
1616
value: main
1717
- name: pathInRepo
1818
value: resolver-pipeline.yaml
19-
- name: token
19+
- name: gitToken
2020
value: "private-repo-auth-secret"
21-
- name: tokenKey
21+
- name: gitTokenKey
2222
value: "private-repo-token"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: tekton.dev/v1
2+
kind: PipelineRun
3+
metadata:
4+
name: git-resolver-pipelinerun-private-url
5+
spec:
6+
pipelineRef:
7+
resolver: git
8+
params:
9+
- name: name
10+
value: resolver-pipeline
11+
- name: url
12+
value: https://github.com/openshift-pipelines/test-private
13+
- name: revision
14+
value: main
15+
- name: pathInRepo
16+
value: resolver-pipeline.yaml
17+
- name: gitToken
18+
value: "private-repo-auth-secret"
19+
- name: gitTokenKey
20+
value: "private-repo-token"

0 commit comments

Comments
 (0)