2014-04-07 18:34:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2016-08-04 16:17:37 +00:00
|
|
|
"encoding/json"
|
2015-01-21 22:34:08 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2017-08-21 22:31:51 +00:00
|
|
|
"sort"
|
2014-05-21 22:07:40 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2015-09-18 17:41:12 +00:00
|
|
|
|
2023-07-27 16:25:39 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2015-12-01 22:01:51 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2022-11-01 10:10:30 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2014-04-07 18:34:07 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLILinksSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLILinksSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLILinksSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLILinksSuite) TestLinksPingUnlinkedContainers(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-27 18:13:25 +00:00
|
|
|
_, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
2014-04-07 18:34:07 +00:00
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
// run ping failed with error
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, exitCode, 1, fmt.Sprintf("error: %v", err))
|
2014-04-07 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 20:14:38 +00:00
|
|
|
// Test for appropriate error when calling --link with an invalid target container
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksInvalidContainerTarget(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
|
2015-02-13 20:14:38 +00:00
|
|
|
|
2015-10-08 15:56:51 +00:00
|
|
|
// an invalid container target should produce an error
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, is.ErrorContains(err, "could not get container for bogus"))
|
|
|
|
assert.Check(c, is.Contains(out, "could not get container"))
|
2015-02-13 20:14:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksPingLinkedContainers(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-07-22 22:42:26 +00:00
|
|
|
// Test with the three different ways of specifying the default network on Linux
|
|
|
|
testLinkPingOnNetwork(c, "")
|
|
|
|
testLinkPingOnNetwork(c, "default")
|
|
|
|
testLinkPingOnNetwork(c, "bridge")
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:55 +00:00
|
|
|
func testLinkPingOnNetwork(c *testing.T, network string) {
|
2016-07-22 22:42:26 +00:00
|
|
|
var postArgs []string
|
|
|
|
if network != "" {
|
|
|
|
postArgs = append(postArgs, []string{"--net", network}...)
|
|
|
|
}
|
|
|
|
postArgs = append(postArgs, []string{"busybox", "top"}...)
|
|
|
|
runArgs1 := append([]string{"run", "-d", "--name", "container1", "--hostname", "fred"}, postArgs...)
|
|
|
|
runArgs2 := append([]string{"run", "-d", "--name", "container2", "--hostname", "wilma"}, postArgs...)
|
|
|
|
|
|
|
|
// Run the two named containers
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, runArgs1...)
|
|
|
|
cli.DockerCmd(c, runArgs2...)
|
2016-07-22 22:42:26 +00:00
|
|
|
|
|
|
|
postArgs = []string{}
|
|
|
|
if network != "" {
|
|
|
|
postArgs = append(postArgs, []string{"--net", network}...)
|
|
|
|
}
|
|
|
|
postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...)
|
2015-02-16 05:00:51 +00:00
|
|
|
|
2016-07-22 22:42:26 +00:00
|
|
|
// Format a run for a container which links to the other two
|
|
|
|
runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...)
|
2015-02-16 05:00:51 +00:00
|
|
|
pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
|
2014-08-28 05:25:57 +00:00
|
|
|
|
2015-02-16 05:00:51 +00:00
|
|
|
// test ping by alias, ping by name, and ping by hostname
|
|
|
|
// 1. Ping by alias
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
|
2015-02-16 05:00:51 +00:00
|
|
|
// 2. Ping by container name
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
|
2015-02-16 05:00:51 +00:00
|
|
|
// 3. Ping by hostname
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
|
2015-02-16 05:00:51 +00:00
|
|
|
|
2016-07-22 22:42:26 +00:00
|
|
|
// Clean for next round
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "rm", "-f", "container1")
|
|
|
|
cli.DockerCmd(c, "rm", "-f", "container2")
|
2014-04-07 18:34:07 +00:00
|
|
|
}
|
2014-05-10 17:27:24 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksPingLinkedContainersAfterRename(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 16:25:39 +00:00
|
|
|
idA := cli.DockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top").Stdout()
|
|
|
|
idB := cli.DockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top").Stdout()
|
|
|
|
cli.DockerCmd(c, "rename", "container1", "container_new")
|
|
|
|
cli.DockerCmd(c, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
|
|
|
|
cli.DockerCmd(c, "kill", strings.TrimSpace(idA))
|
|
|
|
cli.DockerCmd(c, "kill", strings.TrimSpace(idB))
|
2014-10-05 02:47:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksInspectLinksStarted(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
|
2016-01-28 14:19:25 +00:00
|
|
|
links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
|
2014-07-08 20:27:58 +00:00
|
|
|
|
2017-08-21 22:31:51 +00:00
|
|
|
var result []string
|
2016-08-04 16:17:37 +00:00
|
|
|
err := json.Unmarshal([]byte(links), &result)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-07-08 20:27:58 +00:00
|
|
|
|
2022-01-20 12:43:42 +00:00
|
|
|
expected := []string{
|
2017-08-21 22:31:51 +00:00
|
|
|
"/container1:/testinspectlink/alias1",
|
|
|
|
"/container2:/testinspectlink/alias2",
|
|
|
|
}
|
|
|
|
sort.Strings(result)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.DeepEqual(c, result, expected)
|
2014-06-27 08:26:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksInspectLinksStopped(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2017-08-21 22:31:51 +00:00
|
|
|
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
|
2016-01-28 14:19:25 +00:00
|
|
|
links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
|
2014-07-22 22:13:52 +00:00
|
|
|
|
2017-08-21 22:31:51 +00:00
|
|
|
var result []string
|
2016-08-04 16:17:37 +00:00
|
|
|
err := json.Unmarshal([]byte(links), &result)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2014-07-22 22:13:52 +00:00
|
|
|
|
2022-01-20 12:43:42 +00:00
|
|
|
expected := []string{
|
2017-08-21 22:31:51 +00:00
|
|
|
"/container1:/testinspectlink/alias1",
|
|
|
|
"/container2:/testinspectlink/alias2",
|
|
|
|
}
|
|
|
|
sort.Strings(result)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.DeepEqual(c, result, expected)
|
2014-06-27 08:26:02 +00:00
|
|
|
}
|
2014-11-06 23:25:14 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksNotStartedParentNotFail(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "create", "--name=first", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "start", "first")
|
2014-11-06 23:25:14 +00:00
|
|
|
}
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksHostsFilesInject(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2019-07-08 16:31:34 +00:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2023-07-27 16:25:39 +00:00
|
|
|
idOne := cli.DockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top").Stdout()
|
|
|
|
idOne = strings.TrimSpace(idOne)
|
|
|
|
idTwo := cli.DockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top").Stdout()
|
|
|
|
idTwo = strings.TrimSpace(idTwo)
|
|
|
|
cli.WaitRun(c, idTwo)
|
2014-10-24 21:39:12 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
readContainerFileWithExec(c, idOne, "/etc/hosts")
|
|
|
|
contentTwo := readContainerFileWithExec(c, idTwo, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
// Host is not present in updated hosts file
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Assert(c, is.Contains(string(contentTwo), "onetwo"))
|
2014-10-24 21:39:12 +00:00
|
|
|
}
|
2014-12-08 19:33:18 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksUpdateOnRestart(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2019-07-08 16:31:34 +00:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
|
|
|
|
id := cli.DockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top").Stdout()
|
|
|
|
id = strings.TrimSpace(id)
|
2015-01-21 22:34:08 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
|
2017-01-16 15:39:12 +00:00
|
|
|
content := readContainerFileWithExec(c, id, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
|
2015-01-21 22:34:08 +00:00
|
|
|
getIP := func(hosts []byte, hostname string) string {
|
|
|
|
re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
|
|
|
|
matches := re.FindSubmatch(hosts)
|
2019-09-11 10:57:29 +00:00
|
|
|
assert.Assert(c, matches != nil, "Hostname %s have no matches in hosts", hostname)
|
2015-01-21 22:34:08 +00:00
|
|
|
return string(matches[1])
|
|
|
|
}
|
2015-10-08 15:56:51 +00:00
|
|
|
ip := getIP(content, "one")
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, is.Equal(ip, realIP))
|
2015-10-08 15:56:51 +00:00
|
|
|
|
|
|
|
ip = getIP(content, "onetwo")
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, is.Equal(ip, realIP))
|
2015-10-08 15:56:51 +00:00
|
|
|
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "restart", "one")
|
2016-01-28 14:19:25 +00:00
|
|
|
realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
|
2015-10-08 15:56:51 +00:00
|
|
|
|
2017-01-16 15:39:12 +00:00
|
|
|
content = readContainerFileWithExec(c, id, "/etc/hosts")
|
2015-10-08 15:56:51 +00:00
|
|
|
ip = getIP(content, "one")
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, is.Equal(ip, realIP))
|
2015-10-08 15:56:51 +00:00
|
|
|
|
|
|
|
ip = getIP(content, "onetwo")
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, is.Equal(ip, realIP))
|
2015-01-21 22:34:08 +00:00
|
|
|
}
|
2015-04-25 11:42:43 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksEnvs(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
|
|
|
|
out := cli.DockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env").Stdout()
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Assert(c, is.Contains(out, "FIRST_ENV_e1=\n"))
|
|
|
|
assert.Assert(c, is.Contains(out, "FIRST_ENV_e2=v2"))
|
|
|
|
assert.Assert(c, is.Contains(out, "FIRST_ENV_e3=v3=v3"))
|
2015-04-25 11:42:43 +00:00
|
|
|
}
|
2015-05-07 20:02:14 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinkShortDefinition(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 16:25:39 +00:00
|
|
|
cid := cli.DockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top").Stdout()
|
|
|
|
cid = strings.TrimSpace(cid)
|
|
|
|
cli.WaitRun(c, cid)
|
2015-05-07 20:02:14 +00:00
|
|
|
|
2023-07-27 16:25:39 +00:00
|
|
|
cid2 := cli.DockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top").Stdout()
|
|
|
|
cid2 = strings.TrimSpace(cid2)
|
|
|
|
cli.WaitRun(c, cid2)
|
2015-05-07 20:02:14 +00:00
|
|
|
|
2016-01-28 14:19:25 +00:00
|
|
|
links := inspectFieldJSON(c, cid2, "HostConfig.Links")
|
2023-07-05 10:12:16 +00:00
|
|
|
assert.Equal(c, links, `["/shortlinkdef:/link2/shortlinkdef"]`)
|
2015-05-07 20:02:14 +00:00
|
|
|
}
|
2015-09-01 16:10:24 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksNetworkHostContainer(c *testing.T) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
|
2015-09-01 16:10:24 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
|
2015-10-08 15:56:51 +00:00
|
|
|
|
|
|
|
// Running container linking to a container with --net host should have failed
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, err != nil, "out: %s", out)
|
2015-10-08 15:56:51 +00:00
|
|
|
// Running container linking to a container with --net host should have failed
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Check(c, is.Contains(out, runconfig.ErrConflictHostNetworkAndLinks.Error()))
|
2015-09-01 16:10:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksEtcHostsRegularFile(c *testing.T) {
|
2015-09-18 17:41:12 +00:00
|
|
|
testRequires(c, DaemonIsLinux, NotUserNamespace)
|
2023-07-27 16:25:39 +00:00
|
|
|
out := cli.DockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts").Stdout()
|
2015-10-08 15:56:51 +00:00
|
|
|
// /etc/hosts should be a regular file
|
2022-11-01 10:10:30 +00:00
|
|
|
assert.Assert(c, is.Regexp("^-.+\n$", out))
|
2015-09-01 16:10:24 +00:00
|
|
|
}
|
2016-01-20 21:24:16 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLILinksSuite) TestLinksMultipleWithSameName(c *testing.T) {
|
2016-01-21 17:51:09 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2023-07-27 16:25:39 +00:00
|
|
|
cli.DockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
|
|
|
|
cli.DockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
|
2016-01-20 21:24:16 +00:00
|
|
|
}
|