docker_cli_create_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package main
  2. import (
  3. "encoding/json"
  4. "github.com/docker/docker/nat"
  5. "os"
  6. "os/exec"
  7. "testing"
  8. "time"
  9. )
  10. // Make sure we can create a simple container with some args
  11. func TestCreateArgs(t *testing.T) {
  12. defer deleteAllContainers()
  13. runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
  14. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  15. if err != nil {
  16. t.Fatal(out, err)
  17. }
  18. cleanedContainerID := stripTrailingCharacters(out)
  19. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  20. out, _, err = runCommandWithOutput(inspectCmd)
  21. if err != nil {
  22. t.Fatalf("out should've been a container id: %s, %v", out, err)
  23. }
  24. containers := []struct {
  25. ID string
  26. Created time.Time
  27. Path string
  28. Args []string
  29. Image string
  30. }{}
  31. if err := json.Unmarshal([]byte(out), &containers); err != nil {
  32. t.Fatalf("Error inspecting the container: %s", err)
  33. }
  34. if len(containers) != 1 {
  35. t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
  36. }
  37. c := containers[0]
  38. if c.Path != "command" {
  39. t.Fatalf("Unexpected container path. Expected command, received: %s", c.Path)
  40. }
  41. b := false
  42. expected := []string{"arg1", "arg2", "arg with space"}
  43. for i, arg := range expected {
  44. if arg != c.Args[i] {
  45. b = true
  46. break
  47. }
  48. }
  49. if len(c.Args) != len(expected) || b {
  50. t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
  51. }
  52. logDone("create - args")
  53. }
  54. // Make sure we can set hostconfig options too
  55. func TestCreateHostConfig(t *testing.T) {
  56. defer deleteAllContainers()
  57. runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
  58. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  59. if err != nil {
  60. t.Fatal(out, err)
  61. }
  62. cleanedContainerID := stripTrailingCharacters(out)
  63. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  64. out, _, err = runCommandWithOutput(inspectCmd)
  65. if err != nil {
  66. t.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. t.Fatalf("Error inspecting the container: %s", err)
  75. }
  76. if len(containers) != 1 {
  77. t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
  78. }
  79. c := containers[0]
  80. if c.HostConfig == nil {
  81. t.Fatalf("Expected HostConfig, got none")
  82. }
  83. if !c.HostConfig.PublishAllPorts {
  84. t.Fatalf("Expected PublishAllPorts, got false")
  85. }
  86. logDone("create - hostconfig")
  87. }
  88. func TestCreateWithPortRange(t *testing.T) {
  89. defer deleteAllContainers()
  90. runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
  91. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  92. if err != nil {
  93. t.Fatal(out, err)
  94. }
  95. cleanedContainerID := stripTrailingCharacters(out)
  96. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  97. out, _, err = runCommandWithOutput(inspectCmd)
  98. if err != nil {
  99. t.Fatalf("out should've been a container id: %s, %v", out, err)
  100. }
  101. containers := []struct {
  102. HostConfig *struct {
  103. PortBindings map[nat.Port][]nat.PortBinding
  104. }
  105. }{}
  106. if err := json.Unmarshal([]byte(out), &containers); err != nil {
  107. t.Fatalf("Error inspecting the container: %s", err)
  108. }
  109. if len(containers) != 1 {
  110. t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
  111. }
  112. c := containers[0]
  113. if c.HostConfig == nil {
  114. t.Fatalf("Expected HostConfig, got none")
  115. }
  116. if len(c.HostConfig.PortBindings) != 4 {
  117. t.Fatalf("Expected 4 ports bindings, got %d", len(c.HostConfig.PortBindings))
  118. }
  119. for k, v := range c.HostConfig.PortBindings {
  120. if len(v) != 1 {
  121. t.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
  122. }
  123. if k.Port() != v[0].HostPort {
  124. t.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
  125. }
  126. }
  127. logDone("create - port range")
  128. }
  129. func TestCreateWithiLargePortRange(t *testing.T) {
  130. defer deleteAllContainers()
  131. runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
  132. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  133. if err != nil {
  134. t.Fatal(out, err)
  135. }
  136. cleanedContainerID := stripTrailingCharacters(out)
  137. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  138. out, _, err = runCommandWithOutput(inspectCmd)
  139. if err != nil {
  140. t.Fatalf("out should've been a container id: %s, %v", out, err)
  141. }
  142. containers := []struct {
  143. HostConfig *struct {
  144. PortBindings map[nat.Port][]nat.PortBinding
  145. }
  146. }{}
  147. if err := json.Unmarshal([]byte(out), &containers); err != nil {
  148. t.Fatalf("Error inspecting the container: %s", err)
  149. }
  150. if len(containers) != 1 {
  151. t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
  152. }
  153. c := containers[0]
  154. if c.HostConfig == nil {
  155. t.Fatalf("Expected HostConfig, got none")
  156. }
  157. if len(c.HostConfig.PortBindings) != 65535 {
  158. t.Fatalf("Expected 65535 ports bindings, got %d", len(c.HostConfig.PortBindings))
  159. }
  160. for k, v := range c.HostConfig.PortBindings {
  161. if len(v) != 1 {
  162. t.Fatalf("Expected 1 ports binding, for the port %s but found %s", k, v)
  163. }
  164. if k.Port() != v[0].HostPort {
  165. t.Fatalf("Expected host port %d to match published port %d", k.Port(), v[0].HostPort)
  166. }
  167. }
  168. logDone("create - large port range")
  169. }
  170. // "test123" should be printed by docker create + start
  171. func TestCreateEchoStdout(t *testing.T) {
  172. defer deleteAllContainers()
  173. runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
  174. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  175. if err != nil {
  176. t.Fatal(out, err)
  177. }
  178. cleanedContainerID := stripTrailingCharacters(out)
  179. runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID)
  180. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  181. if err != nil {
  182. t.Fatal(out, err)
  183. }
  184. if out != "test123\n" {
  185. t.Errorf("container should've printed 'test123', got %q", out)
  186. }
  187. logDone("create - echo test123")
  188. }
  189. func TestCreateVolumesCreated(t *testing.T) {
  190. defer deleteAllContainers()
  191. name := "test_create_volume"
  192. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
  193. t.Fatal(out, err)
  194. }
  195. dir, err := inspectFieldMap(name, "Volumes", "/foo")
  196. if err != nil {
  197. t.Fatalf("Error getting volume host path: %q", err)
  198. }
  199. if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
  200. t.Fatalf("Volume was not created")
  201. }
  202. if err != nil {
  203. t.Fatalf("Error statting volume host path: %q", err)
  204. }
  205. logDone("create - volumes are created")
  206. }