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>
This commit is contained in:
Vincent Demeester 2016-12-28 23:00:32 +01:00
parent 1dece339c3
commit b2320d121c
No known key found for this signature in database
GPG Key ID: 083CC6FD6EB699A3
4 changed files with 29 additions and 162 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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`,

View File

@ -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 {