e2e-ecs_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "strconv"
  20. "strings"
  21. "testing"
  22. "time"
  23. "gotest.tools/v3/assert"
  24. "gotest.tools/v3/poll"
  25. "gotest.tools/v3/icmd"
  26. . "github.com/docker/api/tests/framework"
  27. )
  28. var binDir string
  29. func TestMain(m *testing.M) {
  30. p, cleanup, err := SetupExistingCLI()
  31. if err != nil {
  32. fmt.Println(err)
  33. os.Exit(1)
  34. }
  35. binDir = p
  36. exitCode := m.Run()
  37. cleanup()
  38. os.Exit(exitCode)
  39. }
  40. func TestSecrets(t *testing.T) {
  41. c, testID := setupTest(t)
  42. secretName := "secret" + testID
  43. description := "description " + testID
  44. t.Run("create secret", func(t *testing.T) {
  45. res := c.RunDockerCmd("secret", "create", secretName, "-u", "user1", "-p", "pass1", "-d", description)
  46. res.Assert(t, icmd.Success)
  47. assert.Check(t, strings.Contains(res.Stdout(), "secret:"+secretName))
  48. })
  49. t.Run("list secrets", func(t *testing.T) {
  50. res := c.RunDockerCmd("secret", "list")
  51. res.Assert(t, icmd.Success)
  52. assert.Check(t, strings.Contains(res.Stdout(), secretName))
  53. assert.Check(t, strings.Contains(res.Stdout(), description))
  54. })
  55. t.Run("inspect secret", func(t *testing.T) {
  56. res := c.RunDockerCmd("secret", "inspect", secretName)
  57. res.Assert(t, icmd.Success)
  58. assert.Check(t, strings.Contains(res.Stdout(), `"Name": "`+secretName+`"`))
  59. assert.Check(t, strings.Contains(res.Stdout(), `"Description": "`+description+`"`))
  60. })
  61. t.Run("rm secret", func(t *testing.T) {
  62. res := c.RunDockerCmd("secret", "rm", secretName)
  63. res.Assert(t, icmd.Success)
  64. res = c.RunDockerCmd("secret", "list")
  65. res.Assert(t, icmd.Success)
  66. assert.Check(t, !strings.Contains(res.Stdout(), secretName))
  67. })
  68. }
  69. func TestCompose(t *testing.T) {
  70. c, stack := setupTest(t)
  71. t.Run("compose up", func(t *testing.T) {
  72. res := c.RunDockerCmd("compose", "up", "--project-name", stack, "-f", "../composefiles/nginx.yaml")
  73. res.Assert(t, icmd.Success)
  74. })
  75. var url string
  76. t.Run("compose ps", func(t *testing.T) {
  77. res := c.RunDockerCmd("compose", "ps", "--project-name", stack)
  78. res.Assert(t, icmd.Success)
  79. lines := strings.Split(res.Stdout(), "\n")
  80. assert.Equal(t, 3, len(lines))
  81. fields := strings.Fields(lines[1])
  82. assert.Equal(t, 4, len(fields))
  83. assert.Check(t, strings.Contains(fields[0], stack))
  84. assert.Equal(t, "nginx", fields[1])
  85. assert.Equal(t, "1/1", fields[2])
  86. assert.Check(t, strings.Contains(fields[3], "->80/http"))
  87. url = "http://" + strings.Replace(fields[3], "->80/http", "", 1)
  88. })
  89. t.Run("nginx GET", func(t *testing.T) {
  90. checkUp := func(t poll.LogT) poll.Result {
  91. r, err := http.Get(url)
  92. if err != nil {
  93. return poll.Continue("Err while getting %s : %v", url, err)
  94. } else if r.StatusCode != http.StatusOK {
  95. return poll.Continue("status %s while getting %s", r.Status, url)
  96. }
  97. b, err := ioutil.ReadAll(r.Body)
  98. if err == nil && strings.Contains(string(b), "Welcome to nginx!") {
  99. return poll.Success()
  100. }
  101. return poll.Error(fmt.Errorf("No nginx welcome page received at %s: \n%s", url, string(b)))
  102. }
  103. poll.WaitOn(t, checkUp, poll.WithDelay(2*time.Second), poll.WithTimeout(60*time.Second))
  104. })
  105. t.Run("compose down", func(t *testing.T) {
  106. res := c.RunDockerCmd("compose", "down", "--project-name", stack, "-f", "../composefiles/nginx.yaml")
  107. res.Assert(t, icmd.Success)
  108. })
  109. }
  110. func setupTest(t *testing.T) (*E2eCLI, string) {
  111. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  112. c := NewParallelE2eCLI(t, binDir)
  113. contextName := "e2e" + t.Name() + startTime
  114. stack := contextName
  115. t.Run("create context", func(t *testing.T) {
  116. localTestProfile := os.Getenv("TEST_AWS_PROFILE")
  117. var res *icmd.Result
  118. if localTestProfile != "" {
  119. region := os.Getenv("TEST_AWS_REGION")
  120. assert.Check(t, region != "")
  121. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", localTestProfile, "--region", region)
  122. res.Assert(t, icmd.Success)
  123. } else {
  124. profile := contextName
  125. region := os.Getenv("AWS_DEFAULT_REGION")
  126. secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
  127. keyID := os.Getenv("AWS_ACCESS_KEY_ID")
  128. assert.Check(t, keyID != "")
  129. assert.Check(t, secretKey != "")
  130. assert.Check(t, region != "")
  131. res = c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", profile, "--region", region, "--secret-key", secretKey, "--key-id", keyID)
  132. res.Assert(t, icmd.Success)
  133. }
  134. res = c.RunDockerCmd("context", "use", contextName)
  135. res.Assert(t, icmd.Expected{Out: contextName})
  136. res = c.RunDockerCmd("context", "ls")
  137. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  138. })
  139. return c, stack
  140. }