e2e-ecs_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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), res.Stdout())
  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), res.Stdout())
  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+`"`), res.Stdout())
  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), res.Stdout())
  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/ecs_e2e/multi_port_secrets.yaml")
  69. })
  70. var webURL, wordsURL, secretsURL string
  71. t.Run("compose ps", func(t *testing.T) {
  72. res := c.RunDockerCmd("compose", "ps", "--project-name", stack)
  73. lines := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  74. assert.Equal(t, 5, len(lines))
  75. var dbDisplayed, wordsDisplayed, webDisplayed, secretsDisplayed bool
  76. for _, line := range lines {
  77. fields := strings.Fields(line)
  78. containerID := fields[0]
  79. serviceName := fields[1]
  80. switch serviceName {
  81. case "db":
  82. dbDisplayed = true
  83. assert.DeepEqual(t, fields, []string{containerID, serviceName, "Running"})
  84. case "words":
  85. wordsDisplayed = true
  86. assert.Check(t, strings.Contains(fields[3], ":8080->8080/tcp"))
  87. wordsURL = "http://" + strings.Replace(fields[3], "->8080/tcp", "", 1) + "/noun"
  88. case "web":
  89. webDisplayed = true
  90. assert.Check(t, strings.Contains(fields[3], ":80->80/tcp"))
  91. webURL = "http://" + strings.Replace(fields[3], "->80/tcp", "", 1)
  92. case "websecrets":
  93. secretsDisplayed = true
  94. assert.Check(t, strings.Contains(fields[3], ":90->90/tcp"))
  95. secretsURL = "http://" + strings.Replace(fields[3], "->90/tcp", "", 1)
  96. }
  97. }
  98. assert.Check(t, dbDisplayed)
  99. assert.Check(t, wordsDisplayed)
  100. assert.Check(t, webDisplayed)
  101. assert.Check(t, secretsDisplayed)
  102. })
  103. t.Run("compose ls", func(t *testing.T) {
  104. res := c.RunDockerCmd("compose", "ls", "--project-name", stack)
  105. lines := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  106. assert.Equal(t, 2, len(lines))
  107. fields := strings.Fields(lines[1])
  108. assert.Equal(t, 2, len(fields))
  109. assert.Equal(t, fields[0], stack)
  110. assert.Equal(t, "Running", fields[1])
  111. })
  112. t.Run("Words GET validating cross service connection", func(t *testing.T) {
  113. out := HTTPGetWithRetry(t, wordsURL, http.StatusOK, 5*time.Second, 240*time.Second)
  114. assert.Assert(t, strings.Contains(out, `"word":`))
  115. })
  116. t.Run("web app GET", func(t *testing.T) {
  117. out := HTTPGetWithRetry(t, webURL, http.StatusOK, 3*time.Second, 120*time.Second)
  118. assert.Assert(t, strings.Contains(out, "Docker Compose demo"))
  119. out = HTTPGetWithRetry(t, webURL+"/words/noun", http.StatusOK, 2*time.Second, 60*time.Second)
  120. assert.Assert(t, strings.Contains(out, `"word":`))
  121. })
  122. t.Run("access secret", func(t *testing.T) {
  123. out := HTTPGetWithRetry(t, secretsURL+"/mysecret1", http.StatusOK, 3*time.Second, 120*time.Second)
  124. out = strings.ReplaceAll(out, "\r", "")
  125. assert.Equal(t, out, "myPassword1\n")
  126. })
  127. t.Run("compose down", func(t *testing.T) {
  128. cmd := c.NewDockerCmd("compose", "down", "--project-name", stack)
  129. res := icmd.StartCmd(cmd)
  130. checkUp := func(t poll.LogT) poll.Result {
  131. out := res.Stdout()
  132. if !strings.Contains(out, "DeleteComplete") {
  133. return poll.Continue("current status \n%s\n", out)
  134. }
  135. return poll.Success()
  136. }
  137. poll.WaitOn(t, checkUp, poll.WithDelay(2*time.Second), poll.WithTimeout(60*time.Second))
  138. })
  139. }
  140. func setupTest(t *testing.T) (*E2eCLI, string) {
  141. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  142. c := NewParallelE2eCLI(t, binDir)
  143. contextName := "e2e" + t.Name() + startTime
  144. stack := contextName
  145. t.Run("create context", func(t *testing.T) {
  146. localTestProfile := os.Getenv("TEST_AWS_PROFILE")
  147. var res *icmd.Result
  148. if localTestProfile != "" {
  149. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", localTestProfile)
  150. } else {
  151. region := os.Getenv("AWS_DEFAULT_REGION")
  152. secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
  153. keyID := os.Getenv("AWS_ACCESS_KEY_ID")
  154. assert.Check(t, keyID != "")
  155. assert.Check(t, secretKey != "")
  156. assert.Check(t, region != "")
  157. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--from-env")
  158. }
  159. res.Assert(t, icmd.Expected{Out: "Successfully created ecs context \"" + contextName + "\""})
  160. res = c.RunDockerCmd("context", "use", contextName)
  161. res.Assert(t, icmd.Expected{Out: contextName})
  162. res = c.RunDockerCmd("context", "ls")
  163. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  164. })
  165. return c, stack
  166. }