e2e-ecs_test.go 6.1 KB

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