e2e-ecs_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "gotest.tools/v3/assert"
  17. "io/ioutil"
  18. "net/http"
  19. "os"
  20. "strconv"
  21. "strings"
  22. "testing"
  23. "time"
  24. . "github.com/docker/api/tests/framework"
  25. "gotest.tools/v3/icmd"
  26. )
  27. var binDir string
  28. func TestMain(m *testing.M) {
  29. p, cleanup, err := SetupExistingCLI()
  30. if err != nil {
  31. fmt.Println(err)
  32. os.Exit(1)
  33. }
  34. binDir = p
  35. exitCode := m.Run()
  36. cleanup()
  37. os.Exit(exitCode)
  38. }
  39. func TestCompose(t *testing.T) {
  40. startTime := strconv.Itoa(int(time.Now().UnixNano()))
  41. c := NewE2eCLI(t, binDir)
  42. contextName := "teste2e" + startTime
  43. stack := contextName
  44. t.Run("create context", func(t *testing.T) {
  45. profile := contextName
  46. region := os.Getenv("AWS_DEFAULT_REGION")
  47. secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
  48. keyID := os.Getenv("AWS_ACCESS_KEY_ID")
  49. res := c.RunDockerCmd("context", "create", "ecs", contextName, "--profile", profile, "--region", region, "--secret-key", secretKey, "--key-id", keyID)
  50. res.Assert(t, icmd.Success)
  51. res = c.RunDockerCmd("context", "use", contextName)
  52. res.Assert(t, icmd.Expected{Out: contextName})
  53. res = c.RunDockerCmd("context", "ls")
  54. res.Assert(t, icmd.Expected{Out: contextName + " *"})
  55. })
  56. t.Run("compose up", func(t *testing.T) {
  57. res := c.RunDockerCmd("compose", "up", "--project-name", stack, "-f", "../composefiles/nginx.yaml")
  58. res.Assert(t, icmd.Success)
  59. })
  60. t.Run("compose ps", func(t *testing.T) {
  61. res := c.RunDockerCmd("compose", "ps", "--project-name", stack)
  62. res.Assert(t, icmd.Success)
  63. lines := strings.Split(res.Stdout(), "\n")
  64. assert.Equal(t,3, len(lines))
  65. fields := strings.Fields(lines[1])
  66. assert.Equal(t, 4, len(fields))
  67. assert.Check(t, strings.Contains(fields[0], stack))
  68. assert.Equal(t, "nginx", fields[1])
  69. assert.Equal(t, "1/1", fields[2])
  70. assert.Check(t, strings.Contains(fields[3], "->80/http"))
  71. url := "http://" + strings.Replace(fields[3], "->80/http", "", 1)
  72. r, err := http.Get(url)
  73. assert.NilError(t, err)
  74. assert.Equal(t, r.StatusCode, http.StatusOK)
  75. b, err := ioutil.ReadAll(r.Body)
  76. assert.NilError(t, err)
  77. assert.Assert(t, strings.Contains(string(b), "Welcome to nginx!"))
  78. })
  79. t.Run("compose down", func(t *testing.T) {
  80. res := c.RunDockerCmd("compose", "down", "--project-name", stack, "-f", "../composefiles/nginx.yaml")
  81. res.Assert(t, icmd.Success)
  82. })
  83. }