Skip to content

Commit

Permalink
Add validate image default output test coverage
Browse files Browse the repository at this point in the history
Motivated by CodeCov complaining about one uncovered code path.

I decided to test the output with and without --show-successes. It's
perhaps more than was needed, but actually this is the first unit
test coverage of the validate image text output, (existing coverage
is all in the acceptance tests), so I think it's worthwhile.

Ref: https://issues.redhat.com/browse/EC-758
  • Loading branch information
simonbaird committed Aug 28, 2024
1 parent acad497 commit c97bb5c
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion cmd/validate/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func happyValidator() imageValidationFunc {
{
Message: "Pass",
Metadata: map[string]interface{}{
"code": "policy.nice",
"code": "policy.nice",
"title": "Very nice",
},
},
},
Expand Down Expand Up @@ -1221,3 +1222,67 @@ func TestValidateImageCommand_RunE(t *testing.T) {
}
}`, effectiveTimeTest, utils.TestPublicKeyJSON, utils.TestPublicKeyJSON), out.String())
}

func TestValidateImageDefaultOutput(t *testing.T) {
commonArgs := []string{
"validate",
"image",
"--image",
"registry/image:tag",
"--policy",
fmt.Sprintf(`{"publicKey": %s}`, utils.TestPublicKeyJSON),
}

commonOutput := hd.Doc(`
Success: true
Result: SUCCESS
Violations: 0, Warnings: 0, Successes: 1
Component: Unnamed
ImageRef: registry/image:tag
Results:
`)

cases := []struct {
args []string
expected string
}{
{
args: commonArgs,
expected: commonOutput,
},
{
args: append(commonArgs, "--show-successes"),
expected: fmt.Sprintf("%s%s", commonOutput, hd.Doc(`
✓ [Success] policy.nice
ImageRef: registry/image:tag
Title: Very nice
`)),
},
}

for _, c := range cases {
validateImageCmd := validateImageCmd(happyValidator())
cmd := setUpCobra(validateImageCmd)

ctx := utils.WithFS(context.Background(), afero.NewMemMapFs())
client := fake.FakeClient{}
commonMockClient(&client)
ctx = oci.WithClient(ctx, &client)
cmd.SetContext(ctx)

// Notice there is no --output flag here
cmd.SetArgs(c.args)

var out bytes.Buffer
cmd.SetOut(&out)

utils.SetTestRekorPublicKey(t)

err := cmd.Execute()
assert.NoError(t, err)

assert.Equal(t, c.expected, out.String())
}
}

0 comments on commit c97bb5c

Please sign in to comment.