2021-02-24 10:53: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 (
|
2021-03-04 14:45:04 +01:00
|
|
|
"os"
|
2021-02-24 10:53:06 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocalComposeRun(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-02-24 10:53:06 +01:00
|
|
|
|
|
|
|
t.Run("compose run", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back")
|
2021-02-24 10:53:06 +01:00
|
|
|
lines := Lines(res.Stdout())
|
|
|
|
assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo",
|
|
|
|
"Hello one more time")
|
2021-02-24 10:53:06 +01:00
|
|
|
lines = Lines(res.Stdout())
|
|
|
|
assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check run container exited", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "ps", "--all")
|
2021-02-24 10:53:06 +01:00
|
|
|
lines := Lines(res.Stdout())
|
|
|
|
var runContainerID string
|
|
|
|
var truncatedSlug string
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
containerID := fields[len(fields)-1]
|
|
|
|
assert.Assert(t, !strings.HasPrefix(containerID, "run-test_front"))
|
|
|
|
if strings.HasPrefix(containerID, "run-test_back") {
|
|
|
|
// only the one-off container for back service
|
|
|
|
assert.Assert(t, strings.HasPrefix(containerID, "run-test_back_run_"), containerID)
|
|
|
|
truncatedSlug = strings.Replace(containerID, "run-test_back_run_", "", 1)
|
|
|
|
runContainerID = containerID
|
|
|
|
}
|
2021-09-22 09:57:31 +02:00
|
|
|
if strings.HasPrefix(containerID, "run-test-db-1") {
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, strings.Contains(line, "Up"), line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Assert(t, runContainerID != "")
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerCmd(t, "inspect", runContainerID)
|
2021-02-24 10:53:06 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: ` "Status": "exited"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.container-number": "1"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.project": "run-test"`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.oneoff": "True",`})
|
|
|
|
res.Assert(t, icmd.Expected{Out: `"com.docker.compose.slug": "` + truncatedSlug})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("compose run --rm", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--rm", "back", "echo",
|
|
|
|
"Hello again")
|
2021-02-24 10:53:06 +01:00
|
|
|
lines := Lines(res.Stdout())
|
|
|
|
assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerCmd(t, "ps", "--all")
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-04-11 10:58:20 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "down", "--remove-orphans")
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "ps", "--all")
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
|
|
|
|
})
|
2021-03-04 14:45:04 +01:00
|
|
|
|
|
|
|
t.Run("compose run --volumes", func(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
assert.NilError(t, err)
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--volumes", wd+":/foo",
|
|
|
|
"back", "/bin/sh", "-c", "ls /foo")
|
2021-03-04 14:45:04 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: "compose_run_test.go"})
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerCmd(t, "ps", "--all")
|
2021-03-04 14:45:04 +01:00
|
|
|
assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("compose run --publish", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "--publish", "8081:80", "-d", "back",
|
|
|
|
"/bin/sh", "-c", "sleep 1")
|
|
|
|
res := c.RunDockerCmd(t, "ps")
|
2021-04-06 23:19:55 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
|
2021-03-04 14:45:04 +01:00
|
|
|
})
|
2021-03-18 17:03:53 +01:00
|
|
|
|
2022-01-13 23:34:46 +01:00
|
|
|
t.Run("compose run orphan", func(t *testing.T) {
|
|
|
|
// Use different compose files to get an orphan container
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/orphan.yaml", "run", "simple")
|
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
|
2022-01-13 23:34:46 +01:00
|
|
|
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
|
|
|
|
|
2022-06-15 21:32:00 +02:00
|
|
|
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
|
2022-01-13 23:34:46 +01:00
|
|
|
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
|
|
|
|
cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
|
|
|
|
})
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
|
|
|
|
})
|
|
|
|
|
2021-03-18 17:03:53 +01:00
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:32:00 +02:00
|
|
|
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "down")
|
2022-06-16 15:34:21 +02:00
|
|
|
icmd.RunCmd(cmd, func(c *icmd.Cmd) {
|
|
|
|
c.Env = append(c.Env, "COMPOSE_REMOVE_ORPHANS=True")
|
|
|
|
})
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "ps", "--all")
|
2022-04-11 10:58:20 +02:00
|
|
|
|
2021-03-18 17:03:53 +01:00
|
|
|
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
|
|
|
|
})
|
2022-06-14 01:01:22 +02:00
|
|
|
|
|
|
|
t.Run("run starts only container and dependencies", func(t *testing.T) {
|
|
|
|
// ensure that even if another service is up run does not start it: https://github.com/docker/compose/issues/9459
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "up", "service_b")
|
2022-06-14 01:01:22 +02:00
|
|
|
res.Assert(t, icmd.Success)
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "run", "service_a")
|
2022-06-14 01:01:22 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Combined(), "shared_dep"), res.Combined())
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), "service_b"), res.Combined())
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/deps.yaml", "down", "--remove-orphans")
|
2022-06-14 01:01:22 +02:00
|
|
|
})
|
2021-02-24 10:53:06 +01:00
|
|
|
}
|