docker_cli_create_test.go 9.3 KB

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