Skip to content

Commit 3afd92a

Browse files
committed
dockerfile: add tests for Git query URL checksums
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 8ef9b54 commit 3afd92a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,14 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
15231523
if cfg.keepGitDir {
15241524
gitOptions = append(gitOptions, llb.KeepGitDir())
15251525
}
1526+
if cfg.checksum != "" && gitRef.Checksum != "" {
1527+
if cfg.checksum != gitRef.Checksum {
1528+
return errors.Errorf("checksum mismatch %q != %q", cfg.checksum, gitRef.Checksum)
1529+
}
1530+
}
1531+
if gitRef.Checksum != "" {
1532+
cfg.checksum = gitRef.Checksum
1533+
}
15261534
if cfg.checksum != "" {
15271535
gitOptions = append(gitOptions, llb.GitChecksum(cfg.checksum))
15281536
}

frontend/dockerfile/dockerfile_addgit_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,23 @@ COPY foo out
398398
}...)
399399
require.NoError(t, err)
400400

401+
// get commit SHA for v0.0.2
402+
cmd := exec.Command("git", "rev-parse", "v0.0.2")
403+
cmd.Dir = gitDir
404+
dt, err := cmd.CombinedOutput()
405+
require.NoError(t, err)
406+
commitHashV2 := strings.TrimSpace(string(dt))
407+
require.Len(t, commitHashV2, 40)
408+
409+
// get commit SHA for latest
410+
cmd = exec.Command("git", "rev-parse", "latest")
411+
cmd.Dir = gitDir
412+
dt, err = cmd.CombinedOutput()
413+
require.NoError(t, err)
414+
commitHashLatest := strings.TrimSpace(string(dt))
415+
require.Len(t, commitHashLatest, 40)
416+
require.NotEqual(t, commitHashV2, commitHashLatest)
417+
401418
server := httptest.NewServer(http.FileServer(http.Dir(filepath.Clean(gitDir))))
402419
defer server.Close()
403420
serverURL := server.URL
@@ -414,6 +431,12 @@ COPY foo out
414431
}
415432

416433
tcases := []tcase{
434+
{
435+
// if this commit is already cached then this will work and ignore tag atm because tag name has no influence to the output
436+
name: "tag with invalid commit",
437+
url: serverURL + "/.git?tag=v0.0.2&commit=" + commitHashLatest,
438+
expectErr: "expected checksum to match",
439+
},
417440
{
418441
name: "old style ref",
419442
url: serverURL + "/.git#v0.0.2",
@@ -444,6 +467,37 @@ COPY foo out
444467
url: serverURL + "/.git?tag=v0.0.2#refs/tags/v0.0.2",
445468
expectOut: "v0.0.2\n",
446469
},
470+
{
471+
name: "v2 by commit",
472+
url: serverURL + "/.git?commit=" + commitHashV2,
473+
expectOut: "v0.0.2\n",
474+
},
475+
{
476+
name: "v2 ref by commit",
477+
url: serverURL + "/.git?ref=" + commitHashV2,
478+
expectOut: "v0.0.2\n",
479+
},
480+
{
481+
name: "tag with commit",
482+
url: serverURL + "/.git?tag=v0.0.2&commit=" + commitHashV2,
483+
expectOut: "v0.0.2\n",
484+
},
485+
{
486+
name: "commit with commit",
487+
url: serverURL + "/.git?ref=" + commitHashV2 + "&commit=" + commitHashV2,
488+
expectOut: "v0.0.2\n",
489+
},
490+
{
491+
name: "latest with commit",
492+
url: serverURL + "/.git?commit=" + commitHashLatest,
493+
expectOut: "latest\n",
494+
},
495+
{
496+
// this only works if there is already cache for commitHashLatest from previous case
497+
name: "tag with invalid commit",
498+
url: serverURL + "/.git?tag=v0.0.2&commit=" + commitHashLatest,
499+
expectOut: "latest\n",
500+
},
447501
{
448502
name: "mismatch refs",
449503
url: serverURL + "/.git?tag=v0.0.2#refs/heads/master",
@@ -488,6 +542,13 @@ COPY foo out
488542
})
489543
}
490544

545+
cl, err := client.New(sb.Context(), sb.Address())
546+
require.NoError(t, err)
547+
defer c.Close()
548+
549+
err = cl.Prune(sb.Context(), nil)
550+
require.NoError(t, err)
551+
491552
for _, tc := range tcases {
492553
dockerfile2 := fmt.Sprintf(`
493554
FROM scratch

0 commit comments

Comments
 (0)