@@ -21,6 +21,7 @@ import (
21
21
22
22
var addGitTests = integration .TestFuncs (
23
23
testAddGit ,
24
+ testAddGitChecksumCache ,
24
25
)
25
26
26
27
func init () {
@@ -244,6 +245,111 @@ RUN [ ! -d /nogitdir/.git ]
244
245
require .Contains (t , err .Error (), "expected hex commit hash" )
245
246
}
246
247
248
+ func testAddGitChecksumCache (t * testing.T , sb integration.Sandbox ) {
249
+ integration .SkipOnPlatform (t , "windows" )
250
+ f := getFrontend (t , sb )
251
+
252
+ gitDir , err := os .MkdirTemp ("" , "buildkit" )
253
+ require .NoError (t , err )
254
+ defer os .RemoveAll (gitDir )
255
+ gitCommands := []string {
256
+ "git init" ,
257
+ "git config --local user.email test" ,
258
+ "git config --local user.name test" ,
259
+ }
260
+ makeCommit := func (tag string ) []string {
261
+ return []string {
262
+ "echo foo of " + tag + " >foo" ,
263
+ "git add foo" ,
264
+ "git commit -m " + tag ,
265
+ "git tag " + tag ,
266
+ }
267
+ }
268
+ gitCommands = append (gitCommands , makeCommit ("v0.0.1" )... )
269
+ gitCommands = append (gitCommands , makeCommit ("v0.0.2" )... )
270
+ gitCommands = append (gitCommands , "git update-server-info" )
271
+ err = runShell (gitDir , gitCommands ... )
272
+ require .NoError (t , err )
273
+
274
+ revParseCmd := exec .Command ("git" , "rev-parse" , "v0.0.2" )
275
+ revParseCmd .Dir = gitDir
276
+ commitHashB , err := revParseCmd .Output ()
277
+ require .NoError (t , err )
278
+ commitHash := strings .TrimSpace (string (commitHashB ))
279
+
280
+ server := httptest .NewServer (http .FileServer (http .Dir (filepath .Clean (gitDir ))))
281
+ defer server .Close ()
282
+ serverURL := server .URL
283
+
284
+ // First build: without checksum, from tag, generate unique.txt from /dev/urandom and copy to scratch
285
+ dockerfile1 := `
286
+ FROM alpine AS src
287
+ ADD --keep-git-dir ` + serverURL + `/.git#v0.0.2 /repo
288
+ RUN head -c 16 /dev/urandom | base64 > /repo/unique.txt
289
+
290
+ FROM scratch
291
+ COPY --from=src /repo/unique.txt /
292
+ `
293
+ dir1 := integration .Tmpdir (t ,
294
+ fstest .CreateFile ("Dockerfile" , []byte (dockerfile1 ), 0600 ),
295
+ )
296
+
297
+ c , err := client .New (sb .Context (), sb .Address ())
298
+ require .NoError (t , err )
299
+ defer c .Close ()
300
+
301
+ destDir1 := t .TempDir ()
302
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
303
+ Exports : []client.ExportEntry {
304
+ {
305
+ Type : client .ExporterLocal ,
306
+ OutputDir : destDir1 ,
307
+ },
308
+ },
309
+ LocalMounts : map [string ]fsutil.FS {
310
+ dockerui .DefaultLocalNameDockerfile : dir1 ,
311
+ dockerui .DefaultLocalNameContext : dir1 ,
312
+ },
313
+ }, nil )
314
+ require .NoError (t , err )
315
+
316
+ unique1 , err := os .ReadFile (filepath .Join (destDir1 , "unique.txt" ))
317
+ require .NoError (t , err )
318
+
319
+ // Second build: with checksum, should match cache even though this one sets commitHash and get same unique.txt
320
+ dockerfile2 := `
321
+ FROM alpine AS src
322
+ ADD --keep-git-dir --checksum=` + commitHash + ` ` + serverURL + `/.git#v0.0.2 /repo
323
+ RUN head -c 16 /dev/urandom | base64 > /repo/unique.txt
324
+
325
+ FROM scratch
326
+ COPY --from=src /repo/unique.txt /
327
+ `
328
+ dir2 := integration .Tmpdir (t ,
329
+ fstest .CreateFile ("Dockerfile" , []byte (dockerfile2 ), 0600 ),
330
+ )
331
+
332
+ destDir2 := t .TempDir ()
333
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
334
+ Exports : []client.ExportEntry {
335
+ {
336
+ Type : client .ExporterLocal ,
337
+ OutputDir : destDir2 ,
338
+ },
339
+ },
340
+ LocalMounts : map [string ]fsutil.FS {
341
+ dockerui .DefaultLocalNameDockerfile : dir2 ,
342
+ dockerui .DefaultLocalNameContext : dir2 ,
343
+ },
344
+ }, nil )
345
+ require .NoError (t , err )
346
+
347
+ unique2 , err := os .ReadFile (filepath .Join (destDir2 , "unique.txt" ))
348
+ require .NoError (t , err )
349
+
350
+ require .Equal (t , string (unique1 ), string (unique2 ), "cache should be matched and unique file content should be the same" )
351
+ }
352
+
247
353
func applyTemplate (tmpl string , x any ) (string , error ) {
248
354
var buf bytes.Buffer
249
355
parsed , err := template .New ("" ).Parse (tmpl )
0 commit comments