Skip to content

Commit

Permalink
test: replace embedded Git repository with dynamically created reposi…
Browse files Browse the repository at this point in the history
…tory (#6824)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 authored Jun 3, 2024
1 parent c24dfba commit b7b8cdc
Show file tree
Hide file tree
Showing 36 changed files with 240 additions and 85 deletions.
132 changes: 132 additions & 0 deletions internal/gittest/server.go
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)
}
36 changes: 36 additions & 0 deletions internal/testutil/fs.go
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)
}
}
}
57 changes: 30 additions & 27 deletions pkg/fanal/artifact/repo/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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",
},
Expand All @@ -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",
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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",
},
Expand All @@ -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",
},
Expand All @@ -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",
},
Expand All @@ -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 {
Expand All @@ -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{
Expand Down
1 change: 1 addition & 0 deletions pkg/fanal/artifact/repo/testdata/test-repo/anothertest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is another text file.
1 change: 1 addition & 0 deletions pkg/fanal/artifact/repo/testdata/test-repo/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a text file.
1 change: 0 additions & 1 deletion pkg/fanal/artifact/repo/testdata/test.git/HEAD

This file was deleted.

6 changes: 0 additions & 6 deletions pkg/fanal/artifact/repo/testdata/test.git/config

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion pkg/fanal/artifact/repo/testdata/test.git/refs/tags/v1.0.0

This file was deleted.

Loading

0 comments on commit b7b8cdc

Please sign in to comment.