docker_cli_create_test.go 7.9 KB

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