2020-11-20 14:52:06 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
2020-12-08 09:58:38 +01:00
|
|
|
"fmt"
|
2020-11-20 14:52:06 +01:00
|
|
|
"net/http"
|
2020-12-08 09:58:38 +01:00
|
|
|
"os"
|
2021-03-05 10:50:06 +01:00
|
|
|
"path/filepath"
|
2020-11-20 14:52:06 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-05-26 12:17:37 +02:00
|
|
|
testify "github.com/stretchr/testify/assert"
|
2020-12-11 16:03:39 +01:00
|
|
|
"gotest.tools/v3/assert"
|
2020-11-20 14:52:06 +01:00
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
)
|
|
|
|
|
2020-12-04 10:28:43 +01:00
|
|
|
func TestLocalComposeUp(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
// this test shares a fixture with TestCompatibility and can't run at the same time
|
|
|
|
c := NewCLI(t)
|
2020-11-20 14:52:06 +01:00
|
|
|
|
|
|
|
const projectName = "compose-e2e-demo"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/sentences/compose.yaml", "--project-name", projectName, "up", "-d")
|
2020-11-26 15:45:17 +01:00
|
|
|
})
|
|
|
|
|
2021-02-03 12:54:22 +01:00
|
|
|
t.Run("check accessing running app", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
|
2020-11-20 14:52:06 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `web`})
|
|
|
|
|
2020-12-15 12:42:17 +01:00
|
|
|
endpoint := "http://localhost:90"
|
2020-11-20 14:52:06 +01:00
|
|
|
output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
|
|
|
|
assert.Assert(t, strings.Contains(output, `"word":`))
|
2020-11-27 16:15:13 +01:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerCmd(t, "network", "ls")
|
2020-12-07 09:31:32 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: projectName + "_default"})
|
2020-11-20 14:52:06 +01:00
|
|
|
})
|
2020-11-26 15:45:17 +01:00
|
|
|
|
2021-03-08 10:27:24 +01:00
|
|
|
t.Run("top", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-p", projectName, "top")
|
2021-03-08 10:27:24 +01:00
|
|
|
output := res.Stdout()
|
2021-08-18 15:50:43 +02:00
|
|
|
head := []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"}
|
|
|
|
for _, h := range head {
|
|
|
|
assert.Assert(t, strings.Contains(output, h), output)
|
|
|
|
}
|
2021-04-08 11:04:24 +02:00
|
|
|
assert.Assert(t, strings.Contains(output, `java -Xmx8m -Xms8m -jar /app/words.jar`), output)
|
|
|
|
assert.Assert(t, strings.Contains(output, `/dispatcher`), output)
|
2021-03-08 10:27:24 +01:00
|
|
|
})
|
|
|
|
|
2020-11-26 15:45:17 +01:00
|
|
|
t.Run("check compose labels", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "inspect", projectName+"-web-1")
|
2020-11-26 15:45:17 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "compose-e2e-demo"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "False",`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.config-hash":`})
|
2021-04-23 11:27:25 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.config_files":`})
|
2020-11-26 15:45:17 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project.working_dir":`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.service": "web"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version":`})
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerCmd(t, "network", "inspect", projectName+"_default")
|
2020-11-26 15:45:17 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.network": "default"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": `})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.version": `})
|
|
|
|
})
|
2020-11-27 16:15:13 +01:00
|
|
|
|
2020-12-08 15:23:24 +01:00
|
|
|
t.Run("check user labels", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "inspect", projectName+"-web-1")
|
2020-12-08 15:23:24 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `"my-label": "test"`})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2021-02-05 12:12:04 +01:00
|
|
|
t.Run("check healthcheck output", func(t *testing.T) {
|
2022-06-15 21:32:00 +02:00
|
|
|
c.WaitForCmdResult(t, c.NewDockerComposeCmd(t, "-p", projectName, "ps", "--format", "json"),
|
2021-09-22 09:57:31 +02:00
|
|
|
StdoutContains(`"Name":"compose-e2e-demo-web-1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`),
|
2021-02-03 12:54:22 +01:00
|
|
|
5*time.Second, 1*time.Second)
|
2021-02-05 12:12:04 +01:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
|
2021-06-22 11:21:27 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `NAME COMMAND SERVICE STATUS PORTS`})
|
2022-05-19 22:52:06 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp`})
|
2021-09-22 09:57:31 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 "docker-entrypoint.s…" db running 5432/tcp`})
|
2021-02-03 12:54:22 +01:00
|
|
|
})
|
|
|
|
|
2021-04-07 13:16:22 +02:00
|
|
|
t.Run("images", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-p", projectName, "images")
|
2021-09-22 09:57:31 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-db-1 gtardif/sentences-db latest`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 gtardif/sentences-web latest`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-words-1 gtardif/sentences-api latest`})
|
2021-04-07 13:16:22 +02:00
|
|
|
})
|
|
|
|
|
2020-11-27 16:15:13 +01:00
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2020-11-27 16:15:13 +01:00
|
|
|
})
|
|
|
|
|
2020-12-07 09:31:32 +01:00
|
|
|
t.Run("check containers after down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "ps", "--all")
|
2020-12-07 09:31:32 +01:00
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check networks after down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "network", "ls")
|
2020-12-07 09:31:32 +01:00
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
|
2020-11-27 16:15:13 +01:00
|
|
|
})
|
2020-11-20 14:52:06 +01:00
|
|
|
}
|
2020-12-04 10:28:43 +01:00
|
|
|
|
2021-04-21 13:40:37 +02:00
|
|
|
func TestDownComposefileInParentFolder(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-04-21 13:40:37 +02:00
|
|
|
|
2022-06-25 08:02:21 +02:00
|
|
|
tmpFolder, err := os.MkdirTemp("fixtures/simple-composefile", "test-tmp")
|
2021-04-21 13:40:37 +02:00
|
|
|
assert.NilError(t, err)
|
2022-08-09 22:43:58 +02:00
|
|
|
defer os.Remove(tmpFolder) //nolint:errcheck
|
2021-04-23 11:27:25 +02:00
|
|
|
projectName := filepath.Base(tmpFolder)
|
2021-04-21 13:40:37 +02:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "--project-directory", tmpFolder, "up", "-d")
|
2021-04-21 13:40:37 +02:00
|
|
|
res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerComposeCmd(t, "-p", projectName, "down")
|
2021-04-21 13:40:37 +02:00
|
|
|
res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
|
|
|
|
}
|
|
|
|
|
2021-03-08 10:22:24 +01:00
|
|
|
func TestAttachRestart(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-03-08 10:22:24 +01:00
|
|
|
|
2022-06-15 21:32:00 +02:00
|
|
|
cmd := c.NewDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
|
2021-04-26 11:44:06 +02:00
|
|
|
res := icmd.StartCmd(cmd)
|
2022-06-15 21:32:00 +02:00
|
|
|
defer c.RunDockerComposeCmd(t, "-p", "attach-restart", "down")
|
2021-03-08 10:22:24 +01:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
c.WaitForCondition(t, func() (bool, string) {
|
2021-04-26 11:44:06 +02:00
|
|
|
debug := res.Combined()
|
2022-06-15 21:55:58 +02:00
|
|
|
return strings.Count(res.Stdout(),
|
|
|
|
"failing-1 exited with code 1") == 3, fmt.Sprintf("'failing-1 exited with code 1' not found 3 times in : \n%s\n",
|
|
|
|
debug)
|
2022-09-19 21:28:59 +02:00
|
|
|
}, 3*time.Minute, 2*time.Second)
|
2021-03-15 11:12:25 +01:00
|
|
|
|
2021-09-22 09:57:31 +02:00
|
|
|
assert.Equal(t, strings.Count(res.Stdout(), "failing-1 | world"), 3, res.Combined())
|
2021-03-08 10:22:24 +01:00
|
|
|
}
|
2021-04-07 11:52:10 +02:00
|
|
|
|
|
|
|
func TestInitContainer(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-04-07 11:52:10 +02:00
|
|
|
|
2022-06-15 21:32:00 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
|
|
|
|
defer c.RunDockerComposeCmd(t, "-p", "init-container", "down")
|
2021-09-22 09:57:31 +02:00
|
|
|
testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
|
2021-04-07 11:52:10 +02:00
|
|
|
}
|
2021-07-29 16:37:13 +02:00
|
|
|
|
|
|
|
func TestRm(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-07-29 16:37:13 +02:00
|
|
|
|
|
|
|
const projectName = "compose-e2e-rm"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "up", "-d")
|
2021-07-29 16:37:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("rm -sf", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
|
|
|
|
"-sf", "simple")
|
2021-07-29 16:37:13 +02:00
|
|
|
res.Assert(t, icmd.Expected{Err: "Removed", ExitCode: 0})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check containers after rm -sf", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "ps", "--all")
|
2021-07-29 16:37:13 +02:00
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
|
|
|
|
})
|
|
|
|
|
2022-03-09 16:20:02 +01:00
|
|
|
t.Run("rm -sf <none>", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-composefile/compose.yaml", "-p", projectName, "rm",
|
|
|
|
"-sf", "simple")
|
2022-03-09 16:20:02 +01:00
|
|
|
res.Assert(t, icmd.Expected{ExitCode: 0})
|
|
|
|
})
|
|
|
|
|
2021-07-29 16:37:13 +02:00
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-p", projectName, "down")
|
2021-07-29 16:37:13 +02:00
|
|
|
})
|
|
|
|
}
|
2021-09-22 09:57:31 +02:00
|
|
|
|
|
|
|
func TestCompatibility(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
// this test shares a fixture with TestLocalComposeUp and can't run at the same time
|
|
|
|
c := NewCLI(t)
|
2021-09-22 09:57:31 +02:00
|
|
|
|
|
|
|
const projectName = "compose-e2e-compatibility"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "--compatibility", "-f", "./fixtures/sentences/compose.yaml", "--project-name",
|
|
|
|
projectName, "up", "-d")
|
2021-09-22 09:57:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check container names", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "ps", "--format", "{{.Names}}")
|
2021-09-22 09:57:31 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_web_1"})
|
|
|
|
res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_words_1"})
|
|
|
|
res.Assert(t, icmd.Expected{Out: "compose-e2e-compatibility_db_1"})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-p", projectName, "down")
|
2021-09-22 09:57:31 +02:00
|
|
|
})
|
|
|
|
}
|
2021-10-11 23:34:35 +02:00
|
|
|
|
|
|
|
func TestConvert(t *testing.T) {
|
|
|
|
const projectName = "compose-e2e-convert"
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-10-11 23:34:35 +02:00
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose.yaml", "-p", projectName, "convert")
|
2021-10-11 23:34:35 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
|
|
|
|
nginx:
|
|
|
|
build:
|
|
|
|
context: %s
|
|
|
|
dockerfile: Dockerfile
|
|
|
|
networks:
|
|
|
|
default: null
|
|
|
|
networks:
|
|
|
|
default:
|
|
|
|
name: compose-e2e-convert_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
|
|
|
|
})
|
|
|
|
}
|
2022-09-08 22:25:23 +02:00
|
|
|
|
|
|
|
func TestConvertInterpolate(t *testing.T) {
|
|
|
|
const projectName = "compose-e2e-convert-interpolate"
|
|
|
|
c := NewParallelCLI(t)
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
t.Run("convert", func(t *testing.T) {
|
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/simple-build-test/compose-interpolate.yaml", "-p", projectName, "convert", "--no-interpolate")
|
|
|
|
res.Assert(t, icmd.Expected{Out: fmt.Sprintf(`services:
|
|
|
|
nginx:
|
|
|
|
build:
|
|
|
|
context: %s
|
|
|
|
dockerfile: ${MYVAR}
|
|
|
|
networks:
|
|
|
|
default: null
|
|
|
|
networks:
|
|
|
|
default:
|
|
|
|
name: compose-e2e-convert-interpolate_default`, filepath.Join(wd, "fixtures", "simple-build-test", "nginx-build")), ExitCode: 0})
|
|
|
|
})
|
|
|
|
}
|