Skip to content

Commit 83238ce

Browse files
authored
Add missing tests for assert approval (#581)
1 parent f472906 commit 83238ce

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

cmd/kosli/assertApproval.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func newAssertApprovalCmd(out io.Writer) *cobra.Command {
5757
if err != nil {
5858
return ErrorBeforePrintingUsage(cmd, err.Error())
5959
}
60+
61+
err = MuXRequiredFlags(cmd, []string{"fingerprint", "artifact-type"}, true)
62+
if err != nil {
63+
return err
64+
}
65+
6066
return ValidateRegistryFlags(cmd, o.fingerprintOptions)
6167

6268
},

cmd/kosli/assertApproval_test.go

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type AssertApprovalCommandTestSuite struct {
2222

2323
type assertApprovalTestConfig struct {
2424
createApproval bool
25+
isRequest bool
2526
}
2627

2728
func (suite *AssertApprovalCommandTestSuite) SetupTest() {
@@ -49,60 +50,83 @@ func (suite *AssertApprovalCommandTestSuite) TestAssertApprovalCmd() {
4950
tests := []cmdTestCase{
5051
{
5152
wantError: true,
52-
name: "missing --org fails",
53+
name: "1 missing --org fails",
5354
cmd: fmt.Sprintf(`assert approval --fingerprint 8e568bd886069f1290def0caabc1e97ce0e7b80c105e611258b57d76fcef234c --flow %s --api-token secret`, suite.flowName),
5455
golden: "Error: --org is not set\nUsage: kosli assert approval [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n",
5556
},
5657
{
5758
wantError: true,
58-
name: "asserting approval for a non existing artifact fails",
59+
name: "2 asserting approval for a non existing artifact fails",
5960
cmd: fmt.Sprintf(`assert approval --fingerprint 8e568bd886069f1290def0caabc1e97ce0e7b80c105e611258b57d76fcef234c --flow %s %s`, suite.flowName, suite.defaultKosliArguments),
6061
golden: "Error: Artifact with fingerprint '8e568bd886069f1290def0caabc1e97ce0e7b80c105e611258b57d76fcef234c' does not exist in flow 'assert-approval' belonging to organization 'docs-cmd-test-user'\n",
6162
},
6263
{
6364
wantError: true,
64-
name: "asserting an existing artifact that does not have an approval (using --fingerprint) works and exits with non-zero code",
65+
name: "3 asserting an existing artifact that does not have an approval (using --fingerprint) works and exits with non-zero code",
6566
cmd: fmt.Sprintf(`assert approval --fingerprint %s --flow %s %s`, suite.fingerprint, suite.flowName, suite.defaultKosliArguments),
6667
golden: "Error: artifact with fingerprint fcf33337634c2577a5d86fd7ecb0a25a7c1bb5d89c14fd236f546a5759252c02 has no approvals created\n",
6768
},
6869
{
6970
wantError: true,
70-
name: "asserting approval of an existing artifact that does not have an approval (using --artifact-type) works and exits with non-zero code",
71+
name: "4 asserting approval of an existing artifact that does not have an approval (using --artifact-type) works and exits with non-zero code",
7172
cmd: fmt.Sprintf(`assert approval %s --artifact-type file --flow %s %s`, suite.artifactPath, suite.flowName, suite.defaultKosliArguments),
7273
golden: "Error: artifact with fingerprint fcf33337634c2577a5d86fd7ecb0a25a7c1bb5d89c14fd236f546a5759252c02 has no approvals created\n",
7374
},
7475
{
75-
name: "asserting approval of an existing artifact that has an approval (using --artifact-type) works and exits with zero code",
76+
name: "5 asserting approval of an existing artifact that has an approval (using --artifact-type) works and exits with zero code",
7677
cmd: fmt.Sprintf(`assert approval %s --artifact-type file --flow %s %s`, suite.artifactPath, suite.flowName, suite.defaultKosliArguments),
7778
golden: "artifact with fingerprint fcf33337634c2577a5d86fd7ecb0a25a7c1bb5d89c14fd236f546a5759252c02 is approved (approval no. [1])\n",
7879
additionalConfig: assertApprovalTestConfig{
7980
createApproval: true,
81+
isRequest: false,
8082
},
8183
},
84+
//The approval created in test 5 is valid for this test also
85+
{
86+
name: "6 asserting approval of an existing artifact that has an approval (using --fingerprint) works and exits with zero code",
87+
cmd: fmt.Sprintf(`assert approval --fingerprint %s --flow %s %s`, suite.fingerprint, suite.flowName, suite.defaultKosliArguments),
88+
golden: "artifact with fingerprint fcf33337634c2577a5d86fd7ecb0a25a7c1bb5d89c14fd236f546a5759252c02 is approved (approval no. [1])\n",
89+
},
8290
{
8391
wantError: true,
84-
name: "not providing --fingerprint nor --artifact-type fails",
92+
name: "7 not providing --fingerprint nor --artifact-type fails",
8593
cmd: fmt.Sprintf(`assert approval --flow %s %s`, suite.flowName, suite.defaultKosliArguments),
8694
golden: "Error: docker image name or file/dir path is required when --fingerprint is not provided\nUsage: kosli assert approval [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n",
8795
},
88-
// TODO: this test case does not pass as the validation does not check for it
89-
// {
90-
// wantError: true,
91-
// name: "providing both --fingerprint and --artifact-type fails",
92-
// cmd: fmt.Sprintf(`assert approval --artifact-type file --fingerprint %s --flow %s %s`, suite.fingerprint, suite.flowName, suite.defaultKosliArguments),
93-
// golden: "COMPLIANT\n",
94-
// },
9596
{
9697
wantError: true,
97-
name: "missing --flow fails",
98+
name: "8 providing both --fingerprint and --artifact-type fails",
99+
cmd: fmt.Sprintf(`assert approval --artifact-type file --fingerprint %s --flow %s %s`, suite.fingerprint, suite.flowName, suite.defaultKosliArguments),
100+
golden: "Error: only one of --fingerprint, --artifact-type is allowed\n",
101+
},
102+
{
103+
wantError: true,
104+
name: "9 missing --flow fails",
98105
cmd: fmt.Sprintf(`assert approval --fingerprint %s %s`, suite.fingerprint, suite.defaultKosliArguments),
99106
golden: "Error: required flag(s) \"flow\" not set\n",
100107
},
108+
{
109+
wantError: true,
110+
name: "10 asserting approval of an unapproved existing artifact (using --artifact-type) works and exits with non-zero code",
111+
cmd: fmt.Sprintf(`assert approval %s --artifact-type file --flow %s %s`, suite.artifactPath, suite.flowName, suite.defaultKosliArguments),
112+
golden: "Error: artifact with fingerprint fcf33337634c2577a5d86fd7ecb0a25a7c1bb5d89c14fd236f546a5759252c02 is not approved\n",
113+
additionalConfig: assertApprovalTestConfig{
114+
createApproval: true,
115+
isRequest: true,
116+
},
117+
},
118+
// The approval request created in test 9 is valid here too
119+
{
120+
wantError: true,
121+
name: "11 asserting approval of an unapproved existing artifact (using --fingerprint) works and exits with non-zero code",
122+
cmd: fmt.Sprintf(`assert approval --fingerprint %s --flow %s %s`, suite.fingerprint, suite.flowName, suite.defaultKosliArguments),
123+
golden: "Error: artifact with fingerprint fcf33337634c2577a5d86fd7ecb0a25a7c1bb5d89c14fd236f546a5759252c02 is not approved\n",
124+
},
101125
}
102126

103127
for _, t := range tests {
104128
if t.additionalConfig != nil && t.additionalConfig.(assertApprovalTestConfig).createApproval {
105-
CreateApproval(suite.flowName, suite.fingerprint, suite.Suite.T())
129+
CreateApproval(suite.flowName, suite.fingerprint, t.additionalConfig.(assertApprovalTestConfig).isRequest, suite.Suite.T())
106130
}
107131
runTestCmd(suite.Suite.T(), []cmdTestCase{t})
108132
}

cmd/kosli/getApproval_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (suite *GetApprovalCommandTestSuite) SetupTest() {
2929

3030
CreateFlow(suite.flowName, suite.Suite.T())
3131
CreateArtifact(suite.flowName, suite.fingerprint, "approved-artifact", suite.Suite.T())
32-
CreateApproval(suite.flowName, suite.fingerprint, suite.Suite.T())
33-
CreateApproval(suite.flowName, suite.fingerprint, suite.Suite.T())
32+
CreateApproval(suite.flowName, suite.fingerprint, false, suite.Suite.T())
33+
CreateApproval(suite.flowName, suite.fingerprint, false, suite.Suite.T())
3434
}
3535

3636
func (suite *GetApprovalCommandTestSuite) TestGetApprovalCmd() {

cmd/kosli/listApprovals_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (suite *ListApprovalsCommandTestSuite) SetupTest() {
4141
suite.fingerprint, err = GetSha256Digest(suite.artifactPath, fingerprintOptions, logger)
4242
require.NoError(suite.Suite.T(), err)
4343
CreateArtifact(suite.flowName2, suite.fingerprint, suite.artifactName, suite.Suite.T())
44-
CreateApproval(suite.flowName2, suite.fingerprint, suite.Suite.T())
44+
CreateApproval(suite.flowName2, suite.fingerprint, false, suite.Suite.T())
4545
}
4646

4747
func (suite *ListApprovalsCommandTestSuite) TestListApprovalsCmd() {

cmd/kosli/testHelpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ func CreateArtifactWithCommit(flowName, artifactFingerprint, artifactName string
405405
}
406406

407407
// CreateApproval creates an approval for an artifact in a flow
408-
func CreateApproval(flowName, fingerprint string, t *testing.T) {
408+
// If isRequest is true, this creates an approval request
409+
func CreateApproval(flowName, fingerprint string, isRequest bool, t *testing.T) {
409410
t.Helper()
410411
o := &reportApprovalOptions{
411412
payload: ApprovalPayload{
@@ -418,7 +419,7 @@ func CreateApproval(flowName, fingerprint string, t *testing.T) {
418419
srcRepoRoot: "../..",
419420
}
420421

421-
err := o.run([]string{"filename"}, false)
422+
err := o.run([]string{"filename"}, isRequest)
422423
require.NoError(t, err, "approval should be created without error")
423424
}
424425

0 commit comments

Comments
 (0)