e2e-ecs_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/nginx.yaml")
  69. })
  70. var url string
  71. t.Run("compose ps", func(t *testing.T) {
  72. res := c.RunDockerCmd("compose", "ps", "--project-name", stack)
  73. lines := strings.Split(res.Stdout(), "\n")
  74. assert.Equal(t, 3, len(lines))
  75. fields := strings.Fields(lines[1])
  76. assert.Equal(t, 4, len(fields))
  77. assert.Check(t, strings.Contains(fields[0], stack))
  78. assert.Equal(t, "nginx", fields[1])
  79. assert.Equal(t, "1/1", fields[2])
  80. assert.Check(t, strings.Contains(fields[3], "->80/http"))
  81. url = "http://" + strings.Replace(fields[3], "->80/http", "", 1)
  82. })
  83. t.Run("compose ls", func(t *testing.T) {
  84. res := c.RunDockerCmd("compose", "ls", "--project-name", stack)
  85. lines := strings.Split(strings.TrimSpace(res.Stdout()), "\n")
  86. assert.Equal(t, 2, len(lines))
  87. fields := strings.Fields(lines[1])
  88. assert.Equal(t, 2, len(fields))
  89. assert.Equal(t, fields[0], stack)
  90. assert.Equal(t, "Running", fields[1])
  91. })
  92. t.Run("nginx GET", func(t *testing.T) {
  93. checkUp := func(t poll.LogT) poll.Result {
  94. r, err := http.Get(url)
  95. if err != nil {
  96. return poll.Continue("Err while getting %s : %v", url, err)
  97. } else if r.StatusCode != http.StatusOK {
  98. return poll.Continue("status %s while getting %s", r.Status, url)
  99. }
  100. b, err := ioutil.ReadAll(r.Body)
  101. if err == nil && strings.Contains(string(b), "Welcome to nginx!") {
  102. return poll.Success()
  103. }
  104. return poll.Error(fmt.Errorf("No nginx welcome page received at %s: \n%s", url, string(b)))
  105. }
  106. poll.WaitOn(t, checkUp, poll.WithDelay(2*time.Second), poll.WithTimeout(60*time.Second))
  107. })
  108. t.Run("compose down", func(t *testing.T) {
  109. cmd := c.NewDockerCmd("compose", "down", "--project-name", stack)
  110. res := icmd.StartCmd(cmd)
  111. checkUp := func(t poll.LogT) poll.Result {
  112. out := res.Stdout()
  113. if !strings.Contains(out, "DELETE_COMPLETE") {
  114. return poll.Continue("current status \n%s\n", out)
  115. }
  116. return poll.Success()
  117. }
  118. poll.WaitOn(t, checkUp, poll.WithDelay(2*time.Second), poll.WithTimeout(60*time.Second))
  119. })
  120. }
  121. func setupTest(t *testing.T) (*E2eCLI, string) {
  122. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  123. c := NewParallelE2eCLI(t, binDir)
  124. contextName := "e2e" + t.Name() + startTime
  125. stack := contextName
  126. t.Run("create context", func(t *testing.T) {
  127. localTestProfile := os.Getenv("TEST_AWS_PROFILE")
  128. var res *icmd.Result
  129. if localTestProfile != "" {
  130. region := os.Getenv("TEST_AWS_REGION")
  131. assert.Check(t, region != "")
  132. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--from-env")
  133. } else {
  134. region := os.Getenv("AWS_DEFAULT_REGION")
  135. secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
  136. keyID := os.Getenv("AWS_ACCESS_KEY_ID")
  137. assert.Check(t, keyID != "")
  138. assert.Check(t, secretKey != "")
  139. assert.Check(t, region != "")
  140. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--from-env")
  141. }
  142. res.Assert(t, icmd.Expected{Out: "Successfully created ecs context \"" + contextName + "\""})
  143. res = c.RunDockerCmd("context", "use", contextName)
  144. res.Assert(t, icmd.Expected{Out: contextName})
  145. res = c.RunDockerCmd("context", "ls")
  146. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  147. })
  148. return c, stack
  149. }