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