From ab4912ff48f8aea8ec48e96079e46ea4af8c546f Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Mon, 16 Dec 2024 12:40:13 -0700 Subject: [PATCH] fix failing tests Signed-off-by: Meredith Lancaster --- pkg/cmd/attestation/api/client.go | 6 +++++- pkg/cmd/attestation/api/client_test.go | 6 ++++++ pkg/cmd/attestation/api/mock_httpClient_test.go | 7 ++++--- pkg/cmd/attestation/verification/attestation.go | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/attestation/api/client.go b/pkg/cmd/attestation/api/client.go index fc5e06f7b..2519aa9e4 100644 --- a/pkg/cmd/attestation/api/client.go +++ b/pkg/cmd/attestation/api/client.go @@ -88,7 +88,11 @@ func (c *LiveClient) GetByOwnerAndDigest(owner, digest string, limit int) ([]*At url := c.buildOwnerAndDigestURL(owner, digest) attestations, err := c.getAttestations(url, owner, digest, limit) if err != nil { - return nil, fmt.Errorf("failed to fetch attestation by repo and digest: %w", err) + return nil, err + } + + if len(attestations) == 0 { + return nil, newErrNoAttestations(owner, digest) } bundles, err := c.fetchBundlesByURL(attestations) diff --git a/pkg/cmd/attestation/api/client_test.go b/pkg/cmd/attestation/api/client_test.go index 9e47870cf..540ae1366 100644 --- a/pkg/cmd/attestation/api/client_test.go +++ b/pkg/cmd/attestation/api/client_test.go @@ -144,6 +144,9 @@ func TestGetByDigest_NoAttestationsFound(t *testing.T) { githubAPI: mockAPIClient{ OnRESTWithNext: fetcher.OnRESTWithNextNoAttestations, }, + httpClient: mockHttpClient{ + OnGet: fetcher.OnGetSuccess, + }, logger: io.NewTestHandler(), } @@ -222,6 +225,9 @@ func TestGetAttestationsRetries(t *testing.T) { githubAPI: mockAPIClient{ OnRESTWithNext: fetcher.FlakyOnRESTSuccessWithNextPageHandler(), }, + httpClient: mockHttpClient{ + OnGet: fetcher.OnGetSuccess, + }, logger: io.NewTestHandler(), } diff --git a/pkg/cmd/attestation/api/mock_httpClient_test.go b/pkg/cmd/attestation/api/mock_httpClient_test.go index 70eceeea5..89584874f 100644 --- a/pkg/cmd/attestation/api/mock_httpClient_test.go +++ b/pkg/cmd/attestation/api/mock_httpClient_test.go @@ -1,11 +1,12 @@ package api import ( + "bytes" "io" "net/http" - "strings" "github.com/cli/cli/v2/pkg/cmd/attestation/test/data" + "github.com/golang/snappy" ) type mockHttpClient struct { @@ -17,9 +18,9 @@ func (m mockHttpClient) Get(url string) (*http.Response, error) { } func (m *mockDataGenerator) OnGetSuccess(url string) (*http.Response, error) { - bundle := data.SigstoreBundle(nil) + compressed := snappy.Encode(nil, data.SigstoreBundleRaw) return &http.Response{ StatusCode: 200, - Body: io.NopCloser(strings.NewReader(bundle.String())), + Body: io.NopCloser(bytes.NewReader(compressed)), }, nil } diff --git a/pkg/cmd/attestation/verification/attestation.go b/pkg/cmd/attestation/verification/attestation.go index 41da71010..8022a4943 100644 --- a/pkg/cmd/attestation/verification/attestation.go +++ b/pkg/cmd/attestation/verification/attestation.go @@ -104,7 +104,7 @@ func GetRemoteAttestations(client api.Client, params FetchRemoteAttestationsPara if params.Repo != "" { bundles, err := client.GetByRepoAndDigest(params.Repo, params.Digest, params.Limit) if err != nil { - return nil, fmt.Errorf("failed to fetch attestation bundles from %s: %w", params.Repo, err) + return nil, fmt.Errorf("failed to fetch attestations from %s: %w", params.Repo, err) } return bundles, nil