Skip to content

Commit

Permalink
WIP/DNM: integration tests for SHA-256
Browse files Browse the repository at this point in the history
This fails in `gitSourceHandler` because it can't handle *both* SHA-1 and SHA-256 before Git 2.45 (see PR comments), and there's not a simple way to fix that without newer Git or larger refactoring.
  • Loading branch information
tianon committed Nov 11, 2024
1 parent 2fc8bf6 commit b160376
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions source/git/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,20 @@ func testRepeatedFetch(t *testing.T, keepGitDir bool) {
require.Equal(t, "subcontents\n", string(dt))
}

func TestFetchBySHA(t *testing.T) {
testFetchBySHA(t, false)
func TestFetchBySHA1(t *testing.T) {
testFetchBySHA(t, "sha1", false)
}
func TestFetchBySHAKeepGitDir(t *testing.T) {
testFetchBySHA(t, true)
func TestFetchBySHA1KeepGitDir(t *testing.T) {
testFetchBySHA(t, "sha1", true)
}
func TestFetchBySHA256(t *testing.T) {
testFetchBySHA(t, "sha256", false)
}
func TestFetchBySHA256KeepGitDir(t *testing.T) {
testFetchBySHA(t, "sha256", true)
}

func testFetchBySHA(t *testing.T, keepGitDir bool) {
func testFetchBySHA(t *testing.T, format string, keepGitDir bool) {
if runtime.GOOS == "windows" {
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
}
Expand All @@ -168,16 +174,26 @@ func testFetchBySHA(t *testing.T, keepGitDir bool) {

gs := setupGitSource(t, t.TempDir())

repo := setupGitRepo(t)
repo := setupGitRepoFormat(t, format)

Check failure on line 177 in source/git/source_test.go

View workflow job for this annotation

GitHub Actions / test / run (./..., 1, integration gateway)

Failed: source/git/TestFetchBySHA256

=== RUN TestFetchBySHA256 === PAUSE TestFetchBySHA256 === CONT TestFetchBySHA256 source_test.go:177: git version 2.45.2 warning: unable to rmdir 'sub': Directory not empty Switched to branch 'master' Reset branch 'master' source_test.go:991: (stdout) Initialized empty Git repository in /tmp/TestFetchBySHA2561068290378/001/snapshots/snapshots/1/ source_test.go:991: (stderr) fatal: Not a valid object name 21cad67fdf1a0cbd71e41ce969f541a8480a4b42a67d0309f31a02e73c6ba20e^{commit} source_test.go:991: (stderr) fatal: couldn't find remote ref 21cad67fdf1a0cbd71e41ce969f541a8480a4b42a67d0309f31a02e73c6ba20e source_test.go:217: Error Trace: /src/source/git/source_test.go:217 /src/source/git/source_test.go:160 Error: Received unexpected error: exit status 128 git stderr: fatal: couldn't find remote ref 21cad67fdf1a0cbd71e41ce969f541a8480a4b42a67d0309f31a02e73c6ba20e github.com/moby/buildkit/util/gitutil.(*GitCLI).Run /src/util/gitutil/git_cli.go:230 github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:493 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256 /src/source/git/source_test.go:160 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 failed to fetch remote http://127.0.0.1:34997/main github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:494 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256 /src/source/git/source_test.go:160 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 Test: TestFetchBySHA256 --- FAIL: TestFetchBySHA256 (0.69s)

Check failure on line 177 in source/git/source_test.go

View workflow job for this annotation

GitHub Actions / test / run (./..., 1, integration gateway)

Failed: source/git/TestFetchBySHA256KeepGitDir

=== RUN TestFetchBySHA256KeepGitDir === PAUSE TestFetchBySHA256KeepGitDir === CONT TestFetchBySHA256KeepGitDir source_test.go:177: git version 2.45.2 source_test.go:991: (stdout) Initialized empty Git repository in /tmp/TestFetchBySHA256KeepGitDir3694537529/001/snapshots/snapshots/1/ source_test.go:991: (stderr) fatal: Not a valid object name e4be1c0fedc5778f1634d255ee3b1a0349084b3755c6500e0d943d1ae947373a^{commit} Switched to a new branch 'feature' Reset branch 'master' source_test.go:991: (stderr) fatal: couldn't find remote ref e4be1c0fedc5778f1634d255ee3b1a0349084b3755c6500e0d943d1ae947373a source_test.go:217: Error Trace: /src/source/git/source_test.go:217 /src/source/git/source_test.go:163 Error: Received unexpected error: exit status 128 git stderr: fatal: couldn't find remote ref e4be1c0fedc5778f1634d255ee3b1a0349084b3755c6500e0d943d1ae947373a github.com/moby/buildkit/util/gitutil.(*GitCLI).Run /src/util/gitutil/git_cli.go:230 github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:493 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256KeepGitDir /src/source/git/source_test.go:163 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 failed to fetch remote http://127.0.0.1:43919/main github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:494 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256KeepGitDir /src/source/git/source_test.go:163 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 Test: TestFetchBySHA256KeepGitDir Switched to a new branch 'feature' --- FAIL: TestFetchBySHA256KeepGitDir (0.70s)

Check failure on line 177 in source/git/source_test.go

View workflow job for this annotation

GitHub Actions / test / run (./..., nydus, 1, integration)

Failed: source/git/TestFetchBySHA256

=== RUN TestFetchBySHA256 === PAUSE TestFetchBySHA256 === CONT TestFetchBySHA256 source_test.go:177: git version 2.45.2 source_test.go:991: (stdout) Initialized empty Git repository in /tmp/TestFetchBySHA2563737059983/001/snapshots/snapshots/1/ Cloning into '/tmp/TestRepeatedFetchKeepGitDir1396773609/002/main/sub'... Reset branch 'master' source_test.go:991: (stderr) fatal: Not a valid object name 271ec4a2bf869494dc1333272551cb96dbb7e972f0f843015ce0f7535d397f4e^{commit} Cloning into '/tmp/TestFetchUnadvertisedRefSha1705807090/002/main/sub'... warning: unable to rmdir 'sub': Directory not empty Switched to branch 'master' Switched to a new branch 'feature' source_test.go:991: (stderr) fatal: couldn't find remote ref 271ec4a2bf869494dc1333272551cb96dbb7e972f0f843015ce0f7535d397f4e Cloning into '/tmp/TestFetchBySHA13321745580/002/main/sub'... source_test.go:217: Error Trace: /src/source/git/source_test.go:217 /src/source/git/source_test.go:160 Error: Received unexpected error: exit status 128 git stderr: fatal: couldn't find remote ref 271ec4a2bf869494dc1333272551cb96dbb7e972f0f843015ce0f7535d397f4e github.com/moby/buildkit/util/gitutil.(*GitCLI).Run /src/util/gitutil/git_cli.go:230 github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:493 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256 /src/source/git/source_test.go:160 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 failed to fetch remote http://127.0.0.1:39347/main github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:494 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256 /src/source/git/source_test.go:160 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 Test: TestFetchBySHA256 --- FAIL: TestFetchBySHA256 (0.69s)

Check failure on line 177 in source/git/source_test.go

View workflow job for this annotation

GitHub Actions / test / run (./..., nydus, 1, integration)

Failed: source/git/TestFetchBySHA256KeepGitDir

=== RUN TestFetchBySHA256KeepGitDir === PAUSE TestFetchBySHA256KeepGitDir === CONT TestFetchBySHA256KeepGitDir Switched to a new branch 'feature' source_test.go:177: git version 2.45.2 Cloning into '/tmp/TestFetchByBranchKeepGitDir4290459395/002/main/sub'... Reset branch 'master' Cloning into '/tmp/TestFetchByBranch267839569/002/main/sub'... Switched to a new branch 'feature' source_test.go:991: (stdout) Initialized empty Git repository in /tmp/TestFetchBySHA256KeepGitDir2602631300/001/snapshots/snapshots/1/ source_test.go:991: (stderr) fatal: Not a valid object name e9ea1241119ff0144901df2b2ea4a17910973b343ee3e628e88aa9054a907b99^{commit} source_test.go:991: (stderr) fatal: couldn't find remote ref e9ea1241119ff0144901df2b2ea4a17910973b343ee3e628e88aa9054a907b99 source_test.go:217: Error Trace: /src/source/git/source_test.go:217 /src/source/git/source_test.go:163 Error: Received unexpected error: exit status 128 git stderr: fatal: couldn't find remote ref e9ea1241119ff0144901df2b2ea4a17910973b343ee3e628e88aa9054a907b99 github.com/moby/buildkit/util/gitutil.(*GitCLI).Run /src/util/gitutil/git_cli.go:230 github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:493 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256KeepGitDir /src/source/git/source_test.go:163 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 failed to fetch remote http://127.0.0.1:40095/main github.com/moby/buildkit/source/git.(*gitSourceHandler).Snapshot /src/source/git/source.go:494 github.com/moby/buildkit/source/git.testFetchBySHA /src/source/git/source_test.go:216 github.com/moby/buildkit/source/git.TestFetchBySHA256KeepGitDir /src/source/git/source_test.go:163 testing.tRunner /usr/local/go/src/testing/testing.go:1690 runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1700 Test: TestFetchBySHA256KeepGitDir --- FAIL: TestFetchBySHA256KeepGitDir (0.67s)

cmd := exec.Command("git", "rev-parse", "feature")
cmd.Dir = repo.mainPath

out, err := cmd.Output()
require.NoError(t, err)

var shaLen int
switch format {
case "sha1":
shaLen = 40
case "sha256":
shaLen = 64
default:
t.Fatalf("unexpected format: %q", format)
}

sha := strings.TrimSpace(string(out))
require.Equal(t, 40, len(sha))
require.Equal(t, shaLen, len(sha))

id := &GitIdentifier{Remote: repo.mainURL, Ref: sha, KeepGitDir: keepGitDir}

Expand All @@ -188,14 +204,14 @@ func testFetchBySHA(t *testing.T, keepGitDir bool) {
require.NoError(t, err)
require.True(t, done)

expLen := 40
expLen := shaLen
if keepGitDir {
expLen += 4
require.GreaterOrEqual(t, len(key1), expLen)
} else {
require.Equal(t, expLen, len(key1))
}
require.Equal(t, 40, len(pin1))
require.Equal(t, shaLen, len(pin1))

ref1, err := g.Snapshot(ctx, nil)
require.NoError(t, err)
Expand Down Expand Up @@ -796,7 +812,13 @@ type gitRepoFixture struct {
mainURL, subURL string // HTTP URLs for the respective repos
}

// small helper for the common case
func setupGitRepo(t *testing.T) gitRepoFixture {
t.Helper()
return setupGitRepoFormat(t, "sha1")
}

func setupGitRepoFormat(t *testing.T, format string) gitRepoFixture {
t.Helper()
dir := t.TempDir()
srv := serveGitRepo(t, dir)
Expand All @@ -810,7 +832,7 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
require.NoError(t, os.MkdirAll(fixture.mainPath, 0700))

runShell(t, fixture.subPath,
"git -c init.defaultBranch=master init",
"git -c init.defaultBranch=master init --object-format="+format,
"git config --local user.email test",
"git config --local user.name test",
"echo subcontents > subfile",
Expand All @@ -827,7 +849,7 @@ func setupGitRepo(t *testing.T) gitRepoFixture {
// * (tag: refs/tags/v1.2.3) second
// * (tag: refs/tags/a/v1.2.3, refs/tags/a/v1.2.3-same) initial
runShell(t, fixture.mainPath,
"git -c init.defaultBranch=master init",
"git -c init.defaultBranch=master init --object-format="+format,
"git config --local user.email test",
"git config --local user.name test",

Expand Down

0 comments on commit b160376

Please sign in to comment.