docker_cli_create_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "encoding/json"
  4. "os"
  5. "os/exec"
  6. "testing"
  7. "time"
  8. )
  9. // Make sure we can create a simple container with some args
  10. func TestCreateArgs(t *testing.T) {
  11. runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
  12. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  13. if err != nil {
  14. t.Fatal(out, err)
  15. }
  16. cleanedContainerID := stripTrailingCharacters(out)
  17. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  18. out, _, err = runCommandWithOutput(inspectCmd)
  19. if err != nil {
  20. t.Fatalf("out should've been a container id: %s, %v", out, err)
  21. }
  22. containers := []struct {
  23. ID string
  24. Created time.Time
  25. Path string
  26. Args []string
  27. Image string
  28. }{}
  29. if err := json.Unmarshal([]byte(out), &containers); err != nil {
  30. t.Fatalf("Error inspecting the container: %s", err)
  31. }
  32. if len(containers) != 1 {
  33. t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
  34. }
  35. c := containers[0]
  36. if c.Path != "command" {
  37. t.Fatalf("Unexpected container path. Expected command, received: %s", c.Path)
  38. }
  39. b := false
  40. expected := []string{"arg1", "arg2", "arg with space"}
  41. for i, arg := range expected {
  42. if arg != c.Args[i] {
  43. b = true
  44. break
  45. }
  46. }
  47. if len(c.Args) != len(expected) || b {
  48. t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
  49. }
  50. deleteAllContainers()
  51. logDone("create - args")
  52. }
  53. // Make sure we can set hostconfig options too
  54. func TestCreateHostConfig(t *testing.T) {
  55. runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
  56. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  57. if err != nil {
  58. t.Fatal(out, err)
  59. }
  60. cleanedContainerID := stripTrailingCharacters(out)
  61. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  62. out, _, err = runCommandWithOutput(inspectCmd)
  63. if err != nil {
  64. t.Fatalf("out should've been a container id: %s, %v", out, err)
  65. }
  66. containers := []struct {
  67. HostConfig *struct {
  68. PublishAllPorts bool
  69. }
  70. }{}
  71. if err := json.Unmarshal([]byte(out), &containers); err != nil {
  72. t.Fatalf("Error inspecting the container: %s", err)
  73. }
  74. if len(containers) != 1 {
  75. t.Fatalf("Unexpected container count. Expected 0, received: %d", len(containers))
  76. }
  77. c := containers[0]
  78. if c.HostConfig == nil {
  79. t.Fatalf("Expected HostConfig, got none")
  80. }
  81. if !c.HostConfig.PublishAllPorts {
  82. t.Fatalf("Expected PublishAllPorts, got false")
  83. }
  84. deleteAllContainers()
  85. logDone("create - hostconfig")
  86. }
  87. // "test123" should be printed by docker create + start
  88. func TestCreateEchoStdout(t *testing.T) {
  89. runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
  90. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  91. if err != nil {
  92. t.Fatal(out, err)
  93. }
  94. cleanedContainerID := stripTrailingCharacters(out)
  95. runCmd = exec.Command(dockerBinary, "start", "-ai", cleanedContainerID)
  96. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  97. if err != nil {
  98. t.Fatal(out, err)
  99. }
  100. if out != "test123\n" {
  101. t.Errorf("container should've printed 'test123', got %q", out)
  102. }
  103. deleteAllContainers()
  104. logDone("create - echo test123")
  105. }
  106. func TestCreateVolumesCreated(t *testing.T) {
  107. name := "test_create_volume"
  108. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
  109. t.Fatal(out, err)
  110. }
  111. dir, err := inspectFieldMap(name, "Volumes", "/foo")
  112. if err != nil {
  113. t.Fatalf("Error getting volume host path: %q", err)
  114. }
  115. if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
  116. t.Fatalf("Volume was not created")
  117. }
  118. if err != nil {
  119. t.Fatalf("Error statting volume host path: %q", err)
  120. }
  121. logDone("create - volumes are created")
  122. }