docker_cli_create_test.go 3.1 KB

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