github.com/rajnmithun/docker@v1.6.0-rc2/integration-cli/docker_cli_create_test.go (about)

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