@@ -47,7 +47,7 @@ func TestExtender(t *testing.T) {
4747 testImageDockerContext := filepath .Join ("testdata" , "extender" )
4848 extendTest = NewPhaseTest (t , "extender" , testImageDockerContext )
4949 extendTest .Start (t )
50- defer extendTest .Stop (t )
50+ t . Cleanup ( func () { extendTest .Stop (t ) } )
5151
5252 extendImage = extendTest .testImageRef
5353 extenderPath = extendTest .containerBinaryPath
@@ -162,7 +162,15 @@ func testExtenderFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
162162 t .Log ("cleans the kaniko directory" )
163163 fis , err := os .ReadDir (kanikoDir )
164164 h .AssertNil (t , err )
165- h .AssertEq (t , len (fis ), 1 ) // 1: /kaniko/cache
165+ var expectedFiles = []string {"cache" }
166+ var actualFiles []string
167+ for _ , fi := range fis {
168+ if fi .Name () == "layers" {
169+ continue
170+ }
171+ actualFiles = append (actualFiles , fi .Name ())
172+ }
173+ h .AssertEq (t , actualFiles , expectedFiles )
166174
167175 t .Log ("second build extends the build image by pulling from the cache directory" )
168176 secondOutput := h .DockerRunWithCombinedOutput (t ,
@@ -220,7 +228,22 @@ func testExtenderFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
220228 t .Log ("cleans the kaniko directory" )
221229 caches , err := os .ReadDir (kanikoDir )
222230 h .AssertNil (t , err )
223- h .AssertEq (t , len (caches ), 1 ) // 1: /kaniko/cache
231+ var expectedFiles = []string {"cache" }
232+ var actualFiles []string
233+ for _ , fi := range caches {
234+ if fi .Name () == "layers" {
235+ continue
236+ }
237+ actualFiles = append (actualFiles , fi .Name ())
238+ }
239+ if len (actualFiles ) != 1 || actualFiles [0 ] != "cache" {
240+ var names []string
241+ for _ , fi := range caches {
242+ names = append (names , fi .Name ())
243+ }
244+ t .Logf ("kanikoDir contents (1): %v" , names )
245+ }
246+ h .AssertEq (t , actualFiles , expectedFiles )
224247
225248 t .Log ("second build extends the build image by pulling from the cache directory" )
226249 secondOutput := h .DockerRunWithCombinedOutput (t ,
@@ -241,7 +264,21 @@ func testExtenderFunc(platformAPI string) func(t *testing.T, when spec.G, it spe
241264 t .Log ("cleans the kaniko directory" )
242265 caches , err = os .ReadDir (kanikoDir )
243266 h .AssertNil (t , err )
244- h .AssertEq (t , len (caches ), 1 ) // 1: /kaniko/cache
267+ actualFiles = nil
268+ for _ , fi := range caches {
269+ if fi .Name () == "layers" {
270+ continue
271+ }
272+ actualFiles = append (actualFiles , fi .Name ())
273+ }
274+ if len (actualFiles ) != 1 || actualFiles [0 ] != "cache" {
275+ var names []string
276+ for _ , fi := range caches {
277+ names = append (names , fi .Name ())
278+ }
279+ t .Logf ("kanikoDir contents (2): %v" , names )
280+ }
281+ h .AssertEq (t , actualFiles , expectedFiles )
245282 })
246283 })
247284 })
@@ -254,20 +291,41 @@ func assertExpectedImage(t *testing.T, imagePath, platformAPI string) {
254291 configFile , err := image .ConfigFile ()
255292 h .AssertNil (t , err )
256293 h .AssertEq (t , configFile .Config .Labels ["io.buildpacks.rebasable" ], "false" )
257- layers , err : = image .Layers ()
294+ _ , err = image .Layers ()
258295 h .AssertNil (t , err )
259296 history := configFile .History
260297 h .AssertEq (t , len (history ), len (configFile .RootFS .DiffIDs ))
298+ var expectedLayers []string
261299 if api .MustParse (platformAPI ).AtLeast ("0.13" ) {
262- h .AssertEq (t , len (layers ), 7 ) // base (3), curl (2), tree (2)
263- h .AssertEq (t , history [3 ].CreatedBy , "Layer: 'RUN apt-get update && apt-get install -y curl', Created by extension: curl" )
264- h .AssertEq (t , history [4 ].CreatedBy , "Layer: 'COPY run-file /', Created by extension: curl" )
265- h .AssertEq (t , history [5 ].CreatedBy , "Layer: 'RUN apt-get update && apt-get install -y tree', Created by extension: tree" )
266- h .AssertEq (t , history [6 ].CreatedBy , "Layer: 'COPY shared-file /shared-run', Created by extension: tree" )
300+ expectedLayers = []string {
301+ "Layer: 'RUN apt-get update && apt-get install -y curl', Created by extension: curl" ,
302+ "Layer: 'COPY run-file /', Created by extension: curl" ,
303+ "Layer: 'RUN apt-get update && apt-get install -y tree', Created by extension: tree" ,
304+ "Layer: 'COPY shared-file /shared-run', Created by extension: tree" ,
305+ }
267306 } else {
268- h .AssertEq (t , len (layers ), 5 ) // base (3), curl (1), tree (1)
269- h .AssertEq (t , history [3 ].CreatedBy , "Layer: 'RUN apt-get update && apt-get install -y curl', Created by extension: curl" )
270- h .AssertEq (t , history [4 ].CreatedBy , "Layer: 'RUN apt-get update && apt-get install -y tree', Created by extension: tree" )
307+ expectedLayers = []string {
308+ "Layer: 'RUN apt-get update && apt-get install -y curl', Created by extension: curl" ,
309+ "Layer: 'RUN apt-get update && apt-get install -y tree', Created by extension: tree" ,
310+ }
311+ }
312+
313+ lastIndex := - 1
314+ for _ , expected := range expectedLayers {
315+ found := false
316+ for i , hItem := range history {
317+ if hItem .CreatedBy == expected {
318+ if i <= lastIndex {
319+ t .Errorf ("expected layer %q to appear after index %d" , expected , lastIndex )
320+ }
321+ lastIndex = i
322+ found = true
323+ break
324+ }
325+ }
326+ if ! found {
327+ t .Errorf ("expected layer %q not found in history" , expected )
328+ }
271329 }
272330}
273331
0 commit comments