2017-07-07 21:35:10 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-07-27 17:34:11 +00:00
|
|
|
"runtime"
|
2017-07-07 21:35:10 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-09-20 04:23:52 +00:00
|
|
|
"github.com/containerd/containerd/content/local"
|
|
|
|
ctdmetadata "github.com/containerd/containerd/metadata"
|
2017-07-07 21:35:10 +00:00
|
|
|
"github.com/containerd/containerd/namespaces"
|
2019-09-20 04:23:52 +00:00
|
|
|
"github.com/containerd/containerd/snapshots"
|
2018-04-03 09:37:16 +00:00
|
|
|
"github.com/containerd/containerd/snapshots/native"
|
2017-07-07 21:35:10 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
2019-09-20 04:23:52 +00:00
|
|
|
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
|
2017-07-07 21:35:10 +00:00
|
|
|
"github.com/moby/buildkit/source"
|
2019-09-20 04:23:52 +00:00
|
|
|
"github.com/moby/buildkit/util/leaseutil"
|
2017-12-09 18:09:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-07 21:35:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-09-20 04:23:52 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2017-07-07 21:35:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepeatedFetch(t *testing.T) {
|
2017-07-08 23:25:07 +00:00
|
|
|
testRepeatedFetch(t, false)
|
|
|
|
}
|
|
|
|
func TestRepeatedFetchKeepGitDir(t *testing.T) {
|
|
|
|
testRepeatedFetch(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRepeatedFetch(t *testing.T, keepGitDir bool) {
|
2020-07-27 17:36:43 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
|
|
|
|
}
|
|
|
|
|
2017-12-09 02:19:08 +00:00
|
|
|
t.Parallel()
|
2017-12-01 19:44:27 +00:00
|
|
|
ctx := context.TODO()
|
2017-07-07 21:35:10 +00:00
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "buildkit-state")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
gs := setupGitSource(t, tmpdir)
|
|
|
|
|
|
|
|
repodir, err := ioutil.TempDir("", "buildkit-gitsource")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(repodir)
|
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
repodir, err = setupGitRepo(repodir)
|
|
|
|
require.NoError(t, err)
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2017-07-08 23:25:07 +00:00
|
|
|
id := &source.GitIdentifier{Remote: repodir, KeepGitDir: keepGitDir}
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
g, err := gs.Resolve(ctx, id, nil, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
key1, _, done, err := g.CacheKey(ctx, nil, 0)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-24 21:00:58 +00:00
|
|
|
require.True(t, done)
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2019-08-14 19:12:25 +00:00
|
|
|
expLen := 40
|
|
|
|
if keepGitDir {
|
|
|
|
expLen += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expLen, len(key1))
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
ref1, err := g.Snapshot(ctx, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref1.Release(context.TODO())
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err := ref1.Mount(ctx, false, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
dir, err := lm.Mount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lm.Unmount()
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(dir, "def"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "bar\n", string(dt))
|
|
|
|
|
|
|
|
_, err = os.Lstat(filepath.Join(dir, "ghi"))
|
|
|
|
require.Error(t, err)
|
2020-04-19 05:17:47 +00:00
|
|
|
require.True(t, errors.Is(err, os.ErrNotExist))
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
_, err = os.Lstat(filepath.Join(dir, "sub/subfile"))
|
|
|
|
require.Error(t, err)
|
2020-04-19 05:17:47 +00:00
|
|
|
require.True(t, errors.Is(err, os.ErrNotExist))
|
2017-12-09 18:09:23 +00:00
|
|
|
|
2017-07-07 21:35:10 +00:00
|
|
|
// second fetch returns same dir
|
2017-07-08 23:25:07 +00:00
|
|
|
id = &source.GitIdentifier{Remote: repodir, Ref: "master", KeepGitDir: keepGitDir}
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
g, err = gs.Resolve(ctx, id, nil, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
key2, _, _, err := g.CacheKey(ctx, nil, 0)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, key1, key2)
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
ref2, err := g.Snapshot(ctx, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref2.Release(context.TODO())
|
|
|
|
|
|
|
|
require.Equal(t, ref1.ID(), ref2.ID())
|
|
|
|
|
2017-07-08 23:25:07 +00:00
|
|
|
id = &source.GitIdentifier{Remote: repodir, Ref: "feature", KeepGitDir: keepGitDir}
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
g, err = gs.Resolve(ctx, id, nil, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
key3, _, _, err := g.CacheKey(ctx, nil, 0)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, key1, key3)
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
ref3, err := g.Snapshot(ctx, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref3.Release(context.TODO())
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err = ref3.Mount(ctx, false, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lm = snapshot.LocalMounter(mount)
|
|
|
|
dir, err = lm.Mount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lm.Unmount()
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(dir, "ghi"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "baz\n", string(dt))
|
2017-12-09 18:09:23 +00:00
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(dir, "sub/subfile"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "subcontents\n", string(dt))
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchBySHA(t *testing.T) {
|
2017-07-08 23:25:07 +00:00
|
|
|
testFetchBySHA(t, false)
|
|
|
|
}
|
|
|
|
func TestFetchBySHAKeepGitDir(t *testing.T) {
|
|
|
|
testFetchBySHA(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFetchBySHA(t *testing.T, keepGitDir bool) {
|
2020-07-27 17:36:43 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
|
|
|
|
}
|
|
|
|
|
2017-12-09 02:19:08 +00:00
|
|
|
t.Parallel()
|
2017-07-07 21:35:10 +00:00
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "buildkit-state")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
gs := setupGitSource(t, tmpdir)
|
|
|
|
|
|
|
|
repodir, err := ioutil.TempDir("", "buildkit-gitsource")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(repodir)
|
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
repodir, err = setupGitRepo(repodir)
|
|
|
|
require.NoError(t, err)
|
2017-07-07 21:35:10 +00:00
|
|
|
|
|
|
|
cmd := exec.Command("git", "rev-parse", "feature")
|
|
|
|
cmd.Dir = repodir
|
|
|
|
|
|
|
|
out, err := cmd.Output()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sha := strings.TrimSpace(string(out))
|
|
|
|
require.Equal(t, 40, len(sha))
|
|
|
|
|
2017-07-08 23:25:07 +00:00
|
|
|
id := &source.GitIdentifier{Remote: repodir, Ref: sha, KeepGitDir: keepGitDir}
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
g, err := gs.Resolve(ctx, id, nil, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
key1, _, done, err := g.CacheKey(ctx, nil, 0)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-25 22:55:49 +00:00
|
|
|
require.True(t, done)
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2019-08-14 19:12:25 +00:00
|
|
|
expLen := 40
|
|
|
|
if keepGitDir {
|
|
|
|
expLen += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expLen, len(key1))
|
2017-07-07 21:35:10 +00:00
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
ref1, err := g.Snapshot(ctx, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref1.Release(context.TODO())
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err := ref1.Mount(ctx, false, nil)
|
2017-07-07 21:35:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
dir, err := lm.Mount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lm.Unmount()
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(dir, "ghi"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "baz\n", string(dt))
|
2017-12-09 18:09:23 +00:00
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(dir, "sub/subfile"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "subcontents\n", string(dt))
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-08 23:25:07 +00:00
|
|
|
func TestMultipleRepos(t *testing.T) {
|
|
|
|
testMultipleRepos(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleReposKeepGitDir(t *testing.T) {
|
|
|
|
testMultipleRepos(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMultipleRepos(t *testing.T, keepGitDir bool) {
|
2020-07-27 17:36:43 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
|
|
|
|
}
|
|
|
|
|
2017-12-09 02:19:08 +00:00
|
|
|
t.Parallel()
|
2017-07-08 23:25:07 +00:00
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "buildkit-state")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
gs := setupGitSource(t, tmpdir)
|
|
|
|
|
|
|
|
repodir, err := ioutil.TempDir("", "buildkit-gitsource")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(repodir)
|
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
repodir, err = setupGitRepo(repodir)
|
|
|
|
require.NoError(t, err)
|
2017-07-08 23:25:07 +00:00
|
|
|
|
|
|
|
repodir2, err := ioutil.TempDir("", "buildkit-gitsource")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(repodir2)
|
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
err = runShell(repodir2,
|
2017-07-08 23:25:07 +00:00
|
|
|
"git init",
|
|
|
|
"git config --local user.email test",
|
|
|
|
"git config --local user.name test",
|
|
|
|
"echo xyz > xyz",
|
|
|
|
"git add xyz",
|
|
|
|
"git commit -m initial",
|
|
|
|
)
|
2017-12-09 18:09:23 +00:00
|
|
|
require.NoError(t, err)
|
2017-07-08 23:25:07 +00:00
|
|
|
|
|
|
|
id := &source.GitIdentifier{Remote: repodir, KeepGitDir: keepGitDir}
|
|
|
|
id2 := &source.GitIdentifier{Remote: repodir2, KeepGitDir: keepGitDir}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
g, err := gs.Resolve(ctx, id, nil, nil)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
g2, err := gs.Resolve(ctx, id2, nil, nil)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-08-14 19:12:25 +00:00
|
|
|
expLen := 40
|
|
|
|
if keepGitDir {
|
|
|
|
expLen += 4
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
key1, _, _, err := g.CacheKey(ctx, nil, 0)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-14 19:12:25 +00:00
|
|
|
require.Equal(t, expLen, len(key1))
|
2017-07-08 23:25:07 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
key2, _, _, err := g2.CacheKey(ctx, nil, 0)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-14 19:12:25 +00:00
|
|
|
require.Equal(t, expLen, len(key2))
|
2017-07-08 23:25:07 +00:00
|
|
|
|
|
|
|
require.NotEqual(t, key1, key2)
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
ref1, err := g.Snapshot(ctx, nil)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref1.Release(context.TODO())
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err := ref1.Mount(ctx, false, nil)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
dir, err := lm.Mount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lm.Unmount()
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
ref2, err := g2.Snapshot(ctx, nil)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref2.Release(context.TODO())
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err = ref2.Mount(ctx, false, nil)
|
2017-07-08 23:25:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lm = snapshot.LocalMounter(mount)
|
|
|
|
dir2, err := lm.Mount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lm.Unmount()
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(dir, "def"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "bar\n", string(dt))
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(dir2, "xyz"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "xyz\n", string(dt))
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:04:50 +00:00
|
|
|
func TestCredentialRedaction(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "buildkit-state")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
gs := setupGitSource(t, tmpdir)
|
|
|
|
|
|
|
|
url := "https://user:keepthissecret@non-existant-host/user/private-repo.git"
|
|
|
|
id := &source.GitIdentifier{Remote: url}
|
|
|
|
|
|
|
|
g, err := gs.Resolve(ctx, id, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, _, err = g.CacheKey(ctx, nil, 0)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.False(t, strings.Contains(err.Error(), "keepthissecret"))
|
|
|
|
}
|
|
|
|
|
2021-05-20 04:10:58 +00:00
|
|
|
func TestSubdir(t *testing.T) {
|
|
|
|
testSubdir(t, false)
|
|
|
|
}
|
|
|
|
func TestSubdirKeepGitDir(t *testing.T) {
|
|
|
|
testSubdir(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSubdir(t *testing.T, keepGitDir bool) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "buildkit-state")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
gs := setupGitSource(t, tmpdir)
|
|
|
|
|
|
|
|
repodir, err := ioutil.TempDir("", "buildkit-gitsource")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(repodir)
|
|
|
|
|
|
|
|
err = runShell(repodir,
|
|
|
|
"git init",
|
|
|
|
"git config --local user.email test",
|
|
|
|
"git config --local user.name test",
|
|
|
|
"echo foo > abc",
|
|
|
|
"mkdir sub",
|
|
|
|
"echo abc > sub/bar",
|
|
|
|
"git add abc sub",
|
|
|
|
"git commit -m initial",
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id := &source.GitIdentifier{Remote: repodir, KeepGitDir: keepGitDir, Subdir: "sub"}
|
|
|
|
|
|
|
|
g, err := gs.Resolve(ctx, id, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
key1, _, done, err := g.CacheKey(ctx, nil, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, done)
|
|
|
|
|
|
|
|
expLen := 44
|
|
|
|
if keepGitDir {
|
|
|
|
expLen += 4
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expLen, len(key1))
|
|
|
|
|
|
|
|
ref1, err := g.Snapshot(ctx, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ref1.Release(context.TODO())
|
|
|
|
|
|
|
|
mount, err := ref1.Mount(ctx, false, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
dir, err := lm.Mount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer lm.Unmount()
|
|
|
|
|
|
|
|
fis, err := ioutil.ReadDir(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(fis))
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(dir, "bar"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, "abc\n", string(dt))
|
|
|
|
}
|
|
|
|
|
2017-07-07 21:35:10 +00:00
|
|
|
func setupGitSource(t *testing.T, tmpdir string) source.Source {
|
2018-04-03 09:37:16 +00:00
|
|
|
snapshotter, err := native.NewSnapshotter(filepath.Join(tmpdir, "snapshots"))
|
2017-07-07 21:35:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-09-20 04:23:52 +00:00
|
|
|
store, err := local.NewStore(tmpdir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
db, err := bolt.Open(filepath.Join(tmpdir, "containerdmeta.db"), 0644, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mdb := ctdmetadata.NewDB(db, store, map[string]snapshots.Snapshotter{
|
|
|
|
"native": snapshotter,
|
|
|
|
})
|
|
|
|
|
2021-07-09 00:09:35 +00:00
|
|
|
md, err := metadata.NewStore(filepath.Join(tmpdir, "metadata.db"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-07-07 21:35:10 +00:00
|
|
|
cm, err := cache.NewManager(cache.ManagerOpt{
|
2019-09-20 04:23:52 +00:00
|
|
|
Snapshotter: snapshot.FromContainerdSnapshotter("native", containerdsnapshot.NSSnapshotter("buildkit", mdb.Snapshotter("native")), nil),
|
|
|
|
MetadataStore: md,
|
2019-09-30 23:38:02 +00:00
|
|
|
LeaseManager: leaseutil.WithNamespace(ctdmetadata.NewLeaseManager(mdb), "buildkit"),
|
2019-09-20 04:23:52 +00:00
|
|
|
ContentStore: mdb.ContentStore(),
|
|
|
|
GarbageCollect: mdb.GarbageCollect,
|
2017-07-07 21:35:10 +00:00
|
|
|
})
|
2019-09-20 04:23:52 +00:00
|
|
|
require.NoError(t, err)
|
2017-07-07 21:35:10 +00:00
|
|
|
|
|
|
|
gs, err := NewSource(Opt{
|
|
|
|
CacheAccessor: cm,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return gs
|
|
|
|
}
|
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
func setupGitRepo(dir string) (string, error) {
|
|
|
|
subPath := filepath.Join(dir, "sub")
|
|
|
|
mainPath := filepath.Join(dir, "main")
|
|
|
|
|
|
|
|
if err := os.MkdirAll(subPath, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(mainPath, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runShell(filepath.Join(dir, "sub"),
|
|
|
|
"git init",
|
|
|
|
"git config --local user.email test",
|
|
|
|
"git config --local user.name test",
|
|
|
|
"echo subcontents > subfile",
|
|
|
|
"git add subfile",
|
|
|
|
"git commit -m initial",
|
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := runShell(filepath.Join(dir, "main"),
|
2017-07-07 21:35:10 +00:00
|
|
|
"git init",
|
|
|
|
"git config --local user.email test",
|
|
|
|
"git config --local user.name test",
|
|
|
|
"echo foo > abc",
|
|
|
|
"git add abc",
|
|
|
|
"git commit -m initial",
|
|
|
|
"echo bar > def",
|
|
|
|
"git add def",
|
|
|
|
"git commit -m second",
|
|
|
|
"git checkout -B feature",
|
|
|
|
"echo baz > ghi",
|
|
|
|
"git add ghi",
|
|
|
|
"git commit -m feature",
|
2017-12-09 18:09:23 +00:00
|
|
|
"git submodule add "+subPath+" sub",
|
|
|
|
"git add -A",
|
|
|
|
"git commit -m withsub",
|
2021-07-06 23:34:43 +00:00
|
|
|
"git checkout master",
|
2017-12-09 18:09:23 +00:00
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return mainPath, nil
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 18:09:23 +00:00
|
|
|
func runShell(dir string, cmds ...string) error {
|
2017-07-07 21:35:10 +00:00
|
|
|
for _, args := range cmds {
|
2020-07-27 17:34:11 +00:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
cmd = exec.Command("powershell", "-command", args)
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command("sh", "-c", args)
|
|
|
|
}
|
2017-07-07 21:35:10 +00:00
|
|
|
cmd.Dir = dir
|
2021-05-20 04:10:58 +00:00
|
|
|
cmd.Stderr = os.Stderr
|
2017-12-09 18:09:23 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return errors.Wrapf(err, "error running %v", args)
|
|
|
|
}
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|
2017-12-09 18:09:23 +00:00
|
|
|
return nil
|
2017-07-07 21:35:10 +00:00
|
|
|
}
|