Browse code

integration-cli: don't build -test images if they already exist

There's no need to try to re-build the test images if they already
exist. This change makes basically no difference to the upstream
integration test-suite running, but for users who want to run the
integration-cli suite on a host machine (such as distributions doing
tests) this change allows images to be pre-loaded such that compilers
aren't needed on the test machine.

However, this does remove the accidental re-compilation of nnp-test, as
well as handling errors far more cleanly (previously if an error
occurred during a test build, further tests won't attempt to rebuild
it).

Signed-off-by: Aleksa Sarai <asarai@suse.de>

Aleksa Sarai authored on 2019/03/12 16:37:31
Showing 2 changed files
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"path/filepath"
9 9
 	"runtime"
10 10
 	"strings"
11
-	"sync"
12 11
 
13 12
 	"github.com/docker/docker/integration-cli/checker"
14 13
 	"github.com/docker/docker/internal/test/fixtures/load"
... ...
@@ -24,17 +23,13 @@ type logT interface {
24 24
 	Logf(string, ...interface{})
25 25
 }
26 26
 
27
-var ensureSyscallTestOnce sync.Once
28
-
29 27
 func ensureSyscallTest(c *check.C) {
30
-	var doIt bool
31
-	ensureSyscallTestOnce.Do(func() {
32
-		doIt = true
33
-	})
34
-	if !doIt {
28
+	defer testEnv.ProtectImage(c, "syscall-test:latest")
29
+
30
+	// If the image already exists, there's nothing left to do.
31
+	if testEnv.HasExistingImage(c, "syscall-test:latest") {
35 32
 		return
36 33
 	}
37
-	defer testEnv.ProtectImage(c, "syscall-test:latest")
38 34
 
39 35
 	// if no match, must build in docker, which is significantly slower
40 36
 	// (slower mostly because of the vfs graphdriver)
... ...
@@ -93,6 +88,14 @@ func ensureSyscallTestBuild(c *check.C) {
93 93
 
94 94
 func ensureNNPTest(c *check.C) {
95 95
 	defer testEnv.ProtectImage(c, "nnp-test:latest")
96
+
97
+	// If the image already exists, there's nothing left to do.
98
+	if testEnv.HasExistingImage(c, "nnp-test:latest") {
99
+		return
100
+	}
101
+
102
+	// if no match, must build in docker, which is significantly slower
103
+	// (slower mostly because of the vfs graphdriver)
96 104
 	if testEnv.OSType != runtime.GOOS {
97 105
 		ensureNNPTestBuild(c)
98 106
 		return
... ...
@@ -8,9 +8,12 @@ import (
8 8
 	"strings"
9 9
 
10 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/api/types/filters"
11 12
 	"github.com/docker/docker/client"
13
+	"github.com/docker/docker/internal/test"
12 14
 	"github.com/docker/docker/internal/test/fixtures/load"
13 15
 	"github.com/pkg/errors"
16
+	"gotest.tools/assert"
14 17
 )
15 18
 
16 19
 // Execution contains information about the current test execution and daemon
... ...
@@ -151,6 +154,26 @@ func (e *Execution) IsUserNamespace() bool {
151 151
 	return root != ""
152 152
 }
153 153
 
154
+// HasExistingImage checks whether there is an image with the given reference.
155
+// Note that this is done by filtering and then checking whether there were any
156
+// results -- so ambiguous references might result in false-positives.
157
+func (e *Execution) HasExistingImage(t testingT, reference string) bool {
158
+	if ht, ok := t.(test.HelperT); ok {
159
+		ht.Helper()
160
+	}
161
+	client := e.APIClient()
162
+	filter := filters.NewArgs()
163
+	filter.Add("dangling", "false")
164
+	filter.Add("reference", reference)
165
+	imageList, err := client.ImageList(context.Background(), types.ImageListOptions{
166
+		All:     true,
167
+		Filters: filter,
168
+	})
169
+	assert.NilError(t, err, "failed to list images")
170
+
171
+	return len(imageList) > 0
172
+}
173
+
154 174
 // EnsureFrozenImagesLinux loads frozen test images into the daemon
155 175
 // if they aren't already loaded
156 176
 func EnsureFrozenImagesLinux(testEnv *Execution) error {