From 5d096e226f30fb6712d9172e8c735b4bf767e2fe Mon Sep 17 00:00:00 2001 From: mrjoelkamp Date: Mon, 5 Aug 2024 10:19:50 -0500 Subject: [PATCH] refactor: fix import cycle for mock resolver --- internal/test/test.go | 9 +++++++++ pkg/attest/verify_test.go | 2 +- pkg/mirror/mirror_test.go | 4 ++-- internal/test/mocks.go => pkg/oci/mock.go | 17 ++--------------- pkg/policy/policy_test.go | 2 +- 5 files changed, 15 insertions(+), 19 deletions(-) rename internal/test/mocks.go => pkg/oci/mock.go (71%) diff --git a/internal/test/test.go b/internal/test/test.go index 4f63668..f769476 100644 --- a/internal/test/test.go +++ b/internal/test/test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "os" + "path/filepath" "strings" "testing" @@ -45,6 +46,14 @@ func CreateTempDir(t *testing.T, dir, pattern string) string { return tempDir } +func GetMockSigner(_ context.Context) (dsse.SignerVerifier, error) { + priv, err := os.ReadFile(filepath.Join("..", "..", "test", "testdata", "test-signing-key.pem")) + if err != nil { + return nil, err + } + return signerverifier.LoadKeyPair(priv) +} + func Setup(t *testing.T) (context.Context, dsse.SignerVerifier) { var tl tlog.TL if UseMockTL { diff --git a/pkg/attest/verify_test.go b/pkg/attest/verify_test.go index 04c4805..dd2c33e 100644 --- a/pkg/attest/verify_test.go +++ b/pkg/attest/verify_test.go @@ -34,7 +34,7 @@ func TestVerifyAttestations(t *testing.T) { env := new(attestation.Envelope) err = json.Unmarshal(ex, env) assert.NoError(t, err) - resolver := &test.MockResolver{ + resolver := &oci.MockResolver{ Envs: []*attestation.Envelope{env}, } diff --git a/pkg/mirror/mirror_test.go b/pkg/mirror/mirror_test.go index 75ca87d..d1ad092 100644 --- a/pkg/mirror/mirror_test.go +++ b/pkg/mirror/mirror_test.go @@ -96,9 +96,9 @@ func TestSavingReferrers(t *testing.T) { err = SaveReferrers(manifest, output) require.NoError(t, err) - reg := &test.MockRegistryResolver{ + reg := &oci.MockRegistryResolver{ Subject: subject, - MockResolver: &test.MockResolver{}, + MockResolver: &oci.MockResolver{}, ImageNameStr: indexName, } require.NoError(t, err) diff --git a/internal/test/mocks.go b/pkg/oci/mock.go similarity index 71% rename from internal/test/mocks.go rename to pkg/oci/mock.go index 096ddf2..537d9ae 100644 --- a/internal/test/mocks.go +++ b/pkg/oci/mock.go @@ -1,15 +1,10 @@ -package test +package oci import ( "context" - "os" - "path/filepath" "github.com/docker/attest/pkg/attestation" - "github.com/docker/attest/pkg/oci" - "github.com/docker/attest/pkg/signerverifier" v1 "github.com/google/go-containerregistry/pkg/v1" - "github.com/secure-systems-lab/go-securesystemslib/dsse" ) type MockResolver struct { @@ -37,7 +32,7 @@ func (r MockResolver) ImageDescriptor(_ context.Context) (*v1.Descriptor, error) } func (r MockResolver) ImagePlatform(_ context.Context) (*v1.Platform, error) { - return oci.ParsePlatform("linux/amd64") + return ParsePlatform("linux/amd64") } type MockRegistryResolver struct { @@ -53,11 +48,3 @@ func (r *MockRegistryResolver) ImageDescriptor(_ context.Context) (*v1.Descripto func (r *MockRegistryResolver) ImageName(_ context.Context) (string, error) { return r.ImageNameStr, nil } - -func GetMockSigner(_ context.Context) (dsse.SignerVerifier, error) { - priv, err := os.ReadFile(filepath.Join("..", "..", "test", "testdata", "test-signing-key.pem")) - if err != nil { - return nil, err - } - return signerverifier.LoadKeyPair(priv) -} diff --git a/pkg/policy/policy_test.go b/pkg/policy/policy_test.go index c9917c1..a3d155f 100644 --- a/pkg/policy/policy_test.go +++ b/pkg/policy/policy_test.go @@ -38,7 +38,7 @@ func TestRegoEvaluator_Evaluate(t *testing.T) { re := policy.NewRegoEvaluator(true) - defaultResolver := test.MockResolver{ + defaultResolver := oci.MockResolver{ Envs: []*attestation.Envelope{loadAttestation(t, ExampleAttestation)}, }