Browse code

Small cleanup in `integration-cli/docker_utils.go` 👼

- Move *one-shot* (one use) function where it is actually used (easier
to know what's going on).
- Remove `pullImageIfNotExist` function as it might be an artifact
from way back. We don't need it as we already have frozen/loaded
image of busybox.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2016/12/29 07:00:32
Showing 4 changed files
... ...
@@ -6,6 +6,8 @@ import (
6 6
 	"fmt"
7 7
 	"os"
8 8
 	"path/filepath"
9
+	"strconv"
10
+	"strings"
9 11
 
10 12
 	"github.com/docker/docker/pkg/integration/checker"
11 13
 	"github.com/docker/docker/pkg/system"
... ...
@@ -37,3 +39,20 @@ func (s *DockerSuite) TestCpCheckDestOwnership(c *check.C) {
37 37
 	c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Copied file not owned by container root UID"))
38 38
 	c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Copied file not owned by container root GID"))
39 39
 }
40
+
41
+func getRootUIDGID() (int, int, error) {
42
+	uidgid := strings.Split(filepath.Base(dockerBasePath), ".")
43
+	if len(uidgid) == 1 {
44
+		//user namespace remapping is not turned on; return 0
45
+		return 0, 0, nil
46
+	}
47
+	uid, err := strconv.Atoi(uidgid[0])
48
+	if err != nil {
49
+		return 0, 0, err
50
+	}
51
+	gid, err := strconv.Atoi(uidgid[1])
52
+	if err != nil {
53
+		return 0, 0, err
54
+	}
55
+	return uid, gid, nil
56
+}
... ...
@@ -224,6 +224,16 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithIncreasedBasesize(c *check.C) {
224 224
 	s.d.Stop(c)
225 225
 }
226 226
 
227
+func convertBasesize(basesizeBytes int64) (int64, error) {
228
+	basesize := units.HumanSize(float64(basesizeBytes))
229
+	basesize = strings.Trim(basesize, " ")[:len(basesize)-3]
230
+	basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
231
+	if err != nil {
232
+		return 0, err
233
+	}
234
+	return int64(basesizeFloat) * 1024 * 1024 * 1024, nil
235
+}
236
+
227 237
 // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
228 238
 // no longer has an IP associated, we should gracefully handle that case and associate
229 239
 // an IP with it rather than fail daemon start
... ...
@@ -12,14 +12,6 @@ import (
12 12
 
13 13
 // tagging a named image in a new unprefixed repo should work
14 14
 func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) {
15
-	// Don't attempt to pull on Windows as not in hub. It's installed
16
-	// as an image through .ensure-frozen-images-windows
17
-	if daemonPlatform != "windows" {
18
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
19
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
20
-		}
21
-	}
22
-
23 15
 	dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz")
24 16
 }
25 17
 
... ...
@@ -53,14 +45,6 @@ func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
53 53
 
54 54
 // ensure we allow the use of valid tags
55 55
 func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
56
-	// Don't attempt to pull on Windows as not in hub. It's installed
57
-	// as an image through .ensure-frozen-images-windows
58
-	if daemonPlatform != "windows" {
59
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
60
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
61
-		}
62
-	}
63
-
64 56
 	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t", "HOSTNAME.DOMAIN.COM:443/foo/bar"}
65 57
 
66 58
 	for _, repo := range validRepos {
... ...
@@ -75,25 +59,10 @@ func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
75 75
 
76 76
 // tag an image with an existed tag name without -f option should work
77 77
 func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) {
78
-	// Don't attempt to pull on Windows as not in hub. It's installed
79
-	// as an image through .ensure-frozen-images-windows
80
-	if daemonPlatform != "windows" {
81
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
82
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
83
-		}
84
-	}
85
-
86 78
 	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
87 79
 }
88 80
 
89 81
 func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
90
-	// Don't attempt to pull on Windows as not in hub. It's installed
91
-	// as an image through .ensure-frozen-images-windows
92
-	if daemonPlatform != "windows" {
93
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
94
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
95
-		}
96
-	}
97 82
 	// test repository name begin with '-'
98 83
 	out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test")
99 84
 	c.Assert(err, checker.NotNil, check.Commentf(out))
... ...
@@ -150,13 +119,6 @@ func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
150 150
 
151 151
 // ensure tags can not match digests
152 152
 func (s *DockerSuite) TestTagMatchesDigest(c *check.C) {
153
-	// Don't attempt to pull on Windows as not in hub. It's installed
154
-	// as an image through .ensure-frozen-images-windows
155
-	if daemonPlatform != "windows" {
156
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
157
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
158
-		}
159
-	}
160 153
 	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
161 154
 	// test setting tag fails
162 155
 	_, _, err := dockerCmdWithError("tag", "busybox:latest", digest)
... ...
@@ -171,14 +133,6 @@ func (s *DockerSuite) TestTagMatchesDigest(c *check.C) {
171 171
 }
172 172
 
173 173
 func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) {
174
-	// Don't attempt to pull on Windows as not in hub. It's installed
175
-	// as an image through .ensure-frozen-images-windows
176
-	if daemonPlatform != "windows" {
177
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
178
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
179
-		}
180
-	}
181
-
182 174
 	// test setting tag fails
183 175
 	_, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag")
184 176
 	if err == nil {
... ...
@@ -188,14 +142,6 @@ func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) {
188 188
 
189 189
 // ensure tags cannot create ambiguity with image ids
190 190
 func (s *DockerSuite) TestTagTruncationAmbiguity(c *check.C) {
191
-	//testRequires(c, DaemonIsLinux)
192
-	// Don't attempt to pull on Windows as not in hub. It's installed
193
-	// as an image through .ensure-frozen-images-windows
194
-	if daemonPlatform != "windows" {
195
-		if err := pullImageIfNotExist("busybox:latest"); err != nil {
196
-			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
197
-		}
198
-	}
199 191
 	imageID, err := buildImage("notbusybox:latest",
200 192
 		`FROM busybox
201 193
 		MAINTAINER dockerio`,
... ...
@@ -31,7 +31,6 @@ import (
31 31
 	icmd "github.com/docker/docker/pkg/integration/cmd"
32 32
 	"github.com/docker/docker/pkg/ioutils"
33 33
 	"github.com/docker/docker/pkg/stringutils"
34
-	units "github.com/docker/go-units"
35 34
 	"github.com/go-check/check"
36 35
 )
37 36
 
... ...
@@ -89,16 +88,6 @@ func init() {
89 89
 	isolation = info.Isolation
90 90
 }
91 91
 
92
-func convertBasesize(basesizeBytes int64) (int64, error) {
93
-	basesize := units.HumanSize(float64(basesizeBytes))
94
-	basesize = strings.Trim(basesize, " ")[:len(basesize)-3]
95
-	basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
96
-	if err != nil {
97
-		return 0, err
98
-	}
99
-	return int64(basesizeFloat) * 1024 * 1024 * 1024, nil
100
-}
101
-
102 92
 func daemonHost() string {
103 93
 	daemonURLStr := "unix://" + opts.DefaultUnixSocket
104 94
 	if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" {
... ...
@@ -370,22 +359,6 @@ func deleteImages(images ...string) error {
370 370
 	return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error
371 371
 }
372 372
 
373
-func imageExists(image string) error {
374
-	return icmd.RunCommand(dockerBinary, "inspect", image).Error
375
-}
376
-
377
-func pullImageIfNotExist(image string) error {
378
-	if err := imageExists(image); err != nil {
379
-		pullCmd := exec.Command(dockerBinary, "pull", image)
380
-		_, exitCode, err := runCommandWithOutput(pullCmd)
381
-
382
-		if err != nil || exitCode != 0 {
383
-			return fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err)
384
-		}
385
-	}
386
-	return nil
387
-}
388
-
389 373
 func dockerCmdWithError(args ...string) (string, int, error) {
390 374
 	if err := validateArgs(args...); err != nil {
391 375
 		return "", 0, err
... ...
@@ -441,18 +414,6 @@ func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error
441 441
 	return result.Combined(), result.ExitCode, result.Error
442 442
 }
443 443
 
444
-// execute a docker command in a directory with a timeout
445
-func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) *icmd.Result {
446
-	if err := validateArgs(args...); err != nil {
447
-		return &icmd.Result{Error: err}
448
-	}
449
-	return icmd.RunCmd(icmd.Cmd{
450
-		Command: binaryWithArgs(args...),
451
-		Timeout: timeout,
452
-		Dir:     path,
453
-	})
454
-}
455
-
456 444
 // validateArgs is a checker to ensure tests are not running commands which are
457 445
 // not supported on platforms. Specifically on Windows this is 'busybox top'.
458 446
 func validateArgs(args ...string) error {
... ...
@@ -853,36 +814,6 @@ func getIDByName(name string) (string, error) {
853 853
 	return inspectFieldWithError(name, "Id")
854 854
 }
855 855
 
856
-// getContainerState returns the exit code of the container
857
-// and true if it's running
858
-// the exit code should be ignored if it's running
859
-func getContainerState(c *check.C, id string) (int, bool, error) {
860
-	var (
861
-		exitStatus int
862
-		running    bool
863
-	)
864
-	out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id)
865
-	if exitCode != 0 {
866
-		return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out)
867
-	}
868
-
869
-	out = strings.Trim(out, "\n")
870
-	splitOutput := strings.Split(out, " ")
871
-	if len(splitOutput) != 2 {
872
-		return 0, false, fmt.Errorf("failed to get container state: output is broken")
873
-	}
874
-	if splitOutput[0] == "true" {
875
-		running = true
876
-	}
877
-	if n, err := strconv.Atoi(splitOutput[1]); err == nil {
878
-		exitStatus = n
879
-	} else {
880
-		return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer")
881
-	}
882
-
883
-	return exitStatus, running, nil
884
-}
885
-
886 856
 func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd {
887 857
 	return daemon.BuildImageCmdWithHost(dockerBinary, name, dockerfile, "", useCache, buildFlags...)
888 858
 }
... ...
@@ -1264,28 +1195,6 @@ func createTmpFile(c *check.C, content string) string {
1264 1264
 	return filename
1265 1265
 }
1266 1266
 
1267
-func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) {
1268
-	args := []string{"--host", socket}
1269
-	buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache)
1270
-	out, exitCode, err := runCommandWithOutput(buildCmd)
1271
-	if err != nil || exitCode != 0 {
1272
-		return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err)
1273
-	}
1274
-	return out, nil
1275
-}
1276
-
1277
-func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *exec.Cmd {
1278
-	args = append(args, []string{"-D", "build", "-t", name}...)
1279
-	if !useCache {
1280
-		args = append(args, "--no-cache")
1281
-	}
1282
-	args = append(args, "-")
1283
-	buildCmd := exec.Command(dockerBinary, args...)
1284
-	buildCmd.Stdin = strings.NewReader(dockerfile)
1285
-	return buildCmd
1286
-
1287
-}
1288
-
1289 1267
 func waitForContainer(contID string, args ...string) error {
1290 1268
 	args = append([]string{dockerBinary, "run", "--name", contID}, args...)
1291 1269
 	result := icmd.RunCmd(icmd.Cmd{Command: args})
... ...
@@ -1346,23 +1255,6 @@ func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string)
1346 1346
 	return dockerCmd(c, args...)
1347 1347
 }
1348 1348
 
1349
-func getRootUIDGID() (int, int, error) {
1350
-	uidgid := strings.Split(filepath.Base(dockerBasePath), ".")
1351
-	if len(uidgid) == 1 {
1352
-		//user namespace remapping is not turned on; return 0
1353
-		return 0, 0, nil
1354
-	}
1355
-	uid, err := strconv.Atoi(uidgid[0])
1356
-	if err != nil {
1357
-		return 0, 0, err
1358
-	}
1359
-	gid, err := strconv.Atoi(uidgid[1])
1360
-	if err != nil {
1361
-		return 0, 0, err
1362
-	}
1363
-	return uid, gid, nil
1364
-}
1365
-
1366 1349
 // minimalBaseImage returns the name of the minimal base image for the current
1367 1350
 // daemon platform.
1368 1351
 func minimalBaseImage() string {