Skip to content

Commit cea4864

Browse files
authored
Jira assert missing tests (#591)
Adds new tests for the `--assert` flag on the `attest jira` command. Enables both server errors (e.g. trying to attest to a nonexistent artifact) and assert errors to be output if both occur.
1 parent 009d636 commit cea4864

2 files changed

Lines changed: 86 additions & 25 deletions

File tree

cmd/kosli/attestJira.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,20 @@ func (o *attestJiraOptions) run(args []string) error {
318318
logger.Info("jira attestation '%s' is reported to trail: %s", o.payload.AttestationName, o.trailName)
319319
}
320320

321-
if len(issueIDs) == 0 && o.assert {
322-
return fmt.Errorf("no Jira references are found in commit message or branch name")
321+
if len(issueIDs) == 0 && o.assert && !global.DryRun {
322+
errString := ""
323+
if err != nil {
324+
errString = fmt.Sprintf("%s\nError: ", err.Error())
325+
}
326+
err = fmt.Errorf("%sno Jira references are found in commit message or branch name", errString)
323327
}
324328

325-
if issueFoundCount != len(issueIDs) && o.assert {
326-
return fmt.Errorf("missing Jira issues from references found in commit message or branch name%s", issueLog)
329+
if issueFoundCount != len(issueIDs) && o.assert && !global.DryRun {
330+
errString := ""
331+
if err != nil {
332+
errString = fmt.Sprintf("%s\nError: ", err.Error())
333+
}
334+
err = fmt.Errorf("%smissing Jira issues from references found in commit message or branch name%s", errString, issueLog)
327335
}
328336
return wrapAttestationError(err)
329337
}

cmd/kosli/attestJira_test.go

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
5757
tests := []cmdTestCase{
5858
{
5959
wantError: true,
60-
name: "fails when more arguments are provided",
60+
name: "01 fails when more arguments are provided",
6161
cmd: fmt.Sprintf("attest jira foo bar %s", suite.defaultKosliArguments),
6262
golden: "Error: accepts at most 1 arg(s), received 2 [foo bar]\n",
6363
},
6464
{
6565
wantError: true,
66-
name: "fails when missing required flags",
66+
name: "02 fails when missing required flags",
6767
cmd: fmt.Sprintf("attest jira foo -t file %s", suite.defaultKosliArguments),
6868
golden: "Error: required flag(s) \"commit\", \"jira-base-url\", \"name\" not set\n",
6969
},
7070
{
7171
wantError: true,
72-
name: "fails when missing --commit flag",
72+
name: "03 fails when missing --commit flag",
7373
cmd: fmt.Sprintf(`attest jira foo -t file --name bar
7474
--jira-base-url https://kosli-test.atlassian.net
7575
--jira-api-token secret
@@ -78,19 +78,19 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
7878
},
7979
{
8080
wantError: true,
81-
name: "fails when both --fingerprint and --artifact-type",
81+
name: "04 fails when both --fingerprint and --artifact-type",
8282
cmd: fmt.Sprintf("attest jira testdata/file1 --fingerprint xxxx --artifact-type file --name bar --commit HEAD --origin-url http://www.example.com %s", suite.defaultKosliArguments),
8383
golden: "Error: only one of --fingerprint, --artifact-type is allowed\n",
8484
},
8585
{
8686
wantError: true,
87-
name: "fails when --fingerprint is not valid",
87+
name: "05 fails when --fingerprint is not valid",
8888
cmd: fmt.Sprintf("attest jira --name foo --fingerprint xxxx --commit HEAD --origin-url http://www..com %s", suite.defaultKosliArguments),
8989
golden: "Error: xxxx is not a valid SHA256 fingerprint. It should match the pattern ^([a-f0-9]{64})$\nUsage: kosli attest jira [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n",
9090
},
9191
{
9292
wantError: true,
93-
name: "attesting against an artifact that does not exist fails",
93+
name: "06 attesting against an artifact that does not exist fails",
9494
cmd: fmt.Sprintf(`attest jira --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
9595
--name foo
9696
--repo-root %s
@@ -103,7 +103,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
103103
},
104104
{
105105
wantError: true,
106-
name: "assert for non-existing Jira issue gives an error",
106+
name: "07 assert for non-existing Jira issue gives an error",
107107
cmd: fmt.Sprintf(`attest jira --name jira-validation
108108
--jira-base-url https://kosli-test.atlassian.net
109109
--repo-root %s
@@ -114,7 +114,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
114114
},
115115
},
116116
{
117-
name: "can attest jira against an artifact using artifact name and --artifact-type",
117+
name: "08 can attest jira against an artifact using artifact name and --artifact-type",
118118
cmd: fmt.Sprintf(`attest jira testdata/file1 --artifact-type file --name foo
119119
--jira-base-url https://kosli-test.atlassian.net
120120
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -124,7 +124,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
124124
},
125125
},
126126
{
127-
name: "can attest jira when the issue doesn't exist",
127+
name: "09 can attest jira when the issue doesn't exist",
128128
cmd: fmt.Sprintf(`attest jira testdata/file1 --artifact-type file --name foo
129129
--jira-base-url https://kosli-test.atlassian.net
130130
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -134,7 +134,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
134134
},
135135
},
136136
{
137-
name: "can attest jira against an artifact using artifact name and --artifact-type when --name does not exist in the trail template",
137+
name: "10 can attest jira against an artifact using artifact name and --artifact-type when --name does not exist in the trail template",
138138
cmd: fmt.Sprintf(`attest jira testdata/file1 --artifact-type file --name bar
139139
--jira-base-url https://kosli-test.atlassian.net
140140
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -144,7 +144,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
144144
},
145145
},
146146
{
147-
name: "can attest jira against an artifact using --fingerprint",
147+
name: "11 can attest jira against an artifact using --fingerprint",
148148
cmd: fmt.Sprintf(`attest jira --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo
149149
--jira-base-url https://kosli-test.atlassian.net
150150
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -154,7 +154,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
154154
},
155155
},
156156
{
157-
name: "can attest jira against a trail",
157+
name: "12 can attest jira against a trail",
158158
cmd: fmt.Sprintf(`attest jira --name bar
159159
--jira-base-url https://kosli-test.atlassian.net
160160
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -164,7 +164,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
164164
},
165165
},
166166
{
167-
name: "can attest jira against a trail with summary and description from jira issue fields",
167+
name: "13 can attest jira against a trail with summary and description from jira issue fields",
168168
cmd: fmt.Sprintf(`attest jira --name bar
169169
--jira-base-url https://kosli-test.atlassian.net
170170
--jira-issue-fields "summary,description"
@@ -175,7 +175,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
175175
},
176176
},
177177
{
178-
name: "can attest jira against a trail when name is not found in the trail template",
178+
name: "14 can attest jira against a trail when name is not found in the trail template",
179179
cmd: fmt.Sprintf(`attest jira --name additional
180180
--jira-base-url https://kosli-test.atlassian.net
181181
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -185,7 +185,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
185185
},
186186
},
187187
{
188-
name: "can attest jira against an artifact it is created using dot syntax in --name",
188+
name: "15 can attest jira against an artifact it is created using dot syntax in --name",
189189
cmd: fmt.Sprintf(`attest jira --name cli.foo
190190
--jira-base-url https://kosli-test.atlassian.net
191191
--repo-root %s %s`, suite.tmpDir, suite.defaultKosliArguments),
@@ -195,7 +195,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
195195
},
196196
},
197197
{
198-
name: "can attest jira against a trail with attachment and external-url",
198+
name: "16 can attest jira against a trail with attachment and external-url",
199199
cmd: fmt.Sprintf(`attest jira --name bar
200200
--jira-base-url https://kosli-test.atlassian.net
201201
--attachments testdata/file1 --external-url foo=https://foo.com --external-url bar=https://bar.com
@@ -206,7 +206,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
206206
},
207207
},
208208
{
209-
name: "can attest jira against a trail with external-url and external-fingerprint",
209+
name: "17 can attest jira against a trail with external-url and external-fingerprint",
210210
cmd: fmt.Sprintf(`attest jira --name bar
211211
--jira-base-url https://kosli-test.atlassian.net
212212
--external-url foo=https://foo.com --external-fingerprint foo=7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
@@ -218,7 +218,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
218218
},
219219
{
220220
wantError: true,
221-
name: "fails when external-url and external-fingerprint labels don't match",
221+
name: "18 fails when external-url and external-fingerprint labels don't match",
222222
cmd: fmt.Sprintf(`attest jira --name bar
223223
--jira-base-url https://kosli-test.atlassian.net
224224
--external-url foo=https://foo.com --external-fingerprint bar=7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
@@ -229,7 +229,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
229229
},
230230
},
231231
{
232-
name: "can specify the jira project key",
232+
name: "19 can specify the jira project key",
233233
cmd: fmt.Sprintf(`attest jira --name bar
234234
--jira-base-url https://kosli-test.atlassian.net
235235
--jira-project-key EX
@@ -241,7 +241,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
241241
},
242242
},
243243
{
244-
name: "can specify lower case and underscore jira project key",
244+
name: "20 can specify lower case and underscore jira project key",
245245
cmd: fmt.Sprintf(`attest jira --name bar
246246
--jira-base-url https://kosli-test.atlassian.net
247247
--jira-project-key low
@@ -255,7 +255,7 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
255255
},
256256
{
257257
wantError: true,
258-
name: "fails with an invalid Jira project key specified",
258+
name: "21 fails with an invalid Jira project key specified",
259259
cmd: fmt.Sprintf(`attest jira --name bar
260260
--jira-base-url https://kosli-test.atlassian.net
261261
--jira-project-key 1AB
@@ -266,6 +266,59 @@ func (suite *AttestJiraCommandTestSuite) TestAttestJiraCmd() {
266266
commitMessage: "EX-1 test commit",
267267
},
268268
},
269+
{
270+
wantError: true,
271+
name: "22 if no matching issue exists, assert fails with a non-zero exit code",
272+
cmd: fmt.Sprintf(`attest jira --name bar
273+
--jira-base-url https://kosli-test.atlassian.net
274+
--jira-project-key ABC
275+
--repo-root %s
276+
--assert %s`, suite.tmpDir, suite.defaultKosliArguments),
277+
golden: "jira attestation 'bar' is reported to trail: test-123\nError: no Jira references are found in commit message or branch name\n",
278+
additionalConfig: jiraTestsAdditionalConfig{
279+
commitMessage: "EX-1 test commit",
280+
},
281+
},
282+
{
283+
name: "23 assert works and exits with zero code if issue exists",
284+
cmd: fmt.Sprintf(`attest jira --name bar
285+
--jira-base-url https://kosli-test.atlassian.net
286+
--repo-root %s
287+
--assert %s`, suite.tmpDir, suite.defaultKosliArguments),
288+
golden: "jira attestation 'bar' is reported to trail: test-123\n",
289+
additionalConfig: jiraTestsAdditionalConfig{
290+
commitMessage: "EX-1 test commit",
291+
},
292+
},
293+
{
294+
wantError: true,
295+
name: "24 if there is a server error, this is output even when assert fails due to no matching issue",
296+
cmd: fmt.Sprintf(`attest jira --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
297+
--name foo
298+
--repo-root %s
299+
--jira-project-key ABC
300+
--jira-base-url https://kosli-test.atlassian.net
301+
--assert
302+
%s`, suite.tmpDir, suite.defaultKosliArguments),
303+
golden: "Error: Artifact with fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 does not exist in trail \"test-123\" of flow \"attest-jira\" belonging to organization \"docs-cmd-test-user\"\nError: no Jira references are found in commit message or branch name\n",
304+
additionalConfig: jiraTestsAdditionalConfig{
305+
commitMessage: "EX-1 test commit",
306+
},
307+
},
308+
{
309+
wantError: true,
310+
name: "25 if there is a server error, this is output even when assert fails due to non-existing issue",
311+
cmd: fmt.Sprintf(`attest jira --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
312+
--name foo
313+
--repo-root %s
314+
--jira-base-url https://kosli-test.atlassian.net
315+
--assert
316+
%s`, suite.tmpDir, suite.defaultKosliArguments),
317+
goldenRegex: "Error: Artifact with fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 does not exist in trail \"test-123\" of flow \"attest-jira\" belonging to organization \"docs-cmd-test-user\"\nError: missing Jira issues from references found in commit message or branch name.*",
318+
additionalConfig: jiraTestsAdditionalConfig{
319+
commitMessage: "SAMI-1 test commit",
320+
},
321+
},
269322
}
270323

271324
for _, test := range tests {

0 commit comments

Comments
 (0)