From b2320d121ca735a3879b40d493018b2e3a1a52da Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 28 Dec 2016 23:00:32 +0100 Subject: [PATCH] =?UTF-8?q?Small=20cleanup=20in=20`integration-cli/docker?= =?UTF-8?q?=5Futils.go`=20=F0=9F=91=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../docker_cli_cp_to_container_unix_test.go | 19 +++ integration-cli/docker_cli_daemon_test.go | 10 ++ integration-cli/docker_cli_tag_test.go | 54 --------- integration-cli/docker_utils.go | 108 ------------------ 4 files changed, 29 insertions(+), 162 deletions(-) diff --git a/integration-cli/docker_cli_cp_to_container_unix_test.go b/integration-cli/docker_cli_cp_to_container_unix_test.go index 45d85ba5d1..97890a9cbb 100644 --- a/integration-cli/docker_cli_cp_to_container_unix_test.go +++ b/integration-cli/docker_cli_cp_to_container_unix_test.go @@ -6,6 +6,8 @@ import ( "fmt" "os" "path/filepath" + "strconv" + "strings" "github.com/docker/docker/pkg/integration/checker" "github.com/docker/docker/pkg/system" @@ -37,3 +39,20 @@ func (s *DockerSuite) TestCpCheckDestOwnership(c *check.C) { c.Assert(stat.UID(), checker.Equals, uint32(uid), check.Commentf("Copied file not owned by container root UID")) c.Assert(stat.GID(), checker.Equals, uint32(gid), check.Commentf("Copied file not owned by container root GID")) } + +func getRootUIDGID() (int, int, error) { + uidgid := strings.Split(filepath.Base(dockerBasePath), ".") + if len(uidgid) == 1 { + //user namespace remapping is not turned on; return 0 + return 0, 0, nil + } + uid, err := strconv.Atoi(uidgid[0]) + if err != nil { + return 0, 0, err + } + gid, err := strconv.Atoi(uidgid[1]) + if err != nil { + return 0, 0, err + } + return uid, gid, nil +} diff --git a/integration-cli/docker_cli_daemon_test.go b/integration-cli/docker_cli_daemon_test.go index bb9cec09ef..23ed03b3b8 100644 --- a/integration-cli/docker_cli_daemon_test.go +++ b/integration-cli/docker_cli_daemon_test.go @@ -224,6 +224,16 @@ func (s *DockerDaemonSuite) TestDaemonRestartWithIncreasedBasesize(c *check.C) { s.d.Stop(c) } +func convertBasesize(basesizeBytes int64) (int64, error) { + basesize := units.HumanSize(float64(basesizeBytes)) + basesize = strings.Trim(basesize, " ")[:len(basesize)-3] + basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64) + if err != nil { + return 0, err + } + return int64(basesizeFloat) * 1024 * 1024 * 1024, nil +} + // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and // no longer has an IP associated, we should gracefully handle that case and associate // an IP with it rather than fail daemon start diff --git a/integration-cli/docker_cli_tag_test.go b/integration-cli/docker_cli_tag_test.go index b7d2b1dfe6..60b89dca04 100644 --- a/integration-cli/docker_cli_tag_test.go +++ b/integration-cli/docker_cli_tag_test.go @@ -12,14 +12,6 @@ import ( // tagging a named image in a new unprefixed repo should work func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) { - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } - dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz") } @@ -53,14 +45,6 @@ func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) { // ensure we allow the use of valid tags func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) { - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } - validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t", "HOSTNAME.DOMAIN.COM:443/foo/bar"} for _, repo := range validRepos { @@ -75,25 +59,10 @@ func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) { // tag an image with an existed tag name without -f option should work func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) { - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } - dockerCmd(c, "tag", "busybox:latest", "busybox:test") } func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) { - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } // test repository name begin with '-' out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test") c.Assert(err, checker.NotNil, check.Commentf(out)) @@ -150,13 +119,6 @@ func (s *DockerSuite) TestTagOfficialNames(c *check.C) { // ensure tags can not match digests func (s *DockerSuite) TestTagMatchesDigest(c *check.C) { - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507" // test setting tag fails _, _, err := dockerCmdWithError("tag", "busybox:latest", digest) @@ -171,14 +133,6 @@ func (s *DockerSuite) TestTagMatchesDigest(c *check.C) { } func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) { - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } - // test setting tag fails _, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag") if err == nil { @@ -188,14 +142,6 @@ func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) { // ensure tags cannot create ambiguity with image ids func (s *DockerSuite) TestTagTruncationAmbiguity(c *check.C) { - //testRequires(c, DaemonIsLinux) - // Don't attempt to pull on Windows as not in hub. It's installed - // as an image through .ensure-frozen-images-windows - if daemonPlatform != "windows" { - if err := pullImageIfNotExist("busybox:latest"); err != nil { - c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") - } - } imageID, err := buildImage("notbusybox:latest", `FROM busybox MAINTAINER dockerio`, diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index 09bba9b155..be4384d0cf 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -31,7 +31,6 @@ import ( icmd "github.com/docker/docker/pkg/integration/cmd" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/stringutils" - units "github.com/docker/go-units" "github.com/go-check/check" ) @@ -89,16 +88,6 @@ func init() { isolation = info.Isolation } -func convertBasesize(basesizeBytes int64) (int64, error) { - basesize := units.HumanSize(float64(basesizeBytes)) - basesize = strings.Trim(basesize, " ")[:len(basesize)-3] - basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64) - if err != nil { - return 0, err - } - return int64(basesizeFloat) * 1024 * 1024 * 1024, nil -} - func daemonHost() string { daemonURLStr := "unix://" + opts.DefaultUnixSocket if daemonHostVar := os.Getenv("DOCKER_HOST"); daemonHostVar != "" { @@ -370,22 +359,6 @@ func deleteImages(images ...string) error { return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error } -func imageExists(image string) error { - return icmd.RunCommand(dockerBinary, "inspect", image).Error -} - -func pullImageIfNotExist(image string) error { - if err := imageExists(image); err != nil { - pullCmd := exec.Command(dockerBinary, "pull", image) - _, exitCode, err := runCommandWithOutput(pullCmd) - - if err != nil || exitCode != 0 { - return fmt.Errorf("image %q wasn't found locally and it couldn't be pulled: %s", image, err) - } - } - return nil -} - func dockerCmdWithError(args ...string) (string, int, error) { if err := validateArgs(args...); err != nil { return "", 0, err @@ -441,18 +414,6 @@ func dockerCmdInDir(c *check.C, path string, args ...string) (string, int, error return result.Combined(), result.ExitCode, result.Error } -// execute a docker command in a directory with a timeout -func dockerCmdInDirWithTimeout(timeout time.Duration, path string, args ...string) *icmd.Result { - if err := validateArgs(args...); err != nil { - return &icmd.Result{Error: err} - } - return icmd.RunCmd(icmd.Cmd{ - Command: binaryWithArgs(args...), - Timeout: timeout, - Dir: path, - }) -} - // validateArgs is a checker to ensure tests are not running commands which are // not supported on platforms. Specifically on Windows this is 'busybox top'. func validateArgs(args ...string) error { @@ -853,36 +814,6 @@ func getIDByName(name string) (string, error) { return inspectFieldWithError(name, "Id") } -// getContainerState returns the exit code of the container -// and true if it's running -// the exit code should be ignored if it's running -func getContainerState(c *check.C, id string) (int, bool, error) { - var ( - exitStatus int - running bool - ) - out, exitCode := dockerCmd(c, "inspect", "--format={{.State.Running}} {{.State.ExitCode}}", id) - if exitCode != 0 { - return 0, false, fmt.Errorf("%q doesn't exist: %s", id, out) - } - - out = strings.Trim(out, "\n") - splitOutput := strings.Split(out, " ") - if len(splitOutput) != 2 { - return 0, false, fmt.Errorf("failed to get container state: output is broken") - } - if splitOutput[0] == "true" { - running = true - } - if n, err := strconv.Atoi(splitOutput[1]); err == nil { - exitStatus = n - } else { - return 0, false, fmt.Errorf("failed to get container state: couldn't parse integer") - } - - return exitStatus, running, nil -} - func buildImageCmd(name, dockerfile string, useCache bool, buildFlags ...string) *exec.Cmd { return daemon.BuildImageCmdWithHost(dockerBinary, name, dockerfile, "", useCache, buildFlags...) } @@ -1264,28 +1195,6 @@ func createTmpFile(c *check.C, content string) string { return filename } -func buildImageWithOutInDamon(socket string, name, dockerfile string, useCache bool) (string, error) { - args := []string{"--host", socket} - buildCmd := buildImageCmdArgs(args, name, dockerfile, useCache) - out, exitCode, err := runCommandWithOutput(buildCmd) - if err != nil || exitCode != 0 { - return out, fmt.Errorf("failed to build the image: %s, error: %v", out, err) - } - return out, nil -} - -func buildImageCmdArgs(args []string, name, dockerfile string, useCache bool) *exec.Cmd { - args = append(args, []string{"-D", "build", "-t", name}...) - if !useCache { - args = append(args, "--no-cache") - } - args = append(args, "-") - buildCmd := exec.Command(dockerBinary, args...) - buildCmd.Stdin = strings.NewReader(dockerfile) - return buildCmd - -} - func waitForContainer(contID string, args ...string) error { args = append([]string{dockerBinary, "run", "--name", contID}, args...) result := icmd.RunCmd(icmd.Cmd{Command: args}) @@ -1346,23 +1255,6 @@ func runSleepingContainerInImage(c *check.C, image string, extraArgs ...string) return dockerCmd(c, args...) } -func getRootUIDGID() (int, int, error) { - uidgid := strings.Split(filepath.Base(dockerBasePath), ".") - if len(uidgid) == 1 { - //user namespace remapping is not turned on; return 0 - return 0, 0, nil - } - uid, err := strconv.Atoi(uidgid[0]) - if err != nil { - return 0, 0, err - } - gid, err := strconv.Atoi(uidgid[1]) - if err != nil { - return 0, 0, err - } - return uid, gid, nil -} - // minimalBaseImage returns the name of the minimal base image for the current // daemon platform. func minimalBaseImage() string {