github.com/michael-k/docker@v1.7.0-rc2/integration-cli/docker_cli_create_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "os/exec" 8 "reflect" 9 "strings" 10 "time" 11 12 "github.com/docker/docker/nat" 13 "github.com/go-check/check" 14 ) 15 16 // Make sure we can create a simple container with some args 17 func (s *DockerSuite) TestCreateArgs(c *check.C) { 18 runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space") 19 out, _, _, err := runCommandWithStdoutStderr(runCmd) 20 if err != nil { 21 c.Fatal(out, err) 22 } 23 24 cleanedContainerID := strings.TrimSpace(out) 25 26 inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) 27 out, _, err = runCommandWithOutput(inspectCmd) 28 if err != nil { 29 c.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 c.Fatalf("Error inspecting the container: %s", err) 41 } 42 if len(containers) != 1 { 43 c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers)) 44 } 45 46 cont := containers[0] 47 if cont.Path != "command" { 48 c.Fatalf("Unexpected container path. Expected command, received: %s", cont.Path) 49 } 50 51 b := false 52 expected := []string{"arg1", "arg2", "arg with space"} 53 for i, arg := range expected { 54 if arg != cont.Args[i] { 55 b = true 56 break 57 } 58 } 59 if len(cont.Args) != len(expected) || b { 60 c.Fatalf("Unexpected args. Expected %v, received: %v", expected, cont.Args) 61 } 62 63 } 64 65 // Make sure we can set hostconfig options too 66 func (s *DockerSuite) TestCreateHostConfig(c *check.C) { 67 68 runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo") 69 out, _, _, err := runCommandWithStdoutStderr(runCmd) 70 if err != nil { 71 c.Fatal(out, err) 72 } 73 74 cleanedContainerID := strings.TrimSpace(out) 75 76 inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) 77 out, _, err = runCommandWithOutput(inspectCmd) 78 if err != nil { 79 c.Fatalf("out should've been a container id: %s, %v", out, err) 80 } 81 82 containers := []struct { 83 HostConfig *struct { 84 PublishAllPorts bool 85 } 86 }{} 87 if err := json.Unmarshal([]byte(out), &containers); err != nil { 88 c.Fatalf("Error inspecting the container: %s", err) 89 } 90 if len(containers) != 1 { 91 c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers)) 92 } 93 94 cont := containers[0] 95 if cont.HostConfig == nil { 96 c.Fatalf("Expected HostConfig, got none") 97 } 98 99 if !cont.HostConfig.PublishAllPorts { 100 c.Fatalf("Expected PublishAllPorts, got false") 101 } 102 103 } 104 105 func (s *DockerSuite) TestCreateWithPortRange(c *check.C) { 106 107 runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo") 108 out, _, _, err := runCommandWithStdoutStderr(runCmd) 109 if err != nil { 110 c.Fatal(out, err) 111 } 112 113 cleanedContainerID := strings.TrimSpace(out) 114 115 inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) 116 out, _, err = runCommandWithOutput(inspectCmd) 117 if err != nil { 118 c.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 c.Fatalf("Error inspecting the container: %s", err) 128 } 129 if len(containers) != 1 { 130 c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers)) 131 } 132 133 cont := containers[0] 134 if cont.HostConfig == nil { 135 c.Fatalf("Expected HostConfig, got none") 136 } 137 138 if len(cont.HostConfig.PortBindings) != 4 { 139 c.Fatalf("Expected 4 ports bindings, got %d", len(cont.HostConfig.PortBindings)) 140 } 141 for k, v := range cont.HostConfig.PortBindings { 142 if len(v) != 1 { 143 c.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v) 144 } 145 if k.Port() != v[0].HostPort { 146 c.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort) 147 } 148 } 149 150 } 151 152 func (s *DockerSuite) TestCreateWithiLargePortRange(c *check.C) { 153 154 runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo") 155 out, _, _, err := runCommandWithStdoutStderr(runCmd) 156 if err != nil { 157 c.Fatal(out, err) 158 } 159 160 cleanedContainerID := strings.TrimSpace(out) 161 162 inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) 163 out, _, err = runCommandWithOutput(inspectCmd) 164 if err != nil { 165 c.Fatalf("out should've been a container id: %s, %v", out, err) 166 } 167 168 containers := []struct { 169 HostConfig *struct { 170 PortBindings map[nat.Port][]nat.PortBinding 171 } 172 }{} 173 if err := json.Unmarshal([]byte(out), &containers); err != nil { 174 c.Fatalf("Error inspecting the container: %s", err) 175 } 176 if len(containers) != 1 { 177 c.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers)) 178 } 179 180 cont := containers[0] 181 if cont.HostConfig == nil { 182 c.Fatalf("Expected HostConfig, got none") 183 } 184 185 if len(cont.HostConfig.PortBindings) != 65535 { 186 c.Fatalf("Expected 65535 ports bindings, got %d", len(cont.HostConfig.PortBindings)) 187 } 188 for k, v := range cont.HostConfig.PortBindings { 189 if len(v) != 1 { 190 c.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v) 191 } 192 if k.Port() != v[0].HostPort { 193 c.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort) 194 } 195 } 196 197 } 198 199 // "test123" should be printed by docker create + start 200 func (s *DockerSuite) TestCreateEchoStdout(c *check.C) { 201 202 runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123") 203 out, _, _, err := runCommandWithStdoutStderr(runCmd) 204 if err != nil { 205 c.Fatal(out, err) 206 } 207 208 cleanedContainerID := strings.TrimSpace(out) 209 210 runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID) 211 out, _, _, err = runCommandWithStdoutStderr(runCmd) 212 if err != nil { 213 c.Fatal(out, err) 214 } 215 216 if out != "test123\n" { 217 c.Errorf("container should've printed 'test123', got %q", out) 218 } 219 220 } 221 222 func (s *DockerSuite) TestCreateVolumesCreated(c *check.C) { 223 testRequires(c, SameHostDaemon) 224 225 name := "test_create_volume" 226 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil { 227 c.Fatal(out, err) 228 } 229 dir, err := inspectFieldMap(name, "Volumes", "/foo") 230 if err != nil { 231 c.Fatalf("Error getting volume host path: %q", err) 232 } 233 234 if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) { 235 c.Fatalf("Volume was not created") 236 } 237 if err != nil { 238 c.Fatalf("Error statting volume host path: %q", err) 239 } 240 241 } 242 243 func (s *DockerSuite) TestCreateLabels(c *check.C) { 244 name := "test_create_labels" 245 expected := map[string]string{"k1": "v1", "k2": "v2"} 246 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k1=v1", "--label", "k2=v2", "busybox")); err != nil { 247 c.Fatal(out, err) 248 } 249 250 actual := make(map[string]string) 251 err := inspectFieldAndMarshall(name, "Config.Labels", &actual) 252 if err != nil { 253 c.Fatal(err) 254 } 255 256 if !reflect.DeepEqual(expected, actual) { 257 c.Fatalf("Expected %s got %s", expected, actual) 258 } 259 } 260 261 func (s *DockerSuite) TestCreateLabelFromImage(c *check.C) { 262 imageName := "testcreatebuildlabel" 263 _, err := buildImage(imageName, 264 `FROM busybox 265 LABEL k1=v1 k2=v2`, 266 true) 267 if err != nil { 268 c.Fatal(err) 269 } 270 271 name := "test_create_labels_from_image" 272 expected := map[string]string{"k2": "x", "k3": "v3", "k1": "v1"} 273 if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-l", "k2=x", "--label", "k3=v3", imageName)); err != nil { 274 c.Fatal(out, err) 275 } 276 277 actual := make(map[string]string) 278 err = inspectFieldAndMarshall(name, "Config.Labels", &actual) 279 if err != nil { 280 c.Fatal(err) 281 } 282 283 if !reflect.DeepEqual(expected, actual) { 284 c.Fatalf("Expected %s got %s", expected, actual) 285 } 286 } 287 288 func (s *DockerSuite) TestCreateHostnameWithNumber(c *check.C) { 289 out, _ := dockerCmd(c, "run", "-h", "web.0", "busybox", "hostname") 290 if strings.TrimSpace(out) != "web.0" { 291 c.Fatalf("hostname not set, expected `web.0`, got: %s", out) 292 } 293 } 294 295 func (s *DockerSuite) TestCreateRM(c *check.C) { 296 // Test to make sure we can 'rm' a new container that is in 297 // "Created" state, and has ever been run. Test "rm -f" too. 298 299 // create a container 300 createCmd := exec.Command(dockerBinary, "create", "busybox") 301 out, _, err := runCommandWithOutput(createCmd) 302 if err != nil { 303 c.Fatalf("Failed to create container:%s\n%s", out, err) 304 } 305 cID := strings.TrimSpace(out) 306 307 rmCmd := exec.Command(dockerBinary, "rm", cID) 308 out, _, err = runCommandWithOutput(rmCmd) 309 if err != nil { 310 c.Fatalf("Failed to rm container:%s\n%s", out, err) 311 } 312 313 // Now do it again so we can "rm -f" this time 314 createCmd = exec.Command(dockerBinary, "create", "busybox") 315 out, _, err = runCommandWithOutput(createCmd) 316 if err != nil { 317 c.Fatalf("Failed to create 2nd container:%s\n%s", out, err) 318 } 319 320 cID = strings.TrimSpace(out) 321 rmCmd = exec.Command(dockerBinary, "rm", "-f", cID) 322 out, _, err = runCommandWithOutput(rmCmd) 323 if err != nil { 324 c.Fatalf("Failed to rm -f container:%s\n%s", out, err) 325 } 326 } 327 328 func (s *DockerSuite) TestCreateModeIpcContainer(c *check.C) { 329 testRequires(c, SameHostDaemon) 330 331 cmd := exec.Command(dockerBinary, "create", "busybox") 332 out, _, err := runCommandWithOutput(cmd) 333 if err != nil { 334 c.Fatal(err, out) 335 } 336 id := strings.TrimSpace(out) 337 338 cmd = exec.Command(dockerBinary, "create", fmt.Sprintf("--ipc=container:%s", id), "busybox") 339 out, _, err = runCommandWithOutput(cmd) 340 if err != nil { 341 c.Fatalf("Create container with ipc mode container should success with non running container: %s\n%s", out, err) 342 } 343 }