reduce test duplication
Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
parent
d1c4bf7dd9
commit
e3fbe9008f
1 changed files with 73 additions and 113 deletions
|
|
@ -42,95 +42,82 @@ func NewClientWithMockGHClient(hasNextPage bool) Client {
|
|||
}
|
||||
}
|
||||
|
||||
var testFetchParams = FetchParams{
|
||||
var testFetchParamsWithOwner = FetchParams{
|
||||
Digest: testDigest,
|
||||
Limit: DefaultLimit,
|
||||
Owner: testOwner,
|
||||
PredicateType: "https://slsa.dev/provenance/v1",
|
||||
}
|
||||
var testFetchParamsWithRepo = FetchParams{
|
||||
Digest: testDigest,
|
||||
Limit: DefaultLimit,
|
||||
Repo: testRepo,
|
||||
PredicateType: "https://slsa.dev/provenance/v1",
|
||||
}
|
||||
|
||||
type getByTestCase struct {
|
||||
name string
|
||||
params FetchParams
|
||||
limit int
|
||||
expectedAttestations int
|
||||
expectedError error
|
||||
hasNextPage bool
|
||||
}
|
||||
|
||||
var getByTestCases = []getByTestCase{
|
||||
{
|
||||
name: "get by digest with owner",
|
||||
params: testFetchParamsWithOwner,
|
||||
expectedAttestations: 5,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "get by digest with repo",
|
||||
params: testFetchParamsWithRepo,
|
||||
expectedAttestations: 5,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "get by digest with attestations greater than limit",
|
||||
params: testFetchParamsWithRepo,
|
||||
limit: 3,
|
||||
expectedAttestations: 3,
|
||||
expectedError: nil,
|
||||
},
|
||||
{
|
||||
name: "get by digest with next page",
|
||||
params: testFetchParamsWithRepo,
|
||||
limit: 30,
|
||||
expectedAttestations: 10,
|
||||
expectedError: nil,
|
||||
hasNextPage: true,
|
||||
},
|
||||
{
|
||||
name: "greater than limit with next page",
|
||||
params: testFetchParamsWithRepo,
|
||||
limit: 7,
|
||||
expectedAttestations: 7,
|
||||
expectedError: nil,
|
||||
hasNextPage: true,
|
||||
},
|
||||
}
|
||||
|
||||
func TestGetByDigest(t *testing.T) {
|
||||
c := NewClientWithMockGHClient(false)
|
||||
testFetchParams.Repo = testRepo
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
for _, tc := range getByTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
c := NewClientWithMockGHClient(tc.hasNextPage)
|
||||
|
||||
require.Equal(t, 5, len(attestations))
|
||||
bundle := (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
if tc.limit > 0 {
|
||||
tc.params.Limit = tc.limit
|
||||
}
|
||||
attestations, err := c.GetByDigest(tc.params)
|
||||
require.NoError(t, err)
|
||||
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, 5, len(attestations))
|
||||
bundle = (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
}
|
||||
|
||||
func TestGetByDigestGreaterThanLimit(t *testing.T) {
|
||||
c := NewClientWithMockGHClient(false)
|
||||
|
||||
limit := 3
|
||||
// The method should return five results when the limit is not set
|
||||
testFetchParams.Limit = limit
|
||||
testFetchParams.Repo = testRepo
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, 3, len(attestations))
|
||||
bundle := (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(attestations), limit)
|
||||
bundle = (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
}
|
||||
|
||||
func TestGetByDigestWithNextPage(t *testing.T) {
|
||||
c := NewClientWithMockGHClient(true)
|
||||
testFetchParams.Repo = testRepo
|
||||
testFetchParams.Limit = 30
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(attestations), 10)
|
||||
bundle := (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(attestations), 10)
|
||||
bundle = (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
}
|
||||
|
||||
func TestGetByDigestGreaterThanLimitWithNextPage(t *testing.T) {
|
||||
c := NewClientWithMockGHClient(true)
|
||||
|
||||
limit := 7
|
||||
// The method should return five results when the limit is not set
|
||||
testFetchParams.Limit = limit
|
||||
testFetchParams.Repo = testRepo
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(attestations), limit)
|
||||
bundle := (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, len(attestations), limit)
|
||||
bundle = (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
require.Equal(t, tc.expectedAttestations, len(attestations))
|
||||
bundle := (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetByDigest_NoAttestationsFound(t *testing.T) {
|
||||
|
|
@ -147,14 +134,7 @@ func TestGetByDigest_NoAttestationsFound(t *testing.T) {
|
|||
logger: io.NewTestHandler(),
|
||||
}
|
||||
|
||||
testFetchParams.Repo = testRepo
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
require.Error(t, err)
|
||||
require.IsType(t, ErrNoAttestationsFound, err)
|
||||
require.Nil(t, attestations)
|
||||
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
attestations, err := c.GetByDigest(testFetchParamsWithRepo)
|
||||
require.Error(t, err)
|
||||
require.IsType(t, ErrNoAttestationsFound, err)
|
||||
require.Nil(t, attestations)
|
||||
|
|
@ -172,13 +152,7 @@ func TestGetByDigest_Error(t *testing.T) {
|
|||
logger: io.NewTestHandler(),
|
||||
}
|
||||
|
||||
testFetchParams.Repo = testRepo
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, attestations)
|
||||
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
attestations, err := c.GetByDigest(testFetchParamsWithRepo)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, attestations)
|
||||
}
|
||||
|
|
@ -383,9 +357,8 @@ func TestGetAttestationsRetries(t *testing.T) {
|
|||
logger: io.NewTestHandler(),
|
||||
}
|
||||
|
||||
testFetchParams.Repo = testRepo
|
||||
testFetchParams.Limit = 30
|
||||
attestations, err := c.GetByDigest(testFetchParams)
|
||||
testFetchParamsWithRepo.Limit = 30
|
||||
attestations, err := c.GetByDigest(testFetchParamsWithRepo)
|
||||
require.NoError(t, err)
|
||||
|
||||
// assert the error path was executed; because this is a paged
|
||||
|
|
@ -396,18 +369,6 @@ func TestGetAttestationsRetries(t *testing.T) {
|
|||
require.Equal(t, len(attestations), 10)
|
||||
bundle := (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
|
||||
// same test as above, but for GetByDigest:
|
||||
testFetchParams.Owner = testOwner
|
||||
attestations, err = c.GetByDigest(testFetchParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
// because we haven't reset the mock, we have added 2 more failed requests
|
||||
fetcher.AssertNumberOfCalls(t, "FlakyOnRESTSuccessWithNextPage:error", 4)
|
||||
|
||||
require.Equal(t, len(attestations), 10)
|
||||
bundle = (attestations)[0].Bundle
|
||||
require.Equal(t, bundle.GetMediaType(), "application/vnd.dev.sigstore.bundle.v0.3+json")
|
||||
}
|
||||
|
||||
// test total retries
|
||||
|
|
@ -425,8 +386,7 @@ func TestGetAttestationsMaxRetries(t *testing.T) {
|
|||
logger: io.NewTestHandler(),
|
||||
}
|
||||
|
||||
testFetchParams.Repo = testRepo
|
||||
_, err := c.GetByDigest(testFetchParams)
|
||||
_, err := c.GetByDigest(testFetchParamsWithRepo)
|
||||
require.Error(t, err)
|
||||
|
||||
fetcher.AssertNumberOfCalls(t, "OnREST500Error", 4)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue