Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 14e1449

Browse files
authored
Merge pull request #898 from cvgw/u/cvgw/improve-integration-test-runner
Only build required docker images for integration tests
2 parents f8507eb + 05447f1 commit 14e1449

1 file changed

Lines changed: 69 additions & 59 deletions

File tree

integration/integration_test.go

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232

3333
"github.com/google/go-containerregistry/pkg/name"
3434
"github.com/google/go-containerregistry/pkg/v1/daemon"
35-
"golang.org/x/sync/errgroup"
3635

3736
"github.com/GoogleContainerTools/kaniko/pkg/timing"
3837
"github.com/GoogleContainerTools/kaniko/pkg/util"
@@ -42,40 +41,6 @@ import (
4241
var config = initGCPConfig()
4342
var imageBuilder *DockerFileBuilder
4443

45-
type gcpConfig struct {
46-
gcsBucket string
47-
imageRepo string
48-
onbuildBaseImage string
49-
hardlinkBaseImage string
50-
}
51-
52-
type imageDetails struct {
53-
name string
54-
numLayers int
55-
digest string
56-
}
57-
58-
func (i imageDetails) String() string {
59-
return fmt.Sprintf("Image: [%s] Digest: [%s] Number of Layers: [%d]", i.name, i.digest, i.numLayers)
60-
}
61-
62-
func initGCPConfig() *gcpConfig {
63-
var c gcpConfig
64-
flag.StringVar(&c.gcsBucket, "bucket", "gs://kaniko-test-bucket", "The gcs bucket argument to uploaded the tar-ed contents of the `integration` dir to.")
65-
flag.StringVar(&c.imageRepo, "repo", "gcr.io/kaniko-test", "The (docker) image repo to build and push images to during the test. `gcloud` must be authenticated with this repo.")
66-
flag.Parse()
67-
68-
if c.gcsBucket == "" || c.imageRepo == "" {
69-
log.Fatalf("You must provide a gcs bucket (\"%s\" was provided) and a docker repo (\"%s\" was provided)", c.gcsBucket, c.imageRepo)
70-
}
71-
if !strings.HasSuffix(c.imageRepo, "/") {
72-
c.imageRepo = c.imageRepo + "/"
73-
}
74-
c.onbuildBaseImage = c.imageRepo + "onbuild-base:latest"
75-
c.hardlinkBaseImage = c.imageRepo + "hardlink-base:latest"
76-
return &c
77-
}
78-
7944
const (
8045
daemonPrefix = "daemon://"
8146
dockerfilesPath = "dockerfiles"
@@ -102,19 +67,6 @@ const (
10267
]`
10368
)
10469

105-
func meetsRequirements() bool {
106-
requiredTools := []string{"container-diff", "gsutil"}
107-
hasRequirements := true
108-
for _, tool := range requiredTools {
109-
_, err := exec.LookPath(tool)
110-
if err != nil {
111-
fmt.Printf("You must have %s installed and on your PATH\n", tool)
112-
hasRequirements = false
113-
}
114-
}
115-
return hasRequirements
116-
}
117-
11870
func TestMain(m *testing.M) {
11971
if !meetsRequirements() {
12072
fmt.Println("Missing required tools")
@@ -188,19 +140,9 @@ func TestMain(m *testing.M) {
188140
}
189141
imageBuilder = NewDockerFileBuilder(dockerfiles)
190142

191-
g := errgroup.Group{}
192-
for dockerfile := range imageBuilder.FilesBuilt {
193-
df := dockerfile
194-
g.Go(func() error {
195-
return imageBuilder.BuildImage(config.imageRepo, config.gcsBucket, dockerfilesPath, df)
196-
})
197-
}
198-
if err := g.Wait(); err != nil {
199-
fmt.Printf("Error building images: %s", err)
200-
os.Exit(1)
201-
}
202143
os.Exit(m.Run())
203144
}
145+
204146
func TestRun(t *testing.T) {
205147
for dockerfile := range imageBuilder.FilesBuilt {
206148
t.Run("test_"+dockerfile, func(t *testing.T) {
@@ -212,6 +154,9 @@ func TestRun(t *testing.T) {
212154
if _, ok := imageBuilder.TestCacheDockerfiles[dockerfile]; ok {
213155
t.SkipNow()
214156
}
157+
158+
imageBuilder.FilesBuilt[dockerfile] = buildImage(t, dockerfile, imageBuilder)
159+
215160
dockerImage := GetDockerImage(config.imageRepo, dockerfile)
216161
kanikoImage := GetKanikoImage(config.imageRepo, dockerfile)
217162

@@ -329,10 +274,14 @@ func TestLayers(t *testing.T) {
329274
for dockerfile := range imageBuilder.FilesBuilt {
330275
t.Run("test_layer_"+dockerfile, func(t *testing.T) {
331276
dockerfile := dockerfile
277+
332278
t.Parallel()
333279
if _, ok := imageBuilder.DockerfilesToIgnore[dockerfile]; ok {
334280
t.SkipNow()
335281
}
282+
283+
imageBuilder.FilesBuilt[dockerfile] = buildImage(t, dockerfile, imageBuilder)
284+
336285
// Pull the kaniko image
337286
dockerImage := GetDockerImage(config.imageRepo, dockerfile)
338287
kanikoImage := GetKanikoImage(config.imageRepo, dockerfile)
@@ -348,6 +297,20 @@ func TestLayers(t *testing.T) {
348297
}
349298
}
350299

300+
func buildImage(t *testing.T, dockerfile string, imageBuilder *DockerFileBuilder) bool {
301+
if imageBuilder.FilesBuilt[dockerfile] {
302+
return true
303+
}
304+
305+
if err := imageBuilder.BuildImage(
306+
config.imageRepo, config.gcsBucket, dockerfilesPath, dockerfile,
307+
); err != nil {
308+
t.Errorf("Error building image: %s", err)
309+
t.FailNow()
310+
}
311+
return true
312+
}
313+
351314
// Build each image with kaniko twice, and then make sure they're exactly the same
352315
func TestCache(t *testing.T) {
353316
populateVolumeCache()
@@ -522,3 +485,50 @@ func logBenchmarks(benchmark string) error {
522485
}
523486
return nil
524487
}
488+
489+
type gcpConfig struct {
490+
gcsBucket string
491+
imageRepo string
492+
onbuildBaseImage string
493+
hardlinkBaseImage string
494+
}
495+
496+
type imageDetails struct {
497+
name string
498+
numLayers int
499+
digest string
500+
}
501+
502+
func (i imageDetails) String() string {
503+
return fmt.Sprintf("Image: [%s] Digest: [%s] Number of Layers: [%d]", i.name, i.digest, i.numLayers)
504+
}
505+
506+
func initGCPConfig() *gcpConfig {
507+
var c gcpConfig
508+
flag.StringVar(&c.gcsBucket, "bucket", "gs://kaniko-test-bucket", "The gcs bucket argument to uploaded the tar-ed contents of the `integration` dir to.")
509+
flag.StringVar(&c.imageRepo, "repo", "gcr.io/kaniko-test", "The (docker) image repo to build and push images to during the test. `gcloud` must be authenticated with this repo.")
510+
flag.Parse()
511+
512+
if c.gcsBucket == "" || c.imageRepo == "" {
513+
log.Fatalf("You must provide a gcs bucket (\"%s\" was provided) and a docker repo (\"%s\" was provided)", c.gcsBucket, c.imageRepo)
514+
}
515+
if !strings.HasSuffix(c.imageRepo, "/") {
516+
c.imageRepo = c.imageRepo + "/"
517+
}
518+
c.onbuildBaseImage = c.imageRepo + "onbuild-base:latest"
519+
c.hardlinkBaseImage = c.imageRepo + "hardlink-base:latest"
520+
return &c
521+
}
522+
523+
func meetsRequirements() bool {
524+
requiredTools := []string{"container-diff", "gsutil"}
525+
hasRequirements := true
526+
for _, tool := range requiredTools {
527+
_, err := exec.LookPath(tool)
528+
if err != nil {
529+
fmt.Printf("You must have %s installed and on your PATH\n", tool)
530+
hasRequirements = false
531+
}
532+
}
533+
return hasRequirements
534+
}

0 commit comments

Comments
 (0)