2018-02-05 21:05:59 +00:00
|
|
|
package links // import "github.com/docker/docker/daemon/links"
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
import (
|
2015-01-06 19:46:03 +00:00
|
|
|
"fmt"
|
2022-10-05 14:28:35 +00:00
|
|
|
"strconv"
|
2013-10-05 02:25:15 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-04-12 14:25:10 +00:00
|
|
|
|
2015-12-18 17:58:48 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2013-10-05 02:25:15 +00:00
|
|
|
)
|
|
|
|
|
2015-07-16 03:45:48 +00:00
|
|
|
// Just to make life easier
|
|
|
|
func newPortNoError(proto, port string) nat.Port {
|
|
|
|
p, _ := nat.NewPort(proto, port)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-05-26 07:40:24 +00:00
|
|
|
func TestLinkNaming(t *testing.T) {
|
|
|
|
ports := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
2014-05-26 07:40:24 +00:00
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, ports)
|
2014-05-26 07:40:24 +00:00
|
|
|
|
|
|
|
rawEnv := link.ToEnv()
|
|
|
|
env := make(map[string]string, len(rawEnv))
|
|
|
|
for _, e := range rawEnv {
|
|
|
|
parts := strings.Split(e, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
env[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := env["DOCKER_1_PORT"]
|
|
|
|
|
|
|
|
if !ok {
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("DOCKER_1_PORT not found in env")
|
2014-05-26 07:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if value != "tcp://172.0.17.2:6379" {
|
|
|
|
t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_1_PORT"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 02:25:15 +00:00
|
|
|
func TestLinkNew(t *testing.T) {
|
2014-02-12 00:48:44 +00:00
|
|
|
ports := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
2013-10-05 02:25:15 +00:00
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports)
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
if link.Name != "/db/docker" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if link.ParentIP != "172.0.17.3" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if link.ChildIP != "172.0.17.2" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
for _, p := range link.Ports {
|
2015-07-16 03:45:48 +00:00
|
|
|
if p != newPortNoError("tcp", "6379") {
|
2013-10-05 02:25:15 +00:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkEnv(t *testing.T) {
|
2014-02-12 00:48:44 +00:00
|
|
|
ports := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
2013-10-05 02:25:15 +00:00
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
rawEnv := link.ToEnv()
|
|
|
|
env := make(map[string]string, len(rawEnv))
|
|
|
|
for _, e := range rawEnv {
|
|
|
|
parts := strings.Split(e, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
env[parts[0]] = parts[1]
|
|
|
|
}
|
2013-10-28 23:58:59 +00:00
|
|
|
if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" {
|
2013-10-24 15:57:40 +00:00
|
|
|
t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"])
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP"] != "tcp://172.0.17.2:6379" {
|
|
|
|
t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP"])
|
|
|
|
}
|
2013-10-29 06:02:28 +00:00
|
|
|
if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" {
|
|
|
|
t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" {
|
|
|
|
t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PORT"] != "6379" {
|
|
|
|
t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT"])
|
|
|
|
}
|
2013-10-05 02:25:15 +00:00
|
|
|
if env["DOCKER_NAME"] != "/db/docker" {
|
|
|
|
t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_ENV_PASSWORD"] != "gordon" {
|
|
|
|
t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"])
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 01:08:30 +00:00
|
|
|
|
|
|
|
func TestLinkMultipleEnv(t *testing.T) {
|
|
|
|
ports := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
|
|
|
ports[newPortNoError("tcp", "6380")] = struct{}{}
|
|
|
|
ports[newPortNoError("tcp", "6381")] = struct{}{}
|
2014-09-17 01:08:30 +00:00
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
|
2014-09-17 01:08:30 +00:00
|
|
|
|
|
|
|
rawEnv := link.ToEnv()
|
|
|
|
env := make(map[string]string, len(rawEnv))
|
|
|
|
for _, e := range rawEnv {
|
|
|
|
parts := strings.Split(e, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
env[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" {
|
|
|
|
t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_START"] != "tcp://172.0.17.2:6379" {
|
|
|
|
t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP_START"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_END"] != "tcp://172.0.17.2:6381" {
|
|
|
|
t.Fatalf("Expected tcp://172.0.17.2:6381, got %s", env["DOCKER_PORT_6379_TCP_END"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" {
|
|
|
|
t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" {
|
|
|
|
t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PORT_START"] != "6379" {
|
|
|
|
t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT_START"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PORT_END"] != "6381" {
|
|
|
|
t.Fatalf("Expected 6381, got %s", env["DOCKER_PORT_6379_TCP_PORT_END"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_NAME"] != "/db/docker" {
|
|
|
|
t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_ENV_PASSWORD"] != "gordon" {
|
|
|
|
t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"])
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 19:46:03 +00:00
|
|
|
|
|
|
|
func TestLinkPortRangeEnv(t *testing.T) {
|
|
|
|
ports := make(nat.PortSet)
|
2015-07-16 03:45:48 +00:00
|
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
|
|
|
ports[newPortNoError("tcp", "6380")] = struct{}{}
|
|
|
|
ports[newPortNoError("tcp", "6381")] = struct{}{}
|
2015-01-06 19:46:03 +00:00
|
|
|
|
2015-07-20 22:19:05 +00:00
|
|
|
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
|
2015-01-06 19:46:03 +00:00
|
|
|
|
|
|
|
rawEnv := link.ToEnv()
|
|
|
|
env := make(map[string]string, len(rawEnv))
|
|
|
|
for _, e := range rawEnv {
|
|
|
|
parts := strings.Split(e, "=")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
env[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" {
|
|
|
|
t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_START"] != "tcp://172.0.17.2:6379" {
|
|
|
|
t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP_START"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_END"] != "tcp://172.0.17.2:6381" {
|
|
|
|
t.Fatalf("Expected tcp://172.0.17.2:6381, got %s", env["DOCKER_PORT_6379_TCP_END"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" {
|
|
|
|
t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" {
|
|
|
|
t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PORT_START"] != "6379" {
|
|
|
|
t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT_START"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_PORT_6379_TCP_PORT_END"] != "6381" {
|
|
|
|
t.Fatalf("Expected 6381, got %s", env["DOCKER_PORT_6379_TCP_PORT_END"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_NAME"] != "/db/docker" {
|
|
|
|
t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"])
|
|
|
|
}
|
|
|
|
if env["DOCKER_ENV_PASSWORD"] != "gordon" {
|
|
|
|
t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"])
|
|
|
|
}
|
2016-11-25 09:02:48 +00:00
|
|
|
for _, i := range []int{6379, 6380, 6381} {
|
2015-01-06 19:46:03 +00:00
|
|
|
tcpaddr := fmt.Sprintf("DOCKER_PORT_%d_TCP_ADDR", i)
|
2016-11-25 09:02:48 +00:00
|
|
|
tcpport := fmt.Sprintf("DOCKER_PORT_%d_TCP_PORT", i)
|
|
|
|
tcpproto := fmt.Sprintf("DOCKER_PORT_%d_TCP_PROTO", i)
|
2015-01-06 19:46:03 +00:00
|
|
|
tcp := fmt.Sprintf("DOCKER_PORT_%d_TCP", i)
|
2016-11-25 09:02:48 +00:00
|
|
|
if env[tcpaddr] != "172.0.17.2" {
|
2015-01-06 19:46:03 +00:00
|
|
|
t.Fatalf("Expected env %s = 172.0.17.2, got %s", tcpaddr, env[tcpaddr])
|
|
|
|
}
|
2022-10-05 14:28:35 +00:00
|
|
|
if env[tcpport] != strconv.Itoa(i) {
|
2015-01-06 19:46:03 +00:00
|
|
|
t.Fatalf("Expected env %s = %d, got %s", tcpport, i, env[tcpport])
|
|
|
|
}
|
2016-11-25 09:02:48 +00:00
|
|
|
if env[tcpproto] != "tcp" {
|
2015-01-06 19:46:03 +00:00
|
|
|
t.Fatalf("Expected env %s = tcp, got %s", tcpproto, env[tcpproto])
|
|
|
|
}
|
2016-11-25 09:02:48 +00:00
|
|
|
if env[tcp] != fmt.Sprintf("tcp://172.0.17.2:%d", i) {
|
2015-01-06 19:46:03 +00:00
|
|
|
t.Fatalf("Expected env %s = tcp://172.0.17.2:%d, got %s", tcp, i, env[tcp])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|