Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Jul 3, 2024
1 parent eedc5cf commit 8952bdf
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions attest/vcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package attest_test

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"

. "github.com/onsi/gomega"

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

type vcsTestCase struct {
URL string
CheckoutTag string
CheckoutHash string
Branch string
LoadPath string
}

func (tc vcsTestCase) Name() string {
rev := tc.CheckoutTag
if rev == "" {
rev = tc.CheckoutHash
}
return fmt.Sprintf("%s@%s", tc.URL, rev)
}

func TestVCS(t *testing.T) {
testCases := []vcsTestCase{
{
URL: "https://github.com/stefanprodan/podinfo",
CheckoutTag: "6.7.0", // => 0b1481aa8ed0a6c34af84f779824a74200d5c1d6
LoadPath: "kustomize",
},
{
URL: "https://github.com/stefanprodan/podinfo",
CheckoutHash: "0b1481aa8ed0a6c34af84f779824a74200d5c1d6", // => 6.7.0
Branch: "master",
LoadPath: "kustomize",
},
}

repoCache := repoCache{}

tempDir := t.TempDir()

for _, tc := range testCases {
t.Run(tc.Name(), makeVCSTest(repoCache, tc, tempDir))
}
}

func makeVCSTest(repoCache repoCache, tc vcsTestCase, tempDir string) func(t *testing.T) {
return func(t *testing.T) {
g := NewWithT(t)

tempDir := func() string {
tempDir, err := os.MkdirTemp(tempDir, "vcs-test-*")
g.Expect(err).NotTo(HaveOccurred())
return tempDir
}

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

loadPath := filepath.Join(checkoutPath, tc.LoadPath)
loader := loader.NewRecursiveManifestDirectoryLoader(loadPath)
g.Expect(loader.Load()).To(Succeed())

repoDetected, attreg, err := DetectVCS(loadPath)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(repoDetected).To(BeTrue())
g.Expect(attreg).ToNot(BeNil())

scanner := imagescanner.NewDefaultImageScanner()
scanner.WithProvinanceAttestor(attreg)

statements := attreg.GetStatements()
g.Expect(statements).To(HaveLen(99))
}
}

type repoCache map[string]string

func (r repoCache) mirror(ctx context.Context, tc vcsTestCase, mktemp func() string) (string, error) {
if _, ok := r[tc.URL]; !ok {
mirrorDir := mktemp()
_, err := gogit.PlainCloneContext(ctx, mirrorDir, true, &gogit.CloneOptions{Mirror: true, URL: tc.URL})
if err != nil {
return "", err
}
r[tc.URL] = mirrorDir
}
return r[tc.URL], nil
}

func (r repoCache) clone(ctx context.Context, tc vcsTestCase, mktemp func() string) (string, error) {
mirrorDir, err := r.mirror(ctx, tc, mktemp)
if err != nil {
return "", err
}
checkoutDir := mktemp()

opts := &gogit.CloneOptions{URL: mirrorDir}
if tc.CheckoutTag != "" {
opts.ReferenceName = plumbing.NewTagReferenceName(tc.CheckoutTag)
} else if tc.Branch != "" {
opts.ReferenceName = plumbing.NewBranchReferenceName(tc.Branch)
}

_, err = gogit.PlainCloneContext(ctx, checkoutDir, false, opts)
if err != nil {
return "", fmt.Errorf("failed to clone: %w", err)
}

// if tc.CheckoutHash != "" {
// workTree, err := repo.Worktree()
// if err != nil {
// return "", err
// }
// opts := &gogit.CheckoutOptions{
// Hash: plumbing.NewHash(tc.CheckoutHash),
// Branch: plumbing.ReferenceName(tc.Branch),
// }

// if err := workTree.Checkout(opts); err != nil {
// return "", err
// }
// }
workDir, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Rel(workDir, checkoutDir)
}

0 comments on commit 8952bdf

Please sign in to comment.