1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_cli_links_test.go
Tibor Vass 1d92789b4f rm-gocheck: check.C -> testing.T
sed -E -i 's#\bcheck\.C\b#testing.T#g' \
-- "integration-cli/check_test.go" "integration-cli/daemon/daemon.go" "integration-cli/daemon/daemon_swarm.go" "integration-cli/daemon_swarm_hack_test.go" "integration-cli/docker_api_attach_test.go" "integration-cli/docker_api_build_test.go" "integration-cli/docker_api_build_windows_test.go" "integration-cli/docker_api_containers_test.go" "integration-cli/docker_api_containers_windows_test.go" "integration-cli/docker_api_exec_resize_test.go" "integration-cli/docker_api_exec_test.go" "integration-cli/docker_api_images_test.go" "integration-cli/docker_api_inspect_test.go" "integration-cli/docker_api_logs_test.go" "integration-cli/docker_api_network_test.go" "integration-cli/docker_api_stats_test.go" "integration-cli/docker_api_swarm_node_test.go" "integration-cli/docker_api_swarm_service_test.go" "integration-cli/docker_api_swarm_test.go" "integration-cli/docker_api_test.go" "integration-cli/docker_cli_attach_test.go" "integration-cli/docker_cli_attach_unix_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_build_unix_test.go" "integration-cli/docker_cli_by_digest_test.go" "integration-cli/docker_cli_commit_test.go" "integration-cli/docker_cli_cp_from_container_test.go" "integration-cli/docker_cli_cp_test.go" "integration-cli/docker_cli_cp_to_container_test.go" "integration-cli/docker_cli_cp_to_container_unix_test.go" "integration-cli/docker_cli_cp_utils_test.go" "integration-cli/docker_cli_create_test.go" "integration-cli/docker_cli_daemon_plugins_test.go" "integration-cli/docker_cli_daemon_test.go" "integration-cli/docker_cli_events_test.go" "integration-cli/docker_cli_events_unix_test.go" "integration-cli/docker_cli_exec_test.go" "integration-cli/docker_cli_exec_unix_test.go" "integration-cli/docker_cli_external_volume_driver_unix_test.go" "integration-cli/docker_cli_health_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_import_test.go" "integration-cli/docker_cli_info_test.go" "integration-cli/docker_cli_info_unix_test.go" "integration-cli/docker_cli_inspect_test.go" "integration-cli/docker_cli_links_test.go" "integration-cli/docker_cli_login_test.go" "integration-cli/docker_cli_logout_test.go" "integration-cli/docker_cli_logs_test.go" "integration-cli/docker_cli_netmode_test.go" "integration-cli/docker_cli_network_unix_test.go" "integration-cli/docker_cli_plugins_logdriver_test.go" "integration-cli/docker_cli_plugins_test.go" "integration-cli/docker_cli_port_test.go" "integration-cli/docker_cli_proxy_test.go" "integration-cli/docker_cli_prune_unix_test.go" "integration-cli/docker_cli_ps_test.go" "integration-cli/docker_cli_pull_local_test.go" "integration-cli/docker_cli_pull_test.go" "integration-cli/docker_cli_push_test.go" "integration-cli/docker_cli_registry_user_agent_test.go" "integration-cli/docker_cli_restart_test.go" "integration-cli/docker_cli_rmi_test.go" "integration-cli/docker_cli_run_test.go" "integration-cli/docker_cli_run_unix_test.go" "integration-cli/docker_cli_save_load_test.go" "integration-cli/docker_cli_save_load_unix_test.go" "integration-cli/docker_cli_search_test.go" "integration-cli/docker_cli_service_create_test.go" "integration-cli/docker_cli_service_health_test.go" "integration-cli/docker_cli_service_logs_test.go" "integration-cli/docker_cli_service_scale_test.go" "integration-cli/docker_cli_sni_test.go" "integration-cli/docker_cli_start_test.go" "integration-cli/docker_cli_stats_test.go" "integration-cli/docker_cli_swarm_test.go" "integration-cli/docker_cli_swarm_unix_test.go" "integration-cli/docker_cli_top_test.go" "integration-cli/docker_cli_update_unix_test.go" "integration-cli/docker_cli_userns_test.go" "integration-cli/docker_cli_v2_only_test.go" "integration-cli/docker_cli_volume_test.go" "integration-cli/docker_deprecated_api_v124_test.go" "integration-cli/docker_deprecated_api_v124_unix_test.go" "integration-cli/docker_hub_pull_suite_test.go" "integration-cli/docker_utils_test.go" "integration-cli/events_utils_test.go" "integration-cli/fixtures_linux_daemon_test.go" "integration-cli/utils_test.go" "pkg/discovery/discovery_test.go" "pkg/discovery/file/file_test.go" "pkg/discovery/generator_test.go" "pkg/discovery/kv/kv_test.go" "pkg/discovery/memory/memory_test.go" "pkg/discovery/nodes/nodes_test.go"

Signed-off-by: Tibor Vass <tibor@docker.com>
2019-09-09 21:05:55 +00:00

240 lines
9.3 KiB
Go

package main
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/runconfig"
"github.com/go-check/check"
"gotest.tools/assert"
)
func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *testing.T) {
testRequires(c, DaemonIsLinux)
_, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
// run ping failed with error
assert.Assert(c, exitCode, checker.Equals, 1, check.Commentf("error: %v", err))
}
// Test for appropriate error when calling --link with an invalid target container
func (s *DockerSuite) TestLinksInvalidContainerTarget(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
// an invalid container target should produce an error
assert.Assert(c, err, checker.NotNil, check.Commentf("out: %s", out))
// an invalid container target should produce an error
// note: convert the output to lowercase first as the error string
// capitalization was changed after API version 1.32
assert.Assert(c, strings.ToLower(out), checker.Contains, "could not get container")
}
func (s *DockerSuite) TestLinksPingLinkedContainers(c *testing.T) {
testRequires(c, DaemonIsLinux)
// Test with the three different ways of specifying the default network on Linux
testLinkPingOnNetwork(c, "")
testLinkPingOnNetwork(c, "default")
testLinkPingOnNetwork(c, "bridge")
}
func testLinkPingOnNetwork(c *testing.T, network string) {
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
dockerCmd(c, runArgs1...)
dockerCmd(c, runArgs2...)
postArgs = []string{}
if network != "" {
postArgs = append(postArgs, []string{"--net", network}...)
}
postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...)
// Format a run for a container which links to the other two
runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...)
pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
// test ping by alias, ping by name, and ping by hostname
// 1. Ping by alias
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
// 2. Ping by container name
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
// 3. Ping by hostname
dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
// Clean for next round
dockerCmd(c, "rm", "-f", "container1")
dockerCmd(c, "rm", "-f", "container2")
}
func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
idA := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
idB := strings.TrimSpace(out)
dockerCmd(c, "rename", "container1", "container_new")
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")
dockerCmd(c, "kill", idA)
dockerCmd(c, "kill", idB)
}
func (s *DockerSuite) TestLinksInspectLinksStarted(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
var result []string
err := json.Unmarshal([]byte(links), &result)
assert.NilError(c, err)
var expected = []string{
"/container1:/testinspectlink/alias1",
"/container2:/testinspectlink/alias2",
}
sort.Strings(result)
assert.DeepEqual(c, result, expected)
}
func (s *DockerSuite) TestLinksInspectLinksStopped(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
var result []string
err := json.Unmarshal([]byte(links), &result)
assert.NilError(c, err)
var expected = []string{
"/container1:/testinspectlink/alias1",
"/container2:/testinspectlink/alias2",
}
sort.Strings(result)
assert.DeepEqual(c, result, expected)
}
func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "create", "--name=first", "busybox", "top")
dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
dockerCmd(c, "start", "first")
}
func (s *DockerSuite) TestLinksHostsFilesInject(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, testEnv.IsLocalDaemon)
out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
idOne := strings.TrimSpace(out)
out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
idTwo := strings.TrimSpace(out)
assert.Assert(c, waitRun(idTwo), checker.IsNil)
readContainerFileWithExec(c, idOne, "/etc/hosts")
contentTwo := readContainerFileWithExec(c, idTwo, "/etc/hosts")
// Host is not present in updated hosts file
assert.Assert(c, string(contentTwo), checker.Contains, "onetwo")
}
func (s *DockerSuite) TestLinksUpdateOnRestart(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, testEnv.IsLocalDaemon)
dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
id := strings.TrimSpace(string(out))
realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
content := readContainerFileWithExec(c, id, "/etc/hosts")
getIP := func(hosts []byte, hostname string) string {
re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
matches := re.FindSubmatch(hosts)
assert.Assert(c, matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname))
return string(matches[1])
}
ip := getIP(content, "one")
assert.Assert(c, ip, checker.Equals, realIP)
ip = getIP(content, "onetwo")
assert.Assert(c, ip, checker.Equals, realIP)
dockerCmd(c, "restart", "one")
realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
content = readContainerFileWithExec(c, id, "/etc/hosts")
ip = getIP(content, "one")
assert.Assert(c, ip, checker.Equals, realIP)
ip = getIP(content, "onetwo")
assert.Assert(c, ip, checker.Equals, realIP)
}
func (s *DockerSuite) TestLinksEnvs(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
assert.Assert(c, out, checker.Contains, "FIRST_ENV_e1=\n")
assert.Assert(c, out, checker.Contains, "FIRST_ENV_e2=v2")
assert.Assert(c, out, checker.Contains, "FIRST_ENV_e3=v3=v3")
}
func (s *DockerSuite) TestLinkShortDefinition(c *testing.T) {
testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
cid := strings.TrimSpace(out)
assert.Assert(c, waitRun(cid), checker.IsNil)
out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
cid2 := strings.TrimSpace(out)
assert.Assert(c, waitRun(cid2), checker.IsNil)
links := inspectFieldJSON(c, cid2, "HostConfig.Links")
assert.Assert(c, links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
}
func (s *DockerSuite) TestLinksNetworkHostContainer(c *testing.T) {
testRequires(c, DaemonIsLinux, NotUserNamespace)
dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
// Running container linking to a container with --net host should have failed
assert.Assert(c, err, checker.NotNil, check.Commentf("out: %s", out))
// Running container linking to a container with --net host should have failed
assert.Assert(c, out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
}
func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *testing.T) {
testRequires(c, DaemonIsLinux, NotUserNamespace)
out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
// /etc/hosts should be a regular file
assert.Assert(c, out, checker.Matches, "^-.+\n")
}
func (s *DockerSuite) TestLinksMultipleWithSameName(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
}