-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: replace embedded Git repository with dynamically created reposi…
…tory (#6824) Signed-off-by: knqyf263 <knqyf263@gmail.com>
- Loading branch information
Showing
36 changed files
with
240 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is another text file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is a text file. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
pkg/fanal/artifact/repo/testdata/test.git/objects/0d/8bf3f07c3970b3e38c2cfb1c619cb86fae76d2
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-59 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/1c/1d7deed649fbecd66fab423ccd9d001bf9ff91
Binary file not shown.
Binary file removed
BIN
-86 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/27/aaec53f92314d9438a53c703f169d2cbf5001a
Binary file not shown.
Binary file removed
BIN
-53 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/5e/fb9bc29c482e023e40e0a2b3b7e49cec842034
Binary file not shown.
3 changes: 0 additions & 3 deletions
3
pkg/fanal/artifact/repo/testdata/test.git/objects/6a/c152fe2b87cb5e243414df71790a32912e778d
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-34 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/c0/42cd14d2b999cade090785af47e9f8b8e342ff
Binary file not shown.
Binary file removed
BIN
-122 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/c9/06fc4a94762f8a2c77c718947143d16e4e9ec7
Binary file not shown.
Binary file removed
BIN
-165 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/d7/937c5f0ce7f2054e4e3be65ab3cd0f9462dc1b
Binary file not shown.
Binary file removed
BIN
-53 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/e2/4866d1d31ddffdb27fbcf583d5deb4386d5145
Binary file not shown.
Binary file removed
BIN
-15 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
Binary file not shown.
Binary file removed
BIN
-40 Bytes
pkg/fanal/artifact/repo/testdata/test.git/objects/f4/836be6497e83e13dc0cfbce7e6b973b1ea511d
Binary file not shown.
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
pkg/fanal/artifact/repo/testdata/test.git/refs/heads/valid-branch
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.