|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/suite" |
| 8 | +) |
| 9 | + |
| 10 | +// Define the suite, and absorb the built-in basic suite |
| 11 | +// functionality from testify - including a T() method which |
| 12 | +// returns the current testing context |
| 13 | +type AttestCustomCommandTestSuite struct { |
| 14 | + flowName string |
| 15 | + trailName string |
| 16 | + artifactFingerprint string |
| 17 | + typeName string |
| 18 | + schemaFilePath string |
| 19 | + jqRules []string |
| 20 | + attestationDataFile string |
| 21 | + suite.Suite |
| 22 | + defaultKosliArguments string |
| 23 | +} |
| 24 | + |
| 25 | +func (suite *AttestCustomCommandTestSuite) SetupTest() { |
| 26 | + suite.flowName = "attest-custom" |
| 27 | + suite.trailName = "test-321" |
| 28 | + suite.artifactFingerprint = "7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9" |
| 29 | + suite.typeName = "person" |
| 30 | + suite.schemaFilePath = "" |
| 31 | + suite.attestationDataFile = "testdata/person-type-data-example.json" |
| 32 | + suite.jqRules = []string{} |
| 33 | + global = &GlobalOpts{ |
| 34 | + ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY", |
| 35 | + Org: "docs-cmd-test-user", |
| 36 | + Host: "http://localhost:8001", |
| 37 | + } |
| 38 | + suite.defaultKosliArguments = fmt.Sprintf(" --type %s --attestation-data %s --flow %s --trail %s --repo-root ../.. --host %s --org %s --api-token %s", suite.typeName, suite.attestationDataFile, suite.flowName, suite.trailName, global.Host, global.Org, global.ApiToken) |
| 39 | + |
| 40 | + CreateCustomAttestationType(suite.typeName, suite.schemaFilePath, suite.jqRules, suite.T()) |
| 41 | + CreateFlow(suite.flowName, suite.T()) |
| 42 | + CreateArtifactOnTrail(suite.flowName, suite.trailName, "cli", suite.artifactFingerprint, "file1", suite.T()) |
| 43 | +} |
| 44 | + |
| 45 | +func (suite *AttestCustomCommandTestSuite) TestAttestCustomCmd() { |
| 46 | + tests := []cmdTestCase{ |
| 47 | + { |
| 48 | + wantError: true, |
| 49 | + name: "fails when more arguments are provided", |
| 50 | + cmd: fmt.Sprintf("attest custom foo bar %s", suite.defaultKosliArguments), |
| 51 | + golden: "Error: accepts at most 1 arg(s), received 2 [foo bar]\n", |
| 52 | + }, |
| 53 | + { |
| 54 | + wantError: true, |
| 55 | + name: "fails when missing a required flag", |
| 56 | + cmd: fmt.Sprintf("attest custom foo -t file %s", suite.defaultKosliArguments), |
| 57 | + golden: "Error: required flag(s) \"name\" not set\n", |
| 58 | + }, |
| 59 | + { |
| 60 | + wantError: true, |
| 61 | + name: "fails when artifact-name is provided and there is no --artifact-type", |
| 62 | + cmd: fmt.Sprintf("attest custom wibble %s", suite.defaultKosliArguments), |
| 63 | + golden: "Error: --artifact-type or --fingerprint must be specified when artifact name ('wibble') argument is supplied.\nUsage: kosli attest custom [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n", |
| 64 | + }, |
| 65 | + { |
| 66 | + wantError: true, |
| 67 | + name: "fails when both --fingerprint and --artifact-type", |
| 68 | + cmd: fmt.Sprintf("attest custom foo --fingerprint xxxx --artifact-type file --name bar --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 69 | + golden: "Error: only one of --fingerprint, --artifact-type is allowed\n", |
| 70 | + }, |
| 71 | + { |
| 72 | + wantError: true, |
| 73 | + name: "fails when --fingerprint is not valid", |
| 74 | + cmd: fmt.Sprintf("attest custom --name foo --fingerprint xxxx --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 75 | + golden: "Error: xxxx is not a valid SHA256 fingerprint. It should match the pattern ^([a-f0-9]{64})$\nUsage: kosli attest custom [IMAGE-NAME | FILE-PATH | DIR-PATH] [flags]\n", |
| 76 | + }, |
| 77 | + { |
| 78 | + wantError: true, |
| 79 | + name: "attesting against an artifact that does not exist fails", |
| 80 | + cmd: fmt.Sprintf("attest custom --fingerprint 3214e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 --name foo --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 81 | + golden: "Error: Artifact with fingerprint 3214e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 does not exist in trail \"test-321\" of flow \"attest-custom\" belonging to organization \"docs-cmd-test-user\"\n", |
| 82 | + }, |
| 83 | + { |
| 84 | + wantError: true, |
| 85 | + name: "fails when --name is passed as empty string", |
| 86 | + cmd: fmt.Sprintf("attest custom --name \"\" --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 87 | + golden: "Error: flag '--name' is required, but empty string was provided\n", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "can attest custom against an artifact using artifact-name and --fingerprint", |
| 91 | + cmd: fmt.Sprintf("attest custom testdata/file1 %s --name foo --fingerprint %s", suite.defaultKosliArguments, suite.artifactFingerprint), |
| 92 | + golden: "custom:person attestation 'foo' is reported to trail: test-321\n", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "can attest custom against an artifact using artifact name and --artifact-type", |
| 96 | + cmd: fmt.Sprintf("attest custom testdata/file1 --artifact-type file --name foo --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 97 | + golden: "custom:person attestation 'foo' is reported to trail: test-321\n", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "can attest custom against an artifact using artifact name and --artifact-type when --name does not exist in the trail template", |
| 101 | + cmd: fmt.Sprintf("attest custom testdata/file1 --artifact-type file --name bar --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 102 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "can attest custom against an artifact using --fingerprint and no artifact-name", |
| 106 | + cmd: fmt.Sprintf("attest custom --fingerprint %s --name foo --commit HEAD --origin-url http://example.com %s", suite.artifactFingerprint, suite.defaultKosliArguments), |
| 107 | + golden: "custom:person attestation 'foo' is reported to trail: test-321\n", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "can attest custom against a trail", |
| 111 | + cmd: fmt.Sprintf("attest custom --name bar --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 112 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "can attest custom against a trail when name is not found in the trail template", |
| 116 | + cmd: fmt.Sprintf("attest custom --name additional --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 117 | + golden: "custom:person attestation 'additional' is reported to trail: test-321\n", |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "can attest custom against an artifact it is created using dot syntax in --name", |
| 121 | + cmd: fmt.Sprintf("attest custom --name cli.foo --commit HEAD --origin-url http://example.com %s", suite.defaultKosliArguments), |
| 122 | + golden: "custom:person attestation 'foo' is reported to trail: test-321\n", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "can attest custom attestation with attachment against a trail", |
| 126 | + cmd: fmt.Sprintf("attest custom --name bar --attachments testdata/file1 %s", suite.defaultKosliArguments), |
| 127 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "can attest custom attestation with external-url against a trail", |
| 131 | + cmd: fmt.Sprintf("attest custom --name bar --external-url foo=https://foo.com %s", suite.defaultKosliArguments), |
| 132 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "can attest custom attestation with external-url and external-fingerprint against a trail", |
| 136 | + cmd: fmt.Sprintf("attest custom --name bar --external-url file=https://foo.com/file --external-fingerprint file=7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 %s", suite.defaultKosliArguments), |
| 137 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 138 | + }, |
| 139 | + { |
| 140 | + wantError: true, |
| 141 | + name: "fails when external-fingerprint has more items more than external-url", |
| 142 | + cmd: fmt.Sprintf("attest custom --name bar --external-fingerprint file=7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 %s", suite.defaultKosliArguments), |
| 143 | + golden: "Error: --external-fingerprints have labels that don't have a URL in --external-url\n", |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "can attest custom attestation with description against a trail", |
| 147 | + cmd: fmt.Sprintf("attest custom --name bar --description 'foo bar foo' %s", suite.defaultKosliArguments), |
| 148 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "can attest with annotations against a trail", |
| 152 | + cmd: fmt.Sprintf("attest custom --name bar --annotate foo=bar --annotate baz=\"data with spaces\" %s", suite.defaultKosliArguments), |
| 153 | + golden: "custom:person attestation 'bar' is reported to trail: test-321\n", |
| 154 | + }, |
| 155 | + { |
| 156 | + wantError: true, |
| 157 | + name: "fails when annotation is not valid", |
| 158 | + cmd: fmt.Sprintf("attest custom --name bar --annotate foo.baz=bar %s", suite.defaultKosliArguments), |
| 159 | + golden: "Error: --annotate flag should be in the format key=value. Invalid key: 'foo.baz'. Key can only contain [A-Za-z0-9_].\n", |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + runTestCmd(suite.T(), tests) |
| 164 | +} |
| 165 | + |
| 166 | +// In order for 'go test' to run this suite, we need to create |
| 167 | +// a normal test function and pass our suite to suite.Run |
| 168 | +func TestAttestCustomCommandTestSuite(t *testing.T) { |
| 169 | + suite.Run(t, new(AttestCustomCommandTestSuite)) |
| 170 | +} |
0 commit comments