github.com/dlintw/docker@v1.5.0-rc4/integration-cli/docker_cli_create_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/docker/docker/nat"
     6  	"os"
     7  	"os/exec"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  // Make sure we can create a simple container with some args
    13  func TestCreateArgs(t *testing.T) {
    14  	runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
    15  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    16  	if err != nil {
    17  		t.Fatal(out, err)
    18  	}
    19  
    20  	cleanedContainerID := stripTrailingCharacters(out)
    21  
    22  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
    23  	out, _, err = runCommandWithOutput(inspectCmd)
    24  	if err != nil {
    25  		t.Fatalf("out should've been a container id: %s, %v", out, err)
    26  	}
    27  
    28  	containers := []struct {
    29  		ID      string
    30  		Created time.Time
    31  		Path    string
    32  		Args    []string
    33  		Image   string
    34  	}{}
    35  	if err := json.Unmarshal([]byte(out), &containers); err != nil {
    36  		t.Fatalf("Error inspecting the container: %s", err)
    37  	}
    38  	if len(containers) != 1 {
    39  		t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
    40  	}
    41  
    42  	c := containers[0]
    43  	if c.Path != "command" {
    44  		t.Fatalf("Unexpected container path. Expected command, received: %s", c.Path)
    45  	}
    46  
    47  	b := false
    48  	expected := []string{"arg1", "arg2", "arg with space"}
    49  	for i, arg := range expected {
    50  		if arg != c.Args[i] {
    51  			b = true
    52  			break
    53  		}
    54  	}
    55  	if len(c.Args) != len(expected) || b {
    56  		t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
    57  	}
    58  
    59  	deleteAllContainers()
    60  
    61  	logDone("create - args")
    62  }
    63  
    64  // Make sure we can set hostconfig options too
    65  func TestCreateHostConfig(t *testing.T) {
    66  	runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
    67  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
    68  	if err != nil {
    69  		t.Fatal(out, err)
    70  	}
    71  
    72  	cleanedContainerID := stripTrailingCharacters(out)
    73  
    74  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
    75  	out, _, err = runCommandWithOutput(inspectCmd)
    76  	if err != nil {
    77  		t.Fatalf("out should've been a container id: %s, %v", out, err)
    78  	}
    79  
    80  	containers := []struct {
    81  		HostConfig *struct {
    82  			PublishAllPorts bool
    83  		}
    84  	}{}
    85  	if err := json.Unmarshal([]byte(out), &containers); err != nil {
    86  		t.Fatalf("Error inspecting the container: %s", err)
    87  	}
    88  	if len(containers) != 1 {
    89  		t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
    90  	}
    91  
    92  	c := containers[0]
    93  	if c.HostConfig == nil {
    94  		t.Fatalf("Expected HostConfig, got none")
    95  	}
    96  
    97  	if !c.HostConfig.PublishAllPorts {
    98  		t.Fatalf("Expected PublishAllPorts, got false")
    99  	}
   100  
   101  	deleteAllContainers()
   102  
   103  	logDone("create - hostconfig")
   104  }
   105  
   106  func TestCreateWithPortRange(t *testing.T) {
   107  	runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
   108  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
   109  	if err != nil {
   110  		t.Fatal(out, err)
   111  	}
   112  
   113  	cleanedContainerID := stripTrailingCharacters(out)
   114  
   115  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
   116  	out, _, err = runCommandWithOutput(inspectCmd)
   117  	if err != nil {
   118  		t.Fatalf("out should've been a container id: %s, %v", out, err)
   119  	}
   120  
   121  	containers := []struct {
   122  		HostConfig *struct {
   123  			PortBindings map[nat.Port][]nat.PortBinding
   124  		}
   125  	}{}
   126  	if err := json.Unmarshal([]byte(out), &containers); err != nil {
   127  		t.Fatalf("Error inspecting the container: %s", err)
   128  	}
   129  	if len(containers) != 1 {
   130  		t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
   131  	}
   132  
   133  	c := containers[0]
   134  	if c.HostConfig == nil {
   135  		t.Fatalf("Expected HostConfig, got none")
   136  	}
   137  
   138  	if len(c.HostConfig.PortBindings) != 4 {
   139  		t.Fatalf("Expected 4 ports bindings, got %d", len(c.HostConfig.PortBindings))
   140  	}
   141  	for k, v := range c.HostConfig.PortBindings {
   142  		if len(v) != 1 {
   143  			t.Fatalf("Expected 1 ports binding, for the port  %s but found %s", k, v)
   144  		}
   145  		if k.Port() != v[0].HostPort {
   146  			t.Fatalf("Expected host port %d to match published port  %d", k.Port(), v[0].HostPort)
   147  		}
   148  	}
   149  
   150  	deleteAllContainers()
   151  
   152  	logDone("create - port range")
   153  }
   154  
   155  func TestCreateWithiLargePortRange(t *testing.T) {
   156  	runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
   157  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
   158  	if err != nil {
   159  		t.Fatal(out, err)
   160  	}
   161  
   162  	cleanedContainerID := stripTrailingCharacters(out)
   163  
   164  	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
   165  	out, _, err = runCommandWithOutput(inspectCmd)
   166  	if err != nil {
   167  		t.Fatalf("out should've been a container id: %s, %v", out, err)
   168  	}
   169  
   170  	containers := []struct {
   171  		HostConfig *struct {
   172  			PortBindings map[nat.Port][]nat.PortBinding
   173  		}
   174  	}{}
   175  	if err := json.Unmarshal([]byte(out), &containers); err != nil {
   176  		t.Fatalf("Error inspecting the container: %s", err)
   177  	}
   178  	if len(containers) != 1 {
   179  		t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
   180  	}
   181  
   182  	c := containers[0]
   183  	if c.HostConfig == nil {
   184  		t.Fatalf("Expected HostConfig, got none")
   185  	}
   186  
   187  	if len(c.HostConfig.PortBindings) != 65535 {
   188  		t.Fatalf("Expected 65535 ports bindings, got %d", len(c.HostConfig.PortBindings))
   189  	}
   190  	for k, v := range c.HostConfig.PortBindings {
   191  		if len(v) != 1 {
   192  			t.Fatalf("Expected 1 ports binding, for the port  %s but found %s", k, v)
   193  		}
   194  		if k.Port() != v[0].HostPort {
   195  			t.Fatalf("Expected host port %d to match published port  %d", k.Port(), v[0].HostPort)
   196  		}
   197  	}
   198  
   199  	deleteAllContainers()
   200  
   201  	logDone("create - large port range")
   202  }
   203  
   204  // "test123" should be printed by docker create + start
   205  func TestCreateEchoStdout(t *testing.T) {
   206  	runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
   207  	out, _, _, err := runCommandWithStdoutStderr(runCmd)
   208  	if err != nil {
   209  		t.Fatal(out, err)
   210  	}
   211  
   212  	cleanedContainerID := stripTrailingCharacters(out)
   213  
   214  	runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID)
   215  	out, _, _, err = runCommandWithStdoutStderr(runCmd)
   216  	if err != nil {
   217  		t.Fatal(out, err)
   218  	}
   219  
   220  	if out != "test123\n" {
   221  		t.Errorf("container should've printed 'test123', got %q", out)
   222  	}
   223  
   224  	deleteAllContainers()
   225  
   226  	logDone("create - echo test123")
   227  }
   228  
   229  func TestCreateVolumesCreated(t *testing.T) {
   230  	name := "test_create_volume"
   231  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
   232  		t.Fatal(out, err)
   233  	}
   234  	dir, err := inspectFieldMap(name, "Volumes", "/foo")
   235  	if err != nil {
   236  		t.Fatalf("Error getting volume host path: %q", err)
   237  	}
   238  
   239  	if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
   240  		t.Fatalf("Volume was not created")
   241  	}
   242  	if err != nil {
   243  		t.Fatalf("Error statting volume host path: %q", err)
   244  	}
   245  
   246  	logDone("create - volumes are created")
   247  }