From b7b8cdc9e96abec35757295582f5deaa43bd3181 Mon Sep 17 00:00:00 2001 From: Teppei Fukuda Date: Mon, 3 Jun 2024 11:34:28 +0400 Subject: [PATCH] test: replace embedded Git repository with dynamically created repository (#6824) Signed-off-by: knqyf263 --- internal/gittest/server.go | 132 ++++++++++++++++++ internal/testutil/fs.go | 36 +++++ pkg/fanal/artifact/repo/git_test.go | 57 ++++---- .../repo/testdata/test-repo/anothertest.txt | 1 + .../artifact/repo/testdata/test-repo/test.txt | 1 + .../artifact/repo/testdata/test.git/HEAD | 1 - .../artifact/repo/testdata/test.git/config | 6 - .../0d/8bf3f07c3970b3e38c2cfb1c619cb86fae76d2 | 2 - .../1c/1d7deed649fbecd66fab423ccd9d001bf9ff91 | Bin 59 -> 0 bytes .../27/aaec53f92314d9438a53c703f169d2cbf5001a | Bin 86 -> 0 bytes .../5e/fb9bc29c482e023e40e0a2b3b7e49cec842034 | Bin 53 -> 0 bytes .../6a/c152fe2b87cb5e243414df71790a32912e778d | 3 - .../c0/42cd14d2b999cade090785af47e9f8b8e342ff | Bin 34 -> 0 bytes .../c9/06fc4a94762f8a2c77c718947143d16e4e9ec7 | Bin 122 -> 0 bytes .../d7/937c5f0ce7f2054e4e3be65ab3cd0f9462dc1b | Bin 165 -> 0 bytes .../e2/4866d1d31ddffdb27fbcf583d5deb4386d5145 | Bin 53 -> 0 bytes .../e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 | Bin 15 -> 0 bytes .../f4/836be6497e83e13dc0cfbce7e6b973b1ea511d | Bin 40 -> 0 bytes .../repo/testdata/test.git/refs/heads/master | 1 - .../testdata/test.git/refs/heads/valid-branch | 1 - .../repo/testdata/test.git/refs/tags/v1.0.0 | 1 - pkg/plugin/manager_unix_test.go | 60 +++++--- pkg/plugin/testdata/test_plugin.git/HEAD | 1 - pkg/plugin/testdata/test_plugin.git/config | 8 -- .../testdata/test_plugin.git/description | 1 - .../testdata/test_plugin.git/info/exclude | 6 - .../08/6aefb548a1150b765d1e163a5e542fc80bd660 | Bin 147 -> 0 bytes .../0a/e1413e3807e024dbc7de4129d12bdcae7dea61 | 3 - .../49/0535e047e795a5c95306ce281c9f08cdb35b7c | Bin 37 -> 0 bytes .../92/9b4718db99b64a38b4e8c3ec8e673976821c08 | Bin 344 -> 0 bytes .../a0/82cf7b16998b8f048e7d2bf8207d9525688a9f | Bin 90 -> 0 bytes .../d7/8abde66b1d35bdac65402f0e2cddf3a96cd377 | Bin 380 -> 0 bytes .../dc/135ebfc7f680300c981029184a492bbdfa6db3 | Bin 91 -> 0 bytes .../testdata/test_plugin.git/packed-refs | 4 - .../test_plugin.git/refs/heads/.gitkeep | 0 .../test_plugin.git/refs/tags/.gitkeep | 0 36 files changed, 240 insertions(+), 85 deletions(-) create mode 100644 internal/gittest/server.go create mode 100644 internal/testutil/fs.go create mode 100644 pkg/fanal/artifact/repo/testdata/test-repo/anothertest.txt create mode 100644 pkg/fanal/artifact/repo/testdata/test-repo/test.txt delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/HEAD delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/config delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/0d/8bf3f07c3970b3e38c2cfb1c619cb86fae76d2 delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/1c/1d7deed649fbecd66fab423ccd9d001bf9ff91 delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/27/aaec53f92314d9438a53c703f169d2cbf5001a delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/5e/fb9bc29c482e023e40e0a2b3b7e49cec842034 delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/6a/c152fe2b87cb5e243414df71790a32912e778d delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/c0/42cd14d2b999cade090785af47e9f8b8e342ff delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/c9/06fc4a94762f8a2c77c718947143d16e4e9ec7 delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/d7/937c5f0ce7f2054e4e3be65ab3cd0f9462dc1b delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/e2/4866d1d31ddffdb27fbcf583d5deb4386d5145 delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/objects/f4/836be6497e83e13dc0cfbce7e6b973b1ea511d delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/refs/heads/master delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/refs/heads/valid-branch delete mode 100644 pkg/fanal/artifact/repo/testdata/test.git/refs/tags/v1.0.0 delete mode 100644 pkg/plugin/testdata/test_plugin.git/HEAD delete mode 100644 pkg/plugin/testdata/test_plugin.git/config delete mode 100644 pkg/plugin/testdata/test_plugin.git/description delete mode 100644 pkg/plugin/testdata/test_plugin.git/info/exclude delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/08/6aefb548a1150b765d1e163a5e542fc80bd660 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/0a/e1413e3807e024dbc7de4129d12bdcae7dea61 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/49/0535e047e795a5c95306ce281c9f08cdb35b7c delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/92/9b4718db99b64a38b4e8c3ec8e673976821c08 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/a0/82cf7b16998b8f048e7d2bf8207d9525688a9f delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/d7/8abde66b1d35bdac65402f0e2cddf3a96cd377 delete mode 100644 pkg/plugin/testdata/test_plugin.git/objects/dc/135ebfc7f680300c981029184a492bbdfa6db3 delete mode 100644 pkg/plugin/testdata/test_plugin.git/packed-refs delete mode 100644 pkg/plugin/testdata/test_plugin.git/refs/heads/.gitkeep delete mode 100644 pkg/plugin/testdata/test_plugin.git/refs/tags/.gitkeep diff --git a/internal/gittest/server.go b/internal/gittest/server.go new file mode 100644 index 000000000000..277d645de241 --- /dev/null +++ b/internal/gittest/server.go @@ -0,0 +1,132 @@ +//go:build unix + +package gittest + +import ( + "errors" + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/sosedoff/gitkit" + "github.com/stretchr/testify/require" + + "github.com/aquasecurity/trivy/internal/testutil" +) + +var signature = &object.Signature{ + Name: "Test", + Email: "test@example.com", + When: time.Now(), +} + +func NewServer(t *testing.T, repo, dir string) *httptest.Server { + wtDir := t.TempDir() + + // git init + r, err := git.PlainInit(wtDir, false) + require.NoError(t, err) + + wt, err := r.Worktree() + require.NoError(t, err) + + testutil.CopyDir(t, dir, wtDir) + + _, err = wt.Add(".") + require.NoError(t, err) + + _, err = wt.Commit("initial commit", &git.CommitOptions{ + Author: signature, + }) + require.NoError(t, err) + + // Create a bare repository + bareDir := t.TempDir() + gitDir := filepath.Join(bareDir, repo+".git") + _, err = git.PlainClone(gitDir, true, &git.CloneOptions{URL: wtDir}) + require.NoError(t, err) + + // Set up a git server + service := gitkit.New(gitkit.Config{Dir: bareDir}) + err = service.Setup() + require.NoError(t, err) + + return httptest.NewServer(service) +} + +func Clone(t *testing.T, ts *httptest.Server, repo, worktree string) *git.Repository { + cloneOptions := git.CloneOptions{ + URL: ts.URL + "/" + repo + ".git", + } + + r, err := git.PlainClone(worktree, false, &cloneOptions) + require.NoError(t, err) + + return r +} + +func CommitAll(t *testing.T, r *git.Repository, msg string) { + w, err := r.Worktree() + require.NoError(t, err) + + _, err = w.Add(".") + require.NoError(t, err) + + _, err = w.Commit(msg, &git.CommitOptions{ + Author: signature, + }) + require.NoError(t, err) +} + +func SetTag(t *testing.T, r *git.Repository, tag string) { + h, err := r.Head() + require.NoError(t, err) + + t.Logf("git tag -a %s %s -m \"%s\"", tag, h.Hash(), tag) + _, err = r.CreateTag(tag, h.Hash(), &git.CreateTagOptions{ + Tagger: signature, + Message: tag, + }) + require.NoError(t, err) +} + +func PushTags(t *testing.T, r *git.Repository) { + t.Log("git push --tags") + err := r.Push(&git.PushOptions{ + RemoteName: "origin", + RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"}, + }) + + if err != nil { + if errors.Is(err, git.NoErrAlreadyUpToDate) { + return + } + require.NoError(t, err) + } +} + +func CreateRemoteBranch(t *testing.T, r *git.Repository, branchName string) { + wt, err := r.Worktree() + require.NoError(t, err) + + ref := plumbing.NewBranchReferenceName(branchName) + err = wt.Checkout(&git.CheckoutOptions{ + Branch: ref, + Create: true, + }) + require.NoError(t, err) + defer func() { + require.NoError(t, wt.Checkout(&git.CheckoutOptions{})) + }() + + err = r.Push(&git.PushOptions{ + RemoteName: "origin", + RefSpecs: []config.RefSpec{config.RefSpec(ref + ":" + ref)}, + }) + require.NoError(t, err) +} diff --git a/internal/testutil/fs.go b/internal/testutil/fs.go new file mode 100644 index 000000000000..4a1162aa1bab --- /dev/null +++ b/internal/testutil/fs.go @@ -0,0 +1,36 @@ +package testutil + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/aquasecurity/trivy/pkg/utils/fsutils" +) + +// CopyDir copies the directory content from src to dst. +// It supports only simple cases for testing. +func CopyDir(t *testing.T, src, dst string) { + srcInfo, err := os.Stat(src) + require.NoError(t, err) + + err = os.MkdirAll(dst, srcInfo.Mode()) + require.NoError(t, err) + + entries, err := os.ReadDir(src) + require.NoError(t, err) + + for _, entry := range entries { + srcPath := filepath.Join(src, entry.Name()) + dstPath := filepath.Join(dst, entry.Name()) + + if entry.IsDir() { + CopyDir(t, srcPath, dstPath) + } else { + _, err = fsutils.CopyFile(srcPath, dstPath) + require.NoError(t, err) + } + } +} diff --git a/pkg/fanal/artifact/repo/git_test.go b/pkg/fanal/artifact/repo/git_test.go index 00ea53ef58d3..0e4c8ee39d4b 100644 --- a/pkg/fanal/artifact/repo/git_test.go +++ b/pkg/fanal/artifact/repo/git_test.go @@ -7,10 +7,11 @@ import ( "net/http/httptest" "testing" - "github.com/sosedoff/gitkit" + "github.com/go-git/go-git/v5" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/aquasecurity/trivy/internal/gittest" "github.com/aquasecurity/trivy/pkg/fanal/artifact" "github.com/aquasecurity/trivy/pkg/fanal/cache" "github.com/aquasecurity/trivy/pkg/fanal/walker" @@ -19,26 +20,29 @@ import ( _ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/secret" ) -func setupGitServer() (*httptest.Server, error) { - service := gitkit.New(gitkit.Config{ - Dir: "./testdata", - AutoCreate: false, - }) +func setupGitRepository(t *testing.T, repo, dir string) (*httptest.Server, *git.Repository) { + gs := gittest.NewServer(t, repo, dir) - if err := service.Setup(); err != nil { - return nil, err - } + worktree := t.TempDir() + r := gittest.Clone(t, gs, repo, worktree) + + // Branch + gittest.CreateRemoteBranch(t, r, "valid-branch") - ts := httptest.NewServer(service) + // Tag + gittest.SetTag(t, r, "v1.0.0") + gittest.PushTags(t, r) - return ts, nil + return gs, r } func TestNewArtifact(t *testing.T) { - ts, err := setupGitServer() - require.NoError(t, err) + ts, repo := setupGitRepository(t, "test-repo", "testdata/test-repo") defer ts.Close() + head, err := repo.Head() + require.NoError(t, err) + type args struct { target string c cache.ArtifactCache @@ -55,7 +59,7 @@ func TestNewArtifact(t *testing.T) { { name: "remote repo", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, noProgress: false, }, @@ -71,9 +75,9 @@ func TestNewArtifact(t *testing.T) { assertion: assert.NoError, }, { - name: "happy noProgress", + name: "no progress", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, noProgress: true, }, @@ -82,7 +86,7 @@ func TestNewArtifact(t *testing.T) { { name: "branch", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, repoBranch: "valid-branch", }, @@ -91,7 +95,7 @@ func TestNewArtifact(t *testing.T) { { name: "tag", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, repoTag: "v1.0.0", }, @@ -100,9 +104,9 @@ func TestNewArtifact(t *testing.T) { { name: "commit", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, - repoCommit: "6ac152fe2b87cb5e243414df71790a32912e778d", + repoCommit: head.String(), }, assertion: assert.NoError, }, @@ -131,7 +135,7 @@ func TestNewArtifact(t *testing.T) { { name: "invalid branch", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, repoBranch: "invalid-branch", }, @@ -142,7 +146,7 @@ func TestNewArtifact(t *testing.T) { { name: "invalid tag", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, repoTag: "v1.0.9", }, @@ -153,7 +157,7 @@ func TestNewArtifact(t *testing.T) { { name: "invalid commit", args: args{ - target: ts.URL + "/test.git", + target: ts.URL + "/test-repo.git", c: nil, repoCommit: "6ac152fe2b87cb5e243414df71790a32912e778e", }, @@ -178,8 +182,7 @@ func TestNewArtifact(t *testing.T) { } func TestArtifact_Inspect(t *testing.T) { - ts, err := setupGitServer() - require.NoError(t, err) + ts, _ := setupGitRepository(t, "test-repo", "testdata/test-repo") defer ts.Close() tests := []struct { @@ -190,9 +193,9 @@ func TestArtifact_Inspect(t *testing.T) { }{ { name: "happy path", - rawurl: ts.URL + "/test.git", + rawurl: ts.URL + "/test-repo.git", want: artifact.Reference{ - Name: ts.URL + "/test.git", + Name: ts.URL + "/test-repo.git", Type: artifact.TypeRepository, ID: "sha256:6a89d4fcd50f840a79da64523c255da80171acd3d286df2acc60056c778d9304", BlobIDs: []string{ diff --git a/pkg/fanal/artifact/repo/testdata/test-repo/anothertest.txt b/pkg/fanal/artifact/repo/testdata/test-repo/anothertest.txt new file mode 100644 index 000000000000..f4836be6497e --- /dev/null +++ b/pkg/fanal/artifact/repo/testdata/test-repo/anothertest.txt @@ -0,0 +1 @@ +this is another text file. \ No newline at end of file diff --git a/pkg/fanal/artifact/repo/testdata/test-repo/test.txt b/pkg/fanal/artifact/repo/testdata/test-repo/test.txt new file mode 100644 index 000000000000..c042cd14d2b9 --- /dev/null +++ b/pkg/fanal/artifact/repo/testdata/test-repo/test.txt @@ -0,0 +1 @@ +this is a text file. \ No newline at end of file diff --git a/pkg/fanal/artifact/repo/testdata/test.git/HEAD b/pkg/fanal/artifact/repo/testdata/test.git/HEAD deleted file mode 100644 index cb089cd89a7d..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/HEAD +++ /dev/null @@ -1 +0,0 @@ -ref: refs/heads/master diff --git a/pkg/fanal/artifact/repo/testdata/test.git/config b/pkg/fanal/artifact/repo/testdata/test.git/config deleted file mode 100644 index e6da231579bc..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/config +++ /dev/null @@ -1,6 +0,0 @@ -[core] - repositoryformatversion = 0 - filemode = true - bare = true - ignorecase = true - precomposeunicode = true diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/0d/8bf3f07c3970b3e38c2cfb1c619cb86fae76d2 b/pkg/fanal/artifact/repo/testdata/test.git/objects/0d/8bf3f07c3970b3e38c2cfb1c619cb86fae76d2 deleted file mode 100644 index 9baa2c164c9e..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/objects/0d/8bf3f07c3970b3e38c2cfb1c619cb86fae76d2 +++ /dev/null @@ -1,2 +0,0 @@ -xAj0s+X˲ `,"^cG Ї{MGxb$nZӄ3dVul|]!rvLy 'WQ>9/DjԺYϥrh߼1\~un[ \ 4[f'}:*? -mb~T \ No newline at end of file diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/1c/1d7deed649fbecd66fab423ccd9d001bf9ff91 b/pkg/fanal/artifact/repo/testdata/test.git/objects/1c/1d7deed649fbecd66fab423ccd9d001bf9ff91 deleted file mode 100644 index 1c1c8f2a4f50c6b83a6645b026b6f2c722d2e748..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 59 zcmb8#W%j<>n*Fxv)5_&?<#!Z>Hon+ P#njD0l#xMWkMec^NU0R} diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/27/aaec53f92314d9438a53c703f169d2cbf5001a b/pkg/fanal/artifact/repo/testdata/test.git/objects/27/aaec53f92314d9438a53c703f169d2cbf5001a deleted file mode 100644 index 1e1970b9b3e25d56678d5ce984f5ed0d98cac0f3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 86 zcmV-c0IC0Y0V^p=O;s?rWH2-^Ff%bxNX*MG$w)0KNi8nXE2$`9_|lyH%(Jffq3wb5 sd!9erS-kO8pe$4=T+IQevm%#v&OCLGlf8Al`^z6Y9y|R90QS@-;8*}CxBvhE diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/5e/fb9bc29c482e023e40e0a2b3b7e49cec842034 b/pkg/fanal/artifact/repo/testdata/test.git/objects/5e/fb9bc29c482e023e40e0a2b3b7e49cec842034 deleted file mode 100644 index d615c02a6b36669ca11cf0c4d92dde442130dcbc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 53 zcmb8#W%j<>n*Fxv)5_&?<#!Z>Hoon JAu&woJ^;~d6Yu~4 diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/6a/c152fe2b87cb5e243414df71790a32912e778d b/pkg/fanal/artifact/repo/testdata/test.git/objects/6a/c152fe2b87cb5e243414df71790a32912e778d deleted file mode 100644 index f40c82e685b2..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/objects/6a/c152fe2b87cb5e243414df71790a32912e778d +++ /dev/null @@ -1,3 +0,0 @@ -xK0DgS>Br5Xqp$mr@-Qym -~dc6.@&*&˔OFD6}qsz6%@;FGVXRs3o cW(<~Kj}FmncH0g8J2biU_keG2K7S-O2)8#l4FCWD diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/d7/937c5f0ce7f2054e4e3be65ab3cd0f9462dc1b b/pkg/fanal/artifact/repo/testdata/test.git/objects/d7/937c5f0ce7f2054e4e3be65ab3cd0f9462dc1b deleted file mode 100644 index c948dc6ea8d0cd5ae18cfce1442ceac08a49b581..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 165 zcmV;W09yZe0i}*j3c@fDMqTF=*$a|CGmVIN1i=$bXC}7L*pjAreY^1l?!NaHkB{b3 z#-^Q5hgLz5XYFY56%;DVqHdGKZmU!usm?8;|C|y*LC^r?fxn_PA8hnP1|^e&VYlUkLK)++U@?d T$v+K+%op8cZBBgweIiasKjBa- diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/e2/4866d1d31ddffdb27fbcf583d5deb4386d5145 b/pkg/fanal/artifact/repo/testdata/test.git/objects/e2/4866d1d31ddffdb27fbcf583d5deb4386d5145 deleted file mode 100644 index 5f1213f2ea4c4104e96cda98384a0f3eb001a3c7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 53 zcmV-50LuS(0V^p=O;s>9V=y!@Ff%bxC`m0Y(JQGaVL0G)R^-yonWye?vbU~xfB9p_ LW2gTBQz8=1`p*`L diff --git a/pkg/fanal/artifact/repo/testdata/test.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 b/pkg/fanal/artifact/repo/testdata/test.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 deleted file mode 100644 index 711223894375fe1186ac5bfffdc48fb1fa1e65cc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15 Wcmb4V<^eUELH%b#Jv2HjMO59lGKV4g|y6^R6PL4{0qQ%+7ogB diff --git a/pkg/fanal/artifact/repo/testdata/test.git/refs/heads/master b/pkg/fanal/artifact/repo/testdata/test.git/refs/heads/master deleted file mode 100644 index 21969947cb30..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/refs/heads/master +++ /dev/null @@ -1 +0,0 @@ -0d8bf3f07c3970b3e38c2cfb1c619cb86fae76d2 diff --git a/pkg/fanal/artifact/repo/testdata/test.git/refs/heads/valid-branch b/pkg/fanal/artifact/repo/testdata/test.git/refs/heads/valid-branch deleted file mode 100644 index 22301d650ca1..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/refs/heads/valid-branch +++ /dev/null @@ -1 +0,0 @@ -d7937c5f0ce7f2054e4e3be65ab3cd0f9462dc1b diff --git a/pkg/fanal/artifact/repo/testdata/test.git/refs/tags/v1.0.0 b/pkg/fanal/artifact/repo/testdata/test.git/refs/tags/v1.0.0 deleted file mode 100644 index bff68b69688e..000000000000 --- a/pkg/fanal/artifact/repo/testdata/test.git/refs/tags/v1.0.0 +++ /dev/null @@ -1 +0,0 @@ -c906fc4a94762f8a2c77c718947143d16e4e9ec7 diff --git a/pkg/plugin/manager_unix_test.go b/pkg/plugin/manager_unix_test.go index 0fd024ff3246..fca1c8fac2ad 100644 --- a/pkg/plugin/manager_unix_test.go +++ b/pkg/plugin/manager_unix_test.go @@ -14,11 +14,12 @@ import ( "testing" "time" + "github.com/go-git/go-git/v5" "github.com/google/go-containerregistry/pkg/v1" - "github.com/sosedoff/gitkit" // Not work on Windows "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/aquasecurity/trivy/internal/gittest" "github.com/aquasecurity/trivy/pkg/clock" ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" "github.com/aquasecurity/trivy/pkg/log" @@ -26,24 +27,43 @@ import ( "github.com/aquasecurity/trivy/pkg/utils/fsutils" ) -func setupGitServer() (*httptest.Server, error) { - service := gitkit.New(gitkit.Config{ - Dir: "./testdata", - AutoCreate: false, - }) +func setupGitRepository(t *testing.T, repo, dir string) *httptest.Server { + gs := gittest.NewServer(t, repo, dir) - if err := service.Setup(); err != nil { - return nil, err - } + worktree := t.TempDir() + r := gittest.Clone(t, gs, repo, worktree) + + // git tag + gittest.SetTag(t, r, "v0.2.0") + + // git commit + modifyManifest(t, worktree, "0.3.0") + gittest.CommitAll(t, r, "bump up to 0.3.0") + + err := r.Push(&git.PushOptions{}) + require.NoError(t, err) + + // git tag + gittest.SetTag(t, r, "v0.3.0") - ts := httptest.NewServer(service) + // git push --tags + gittest.PushTags(t, r) - return ts, nil + return gs } -func TestManager_Install(t *testing.T) { - gs, err := setupGitServer() +func modifyManifest(t *testing.T, worktree, version string) { + manifestPath := filepath.Join(worktree, "plugin.yaml") + b, err := os.ReadFile(manifestPath) + require.NoError(t, err) + + b = bytes.ReplaceAll(b, []byte("0.2.0"), []byte(version)) + err = os.WriteFile(manifestPath, b, 0644) require.NoError(t, err) +} + +func TestManager_Install(t *testing.T) { + gs := setupGitRepository(t, "test_plugin", "testdata/test_plugin") t.Cleanup(gs.Close) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -76,8 +96,8 @@ func TestManager_Install(t *testing.T) { }, }, } - wantPluginWithVersion := wantPlugin - wantPluginWithVersion.Version = "0.1.0" + wantPluginWithGit := wantPlugin + wantPluginWithGit.Version = "0.3.0" wantLogs := `2021-08-25T12:20:30Z INFO Installing the plugin... src="%s" 2021-08-25T12:20:30Z INFO Plugin successfully installed name="test_plugin" version="%s" @@ -109,16 +129,16 @@ func TestManager_Install(t *testing.T) { { name: "git", pluginName: "git::" + gs.URL + "/test_plugin.git", - want: wantPlugin, + want: wantPluginWithGit, wantFile: ".trivy/plugins/test_plugin/test.sh", - wantLogs: fmt.Sprintf(wantLogs, "git::"+gs.URL+"/test_plugin.git", "0.2.0"), + wantLogs: fmt.Sprintf(wantLogs, "git::"+gs.URL+"/test_plugin.git", "0.3.0"), }, { name: "with version", - pluginName: "git::" + gs.URL + "/test_plugin.git@v0.1.0", - want: wantPluginWithVersion, + pluginName: "git::" + gs.URL + "/test_plugin.git@v0.2.0", + want: wantPlugin, wantFile: ".trivy/plugins/test_plugin/test.sh", - wantLogs: fmt.Sprintf(wantLogs, "git::"+gs.URL+"/test_plugin.git", "0.1.0"), + wantLogs: fmt.Sprintf(wantLogs, "git::"+gs.URL+"/test_plugin.git", "0.2.0"), }, { name: "via index", diff --git a/pkg/plugin/testdata/test_plugin.git/HEAD b/pkg/plugin/testdata/test_plugin.git/HEAD deleted file mode 100644 index b870d82622c1..000000000000 --- a/pkg/plugin/testdata/test_plugin.git/HEAD +++ /dev/null @@ -1 +0,0 @@ -ref: refs/heads/main diff --git a/pkg/plugin/testdata/test_plugin.git/config b/pkg/plugin/testdata/test_plugin.git/config deleted file mode 100644 index 9512a718938b..000000000000 --- a/pkg/plugin/testdata/test_plugin.git/config +++ /dev/null @@ -1,8 +0,0 @@ -[core] - repositoryformatversion = 0 - filemode = true - bare = true - ignorecase = true - precomposeunicode = true -[remote "origin"] - url = /Users/teppei/src/github.com/aquasecurity/trivy/pkg/plugin/testdata/test_plugin diff --git a/pkg/plugin/testdata/test_plugin.git/description b/pkg/plugin/testdata/test_plugin.git/description deleted file mode 100644 index 498b267a8c78..000000000000 --- a/pkg/plugin/testdata/test_plugin.git/description +++ /dev/null @@ -1 +0,0 @@ -Unnamed repository; edit this file 'description' to name the repository. diff --git a/pkg/plugin/testdata/test_plugin.git/info/exclude b/pkg/plugin/testdata/test_plugin.git/info/exclude deleted file mode 100644 index a5196d1be8fb..000000000000 --- a/pkg/plugin/testdata/test_plugin.git/info/exclude +++ /dev/null @@ -1,6 +0,0 @@ -# git ls-files --others --exclude-from=.git/info/exclude -# Lines that start with '#' are comments. -# For a project mostly in C, the following would be a good set of -# exclude patterns (uncomment them if you want to use them): -# *.[oa] -# *~ diff --git a/pkg/plugin/testdata/test_plugin.git/objects/08/6aefb548a1150b765d1e163a5e542fc80bd660 b/pkg/plugin/testdata/test_plugin.git/objects/08/6aefb548a1150b765d1e163a5e542fc80bd660 deleted file mode 100644 index e7d696256b86d9c3323ac0740015d918dae20479..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 147 zcmV;E0Brww0Zooe4#F@DL|Nw)R`-O05K?m)}bpc2I(oI`pUXp0py9{vdRpd=i8&5in`$3aPP4!^isNtVPhcckT1 zw3u%pIWY=g4?>ofdcUbQK@3>-<@wQ=mdyaV^>Xe`No+v(7qo2Mnz`2htT&%uLTVfd BMbrQQ diff --git a/pkg/plugin/testdata/test_plugin.git/objects/0a/e1413e3807e024dbc7de4129d12bdcae7dea61 b/pkg/plugin/testdata/test_plugin.git/objects/0a/e1413e3807e024dbc7de4129d12bdcae7dea61 deleted file mode 100644 index b16c882bc30f..000000000000 --- a/pkg/plugin/testdata/test_plugin.git/objects/0a/e1413e3807e024dbc7de4129d12bdcae7dea61 +++ /dev/null @@ -1,3 +0,0 @@ -xM -0 S[7a}ɺ -#iE޵(s,>-0!b C!)A1$$~h  AI ZI,\:r*{,A8'oMhd^ݩ [Bi \ No newline at end of file diff --git a/pkg/plugin/testdata/test_plugin.git/objects/49/0535e047e795a5c95306ce281c9f08cdb35b7c b/pkg/plugin/testdata/test_plugin.git/objects/49/0535e047e795a5c95306ce281c9f08cdb35b7c deleted file mode 100644 index 964cbf82f49bbc0825b9a3b0ce88f1ab88927e94..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 37 tcmbGR=C3>B9+OaM$w5k~+3 diff --git a/pkg/plugin/testdata/test_plugin.git/objects/92/9b4718db99b64a38b4e8c3ec8e673976821c08 b/pkg/plugin/testdata/test_plugin.git/objects/92/9b4718db99b64a38b4e8c3ec8e673976821c08 deleted file mode 100644 index ba6cfbad6f8c1f126ac73ae05fca5053271761db..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 344 zcmV-e0jK_W0hN$TZ-PJ+g}a_#F?&;;VL&ixo1#!4h4BFt!wyCm1r!nLgI{0Jbk|K! za+7nv4oJIIf`-f{TcYpNR- zg|2Ht*D|Q;`78a-0rWAod{mffuVpuy(>t3HTH3)^?qoi8yu4?8lsR@wKDEr`m+hg> zCh3y#RMAWh;A#C)In)UyZ*h$Vh}{!fC={rVJ`}c*p?BA;Zk>8Pn-SVZGhBq4g{h}Y z0QPjLYn4r;g>g!Pfhs$*twD<0Jhs~?3#!%munJUDR7&Z1sa4fa$*pXH+%j(SIX#Ex q^)&`?vz$+hQ8sEAVK6i>Ff%bxD99;I&&<=SOw7$;;K+Kv)nlP3cUi2Qm{nYe w{t51D2|#7$rltxdsl_FF#Tg8qtfmj#pHE$SGMMe0hRl49vzw!906R(?UD|Xgng9R* diff --git a/pkg/plugin/testdata/test_plugin.git/objects/d7/8abde66b1d35bdac65402f0e2cddf3a96cd377 b/pkg/plugin/testdata/test_plugin.git/objects/d7/8abde66b1d35bdac65402f0e2cddf3a96cd377 deleted file mode 100644 index 78b94e8996dd4a8bb2cdcb6c052a4c599e3a7813..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 380 zcmV-?0fYW{0hN$TZ<|0Ag}a_#G5e_YJb27VZB=8aO>L&Nfi`6~49pk|h7h8}{`G-G z-F4F=UFqCUI#*Xmb!}UX(8lb;IFu!nh!LgT96Ow|oM~jS(k)1?89{ErZDKo&Nv=UJ zhw?H)PIzv)m=!taxnoJna;s#DmWq{*OS$VXf)!#wK8>etfalA@RTGE8Pk-H4)5@w@ zsIL72v5Rd+DP|PjAqyce(doUh{O_RY&0}TY$5~#m2?dPfEsVpBSS9H(c;f)3ScEBR zwyRl(Y5Zt*dqqXJ|NZM6)hUjW^UTb$j`laY^Xsj7K3yJbt`Ev+=5Gyv!~M61NJN>P z{CtsjNWCTej5KJDzAL-QO)y;xr`1blwx;4H*SN|25Q`+R0i5aKyC~wsJId24wf_`- z!lWguq3HLPcR!APt$neJ&25DXS(c2h} acGpAn8NAmK2>)MY0JIAVK6i>Ff%bxD99;I&&<=SOw7$;;Ckq2XTknJ<@WJ=j+z&> x@2snRl?YU3ZfdGfl3HA%SDeA%$!hw*{rS|TCxh9}X~@jyIJ-Hz1^}iS9{935F311? diff --git a/pkg/plugin/testdata/test_plugin.git/packed-refs b/pkg/plugin/testdata/test_plugin.git/packed-refs deleted file mode 100644 index 00a4f957a23f..000000000000 --- a/pkg/plugin/testdata/test_plugin.git/packed-refs +++ /dev/null @@ -1,4 +0,0 @@ -# pack-refs with: peeled fully-peeled sorted -d78abde66b1d35bdac65402f0e2cddf3a96cd377 refs/heads/main -929b4718db99b64a38b4e8c3ec8e673976821c08 refs/tags/v0.1.0 -d78abde66b1d35bdac65402f0e2cddf3a96cd377 refs/tags/v0.2.0 diff --git a/pkg/plugin/testdata/test_plugin.git/refs/heads/.gitkeep b/pkg/plugin/testdata/test_plugin.git/refs/heads/.gitkeep deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/pkg/plugin/testdata/test_plugin.git/refs/tags/.gitkeep b/pkg/plugin/testdata/test_plugin.git/refs/tags/.gitkeep deleted file mode 100644 index e69de29bb2d1..000000000000