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 (
|
2022-07-29 19:57:57 +02:00
|
|
|
"fmt"
|
2021-02-24 10:53:06 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworks(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
// fixture is shared with TestNetworkModes and is not safe to run concurrently
|
|
|
|
c := NewCLI(t)
|
2021-02-24 10:53:06 +01:00
|
|
|
|
2022-07-29 18:55:22 +02:00
|
|
|
const projectName = "network-e2e"
|
2021-02-24 10:53:06 +01:00
|
|
|
|
|
|
|
t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
|
2022-07-29 18:55:22 +02:00
|
|
|
c.RunDockerOrExitError(t, "network", "rm", projectName+"-dbnet")
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerOrExitError(t, "network", "rm", "microservices")
|
2021-02-24 10:53:06 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "up",
|
|
|
|
"-d")
|
2021-02-24 10:53:06 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check running project", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps")
|
2021-02-24 10:53:06 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `web`})
|
|
|
|
|
|
|
|
endpoint := "http://localhost:80"
|
|
|
|
output := HTTPGetWithRetry(t, endpoint+"/words/noun", http.StatusOK, 2*time.Second, 20*time.Second)
|
|
|
|
assert.Assert(t, strings.Contains(output, `"word":`))
|
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
res = c.RunDockerCmd(t, "network", "ls")
|
2022-08-01 17:25:43 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: projectName + "_dbnet"})
|
2021-02-24 10:53:06 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: "microservices"})
|
|
|
|
})
|
|
|
|
|
2021-03-19 09:49:14 +01:00
|
|
|
t.Run("port", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "port", "words", "8080")
|
2021-03-19 09:49:14 +01:00
|
|
|
res.Assert(t, icmd.Expected{Out: `0.0.0.0:8080`})
|
|
|
|
})
|
|
|
|
|
2021-02-24 10:53:06 +01:00
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2021-02-24 10:53:06 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check networks after down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "network", "ls")
|
2021-02-24 10:53:06 +01:00
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), projectName), res.Combined())
|
|
|
|
assert.Assert(t, !strings.Contains(res.Combined(), "microservices"), res.Combined())
|
|
|
|
})
|
|
|
|
}
|
2021-05-03 14:12:58 +02:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
func TestNetworkAliases(t *testing.T) {
|
|
|
|
c := NewParallelCLI(t)
|
2021-05-03 14:12:58 +02:00
|
|
|
|
|
|
|
const projectName = "network_alias_e2e"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up",
|
|
|
|
"-d")
|
2021-05-03 14:12:58 +02:00
|
|
|
})
|
|
|
|
|
2021-06-15 11:00:07 +02:00
|
|
|
t.Run("curl alias", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName,
|
|
|
|
"exec", "-T", "container1", "curl", "http://alias-of-container2/")
|
2021-06-09 17:41:17 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
|
2021-05-03 14:12:58 +02:00
|
|
|
})
|
|
|
|
|
2021-06-15 11:00:07 +02:00
|
|
|
t.Run("curl links", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName,
|
|
|
|
"exec", "-T", "container1", "curl", "http://container/")
|
2021-06-15 11:00:07 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
|
|
|
|
})
|
|
|
|
|
2021-05-03 14:12:58 +02:00
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2021-05-03 14:12:58 +02:00
|
|
|
})
|
|
|
|
}
|
2021-06-22 09:56:32 +02:00
|
|
|
|
2022-06-02 01:53:20 +02:00
|
|
|
func TestNetworkLinks(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2022-06-02 01:53:20 +02:00
|
|
|
|
|
|
|
const projectName = "network_link_e2e"
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/network-links/compose.yaml", "--project-name", projectName, "up",
|
|
|
|
"-d")
|
2022-06-02 01:53:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("curl links in default bridge network", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-links/compose.yaml", "--project-name", projectName,
|
|
|
|
"exec", "-T", "container2", "curl", "http://container1/")
|
2022-06-02 01:53:20 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Stdout(), "Welcome to nginx!"), res.Stdout())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2022-06-02 01:53:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:56:32 +02:00
|
|
|
func TestIPAMConfig(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2021-06-22 09:56:32 +02:00
|
|
|
|
|
|
|
const projectName = "ipam_e2e"
|
|
|
|
|
|
|
|
t.Run("ensure we do not reuse previous networks", func(t *testing.T) {
|
2022-08-01 17:25:43 +02:00
|
|
|
c.RunDockerOrExitError(t, "network", "rm", projectName+"_default")
|
2021-06-22 09:56:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("up", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "-f", "./fixtures/ipam/compose.yaml", "--project-name", projectName, "up", "-d")
|
2021-06-22 09:56:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ensure service get fixed IP assigned", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
res := c.RunDockerCmd(t, "inspect", projectName+"-foo-1", "-f",
|
2022-08-01 17:25:43 +02:00
|
|
|
fmt.Sprintf(`{{ $network := index .NetworkSettings.Networks "%s_default" }}{{ $network.IPAMConfig.IPv4Address }}`, projectName))
|
2021-06-22 09:56:32 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: "10.1.0.100"})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2021-06-22 09:56:32 +02:00
|
|
|
})
|
|
|
|
}
|
2021-10-13 12:20:24 +02:00
|
|
|
|
|
|
|
func TestNetworkModes(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
// fixture is shared with TestNetworks and is not safe to run concurrently
|
|
|
|
c := NewCLI(t)
|
2021-10-13 12:20:24 +02:00
|
|
|
|
|
|
|
const projectName = "network_mode_service_run"
|
|
|
|
|
|
|
|
t.Run("run with service mode dependency", func(t *testing.T) {
|
2022-06-15 21:32:00 +02:00
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "run", "-T", "mydb", "echo", "success")
|
2021-10-13 12:20:24 +02:00
|
|
|
res.Assert(t, icmd.Expected{Out: "success"})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("down", func(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2021-10-13 12:20:24 +02:00
|
|
|
})
|
|
|
|
}
|