Skip to content

Commit cc8e2bf

Browse files
committed
Update acceptance tests for Moby
1 parent 1119017 commit cc8e2bf

File tree

9 files changed

+80
-22
lines changed

9 files changed

+80
-22
lines changed

acceptance/acceptance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func TestVersion(t *testing.T) {
2929
var err error
3030
buildDir, err = os.MkdirTemp("", "lifecycle-acceptance")
3131
h.AssertNil(t, err)
32-
defer func() {
32+
t.Cleanup(func() {
3333
h.AssertNil(t, os.RemoveAll(buildDir))
34-
}()
34+
})
3535

3636
outDir := filepath.Join(buildDir, fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH), "lifecycle")
3737
h.AssertNil(t, os.MkdirAll(outDir, 0755))

acceptance/analyzer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestAnalyzer(t *testing.T) {
3232
testImageDockerContext := filepath.Join("testdata", "analyzer")
3333
analyzeTest = NewPhaseTest(t, "analyzer", testImageDockerContext)
3434
analyzeTest.Start(t)
35-
defer analyzeTest.Stop(t)
35+
t.Cleanup(func() { analyzeTest.Stop(t) })
3636

3737
analyzeImage = analyzeTest.testImageRef
3838
analyzerPath = analyzeTest.containerBinaryPath

acceptance/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestBuilder(t *testing.T) {
4848
"-f", filepath.Join(builderDockerContext, dockerfileName),
4949
),
5050
)
51-
defer h.DockerImageRemoveSafe(t, builderImage)
51+
t.Cleanup(func() { h.DockerImageRemoveSafe(t, builderImage) })
5252

5353
spec.Run(t, "acceptance-builder", testBuilder, spec.Parallel(), spec.Report(report.Terminal{}))
5454
}

acceptance/creator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestCreator(t *testing.T) {
3232
testImageDockerContext := filepath.Join("testdata", "creator")
3333
createTest = NewPhaseTest(t, "creator", testImageDockerContext)
3434
createTest.Start(t)
35-
defer createTest.Stop(t)
35+
t.Cleanup(func() { createTest.Stop(t) })
3636

3737
createImage = createTest.testImageRef
3838
creatorPath = createTest.containerBinaryPath

acceptance/detector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestDetector(t *testing.T) {
4747
detectDockerContext,
4848
h.WithArgs("--build-arg", fmt.Sprintf("cnb_platform_api=%s", api.Platform.Latest())),
4949
)
50-
defer h.DockerImageRemoveSafe(t, detectImage)
50+
t.Cleanup(func() { h.DockerImageRemoveSafe(t, detectImage) })
5151

5252
for _, platformAPI := range api.Platform.Supported {
5353
if platformAPI.LessThan("0.12") {

acceptance/exporter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestExporter(t *testing.T) {
4848
exportTest = NewPhaseTest(t, "exporter", testImageDockerContext)
4949

5050
exportTest.Start(t, updateTOMLFixturesWithTestRegistry)
51-
defer exportTest.Stop(t)
51+
t.Cleanup(func() { exportTest.Stop(t) })
5252

5353
exportImage = exportTest.testImageRef
5454
exporterPath = exportTest.containerBinaryPath

acceptance/extender_test.go

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

acceptance/launcher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestLauncher(t *testing.T) {
2828
phaseTest.containerBinaryDir = containerBinaryDir
2929
}
3030
launchTest.Start(t, withCustomContainerBinaryDir)
31-
defer launchTest.Stop(t)
31+
t.Cleanup(func() { launchTest.Stop(t) })
3232

3333
launchImage = launchTest.testImageRef
3434
launcherPath = launchTest.containerBinaryPath

acceptance/restorer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestRestorer(t *testing.T) {
3434
testImageDockerContext := filepath.Join("testdata", "restorer")
3535
restoreTest = NewPhaseTest(t, "restorer", testImageDockerContext)
3636
restoreTest.Start(t, updateTOMLFixturesWithTestRegistry)
37-
defer restoreTest.Stop(t)
37+
t.Cleanup(func() { restoreTest.Stop(t) })
3838

3939
restoreImage = restoreTest.testImageRef
4040
restorerPath = restoreTest.containerBinaryPath

0 commit comments

Comments
 (0)