Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Jul 4, 2024
1 parent bc33eba commit 1f9ef7b
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions attest/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (

gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-containerregistry/pkg/name"

. "github.com/onsi/gomega"

. "github.com/errordeveloper/tape/attest"
"github.com/errordeveloper/tape/manifest/imagescanner"
"github.com/errordeveloper/tape/manifest/loader"
"github.com/errordeveloper/tape/oci"
)

type vcsTestCase struct {
Expand All @@ -24,6 +26,7 @@ type vcsTestCase struct {
CheckoutHash string
Branch string
LoadPath string
ExpectTags []string
}

func (tc vcsTestCase) Name() string {
Expand All @@ -40,12 +43,21 @@ func TestVCS(t *testing.T) {
URL: "https://github.com/stefanprodan/podinfo",
CheckoutTag: "6.7.0", // => 0b1481aa8ed0a6c34af84f779824a74200d5c1d6
LoadPath: "kustomize",
ExpectTags: []string{"6.7.0"},
},
{
URL: "https://github.com/stefanprodan/podinfo",
CheckoutHash: "0b1481aa8ed0a6c34af84f779824a74200d5c1d6", // => 6.7.0
Branch: "master",
LoadPath: "kustomize",
ExpectTags: []string{"6.7.0"},
},
{
URL: "https://github.com/stefanprodan/podinfo",
CheckoutHash: "ba6f4ffd7cba98ea2ef52bee7c1bead8e1d6a7d3", // pre-6.7.0
Branch: "master",
LoadPath: "kustomize",
ExpectTags: nil,
},
}

Expand All @@ -62,7 +74,8 @@ func makeVCSTest(repos *repos, tc vcsTestCase) func(t *testing.T) {
return func(t *testing.T) {
g := NewWithT(t)

checkoutPath, err := repos.clone(context.Background(), tc)
ctx := context.Background()
checkoutPath, err := repos.clone(ctx, tc)
g.Expect(err).NotTo(HaveOccurred())

loadPath := filepath.Join(checkoutPath, tc.LoadPath)
Expand All @@ -77,14 +90,37 @@ func makeVCSTest(repos *repos, tc vcsTestCase) func(t *testing.T) {
scanner := imagescanner.NewDefaultImageScanner()
scanner.WithProvinanceAttestor(attreg)

g.Expect(loader.Paths()).To(HaveLen(4))

g.Expect(scanner.Scan(loader.RelPaths())).To(Succeed())

collection, err := attreg.MakePathCheckSummarySummaryCollection()
g.Expect(err).NotTo(HaveOccurred())
g.Expect(collection).ToNot(BeNil())
g.Expect(collection.Providers).To(ConsistOf("git"))
g.Expect(collection.EntryGroups).To(HaveLen(1))
g.Expect(collection.EntryGroups[0]).To(HaveLen(5))

vcsSummary := attreg.BaseDirSummary()
g.Expect(vcsSummary).ToNot(BeNil())
summaryJSON, err := json.Marshal(vcsSummary.Full())
g.Expect(err).NotTo(HaveOccurred())
t.Logf("VCS info for %q: %s", tc.LoadPath, summaryJSON)

g.Expect(attreg.AssociateCoreStatements()).To(Succeed())

statements := attreg.GetStatements()
g.Expect(statements).To(HaveLen(99))
g.Expect(statements).To(HaveLen(1))
g.Expect(statements[0].GetSubject()).To(HaveLen(4))

// TODO: validate schema

image, err := name.NewRepository("podinfo")
g.Expect(err).NotTo(HaveOccurred())

semVerTags := oci.SemVerTagsFromAttestations(ctx, image.Tag("test.123456"), statements...)

g.Expect(semVerTags).To(HaveLen(len(tc.ExpectTags)))
}
}

Expand All @@ -100,7 +136,7 @@ func (r *repos) init() error {
return err
}
r.workDir = workDir
tempDir, err := os.MkdirTemp(workDir, ".vcs-test-*")
tempDir, err := os.MkdirTemp("", ".vcs-test-*")
if err != nil {
return err
}
Expand Down Expand Up @@ -164,7 +200,6 @@ func (r *repos) clone(ctx context.Context, tc vcsTestCase) (string, error) {
}
opts := &gogit.CheckoutOptions{
Hash: plumbing.NewHash(tc.CheckoutHash),
//Branch: plumbing.ReferenceName(tc.Branch),
}

if err := workTree.Checkout(opts); err != nil {
Expand Down

0 comments on commit 1f9ef7b

Please sign in to comment.