e2e-ecs_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. "testing"
  23. "time"
  24. "gotest.tools/v3/assert"
  25. "gotest.tools/v3/icmd"
  26. "gotest.tools/v3/poll"
  27. . "github.com/docker/compose-cli/tests/framework"
  28. )
  29. var binDir string
  30. func TestMain(m *testing.M) {
  31. p, cleanup, err := SetupExistingCLI()
  32. if err != nil {
  33. fmt.Println(err)
  34. os.Exit(1)
  35. }
  36. binDir = p
  37. exitCode := m.Run()
  38. cleanup()
  39. os.Exit(exitCode)
  40. }
  41. func TestSecrets(t *testing.T) {
  42. cmd, testID := setupTest(t)
  43. secretName := "secret" + testID
  44. t.Run("create secret", func(t *testing.T) {
  45. secretFile := filepath.Join(cmd.BinDir, "secret.txt")
  46. err := ioutil.WriteFile(secretFile, []byte("pass1"), 0644)
  47. assert.Check(t, err == nil)
  48. res := cmd.RunDockerCmd("secret", "create", secretName, secretFile)
  49. assert.Check(t, strings.Contains(res.Stdout(), secretName))
  50. })
  51. t.Run("list secrets", func(t *testing.T) {
  52. res := cmd.RunDockerCmd("secret", "list")
  53. assert.Check(t, strings.Contains(res.Stdout(), secretName))
  54. })
  55. t.Run("inspect secret", func(t *testing.T) {
  56. res := cmd.RunDockerCmd("secret", "inspect", secretName)
  57. assert.Check(t, strings.Contains(res.Stdout(), `"Name": "`+secretName+`"`))
  58. })
  59. t.Run("rm secret", func(t *testing.T) {
  60. cmd.RunDockerCmd("secret", "rm", secretName)
  61. res := cmd.RunDockerCmd("secret", "list")
  62. assert.Check(t, !strings.Contains(res.Stdout(), secretName))
  63. })
  64. }
  65. func TestCompose(t *testing.T) {
  66. c, stack := setupTest(t)
  67. t.Run("compose up", func(t *testing.T) {
  68. c.RunDockerCmd("compose", "up", "--project-name", stack, "-f", "../composefiles/demo_multi_port.yaml")
  69. })
  70. var webURL, wordsURL string
  71. t.Run("compose ps", func(t *testing.T) {
  72. res := c.RunDockerCmd("compose", "ps", "--project-name", stack)
  73. fmt.Println(strings.TrimSpace(res.Stdout()))
  74. lines := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  75. assert.Equal(t, 4, len(lines))
  76. var dbDisplayed, wordsDisplayed, webDisplayed bool
  77. for _, line := range lines {
  78. fields := strings.Fields(line)
  79. containerID := fields[0]
  80. serviceName := fields[1]
  81. switch serviceName {
  82. case "db":
  83. dbDisplayed = true
  84. assert.DeepEqual(t, fields, []string{containerID, serviceName, "1/1"})
  85. case "words":
  86. wordsDisplayed = true
  87. assert.Check(t, strings.Contains(fields[3], ":8080->8080/tcp"))
  88. wordsURL = "http://" + strings.Replace(fields[3], "->8080/tcp", "", 1) + "/noun"
  89. case "web":
  90. webDisplayed = true
  91. assert.Equal(t, fields[1], "web")
  92. assert.Check(t, strings.Contains(fields[3], ":80->80/tcp"))
  93. webURL = "http://" + strings.Replace(fields[3], "->80/tcp", "", 1)
  94. }
  95. }
  96. assert.Check(t, dbDisplayed)
  97. assert.Check(t, wordsDisplayed)
  98. assert.Check(t, webDisplayed)
  99. })
  100. t.Run("compose ls", func(t *testing.T) {
  101. res := c.RunDockerCmd("compose", "ls", "--project-name", stack)
  102. lines := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  103. assert.Equal(t, 2, len(lines))
  104. fields := strings.Fields(lines[1])
  105. assert.Equal(t, 2, len(fields))
  106. assert.Equal(t, fields[0], stack)
  107. assert.Equal(t, "Running", fields[1])
  108. })
  109. t.Run("Words GET validating cross service connection", func(t *testing.T) {
  110. out := HTTPGetWithRetry(t, wordsURL, http.StatusOK, 5*time.Second, 180*time.Second)
  111. assert.Assert(t, strings.Contains(out, `"word":`))
  112. })
  113. t.Run("web app GET", func(t *testing.T) {
  114. out := HTTPGetWithRetry(t, webURL, http.StatusOK, 3*time.Second, 120*time.Second)
  115. assert.Assert(t, strings.Contains(out, "Docker Compose demo"))
  116. out = HTTPGetWithRetry(t, webURL+"/words/noun", http.StatusOK, 2*time.Second, 60*time.Second)
  117. assert.Assert(t, strings.Contains(out, `"word":`))
  118. })
  119. t.Run("compose down", func(t *testing.T) {
  120. cmd := c.NewDockerCmd("compose", "down", "--project-name", stack)
  121. res := icmd.StartCmd(cmd)
  122. checkUp := func(t poll.LogT) poll.Result {
  123. out := res.Stdout()
  124. if !strings.Contains(out, "DELETE_COMPLETE") {
  125. return poll.Continue("current status \n%s\n", out)
  126. }
  127. return poll.Success()
  128. }
  129. poll.WaitOn(t, checkUp, poll.WithDelay(2*time.Second), poll.WithTimeout(60*time.Second))
  130. })
  131. }
  132. func setupTest(t *testing.T) (*E2eCLI, string) {
  133. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  134. c := NewParallelE2eCLI(t, binDir)
  135. contextName := "e2e" + t.Name() + startTime
  136. stack := contextName
  137. t.Run("create context", func(t *testing.T) {
  138. localTestProfile := os.Getenv("TEST_AWS_PROFILE")
  139. var res *icmd.Result
  140. if localTestProfile != "" {
  141. region := os.Getenv("TEST_AWS_REGION")
  142. assert.Check(t, region != "")
  143. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", "default", "--region", region)
  144. } else {
  145. profile := "default"
  146. region := os.Getenv("AWS_DEFAULT_REGION")
  147. secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
  148. keyID := os.Getenv("AWS_ACCESS_KEY_ID")
  149. assert.Check(t, keyID != "")
  150. assert.Check(t, secretKey != "")
  151. assert.Check(t, region != "")
  152. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", profile, "--region", region)
  153. }
  154. res.Assert(t, icmd.Expected{Out: "Successfully created ecs context \"" + contextName + "\""})
  155. res = c.RunDockerCmd("context", "use", contextName)
  156. res.Assert(t, icmd.Expected{Out: contextName})
  157. res = c.RunDockerCmd("context", "ls")
  158. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  159. })
  160. return c, stack
  161. }