compose_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 e2e
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "testing"
  20. "time"
  21. testify "github.com/stretchr/testify/assert"
  22. "gotest.tools/v3/assert"
  23. "gotest.tools/v3/icmd"
  24. . "github.com/docker/compose-cli/utils/e2e"
  25. )
  26. var binDir string
  27. func TestMain(m *testing.M) {
  28. p, cleanup, err := SetupExistingCLI()
  29. if err != nil {
  30. fmt.Println(err)
  31. os.Exit(1)
  32. }
  33. binDir = p
  34. exitCode := m.Run()
  35. cleanup()
  36. os.Exit(exitCode)
  37. }
  38. func TestComposeUp(t *testing.T) {
  39. c := NewParallelE2eCLI(t, binDir)
  40. const projectName = "compose-kube-demo"
  41. kubeconfig := filepath.Join(c.ConfigDir, "kubeconfig")
  42. kindClusterName := "e2e"
  43. kubeContextName := "kind-" + kindClusterName
  44. dockerContextName := "kube-e2e-ctx"
  45. t.Run("create kube cluster", func(t *testing.T) {
  46. c.RunCmd("kind", "create", "cluster", "--name", kindClusterName, "--kubeconfig", kubeconfig, "--wait", "180s")
  47. })
  48. defer func() {
  49. c.RunDockerCmd("context", "use", "default")
  50. c.RunCmd("kind", "delete", "cluster", "--name", kindClusterName, "--kubeconfig", kubeconfig)
  51. }()
  52. t.Run("create kube context", func(t *testing.T) {
  53. res := c.RunDockerCmd("context", "create", "kubernetes", "--kubeconfig", kubeconfig, "--kubecontext", kubeContextName, dockerContextName)
  54. res.Assert(t, icmd.Expected{Out: fmt.Sprintf("Successfully created kube context %q", dockerContextName)})
  55. c.RunDockerCmd("context", "use", dockerContextName)
  56. res = c.RunDockerCmd("context", "ls")
  57. res.Assert(t, icmd.Expected{Out: fmt.Sprintf("%s * kube %s (in %s)", dockerContextName, kubeContextName, kubeconfig)})
  58. })
  59. t.Run("up", func(t *testing.T) {
  60. c.RunDockerCmd("compose", "-f", "./kube-simple-demo/demo_sentences.yaml", "--project-name", projectName, "up", "-d")
  61. })
  62. t.Run("compose ls", func(t *testing.T) {
  63. res := c.RunDockerCmd("compose", "ls", "--format", "json")
  64. res.Assert(t, icmd.Expected{Out: `[{"Name":"compose-kube-demo","Status":"deployed"}]`})
  65. })
  66. t.Run("compose ps --all", func(t *testing.T) {
  67. getServiceRegx := func(service string) string {
  68. // match output with random hash / spaces like:
  69. // db-698f4dd798-jd9gw db Running
  70. return fmt.Sprintf("%s-.*\\s+%s\\s+Pending\\s+", service, service)
  71. }
  72. res := c.RunDockerCmd("compose", "-p", projectName, "ps", "--all")
  73. testify.Regexp(t, getServiceRegx("db"), res.Stdout())
  74. testify.Regexp(t, getServiceRegx("words"), res.Stdout())
  75. testify.Regexp(t, getServiceRegx("web"), res.Stdout())
  76. assert.Equal(t, len(Lines(res.Stdout())), 4, res.Stdout())
  77. })
  78. t.Run("compose ps hides non running containers", func(t *testing.T) {
  79. res := c.RunDockerCmd("compose", "-p", projectName, "ps")
  80. assert.Equal(t, len(Lines(res.Stdout())), 1, res.Stdout())
  81. })
  82. t.Run("check running project", func(t *testing.T) {
  83. // Docker Desktop kube cluster automatically exposes ports on the host, this is not the case with kind on Desktop,
  84. //we need to connect to the clusterIP, from the kind container
  85. res := c.RunCmd("sh", "-c", "kubectl --kubeconfig "+kubeconfig+" get service/web -o json | jq -r '.spec.clusterIP'")
  86. clusterIP := strings.ReplaceAll(strings.TrimSpace(res.Stdout()), `"`, "")
  87. endpoint := fmt.Sprintf("http://%s:80/words/noun", clusterIP)
  88. c.WaitForCmdResult(icmd.Command("docker", "--context", "default", "exec", "e2e-control-plane", "curl", endpoint), StdoutContains(`"word":`), 3*time.Minute, 3*time.Second)
  89. })
  90. t.Run("compose logs web", func(t *testing.T) {
  91. res := c.RunDockerCmd("compose", "--project-name", projectName, "logs", "web")
  92. assert.Assert(t, strings.Contains(res.Stdout(), "Listening on port 80"), res.Stdout())
  93. })
  94. t.Run("down", func(t *testing.T) {
  95. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  96. })
  97. t.Run("check stack after down", func(t *testing.T) {
  98. res := c.RunDockerCmd("compose", "ls")
  99. assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
  100. })
  101. }