CI: use dockerCmd in integration-cli when possible

Signed-off-by: Hu Keping <hukeping@huawei.com>
This commit is contained in:
Hu Keping 2015-07-22 02:01:24 +08:00
parent c986f85f73
commit 012b67c3ea
6 changed files with 90 additions and 278 deletions

View File

@ -22,7 +22,7 @@ func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
out, _ = dockerCmd(c, "logs", cleanedContainerID)
if out != "foobar\nfoobar\n" {
c.Errorf("container should've printed 'foobar' twice")
c.Errorf("container should've printed 'foobar' twice, got %v", out)
}
}

View File

@ -16,25 +16,12 @@ import (
// save a repo using gz compression and try to load it using stdout
func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *check.C) {
name := "test-save-xz-and-load-repo-stdout"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf("failed to create a container: %v %v", out, err)
}
dockerCmd(c, "run", "--name", name, "busybox", "true")
repoName := "foobar-save-load-test-xz-gz"
out, _ := dockerCmd(c, "commit", name, repoName)
commitCmd := exec.Command(dockerBinary, "commit", name, repoName)
out, _, err = runCommandWithOutput(commitCmd)
if err != nil {
c.Fatalf("failed to commit container: %v %v", out, err)
}
inspectCmd := exec.Command(dockerBinary, "inspect", repoName)
before, _, err := runCommandWithOutput(inspectCmd)
if err != nil {
c.Fatalf("the repo should exist before saving it: %v %v", before, err)
}
dockerCmd(c, "inspect", repoName)
repoTarball, _, err := runCommandPipelineWithOutput(
exec.Command(dockerBinary, "save", repoName),
@ -52,8 +39,7 @@ func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *check.C) {
c.Fatalf("expected error, but succeeded with no error and output: %v", out)
}
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
after, _, err := runCommandWithOutput(inspectCmd)
after, _, err := dockerCmdWithError(c, "inspect", repoName)
if err == nil {
c.Fatalf("the repo should not exist: %v", after)
}
@ -62,27 +48,14 @@ func (s *DockerSuite) TestSaveXzAndLoadRepoStdout(c *check.C) {
// save a repo using xz+gz compression and try to load it using stdout
func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *check.C) {
name := "test-save-xz-gz-and-load-repo-stdout"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf("failed to create a container: %v %v", out, err)
}
dockerCmd(c, "run", "--name", name, "busybox", "true")
repoName := "foobar-save-load-test-xz-gz"
dockerCmd(c, "commit", name, repoName)
commitCmd := exec.Command(dockerBinary, "commit", name, repoName)
out, _, err = runCommandWithOutput(commitCmd)
if err != nil {
c.Fatalf("failed to commit container: %v %v", out, err)
}
dockerCmd(c, "inspect", repoName)
inspectCmd := exec.Command(dockerBinary, "inspect", repoName)
before, _, err := runCommandWithOutput(inspectCmd)
if err != nil {
c.Fatalf("the repo should exist before saving it: %v %v", before, err)
}
out, _, err = runCommandPipelineWithOutput(
out, _, err := runCommandPipelineWithOutput(
exec.Command(dockerBinary, "save", repoName),
exec.Command("xz", "-c"),
exec.Command("gzip", "-c"))
@ -99,8 +72,7 @@ func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *check.C) {
c.Fatalf("expected error, but succeeded with no error and output: %v", out)
}
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
after, _, err := runCommandWithOutput(inspectCmd)
after, _, err := dockerCmdWithError(c, "inspect", repoName)
if err == nil {
c.Fatalf("the repo should not exist: %v", after)
}
@ -108,55 +80,34 @@ func (s *DockerSuite) TestSaveXzGzAndLoadRepoStdout(c *check.C) {
func (s *DockerSuite) TestSaveSingleTag(c *check.C) {
repoName := "foobar-save-single-tag-test"
dockerCmd(c, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", fmt.Sprintf("%v:latest", repoName))
if out, _, err := runCommandWithOutput(tagCmd); err != nil {
c.Fatalf("failed to tag repo: %s, %v", out, err)
}
idCmd := exec.Command(dockerBinary, "images", "-q", "--no-trunc", repoName)
out, _, err := runCommandWithOutput(idCmd)
if err != nil {
c.Fatalf("failed to get repo ID: %s, %v", out, err)
}
out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
cleanedImageID := strings.TrimSpace(out)
out, _, err = runCommandPipelineWithOutput(
out, _, err := runCommandPipelineWithOutput(
exec.Command(dockerBinary, "save", fmt.Sprintf("%v:latest", repoName)),
exec.Command("tar", "t"),
exec.Command("grep", "-E", fmt.Sprintf("(^repositories$|%v)", cleanedImageID)))
if err != nil {
c.Fatalf("failed to save repo with image ID and 'repositories' file: %s, %v", out, err)
}
}
func (s *DockerSuite) TestSaveImageId(c *check.C) {
repoName := "foobar-save-image-id-test"
dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
tagCmd := exec.Command(dockerBinary, "tag", "emptyfs:latest", fmt.Sprintf("%v:latest", repoName))
if out, _, err := runCommandWithOutput(tagCmd); err != nil {
c.Fatalf("failed to tag repo: %s, %v", out, err)
}
idLongCmd := exec.Command(dockerBinary, "images", "-q", "--no-trunc", repoName)
out, _, err := runCommandWithOutput(idLongCmd)
if err != nil {
c.Fatalf("failed to get repo ID: %s, %v", out, err)
}
out, _ := dockerCmd(c, "images", "-q", "--no-trunc", repoName)
cleanedLongImageID := strings.TrimSpace(out)
idShortCmd := exec.Command(dockerBinary, "images", "-q", repoName)
out, _, err = runCommandWithOutput(idShortCmd)
if err != nil {
c.Fatalf("failed to get repo short ID: %s, %v", out, err)
}
out, _ = dockerCmd(c, "images", "-q", repoName)
cleanedShortImageID := strings.TrimSpace(out)
saveCmd := exec.Command(dockerBinary, "save", cleanedShortImageID)
tarCmd := exec.Command("tar", "t")
var err error
tarCmd.Stdin, err = saveCmd.StdoutPipe()
if err != nil {
c.Fatalf("cannot set stdout pipe for tar: %v", err)
@ -181,45 +132,28 @@ func (s *DockerSuite) TestSaveImageId(c *check.C) {
if err != nil {
c.Fatalf("failed to save repo with image ID: %s, %v", out, err)
}
}
// save a repo and try to load it using flags
func (s *DockerSuite) TestSaveAndLoadRepoFlags(c *check.C) {
name := "test-save-and-load-repo-flags"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf("failed to create a container: %s, %v", out, err)
}
dockerCmd(c, "run", "--name", name, "busybox", "true")
repoName := "foobar-save-load-test"
commitCmd := exec.Command(dockerBinary, "commit", name, repoName)
deleteImages(repoName)
if out, _, err = runCommandWithOutput(commitCmd); err != nil {
c.Fatalf("failed to commit container: %s, %v", out, err)
}
dockerCmd(c, "commit", name, repoName)
inspectCmd := exec.Command(dockerBinary, "inspect", repoName)
before, _, err := runCommandWithOutput(inspectCmd)
if err != nil {
c.Fatalf("the repo should exist before saving it: %s, %v", before, err)
before, _ := dockerCmd(c, "inspect", repoName)
}
out, _, err = runCommandPipelineWithOutput(
out, _, err := runCommandPipelineWithOutput(
exec.Command(dockerBinary, "save", repoName),
exec.Command(dockerBinary, "load"))
if err != nil {
c.Fatalf("failed to save and load repo: %s, %v", out, err)
}
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
after, _, err := runCommandWithOutput(inspectCmd)
if err != nil {
c.Fatalf("the repo should exist after loading it: %s, %v", after, err)
}
after, _ := dockerCmd(c, "inspect", repoName)
if before != after {
c.Fatalf("inspect is not the same after a save / load")
}
@ -229,19 +163,12 @@ func (s *DockerSuite) TestSaveMultipleNames(c *check.C) {
repoName := "foobar-save-multi-name-test"
// Make one image
tagCmd := exec.Command(dockerBinary, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
if out, _, err := runCommandWithOutput(tagCmd); err != nil {
c.Fatalf("failed to tag repo: %s, %v", out, err)
}
dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-one:latest", repoName))
// Make two images
tagCmd = exec.Command(dockerBinary, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
out, _, err := runCommandWithOutput(tagCmd)
if err != nil {
c.Fatalf("failed to tag repo: %s, %v", out, err)
}
dockerCmd(c, "tag", "emptyfs:latest", fmt.Sprintf("%v-two:latest", repoName))
out, _, err = runCommandPipelineWithOutput(
out, _, err := runCommandPipelineWithOutput(
exec.Command(dockerBinary, "save", fmt.Sprintf("%v-one", repoName), fmt.Sprintf("%v-two:latest", repoName)),
exec.Command("tar", "xO", "repositories"),
exec.Command("grep", "-q", "-E", "(-one|-two)"),
@ -249,26 +176,18 @@ func (s *DockerSuite) TestSaveMultipleNames(c *check.C) {
if err != nil {
c.Fatalf("failed to save multiple repos: %s, %v", out, err)
}
}
func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
makeImage := func(from string, tag string) string {
runCmd := exec.Command(dockerBinary, "run", "-d", from, "true")
var (
out string
err error
)
if out, _, err = runCommandWithOutput(runCmd); err != nil {
c.Fatalf("failed to create a container: %v %v", out, err)
}
out, _ = dockerCmd(c, "run", "-d", from, "true")
cleanedContainerID := strings.TrimSpace(out)
commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID, tag)
if out, _, err = runCommandWithOutput(commitCmd); err != nil {
c.Fatalf("failed to commit container: %v %v", out, err)
}
out, _ = dockerCmd(c, "commit", cleanedContainerID, tag)
imageID := strings.TrimSpace(out)
return imageID
}
@ -294,11 +213,7 @@ func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
actual := strings.Split(strings.TrimSpace(out), "\n")
// make the list of expected layers
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "history", "-q", "--no-trunc", "busybox:latest"))
if err != nil {
c.Fatalf("failed to get history: %s, %v", out, err)
}
out, _ = dockerCmd(c, "history", "-q", "--no-trunc", "busybox:latest")
expected := append(strings.Split(strings.TrimSpace(out), "\n"), idFoo, idBar)
sort.Strings(actual)
@ -306,7 +221,6 @@ func (s *DockerSuite) TestSaveRepoWithMultipleImages(c *check.C) {
if !reflect.DeepEqual(expected, actual) {
c.Fatalf("archive does not contains the right layers: got %v, expected %v", actual, expected)
}
}
// Issue #6722 #5892 ensure directories are included in changes

View File

@ -15,24 +15,12 @@ import (
// save a repo and try to load it using stdout
func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
name := "test-save-and-load-repo-stdout"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf("failed to create a container: %s, %v", out, err)
}
dockerCmd(c, "run", "--name", name, "busybox", "true")
repoName := "foobar-save-load-test"
out, _ := dockerCmd(c, "commit", name, repoName)
commitCmd := exec.Command(dockerBinary, "commit", name, repoName)
if out, _, err = runCommandWithOutput(commitCmd); err != nil {
c.Fatalf("failed to commit container: %s, %v", out, err)
}
inspectCmd := exec.Command(dockerBinary, "inspect", repoName)
before, _, err := runCommandWithOutput(inspectCmd)
if err != nil {
c.Fatalf("the repo should exist before saving it: %s, %v", before, err)
}
before, _ := dockerCmd(c, "inspect", repoName)
tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
c.Assert(err, check.IsNil)
@ -57,11 +45,7 @@ func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
c.Fatalf("failed to load repo: %s, %v", out, err)
}
inspectCmd = exec.Command(dockerBinary, "inspect", repoName)
after, _, err := runCommandWithOutput(inspectCmd)
if err != nil {
c.Fatalf("the repo should exist after loading it: %s %v", after, err)
}
after, _ := dockerCmd(c, "inspect", repoName)
if before != after {
c.Fatalf("inspect is not the same after a save / load")
@ -94,5 +78,4 @@ func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *check.C) {
if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
c.Fatal("help output is not being yielded", out)
}
}

View File

@ -1,7 +1,6 @@
package main
import (
"os/exec"
"strings"
"github.com/go-check/check"
@ -10,21 +9,19 @@ import (
// search for repos named "registry" on the central registry
func (s *DockerSuite) TestSearchOnCentralRegistry(c *check.C) {
testRequires(c, Network)
searchCmd := exec.Command(dockerBinary, "search", "busybox")
out, exitCode, err := runCommandWithOutput(searchCmd)
if err != nil || exitCode != 0 {
c.Fatalf("failed to search on the central registry: %s, %v", out, err)
out, exitCode := dockerCmd(c, "search", "busybox")
if exitCode != 0 {
c.Fatalf("failed to search on the central registry: %s", out)
}
if !strings.Contains(out, "Busybox base image.") {
c.Fatal("couldn't find any repository named (or containing) 'Busybox base image.'")
}
}
func (s *DockerSuite) TestSearchStarsOptionWithWrongParameter(c *check.C) {
searchCmdStarsChars := exec.Command(dockerBinary, "search", "--stars=a", "busybox")
out, exitCode, err := runCommandWithOutput(searchCmdStarsChars)
out, exitCode, err := dockerCmdWithError(c, "search", "--stars=a", "busybox")
if err == nil || exitCode == 0 {
c.Fatalf("Should not get right information: %s, %v", out, err)
}
@ -33,8 +30,7 @@ func (s *DockerSuite) TestSearchStarsOptionWithWrongParameter(c *check.C) {
c.Fatal("couldn't find the invalid value warning")
}
searchCmdStarsNegativeNumber := exec.Command(dockerBinary, "search", "-s=-1", "busybox")
out, exitCode, err = runCommandWithOutput(searchCmdStarsNegativeNumber)
out, exitCode, err = dockerCmdWithError(c, "search", "-s=-1", "busybox")
if err == nil || exitCode == 0 {
c.Fatalf("Should not get right information: %s, %v", out, err)
}
@ -42,64 +38,54 @@ func (s *DockerSuite) TestSearchStarsOptionWithWrongParameter(c *check.C) {
if !strings.Contains(out, "invalid value") {
c.Fatal("couldn't find the invalid value warning")
}
}
func (s *DockerSuite) TestSearchCmdOptions(c *check.C) {
testRequires(c, Network)
searchCmdhelp := exec.Command(dockerBinary, "search", "--help")
out, exitCode, err := runCommandWithOutput(searchCmdhelp)
if err != nil || exitCode != 0 {
c.Fatalf("failed to get search help information: %s, %v", out, err)
out, exitCode := dockerCmd(c, "search", "--help")
if exitCode != 0 {
c.Fatalf("failed to get search help information: %s", out)
}
if !strings.Contains(out, "Usage:\tdocker search [OPTIONS] TERM") {
c.Fatalf("failed to show docker search usage: %s, %v", out, err)
c.Fatalf("failed to show docker search usage: %s", out)
}
searchCmd := exec.Command(dockerBinary, "search", "busybox")
outSearchCmd, exitCode, err := runCommandWithOutput(searchCmd)
if err != nil || exitCode != 0 {
c.Fatalf("failed to search on the central registry: %s, %v", outSearchCmd, err)
outSearchCmd, exitCode := dockerCmd(c, "search", "busybox")
if exitCode != 0 {
c.Fatalf("failed to search on the central registry: %s", outSearchCmd)
}
searchCmdNotrunc := exec.Command(dockerBinary, "search", "--no-trunc=true", "busybox")
outSearchCmdNotrunc, _, err := runCommandWithOutput(searchCmdNotrunc)
if err != nil {
c.Fatalf("failed to search on the central registry: %s, %v", outSearchCmdNotrunc, err)
}
outSearchCmdNotrunc, _ := dockerCmd(c, "search", "--no-trunc=true", "busybox")
if len(outSearchCmd) > len(outSearchCmdNotrunc) {
c.Fatalf("The no-trunc option can't take effect.")
}
searchCmdautomated := exec.Command(dockerBinary, "search", "--automated=true", "busybox")
outSearchCmdautomated, exitCode, err := runCommandWithOutput(searchCmdautomated) //The busybox is a busybox base image, not an AUTOMATED image.
if err != nil || exitCode != 0 {
c.Fatalf("failed to search with automated=true on the central registry: %s, %v", outSearchCmdautomated, err)
outSearchCmdautomated, exitCode := dockerCmd(c, "search", "--automated=true", "busybox") //The busybox is a busybox base image, not an AUTOMATED image.
if exitCode != 0 {
c.Fatalf("failed to search with automated=true on the central registry: %s", outSearchCmdautomated)
}
outSearchCmdautomatedSlice := strings.Split(outSearchCmdautomated, "\n")
for i := range outSearchCmdautomatedSlice {
if strings.HasPrefix(outSearchCmdautomatedSlice[i], "busybox ") {
c.Fatalf("The busybox is not an AUTOMATED image: %s, %v", out, err)
c.Fatalf("The busybox is not an AUTOMATED image: %s", out)
}
}
searchCmdStars := exec.Command(dockerBinary, "search", "-s=2", "busybox")
outSearchCmdStars, exitCode, err := runCommandWithOutput(searchCmdStars)
if err != nil || exitCode != 0 {
c.Fatalf("failed to search with stars=2 on the central registry: %s, %v", outSearchCmdStars, err)
outSearchCmdStars, exitCode := dockerCmd(c, "search", "-s=2", "busybox")
if exitCode != 0 {
c.Fatalf("failed to search with stars=2 on the central registry: %s", outSearchCmdStars)
}
if strings.Count(outSearchCmdStars, "[OK]") > strings.Count(outSearchCmd, "[OK]") {
c.Fatalf("The quantity of images with stars should be less than that of all images: %s, %v", outSearchCmdStars, err)
c.Fatalf("The quantity of images with stars should be less than that of all images: %s", outSearchCmdStars)
}
searchCmdOptions := exec.Command(dockerBinary, "search", "--stars=2", "--automated=true", "--no-trunc=true", "busybox")
out, exitCode, err = runCommandWithOutput(searchCmdOptions)
if err != nil || exitCode != 0 {
c.Fatalf("failed to search with stars&automated&no-trunc options on the central registry: %s, %v", out, err)
out, exitCode = dockerCmd(c, "search", "--stars=2", "--automated=true", "--no-trunc=true", "busybox")
if exitCode != 0 {
c.Fatalf("failed to search with stars&automated&no-trunc options on the central registry: %s", out)
}
}

View File

@ -4,7 +4,6 @@ package main
import (
"fmt"
"os/exec"
"strings"
"github.com/go-check/check"
@ -23,9 +22,7 @@ func assertSrvNotAvailable(c *check.C, sname, name string) {
}
func isSrvPresent(c *check.C, sname, name string) bool {
runCmd := exec.Command(dockerBinary, "service", "ls")
out, _, _, err := runCommandWithStdoutStderr(runCmd)
c.Assert(err, check.IsNil)
out, _, _ := dockerCmdWithStdoutStderr(c, "service", "ls")
lines := strings.Split(out, "\n")
for i := 1; i < len(lines)-1; i++ {
if strings.Contains(lines[i], sname) && strings.Contains(lines[i], name) {
@ -36,9 +33,7 @@ func isSrvPresent(c *check.C, sname, name string) bool {
}
func isCntPresent(c *check.C, cname, sname, name string) bool {
runCmd := exec.Command(dockerBinary, "service", "ls", "--no-trunc")
out, _, _, err := runCommandWithStdoutStderr(runCmd)
c.Assert(err, check.IsNil)
out, _, _ := dockerCmdWithStdoutStderr(c, "service", "ls", "--no-trunc")
lines := strings.Split(out, "\n")
for i := 1; i < len(lines)-1; i++ {
fmt.Println(lines)
@ -50,37 +45,25 @@ func isCntPresent(c *check.C, cname, sname, name string) bool {
}
func (s *DockerSuite) TestDockerServiceCreateDelete(c *check.C) {
runCmd := exec.Command(dockerBinary, "network", "create", "test")
_, _, _, err := runCommandWithStdoutStderr(runCmd)
c.Assert(err, check.IsNil)
dockerCmdWithStdoutStderr(c, "network", "create", "test")
assertNwIsAvailable(c, "test")
runCmd = exec.Command(dockerBinary, "service", "publish", "s1.test")
_, _, _, err = runCommandWithStdoutStderr(runCmd)
c.Assert(err, check.IsNil)
dockerCmdWithStdoutStderr(c, "service", "publish", "s1.test")
assertSrvIsAvailable(c, "s1", "test")
runCmd = exec.Command(dockerBinary, "service", "unpublish", "s1.test")
_, _, _, err = runCommandWithStdoutStderr(runCmd)
c.Assert(err, check.IsNil)
dockerCmdWithStdoutStderr(c, "service", "unpublish", "s1.test")
assertSrvNotAvailable(c, "s1", "test")
runCmd = exec.Command(dockerBinary, "network", "rm", "test")
_, _, _, err = runCommandWithStdoutStderr(runCmd)
c.Assert(err, check.IsNil)
dockerCmdWithStdoutStderr(c, "network", "rm", "test")
assertNwNotAvailable(c, "test")
}
func (s *DockerSuite) TestDockerPublishServiceFlag(c *check.C) {
// Run saying the container is the backend for the specified service on the specified network
runCmd := exec.Command(dockerBinary, "run", "-d", "--expose=23", "--publish-service", "telnet.production", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd)
c.Assert(err, check.IsNil)
out, _ := dockerCmd(c, "run", "-d", "--expose=23", "--publish-service", "telnet.production", "busybox", "top")
cid := strings.TrimSpace(out)
// Verify container is attached in service ps o/p
assertSrvIsAvailable(c, "telnet", "production")
runCmd = exec.Command(dockerBinary, "rm", "-f", cid)
out, _, err = runCommandWithOutput(runCmd)
c.Assert(err, check.IsNil)
dockerCmd(c, "rm", "-f", cid)
}

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"os/exec"
"strings"
"time"
@ -11,12 +10,11 @@ import (
// Regression test for https://github.com/docker/docker/issues/7843
func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
dockerCmd(c, "run", "-d", "--name", "test", "busybox")
dockerCmd(c, "wait", "test")
// Expect this to fail because the above container is stopped, this is what we want
if _, err := runCommand(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "--link", "test:test", "busybox")); err == nil {
if _, _, err := dockerCmdWithError(c, "run", "-d", "--name", "test2", "--link", "test:test", "busybox"); err == nil {
c.Fatal("Expected error but got none")
}
@ -24,7 +22,7 @@ func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
go func() {
// Attempt to start attached to the container that won't start
// This should return an error immediately since the container can't be started
if _, err := runCommand(exec.Command(dockerBinary, "start", "-a", "test2")); err == nil {
if _, _, err := dockerCmdWithError(c, "start", "-a", "test2"); err == nil {
ch <- fmt.Errorf("Expected error but got none")
}
close(ch)
@ -36,28 +34,17 @@ func (s *DockerSuite) TestStartAttachReturnsOnError(c *check.C) {
case <-time.After(time.Second):
c.Fatalf("Attach did not exit properly")
}
}
// gh#8555: Exit code should be passed through when using start -a
func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil {
c.Fatalf("failed to run container: %v, output: %q", err, out)
}
out, _, _ := dockerCmdWithStdoutStderr(c, "run", "-d", "busybox", "sh", "-c", "sleep 2; exit 1")
out = strings.TrimSpace(out)
// make sure the container has exited before trying the "start -a"
waitCmd := exec.Command(dockerBinary, "wait", out)
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
c.Fatalf("Failed to wait on container: %v", err)
}
dockerCmd(c, "wait", out)
startCmd := exec.Command(dockerBinary, "start", "-a", out)
startOut, exitCode, err := runCommandWithOutput(startCmd)
startOut, exitCode, err := dockerCmdWithError(c, "start", "-a", out)
if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
}
@ -68,29 +55,16 @@ func (s *DockerSuite) TestStartAttachCorrectExitCode(c *check.C) {
}
func (s *DockerSuite) TestStartAttachSilent(c *check.C) {
name := "teststartattachcorrectexitcode"
runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "echo", "test")
out, _, _, err := runCommandWithStdoutStderr(runCmd)
if err != nil {
c.Fatalf("failed to run container: %v, output: %q", err, out)
}
dockerCmd(c, "run", "--name", name, "busybox", "echo", "test")
// make sure the container has exited before trying the "start -a"
waitCmd := exec.Command(dockerBinary, "wait", name)
if _, _, err = runCommandWithOutput(waitCmd); err != nil {
c.Fatalf("wait command failed with error: %v", err)
}
dockerCmd(c, "wait", name)
startCmd := exec.Command(dockerBinary, "start", "-a", name)
startOut, _, err := runCommandWithOutput(startCmd)
if err != nil {
c.Fatalf("start command failed unexpectedly with error: %v, output: %q", err, startOut)
}
startOut, _ := dockerCmd(c, "start", "-a", name)
if expected := "test\n"; startOut != expected {
c.Fatalf("start -a produced unexpected output: expected %q, got %q", expected, startOut)
}
}
func (s *DockerSuite) TestStartRecordError(c *check.C) {
@ -104,10 +78,11 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
}
// Expect this to fail and records error because of ports conflict
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top"))
out, _, err := dockerCmdWithError(c, "run", "-d", "--name", "test2", "-p", "9999:9999", "busybox", "top")
if err == nil {
c.Fatalf("Expected error but got none, output %q", out)
}
stateErr, err = inspectField("test2", "State.Error")
c.Assert(err, check.IsNil)
expected := "port is already allocated"
@ -123,47 +98,31 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
if stateErr != "" {
c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
}
}
func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
defer unpauseAllContainers()
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
runCmd = exec.Command(dockerBinary, "pause", "testing")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "pause", "testing")
runCmd = exec.Command(dockerBinary, "start", "testing")
if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
if out, _, err := dockerCmdWithError(c, "start", "testing"); err == nil || !strings.Contains(out, "Cannot start a paused container, try unpause instead.") {
c.Fatalf("an error should have been shown that you cannot start paused container: %s\n%v", out, err)
}
}
func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
// run a container named 'parent' and create two container link to `parent`
cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
for _, container := range []string{"child_first", "child_second"} {
cmd = exec.Command(dockerBinary, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "create", "--name", container, "--link", "parent:parent", "busybox", "top")
}
// stop 'parent' container
cmd = exec.Command(dockerBinary, "stop", "parent")
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "stop", "parent")
out, err := inspectField("parent", "State.Running")
c.Assert(err, check.IsNil)
if out != "false" {
@ -172,8 +131,7 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
// start all the three containers, container `child_first` start first which should be failed
// container 'parent' start second and then start container 'child_second'
cmd = exec.Command(dockerBinary, "start", "child_first", "parent", "child_second")
out, _, err = runCommandWithOutput(cmd)
out, _, err = dockerCmdWithError(c, "start", "child_first", "parent", "child_second")
if !strings.Contains(out, "Cannot start container child_first") || err == nil {
c.Fatal("Expected error but got none")
}
@ -186,33 +144,22 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
}
}
}
func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
var cmd *exec.Cmd
// run multiple containers to test
for _, container := range []string{"test1", "test2", "test3"} {
cmd = exec.Command(dockerBinary, "run", "-d", "--name", container, "busybox", "top")
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "run", "-d", "--name", container, "busybox", "top")
}
// stop all the containers
for _, container := range []string{"test1", "test2", "test3"} {
cmd = exec.Command(dockerBinary, "stop", container)
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatal(out, err)
}
dockerCmd(c, "stop", container)
}
// test start and attach multiple containers at once, expected error
for _, option := range []string{"-a", "-i", "-ai"} {
cmd = exec.Command(dockerBinary, "start", option, "test1", "test2", "test3")
out, _, err := runCommandWithOutput(cmd)
out, _, err := dockerCmdWithError(c, "start", option, "test1", "test2", "test3")
if !strings.Contains(out, "You cannot start and attach multiple containers at once.") || err == nil {
c.Fatal("Expected error but got none")
}
@ -228,5 +175,4 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
c.Fatal("Container running state wrong")
}
}
}