docker_cli_create_test.go 8.8 KB

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