docker_cli_create_test.go 7.9 KB

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