2014-05-09 06:32:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-05-22 19:15:14 -04:00
|
|
|
"fmt"
|
2014-11-11 11:17:33 -05:00
|
|
|
"os"
|
2014-05-09 06:32:19 -04:00
|
|
|
"os/exec"
|
2015-01-06 19:04:10 -05:00
|
|
|
"reflect"
|
2015-04-06 09:21:18 -04:00
|
|
|
"strings"
|
2014-05-09 06:32:19 -04:00
|
|
|
"time"
|
2015-02-25 23:16:44 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/nat"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-05-09 06:32:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Make sure we can create a simple container with some args
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateArgs(c *check.C) {
|
2014-05-09 06:32:19 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 15:34:02 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:34:02 -04:00
|
|
|
}
|
2014-05-09 06:32:19 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-09 06:32:19 -04:00
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
2014-10-14 15:34:02 -04:00
|
|
|
out, _, err = runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
2014-10-14 15:34:02 -04:00
|
|
|
}
|
2014-05-09 06:32:19 -04:00
|
|
|
|
|
|
|
containers := []struct {
|
|
|
|
ID string
|
|
|
|
Created time.Time
|
|
|
|
Path string
|
|
|
|
Args []string
|
|
|
|
Image string
|
|
|
|
}{}
|
2014-10-14 15:34:02 -04:00
|
|
|
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error inspecting the container: %s", err)
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
if len(containers) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
cont := containers[0]
|
|
|
|
if cont.Path != "command" {
|
|
|
|
c.Fatalf("Unexpected container path. Expected command, received: %s", cont.Path)
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := false
|
|
|
|
expected := []string{"arg1", "arg2", "arg with space"}
|
|
|
|
for i, arg := range expected {
|
2015-04-18 12:46:47 -04:00
|
|
|
if arg != cont.Args[i] {
|
2014-05-09 06:32:19 -04:00
|
|
|
b = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 12:46:47 -04:00
|
|
|
if len(cont.Args) != len(expected) || b {
|
|
|
|
c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args)
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can set hostconfig options too
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateHostConfig(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-05-09 06:32:19 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 15:34:02 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:34:02 -04:00
|
|
|
}
|
2014-05-09 06:32:19 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-09 06:32:19 -04:00
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
2014-10-14 15:34:02 -04:00
|
|
|
out, _, err = runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
2014-10-14 15:34:02 -04:00
|
|
|
}
|
2014-05-09 06:32:19 -04:00
|
|
|
|
|
|
|
containers := []struct {
|
|
|
|
HostConfig *struct {
|
|
|
|
PublishAllPorts bool
|
|
|
|
}
|
|
|
|
}{}
|
2014-10-14 15:34:02 -04:00
|
|
|
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error inspecting the container: %s", err)
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
if len(containers) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
cont := containers[0]
|
|
|
|
if cont.HostConfig == nil {
|
|
|
|
c.Fatalf("Expected HostConfig, got none")
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if !cont.HostConfig.PublishAllPorts {
|
|
|
|
c.Fatalf("Expected PublishAllPorts, got false")
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateWithPortRange(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-11-03 13:15:55 -05:00
|
|
|
runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-11-03 13:15:55 -05:00
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
containers := []struct {
|
|
|
|
HostConfig *struct {
|
|
|
|
PortBindings map[nat.Port][]nat.PortBinding
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error inspecting the container: %s", err)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
if len(containers) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
cont := containers[0]
|
|
|
|
if cont.HostConfig == nil {
|
|
|
|
c.Fatalf("Expected HostConfig, got none")
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if len(cont.HostConfig.PortBindings) != 4 {
|
|
|
|
c.Fatalf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings))
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
2015-04-18 12:46:47 -04:00
|
|
|
for k, v := range cont.HostConfig.PortBindings {
|
2014-11-03 13:15:55 -05:00
|
|
|
if len(v) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
if k.Port() != v[0].HostPort {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateWithiLargePortRange(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-11-03 13:15:55 -05:00
|
|
|
runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-11-03 13:15:55 -05:00
|
|
|
|
|
|
|
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(inspectCmd)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("out should've been a container id: %s, %v", out, err)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
containers := []struct {
|
|
|
|
HostConfig *struct {
|
|
|
|
PortBindings map[nat.Port][]nat.PortBinding
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal([]byte(out), &containers); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error inspecting the container: %s", err)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
if len(containers) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
cont := containers[0]
|
|
|
|
if cont.HostConfig == nil {
|
|
|
|
c.Fatalf("Expected HostConfig, got none")
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
if len(cont.HostConfig.PortBindings) != 65535 {
|
|
|
|
c.Fatalf("Expected 65535 ports bindings, got %d", len(cont.HostConfig.PortBindings))
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
2015-04-18 12:46:47 -04:00
|
|
|
for k, v := range cont.HostConfig.PortBindings {
|
2014-11-03 13:15:55 -05:00
|
|
|
if len(v) != 1 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
if k.Port() != v[0].HostPort {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
|
2014-11-03 13:15:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-09 06:32:19 -04:00
|
|
|
// "test123" should be printed by docker create + start
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateEchoStdout(c *check.C) {
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-05-09 06:32:19 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
|
|
|
|
out, _, _, err := runCommandWithStdoutStderr(runCmd)
|
2014-10-14 15:34:02 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:34:02 -04:00
|
|
|
}
|
2014-05-09 06:32:19 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-05-09 06:32:19 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID)
|
|
|
|
out, _, _, err = runCommandWithStdoutStderr(runCmd)
|
2014-10-14 15:34:02 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:34:02 -04:00
|
|
|
}
|
2014-05-09 06:32:19 -04:00
|
|
|
|
|
|
|
if out != "test123\n" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Errorf("container should've printed 'test123', got %q", out)
|
2014-05-09 06:32:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-11 11:17:33 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
2015-02-20 01:56:02 -05:00
|
|
|
|
2014-11-11 11:17:33 -05:00
|
|
|
name := "test_create_volume"
|
2014-12-03 18:53:19 -05:00
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-12-03 18:53:19 -05:00
|
|
|
}
|
2014-11-11 11:17:33 -05:00
|
|
|
dir, err := inspectFieldMap(name, "Volumes", "/foo")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error getting volume host path: %q", err)
|
2014-11-11 11:17:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Volume was not created")
|
2014-11-11 11:17:33 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error statting volume host path: %q", err)
|
2014-11-11 11:17:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-01-06 19:04:10 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateLabels(c *check.C) {
|
2015-01-06 19:04:10 -05:00
|
|
|
name := "test_create_labels"
|
|
|
|
expected := map[string]string{"k1": "v1", "k2": "v2"}
|
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := make(map[string]string)
|
|
|
|
err := inspectFieldAndMarshall(name, "Config.Labels", &actual)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected %s got %s", expected, actual)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) {
|
2015-01-06 19:04:10 -05:00
|
|
|
imageName := "testcreatebuildlabel"
|
|
|
|
_, err := buildImage(imageName,
|
|
|
|
`FROM busybox
|
|
|
|
LABEL k1=v1 k2=v2`,
|
|
|
|
true)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
name := "test_create_labels_from_image"
|
2015-06-05 13:56:25 -04:00
|
|
|
expected := map[string]string{"k2": "x", "k3": "v3"}
|
2015-01-06 19:04:10 -05:00
|
|
|
if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)); err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := make(map[string]string)
|
|
|
|
err = inspectFieldAndMarshall(name, "Config.Labels", &actual)
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(err)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Expected %s got %s", expected, actual)
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 17:06:43 -04:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) {
|
|
|
|
out, _ := dockerCmd(c, "run", "-h", "web.0", "busybox", "hostname")
|
2015-04-10 17:06:43 -04:00
|
|
|
if strings.TrimSpace(out) != "web.0" {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("hostname not set, expected `web.0`, got: %s", out)
|
2015-04-10 17:06:43 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-21 10:30:51 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestCreateRM(c *check.C) {
|
|
|
|
// Test to make sure we can 'rm' a new container that is in
|
|
|
|
// "Created" state, and has ever been run. Test "rm -f" too.
|
|
|
|
|
|
|
|
// create a container
|
|
|
|
createCmd := exec.Command(dockerBinary, "create", "busybox")
|
|
|
|
out, _, err := runCommandWithOutput(createCmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to create container:%s\n%s", out, err)
|
|
|
|
}
|
|
|
|
cID := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
rmCmd := exec.Command(dockerBinary, "rm", cID)
|
|
|
|
out, _, err = runCommandWithOutput(rmCmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to rm container:%s\n%s", out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now do it again so we can "rm -f" this time
|
|
|
|
createCmd = exec.Command(dockerBinary, "create", "busybox")
|
|
|
|
out, _, err = runCommandWithOutput(createCmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to create 2nd container:%s\n%s", out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cID = strings.TrimSpace(out)
|
|
|
|
rmCmd = exec.Command(dockerBinary, "rm", "-f", cID)
|
|
|
|
out, _, err = runCommandWithOutput(rmCmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to rm -f container:%s\n%s", out, err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 19:15:14 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "create", "busybox")
|
|
|
|
out, _, err := runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err, out)
|
|
|
|
}
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
cmd = exec.Command(dockerBinary, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox")
|
|
|
|
out, _, err = runCommandWithOutput(cmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Create container with ipc mode container should success with non running container: %s\n%s", out, err)
|
|
|
|
}
|
|
|
|
}
|