docker_cli_create_test.go 6.5 KB

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