Преглед на файлове

integration-cli: Add docker create tests

Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)

Docker-DCO-1.1-Signed-off-by: Tibor Vass <teabee89@gmail.com> (github: tiborvass)
Alexander Larsson преди 11 години
родител
ревизия
b0cb37fd3b
променени са 2 файла, в които са добавени 118 реда и са изтрити 2 реда
  1. 2 2
      api/client/commands.go
  2. 116 0
      integration-cli/docker_cli_create_test.go

+ 2 - 2
api/client/commands.go

@@ -1988,7 +1988,7 @@ func (cid *cidFile) Close() error {
 
 
 	if !cid.written {
 	if !cid.written {
 		if err := os.Remove(cid.path); err != nil {
 		if err := os.Remove(cid.path); err != nil {
-			return fmt.Errorf("failed to remove CID file '%s': %s \n", cid.path, err)
+			return fmt.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
 		}
 		}
 	}
 	}
 
 
@@ -2096,7 +2096,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
 	// These are flags not stored in Config/HostConfig
 	// These are flags not stored in Config/HostConfig
 	var (
 	var (
 		flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
 		flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
-		flDetach     = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run container in the background and print new container ID")
+		flDetach     = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run the container in the background and print the new container ID")
 		flSigProxy   = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy received signals to the process (even in non-TTY mode). SIGCHLD, SIGSTOP, and SIGKILL are not proxied.")
 		flSigProxy   = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy received signals to the process (even in non-TTY mode). SIGCHLD, SIGSTOP, and SIGKILL are not proxied.")
 		flName       = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
 		flName       = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
 
 

+ 116 - 0
integration-cli/docker_cli_create_test.go

@@ -0,0 +1,116 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"os/exec"
+	"testing"
+	"time"
+)
+
+// Make sure we can create a simple container with some args
+func TestDockerCreateArgs(t *testing.T) {
+	runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
+	out, _, _, err := runCommandWithStdoutStderr(runCmd)
+	errorOut(err, t, out)
+
+	cleanedContainerID := stripTrailingCharacters(out)
+
+	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
+	inspectOut, _, err := runCommandWithOutput(inspectCmd)
+	errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))
+
+	containers := []struct {
+		ID      string
+		Created time.Time
+		Path    string
+		Args    []string
+		Image   string
+	}{}
+	if err := json.Unmarshal([]byte(inspectOut), &containers); err != nil {
+		t.Fatalf("Error inspecting the container: %s", err)
+	}
+	if len(containers) != 1 {
+		t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
+	}
+
+	c := containers[0]
+	if c.Path != "command" {
+		t.Fatalf("Unexpected container path. Expected command, received: %s", c.Path)
+	}
+
+	b := false
+	expected := []string{"arg1", "arg2", "arg with space"}
+	for i, arg := range expected {
+		if arg != c.Args[i] {
+			b = true
+			break
+		}
+	}
+	if len(c.Args) != len(expected) || b {
+		t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
+	}
+
+	deleteAllContainers()
+
+	logDone("create - args")
+}
+
+// Make sure we can set hostconfig options too
+func TestDockerCreateHostConfig(t *testing.T) {
+	runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
+	out, _, _, err := runCommandWithStdoutStderr(runCmd)
+	errorOut(err, t, out)
+
+	cleanedContainerID := stripTrailingCharacters(out)
+
+	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
+	inspectOut, _, err := runCommandWithOutput(inspectCmd)
+	errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))
+
+	containers := []struct {
+		HostConfig *struct {
+			PublishAllPorts bool
+		}
+	}{}
+	if err := json.Unmarshal([]byte(inspectOut), &containers); err != nil {
+		t.Fatalf("Error inspecting the container: %s", err)
+	}
+	if len(containers) != 1 {
+		t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
+	}
+
+	c := containers[0]
+	if c.HostConfig == nil {
+		t.Fatalf("Expected HostConfig, got none")
+	}
+
+	if !c.HostConfig.PublishAllPorts {
+		t.Fatalf("Expected PublishAllPorts, got false")
+	}
+
+	deleteAllContainers()
+
+	logDone("create - hostconfig")
+}
+
+// "test123" should be printed by docker create + start
+func TestDockerCreateEchoStdout(t *testing.T) {
+	runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
+	out, _, _, err := runCommandWithStdoutStderr(runCmd)
+	errorOut(err, t, out)
+
+	cleanedContainerID := stripTrailingCharacters(out)
+
+	runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID)
+	out, _, _, err = runCommandWithStdoutStderr(runCmd)
+	errorOut(err, t, out)
+
+	if out != "test123\n" {
+		t.Errorf("container should've printed 'test123', got '%s'", out)
+	}
+
+	deleteAllContainers()
+
+	logDone("create - echo test123")
+}