From 4ec696dacd7a387f658c139a538ac414f2379033 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 28 Oct 2024 13:40:48 -0600 Subject: [PATCH] create common test fixture, organize tests Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/verify/options_test.go | 112 +++++++++------------ 1 file changed, 46 insertions(+), 66 deletions(-) diff --git a/pkg/cmd/attestation/verify/options_test.go b/pkg/cmd/attestation/verify/options_test.go index b9be054a5..77c0e3b23 100644 --- a/pkg/cmd/attestation/verify/options_test.go +++ b/pkg/cmd/attestation/verify/options_test.go @@ -13,29 +13,28 @@ var ( publicGoodBundlePath = test.NormalizeRelativePath("../test/data/psigstore-js-2.1.0-bundle.json") ) +var baseOptions = Options{ + ArtifactPath: publicGoodArtifactPath, + BundlePath: publicGoodBundlePath, + DigestAlgorithm: "sha512", + Limit: 1, + Owner: "sigstore", + OIDCIssuer: "some issuer", +} + func TestAreFlagsValid(t *testing.T) { t.Run("has invalid Repo value", func(t *testing.T) { - opts := Options{ - ArtifactPath: publicGoodArtifactPath, - DigestAlgorithm: "sha512", - OIDCIssuer: "some issuer", - Repo: "sigstoresigstore-js", - } + opts := baseOptions + opts.Repo = "sigstoresigstore-js" err := opts.AreFlagsValid() require.Error(t, err) require.ErrorContains(t, err, "invalid value provided for repo") }) - t.Run("invalid limit < 0", func(t *testing.T) { - opts := Options{ - ArtifactPath: publicGoodArtifactPath, - BundlePath: publicGoodBundlePath, - DigestAlgorithm: "sha512", - Owner: "sigstore", - OIDCIssuer: "some issuer", - Limit: 0, - } + t.Run("invalid limit == 0", func(t *testing.T) { + opts := baseOptions + opts.Limit = 0 err := opts.AreFlagsValid() require.Error(t, err) @@ -43,19 +42,43 @@ func TestAreFlagsValid(t *testing.T) { }) t.Run("invalid limit > 1000", func(t *testing.T) { - opts := Options{ - ArtifactPath: publicGoodArtifactPath, - BundlePath: publicGoodBundlePath, - DigestAlgorithm: "sha512", - Owner: "sigstore", - OIDCIssuer: "some issuer", - Limit: 1001, - } + opts := baseOptions + opts.Limit = 1001 err := opts.AreFlagsValid() require.Error(t, err) require.ErrorContains(t, err, "limit 1001 not allowed, must be between 1 and 1000") }) + + t.Run("returns error when UseBundleFromRegistry is true and ArtifactPath is not an OCI path", func(t *testing.T) { + opts := baseOptions + opts.BundlePath = "" + opts.UseBundleFromRegistry = true + + err := opts.AreFlagsValid() + require.Error(t, err) + require.ErrorContains(t, err, "bundle-from-oci flag can only be used with OCI artifact paths") + }) + + t.Run("does not return error when UseBundleFromRegistry is true and ArtifactPath is an OCI path", func(t *testing.T) { + opts := baseOptions + opts.ArtifactPath = "oci://sigstore/sigstore-js:2.1.0" + opts.BundlePath = "" + opts.UseBundleFromRegistry = true + + err := opts.AreFlagsValid() + require.NoError(t, err) + }) + + t.Run("returns error when UseBundleFromRegistry is true and BundlePath is provided", func(t *testing.T) { + opts := baseOptions + opts.ArtifactPath = "oci://sigstore/sigstore-js:2.1.0" + opts.UseBundleFromRegistry = true + + err := opts.AreFlagsValid() + require.Error(t, err) + require.ErrorContains(t, err, "bundle-from-oci flag cannot be used with bundle-path flag") + }) } func TestSetPolicyFlags(t *testing.T) { @@ -116,47 +139,4 @@ func TestSetPolicyFlags(t *testing.T) { require.Equal(t, "sigstore", opts.Owner) require.Equal(t, "^https://github/foo", opts.SANRegex) }) - - t.Run("returns error when UseBundleFromRegistry is true and ArtifactPath is not an OCI path", func(t *testing.T) { - opts := Options{ - ArtifactPath: publicGoodArtifactPath, - DigestAlgorithm: "sha512", - Owner: "sigstore", - UseBundleFromRegistry: true, - Limit: 1, - } - - err := opts.AreFlagsValid() - require.Error(t, err) - require.ErrorContains(t, err, "bundle-from-oci flag can only be used with OCI artifact paths") - }) - - t.Run("does not return error when UseBundleFromRegistry is true and ArtifactPath is an OCI path", func(t *testing.T) { - opts := Options{ - ArtifactPath: "oci://sigstore/sigstore-js:2.1.0", - DigestAlgorithm: "sha512", - OIDCIssuer: "some issuer", - Owner: "sigstore", - UseBundleFromRegistry: true, - Limit: 1, - } - - err := opts.AreFlagsValid() - require.NoError(t, err) - }) - - t.Run("returns error when UseBundleFromRegistry is true and BundlePath is provided", func(t *testing.T) { - opts := Options{ - ArtifactPath: "oci://sigstore/sigstore-js:2.1.0", - BundlePath: publicGoodBundlePath, - DigestAlgorithm: "sha512", - Owner: "sigstore", - UseBundleFromRegistry: true, - Limit: 1, - } - - err := opts.AreFlagsValid() - require.Error(t, err) - require.ErrorContains(t, err, "bundle-from-oci flag cannot be used with bundle-path flag") - }) }