@@ -18,12 +18,16 @@ package executor
1818
1919import (
2020 "fmt"
21+ "net/http/httptest"
22+ "net/url"
2123 "os"
2224 "path/filepath"
2325 "strings"
2426 "testing"
2527 "time"
2628
29+ "github.com/google/go-containerregistry/pkg/registry"
30+
2731 "github.com/GoogleContainerTools/kaniko/pkg/config"
2832 "github.com/GoogleContainerTools/kaniko/pkg/constants"
2933 "github.com/GoogleContainerTools/kaniko/testutil"
@@ -37,8 +41,7 @@ func TestDoCacheProbe(t *testing.T) {
3741COPY foo/bar.txt copied/
3842`
3943 os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
40- // Populate the cache by doing an initial build
41- cacheDir := t .TempDir ()
44+ regCache := setupCacheRegistry (t )
4245 opts := & config.KanikoOptions {
4346 DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
4447 SrcContext : filepath .Join (testDir , "workspace" ),
@@ -49,10 +52,10 @@ COPY foo/bar.txt copied/
4952 },
5053 CacheCopyLayers : true ,
5154 CacheRunLayers : true ,
52- CacheRepo : "oci:/" + cacheDir ,
55+ CacheRepo : regCache + "/test" ,
5356 }
5457 _ , err := DoCacheProbe (opts )
55- if err == nil || ! strings .Contains (err .Error (), "not supported in fake build " ) {
58+ if err == nil || ! strings .Contains (err .Error (), "uncached command " ) {
5659 t .Errorf ("unexpected error, got %v" , err )
5760 }
5861 })
@@ -64,7 +67,7 @@ COPY foo/bar.txt copied/
6467COPY foo/bar.txt copied/
6568`
6669 os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
67- cacheDir := t . TempDir ( )
70+ regCache := setupCacheRegistry ( t )
6871 opts := & config.KanikoOptions {
6972 DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
7073 SrcContext : filepath .Join (testDir , "workspace" ),
@@ -75,8 +78,9 @@ COPY foo/bar.txt copied/
7578 },
7679 CacheCopyLayers : true ,
7780 CacheRunLayers : true ,
78- CacheRepo : "oci:/" + cacheDir ,
81+ CacheRepo : regCache + "/test" ,
7982 }
83+ // Populate the cache by doing an initial build
8084 _ , err := DoBuild (opts )
8185 testutil .CheckNoError (t , err )
8286 opts .Reproducible = true
@@ -91,18 +95,18 @@ COPY foo/bar.txt copied/
9195COPY foo/bar.txt copied/
9296`
9397 os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
94- cacheDir := t . TempDir ( )
98+ regCache := setupCacheRegistry ( t )
9599 opts := & config.KanikoOptions {
96100 DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
97101 SrcContext : filepath .Join (testDir , "workspace" ),
98- SnapshotMode : constants .SnapshotModeFull ,
102+ SnapshotMode : constants .SnapshotModeRedo ,
99103 Cache : true ,
100104 CacheOptions : config.CacheOptions {
101105 CacheTTL : time .Hour ,
102106 },
103107 CacheCopyLayers : true ,
104108 CacheRunLayers : true ,
105- CacheRepo : "oci:/" + cacheDir ,
109+ CacheRepo : regCache + "/test" ,
106110 }
107111 _ , err := DoBuild (opts )
108112 testutil .CheckNoError (t , err )
@@ -115,10 +119,61 @@ COPY foo/baz.txt copied/
115119`
116120 os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
117121 _ , err = DoCacheProbe (opts )
118- if err == nil || ! strings .Contains (err .Error (), "not supported in fake build " ) {
122+ if err == nil || ! strings .Contains (err .Error (), "uncached command " ) {
119123 t .Errorf ("unexpected error, got %v" , err )
120124 }
121125 })
126+
127+ t .Run ("MultiStage" , func (t * testing.T ) {
128+ t .Skip ("TODO: https://github.com/coder/envbuilder/issues/230" )
129+ testDir , fn := setupMultistageTests (t )
130+ defer fn ()
131+ dockerFile := `
132+ FROM scratch as first
133+ COPY foo/bam.txt copied/
134+ ENV test test
135+
136+ From scratch as second
137+ COPY --from=first copied/bam.txt output/bam.txt`
138+ os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
139+ regCache := setupCacheRegistry (t )
140+ opts := & config.KanikoOptions {
141+ DockerfilePath : filepath .Join (testDir , "workspace" , "Dockerfile" ),
142+ SrcContext : filepath .Join (testDir , "workspace" ),
143+ SnapshotMode : constants .SnapshotModeRedo ,
144+ Cache : true ,
145+ CacheOptions : config.CacheOptions {
146+ CacheTTL : time .Hour ,
147+ },
148+ CacheCopyLayers : true ,
149+ CacheRunLayers : true ,
150+ CacheRepo : regCache + "/test" ,
151+ }
152+ _ , err := DoBuild (opts )
153+ testutil .CheckNoError (t , err )
154+ os .WriteFile (filepath .Join (testDir , "workspace" , "Dockerfile" ), []byte (dockerFile ), 0755 )
155+ opts .Reproducible = true
156+ _ , err = DoCacheProbe (opts )
157+ testutil .CheckNoError (t , err )
158+ // Check Image has one layer bam.txt
159+ files , err := readDirectory (filepath .Join (testDir , "output" ))
160+ if err != nil {
161+ t .Fatal (err )
162+ }
163+ testutil .CheckDeepEqual (t , 1 , len (files ))
164+ testutil .CheckDeepEqual (t , files [0 ].Name (), "bam.txt" )
165+ })
166+ }
167+
168+ func setupCacheRegistry (t * testing.T ) string {
169+ t .Helper ()
170+ tempDir := t .TempDir ()
171+ testReg := registry .New (registry .WithBlobHandler (registry .NewDiskBlobHandler (tempDir )))
172+ regSrv := httptest .NewServer (testReg )
173+ t .Cleanup (func () { regSrv .Close () })
174+ regSrvURL , err := url .Parse (regSrv .URL )
175+ testutil .CheckNoError (t , err )
176+ return fmt .Sprintf ("localhost:%s" , regSrvURL .Port ())
122177}
123178
124179func setupCacheProbeTests (t * testing.T ) (string , func ()) {
0 commit comments