fix failing tests

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-12-16 12:40:13 -07:00
parent e4431a3f55
commit ab4912ff48
4 changed files with 16 additions and 5 deletions

View file

@ -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)

View file

@ -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(),
}

View file

@ -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
}

View file

@ -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