docker_cli_create_test.go 3.1 KB

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