From 578a5e5bcf2c89ef69e733f4aecc9f3c41a94728 Mon Sep 17 00:00:00 2001 From: Faye Date: Fri, 28 Nov 2025 12:43:46 +0100 Subject: [PATCH 1/2] Add tests for assert flag in attest gitlab pr --- cmd/kosli/attestPRGitlab_test.go | 76 ++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/cmd/kosli/attestPRGitlab_test.go b/cmd/kosli/attestPRGitlab_test.go index 886fd7b83..0bc6cbbb5 100644 --- a/cmd/kosli/attestPRGitlab_test.go +++ b/cmd/kosli/attestPRGitlab_test.go @@ -17,6 +17,8 @@ type AttestGitlabPRCommandTestSuite struct { flowName string trailName string tmpDir string + commitWithPR string + commitWithNoPR string artifactFingerprint string suite.Suite defaultKosliArguments string @@ -27,6 +29,8 @@ func (suite *AttestGitlabPRCommandTestSuite) SetupTest() { suite.flowName = "attest-gitlab-pr" suite.trailName = "test-123" + suite.commitWithPR = "f6d2c1a288f2c400c04e8451f4fdddb1f3b4ce01" + suite.commitWithNoPR = "30a7a221184e88524adabbccabe8a3d63371e443" suite.artifactFingerprint = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9" global = &GlobalOpts{ ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY", @@ -40,7 +44,7 @@ func (suite *AttestGitlabPRCommandTestSuite) SetupTest() { _, err = testHelpers.CloneGitRepo("https://gitlab.com/kosli-dev/merkely-gitlab-demo.git", suite.tmpDir) require.NoError(suite.Suite.T(), err) - suite.defaultKosliArguments = fmt.Sprintf(" --flow %s --trail %s --repo-root %s --commit f6d2c1a288f2c400c04e8451f4fdddb1f3b4ce01 --host %s --org %s --api-token %s", suite.flowName, suite.trailName, suite.tmpDir, global.Host, global.Org, global.ApiToken) + suite.defaultKosliArguments = fmt.Sprintf(" --flow %s --trail %s --repo-root %s --host %s --org %s --api-token %s", suite.flowName, suite.trailName, suite.tmpDir, global.Host, global.Org, global.ApiToken) CreateFlowWithTemplate(suite.flowName, "testdata/valid_template.yml", suite.Suite.T()) BeginTrail(suite.trailName, suite.flowName, "", suite.Suite.T()) CreateArtifactOnTrail(suite.flowName, suite.trailName, "cli", suite.artifactFingerprint, "file1", suite.Suite.T()) @@ -54,71 +58,97 @@ func (suite *AttestGitlabPRCommandTestSuite) TestAttestGitlabPRCmd() { tests := []cmdTestCase{ { wantError: true, - name: "fails when more arguments are provided", - cmd: fmt.Sprintf("attest pullrequest gitlab foo bar %s", suite.defaultKosliArguments), + name: "01 fails when more arguments are provided", + cmd: fmt.Sprintf("attest pullrequest gitlab foo bar --commit %s %s", suite.commitWithPR, suite.defaultKosliArguments), golden: "Error: accepts at most 1 arg(s), received 2 [foo bar]\n", }, { wantError: true, - name: "fails when missing a required flags", - cmd: fmt.Sprintf("attest pullrequest gitlab foo -t file %s", suite.defaultKosliArguments), + name: "02 fails when missing a required flags", + cmd: fmt.Sprintf("attest pullrequest gitlab foo -t file --commit %s %s", suite.commitWithPR, suite.defaultKosliArguments), golden: "Error: required flag(s) \"gitlab-org\", \"name\", \"repository\" not set\n", }, { wantError: true, - name: "fails when both --fingerprint and --artifact-type", - cmd: fmt.Sprintf("attest pullrequest gitlab testdata/file1 --fingerprint xxxx --artifact-type file --name bar %s", suite.defaultKosliArguments), + name: "03 fails when both --fingerprint and --artifact-type", + cmd: fmt.Sprintf("attest pullrequest gitlab testdata/file1 --fingerprint xxxx --artifact-type file --name bar --commit %s %s", suite.commitWithPR, suite.defaultKosliArguments), golden: "Error: only one of --fingerprint, --artifact-type is allowed\n", }, { wantError: true, - name: "fails when --fingerprint is not valid", - cmd: fmt.Sprintf("attest pullrequest gitlab --name foo --fingerprint xxxx %s", suite.defaultKosliArguments), + name: "04 fails when --fingerprint is not valid", + cmd: fmt.Sprintf("attest pullrequest gitlab --name foo --fingerprint xxxx --commit %s %s", suite.commitWithPR, suite.defaultKosliArguments), golden: "Error: xxxx is not a valid SHA256 fingerprint. It should match the pattern ^([a-f0-9]{64})$\nUsage: kosli attest pullrequest gitlab [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n", }, { wantError: true, - name: "attesting against an artifact that does not exist fails", + name: "05 attesting against an artifact that does not exist fails", cmd: fmt.Sprintf(`attest pullrequest gitlab --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\nError: Artifact with fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 does not exist in trail \"test-123\" of flow \"attest-gitlab-pr\" belonging to organization \"docs-cmd-test-user\"\n", }, { - name: "can attest gitlab pr against an artifact using artifact name and --artifact-type", + name: "06 can attest gitlab pr against an artifact using artifact name and --artifact-type", cmd: fmt.Sprintf(`attest pullrequest gitlab testdata/file1 --artifact-type file --name foo - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'foo' is reported to trail: test-123\n", }, { - name: "can attest gitlab pr against an artifact using artifact name and --artifact-type when --name does not exist in the trail template", + name: "07 can attest gitlab pr against an artifact using artifact name and --artifact-type when --name does not exist in the trail template", cmd: fmt.Sprintf(`attest pullrequest gitlab testdata/file1 --artifact-type file --name bar - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\n", }, { - name: "can attest gitlab pr against an artifact using --fingerprint", + name: "08 can attest gitlab pr against an artifact using --fingerprint", cmd: fmt.Sprintf(`attest pullrequest gitlab --fingerprint 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'foo' is reported to trail: test-123\n", }, { - name: "can attest gitlab pr against a trail", + name: "09 can attest gitlab pr against a trail", cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\n", }, { - name: "can attest gitlab pr against a trail when name is not found in the trail template", + name: "10 can attest gitlab pr against a trail when name is not found in the trail template", cmd: fmt.Sprintf(`attest pullrequest gitlab --name additional - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'additional' is reported to trail: test-123\n", }, { - name: "can attest gitlab pr against an artifact it is created using dot syntax in --name", + name: "11 can attest gitlab pr against an artifact it is created using dot syntax in --name", cmd: fmt.Sprintf(`attest pullrequest gitlab --name cli.foo - --gitlab-org kosli-dev --repository merkely-gitlab-demo %s`, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'foo' is reported to trail: test-123\n", }, + { + wantError: true, + name: "12 assert fails with non-zero exit code when commit has no merge requests", + cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar + --github-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithNoPR, suite.defaultKosliArguments), + goldenRegex: "found 0 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\nError: assert failed: no merge request found for the given commit: .*\n", + }, + { + name: "13 assert works and has zero exit code when commit has merge request(s)", + cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar + --github-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithPR, suite.defaultKosliArguments), + goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\n", + }, + { + wantError: true, + name: "14 if there is a server error, this is output even when assert fails", + cmd: fmt.Sprintf(`attest pullrequest gitlab --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo + --github-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithNoPR, suite.defaultKosliArguments), + goldenRegex: "found 0 merge request\\(s\\) for commit: .*\nError: Artifact with fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 does not exist in trail \"test-123\" of flow \"attest-gitlab-pr\" belonging to organization \"docs-cmd-test-user\"\nError: assert failed: no merge request found for the given commit: .*\n", + }, + { + name: "15 can attest github pr even if commit has no merge request", + cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar + --github-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithNoPR, suite.defaultKosliArguments), + goldenRegex: "found 0 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\n", + }, } runTestCmd(suite.Suite.T(), tests) From a1b5ee898b1065d4a6ea4add723b0f676fec185a Mon Sep 17 00:00:00 2001 From: Faye Date: Fri, 28 Nov 2025 13:08:48 +0100 Subject: [PATCH 2/2] Fix typos in tests --- cmd/kosli/attestPRGitlab_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/kosli/attestPRGitlab_test.go b/cmd/kosli/attestPRGitlab_test.go index 0bc6cbbb5..2979c98a7 100644 --- a/cmd/kosli/attestPRGitlab_test.go +++ b/cmd/kosli/attestPRGitlab_test.go @@ -127,26 +127,26 @@ func (suite *AttestGitlabPRCommandTestSuite) TestAttestGitlabPRCmd() { wantError: true, name: "12 assert fails with non-zero exit code when commit has no merge requests", cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar - --github-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithNoPR, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithNoPR, suite.defaultKosliArguments), goldenRegex: "found 0 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\nError: assert failed: no merge request found for the given commit: .*\n", }, { name: "13 assert works and has zero exit code when commit has merge request(s)", cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar - --github-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithPR, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithPR, suite.defaultKosliArguments), goldenRegex: "found 1 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\n", }, { wantError: true, name: "14 if there is a server error, this is output even when assert fails", cmd: fmt.Sprintf(`attest pullrequest gitlab --fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo - --github-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithNoPR, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s --assert %s`, suite.commitWithNoPR, suite.defaultKosliArguments), goldenRegex: "found 0 merge request\\(s\\) for commit: .*\nError: Artifact with fingerprint 1234e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 does not exist in trail \"test-123\" of flow \"attest-gitlab-pr\" belonging to organization \"docs-cmd-test-user\"\nError: assert failed: no merge request found for the given commit: .*\n", }, { name: "15 can attest github pr even if commit has no merge request", cmd: fmt.Sprintf(`attest pullrequest gitlab --name bar - --github-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithNoPR, suite.defaultKosliArguments), + --gitlab-org kosli-dev --repository merkely-gitlab-demo --commit %s %s`, suite.commitWithNoPR, suite.defaultKosliArguments), goldenRegex: "found 0 merge request\\(s\\) for commit: .*\ngitlab merge request attestation 'bar' is reported to trail: test-123\n", }, }