docker_cli_create_test.go 7.0 KB

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