e2e-ecs_test.go 6.4 KB

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