e2e_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "testing"
  7. "time"
  8. . "github.com/onsi/gomega"
  9. "github.com/stretchr/testify/suite"
  10. . "github.com/docker/api/tests/framework"
  11. )
  12. type E2eSuite struct {
  13. Suite
  14. }
  15. func (s *E2eSuite) TestContextHelp() {
  16. It("ensures context command includes azure-login and aci-create", func() {
  17. output := s.NewDockerCommand("context", "create", "--help").ExecOrDie()
  18. Expect(output).To(ContainSubstring("docker context create CONTEXT BACKEND [OPTIONS] [flags]"))
  19. Expect(output).To(ContainSubstring("--aci-location"))
  20. Expect(output).To(ContainSubstring("--aci-subscription-id"))
  21. Expect(output).To(ContainSubstring("--aci-resource-group"))
  22. })
  23. }
  24. func (s *E2eSuite) TestContextDefault() {
  25. It("should be initialized with default context", func() {
  26. s.NewDockerCommand("context", "use", "default").ExecOrDie()
  27. output := s.NewDockerCommand("context", "show").ExecOrDie()
  28. Expect(output).To(ContainSubstring("default"))
  29. output = s.NewCommand("docker", "context", "ls").ExecOrDie()
  30. Expect(output).To(Not(ContainSubstring("test-example")))
  31. Expect(output).To(ContainSubstring("default *"))
  32. })
  33. }
  34. func (s *E2eSuite) TestLegacy() {
  35. It("should list all legacy commands", func() {
  36. output := s.NewDockerCommand("--help").ExecOrDie()
  37. Expect(output).To(ContainSubstring("swarm"))
  38. })
  39. It("should execute legacy commands", func() {
  40. output, _ := s.NewDockerCommand("swarm", "join").Exec()
  41. Expect(output).To(ContainSubstring("\"docker swarm join\" requires exactly 1 argument."))
  42. })
  43. It("should run local container in less than 10 secs", func() {
  44. s.NewDockerCommand("pull", "hello-world").ExecOrDie()
  45. output := s.NewDockerCommand("run", "--rm", "hello-world").WithTimeout(time.NewTimer(10 * time.Second).C).ExecOrDie()
  46. Expect(output).To(ContainSubstring("Hello from Docker!"))
  47. })
  48. }
  49. func (s *E2eSuite) TestMockBackend() {
  50. It("creates a new test context to hardcoded example backend", func() {
  51. s.NewDockerCommand("context", "create", "test-example", "example").ExecOrDie()
  52. // Expect(output).To(ContainSubstring("test-example context acitest created"))
  53. })
  54. It("uses the test context", func() {
  55. currentContext := s.NewDockerCommand("context", "use", "test-example").ExecOrDie()
  56. Expect(currentContext).To(ContainSubstring("test-example"))
  57. output := s.NewDockerCommand("context", "ls").ExecOrDie()
  58. Expect(output).To(ContainSubstring("test-example *"))
  59. output = s.NewDockerCommand("context", "show").ExecOrDie()
  60. Expect(output).To(ContainSubstring("test-example"))
  61. })
  62. It("can run ps command", func() {
  63. output := s.NewDockerCommand("ps").ExecOrDie()
  64. lines := Lines(output)
  65. Expect(len(lines)).To(Equal(3))
  66. Expect(lines[2]).To(ContainSubstring("1234 alpine"))
  67. })
  68. It("can run quiet ps command", func() {
  69. output := s.NewDockerCommand("ps", "-q").ExecOrDie()
  70. lines := Lines(output)
  71. Expect(len(lines)).To(Equal(2))
  72. Expect(lines[0]).To(Equal("id"))
  73. Expect(lines[1]).To(Equal("1234"))
  74. })
  75. It("can run ps command with all ", func() {
  76. output := s.NewDockerCommand("ps", "-q", "--all").ExecOrDie()
  77. lines := Lines(output)
  78. Expect(len(lines)).To(Equal(3))
  79. Expect(lines[0]).To(Equal("id"))
  80. Expect(lines[1]).To(Equal("1234"))
  81. Expect(lines[2]).To(Equal("stopped"))
  82. })
  83. It("can run 'run' command", func() {
  84. output := s.NewDockerCommand("run", "nginx", "-p", "80:80").ExecOrDie()
  85. Expect(output).To(ContainSubstring("Running container \"nginx\" with name"))
  86. })
  87. }
  88. func (s *E2eSuite) TestAPIServer() {
  89. _, err := exec.LookPath("yarn")
  90. if err != nil || os.Getenv("SKIP_NODE") != "" {
  91. s.T().Skip("skipping, yarn not installed")
  92. }
  93. It("can run 'serve' command", func() {
  94. cName := "test-example"
  95. s.NewDockerCommand("context", "create", cName, "example").ExecOrDie()
  96. sPath := fmt.Sprintf("unix:///%s/docker.sock", s.ConfigDir)
  97. server, err := serveAPI(s.ConfigDir, sPath)
  98. Expect(err).To(BeNil())
  99. defer killProcess(server)
  100. s.NewCommand("yarn", "install").WithinDirectory("../node-client").ExecOrDie()
  101. output := s.NewCommand("yarn", "run", "start", cName, sPath).WithinDirectory("../node-client").ExecOrDie()
  102. Expect(output).To(ContainSubstring("nginx"))
  103. })
  104. }
  105. func TestE2e(t *testing.T) {
  106. suite.Run(t, new(E2eSuite))
  107. }
  108. func killProcess(process *os.Process) {
  109. err := process.Kill()
  110. Expect(err).To(BeNil())
  111. }
  112. func serveAPI(configDir string, address string) (*os.Process, error) {
  113. cmd := exec.Command("../../bin/docker", "--config", configDir, "serve", "--address", address)
  114. err := cmd.Start()
  115. if err != nil {
  116. return nil, err
  117. }
  118. return cmd.Process, nil
  119. }