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"
|
2021-04-23 11:27:25 +02:00
|
|
|
"io/ioutil"
|
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-08 09:58:38 +01:00
|
|
|
var binDir string
|
|
|
|
|
2020-12-04 10:28:43 +01:00
|
|
|
func TestLocalComposeUp(t *testing.T) {
|
2020-11-20 14:52:06 +01:00
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
|
|
|
const projectName = "compose-e2e-demo"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2021-12-07 05:49:04 +01:00
|
|
|
c.RunDockerComposeCmd("-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) {
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("-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
|
|
|
|
2020-12-04 18:09:12 +01:00
|
|
|
res = c.RunDockerCmd("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) {
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("-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) {
|
2021-09-22 09:57:31 +02:00
|
|
|
res := c.RunDockerCmd("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":`})
|
|
|
|
|
2020-12-04 18:09:12 +01:00
|
|
|
res = c.RunDockerCmd("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) {
|
2021-09-22 09:57:31 +02:00
|
|
|
res := c.RunDockerCmd("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) {
|
2021-02-03 12:54:22 +01:00
|
|
|
c.WaitForCmdResult(c.NewDockerCmd("compose", "-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
|
|
|
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("-p", projectName, "ps")
|
2021-06-22 11:21:27 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `NAME COMMAND SERVICE STATUS PORTS`})
|
2021-09-22 09:57:31 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: `compose-e2e-demo-web-1 "/dispatcher" web running (healthy) 0.0.0.0:90->80/tcp, :::90->80/tcp`})
|
|
|
|
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) {
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("-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) {
|
2021-12-07 05:49:04 +01:00
|
|
|
_ = c.RunDockerComposeCmd("--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) {
|
|
|
|
res := c.RunDockerCmd("ps", "--all")
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check networks after down", func(t *testing.T) {
|
2020-12-04 18:09:12 +01:00
|
|
|
res := c.RunDockerCmd("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
|
|
|
|
2020-12-11 16:03:39 +01:00
|
|
|
func TestComposePull(t *testing.T) {
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
2021-02-25 21:37:11 +01:00
|
|
|
res := c.RunDockerOrExitError("compose", "--project-directory", "fixtures/simple-composefile", "pull")
|
2020-12-11 16:03:39 +01:00
|
|
|
output := res.Combined()
|
|
|
|
|
|
|
|
assert.Assert(t, strings.Contains(output, "simple Pulled"))
|
|
|
|
assert.Assert(t, strings.Contains(output, "another Pulled"))
|
|
|
|
}
|
2021-03-08 10:22:24 +01:00
|
|
|
|
2021-04-21 13:40:37 +02:00
|
|
|
func TestDownComposefileInParentFolder(t *testing.T) {
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
2021-04-23 11:27:25 +02:00
|
|
|
tmpFolder, err := ioutil.TempDir("fixtures/simple-composefile", "test-tmp")
|
2021-04-21 13:40:37 +02:00
|
|
|
assert.NilError(t, err)
|
2021-08-18 15:50:43 +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
|
|
|
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("--project-directory", tmpFolder, "up", "-d")
|
2021-04-21 13:40:37 +02:00
|
|
|
res.Assert(t, icmd.Expected{Err: "Started", ExitCode: 0})
|
|
|
|
|
2021-12-07 05:49:04 +01:00
|
|
|
res = c.RunDockerComposeCmd("-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) {
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
2021-04-26 11:44:06 +02:00
|
|
|
cmd := c.NewDockerCmd("compose", "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
|
|
|
|
res := icmd.StartCmd(cmd)
|
2021-04-19 16:52:14 +02:00
|
|
|
defer c.RunDockerOrExitError("compose", "-p", "attach-restart", "down")
|
2021-03-08 10:22:24 +01:00
|
|
|
|
2021-04-26 11:44:06 +02:00
|
|
|
c.WaitForCondition(func() (bool, string) {
|
|
|
|
debug := res.Combined()
|
2021-09-22 09:57:31 +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)
|
2021-04-26 11:44:06 +02:00
|
|
|
}, 2*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) {
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
|
|
|
res := c.RunDockerOrExitError("compose", "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
|
2021-04-19 16:52:14 +02:00
|
|
|
defer c.RunDockerOrExitError("compose", "-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) {
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
|
|
|
const projectName = "compose-e2e-rm"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2021-12-07 05:49:04 +01:00
|
|
|
c.RunDockerComposeCmd("-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) {
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("-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) {
|
|
|
|
res := c.RunDockerCmd("ps", "--all")
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), projectName+"_simple"), res.Combined())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("down", func(t *testing.T) {
|
2021-12-07 05:49:04 +01:00
|
|
|
c.RunDockerComposeCmd("-p", projectName, "down")
|
2021-07-29 16:37:13 +02:00
|
|
|
})
|
|
|
|
}
|
2021-09-22 09:57:31 +02:00
|
|
|
|
|
|
|
func TestCompatibility(t *testing.T) {
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
|
|
|
const projectName = "compose-e2e-compatibility"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2021-12-07 05:49:04 +01:00
|
|
|
c.RunDockerComposeCmd("--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) {
|
|
|
|
res := c.RunDockerCmd("ps", "--format", "{{.Names}}")
|
|
|
|
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) {
|
2021-12-07 05:49:04 +01:00
|
|
|
c.RunDockerComposeCmd("-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"
|
|
|
|
c := NewParallelE2eCLI(t, binDir)
|
|
|
|
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2021-12-07 05:49:04 +01:00
|
|
|
res := c.RunDockerComposeCmd("-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})
|
|
|
|
})
|
|
|
|
}
|