Skip to content

Commit

Permalink
Merge pull request moby#49232 from thaJeztah/link_test_cleanup
Browse files Browse the repository at this point in the history
daemon/links: use gotest.tools, remove unneeded utility and duplicated test
  • Loading branch information
thaJeztah authored Jan 8, 2025
2 parents 6ad0bfd + 34ab957 commit 53287e4
Showing 1 changed file with 80 additions and 193 deletions.
273 changes: 80 additions & 193 deletions daemon/links/links_test.go
Original file line number Diff line number Diff line change
@@ -1,214 +1,101 @@
package links // import "github.com/docker/docker/daemon/links"

import (
"fmt"
"strconv"
"strings"
"sort"
"testing"

"github.com/docker/go-connections/nat"
"gotest.tools/v3/assert"
)

// Just to make life easier
func newPortNoError(proto, port string) nat.Port {
p, _ := nat.NewPort(proto, port)
return p
}

func TestLinkNaming(t *testing.T) {
ports := make(nat.PortSet)
ports[newPortNoError("tcp", "6379")] = struct{}{}

link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, ports)

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 {
t.Fatal("DOCKER_1_PORT not found in env")
}

if value != "tcp://172.0.17.2:6379" {
t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_1_PORT"])
}
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, nat.PortSet{
"6379/tcp": struct{}{},
})

expectedEnv := []string{
"DOCKER_1_NAME=/db/docker-1",
"DOCKER_1_PORT=tcp://172.0.17.2:6379",
"DOCKER_1_PORT_6379_TCP=tcp://172.0.17.2:6379",
"DOCKER_1_PORT_6379_TCP_ADDR=172.0.17.2",
"DOCKER_1_PORT_6379_TCP_PORT=6379",
"DOCKER_1_PORT_6379_TCP_PROTO=tcp",
}

actual := link.ToEnv()
sort.Strings(actual) // order of env-vars is not relevant
assert.DeepEqual(t, expectedEnv, actual)
}

func TestLinkNew(t *testing.T) {
ports := make(nat.PortSet)
ports[newPortNoError("tcp", "6379")] = struct{}{}

link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports)
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, nat.PortSet{
"6379/tcp": struct{}{},
})

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 {
if p != newPortNoError("tcp", "6379") {
t.Fail()
}
expected := &Link{
Name: "/db/docker",
ParentIP: "172.0.17.3",
ChildIP: "172.0.17.2",
Ports: []nat.Port{"6379/tcp"},
}

assert.DeepEqual(t, expected, link)
}

func TestLinkEnv(t *testing.T) {
ports := make(nat.PortSet)
ports[newPortNoError("tcp", "6379")] = struct{}{}

link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)

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"] != "tcp://172.0.17.2:6379" {
t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP"])
}
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"])
}
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"])
}
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, nat.PortSet{
"6379/tcp": struct{}{},
})

expectedEnv := []string{
"DOCKER_ENV_PASSWORD=gordon",
"DOCKER_NAME=/db/docker",
"DOCKER_PORT=tcp://172.0.17.2:6379",
"DOCKER_PORT_6379_TCP=tcp://172.0.17.2:6379",
"DOCKER_PORT_6379_TCP_ADDR=172.0.17.2",
"DOCKER_PORT_6379_TCP_PORT=6379",
"DOCKER_PORT_6379_TCP_PROTO=tcp",
}

actual := link.ToEnv()
sort.Strings(actual) // order of env-vars is not relevant
assert.DeepEqual(t, expectedEnv, actual)
}

func TestLinkMultipleEnv(t *testing.T) {
ports := make(nat.PortSet)
ports[newPortNoError("tcp", "6379")] = struct{}{}
ports[newPortNoError("tcp", "6380")] = struct{}{}
ports[newPortNoError("tcp", "6381")] = struct{}{}

link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)

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"])
}
}

func TestLinkPortRangeEnv(t *testing.T) {
ports := make(nat.PortSet)
ports[newPortNoError("tcp", "6379")] = struct{}{}
ports[newPortNoError("tcp", "6380")] = struct{}{}
ports[newPortNoError("tcp", "6381")] = struct{}{}

link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)

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"])
}
for _, i := range []int{6379, 6380, 6381} {
tcpaddr := fmt.Sprintf("DOCKER_PORT_%d_TCP_ADDR", i)
tcpport := fmt.Sprintf("DOCKER_PORT_%d_TCP_PORT", i)
tcpproto := fmt.Sprintf("DOCKER_PORT_%d_TCP_PROTO", i)
tcp := fmt.Sprintf("DOCKER_PORT_%d_TCP", i)
if env[tcpaddr] != "172.0.17.2" {
t.Fatalf("Expected env %s = 172.0.17.2, got %s", tcpaddr, env[tcpaddr])
}
if env[tcpport] != strconv.Itoa(i) {
t.Fatalf("Expected env %s = %d, got %s", tcpport, i, env[tcpport])
}
if env[tcpproto] != "tcp" {
t.Fatalf("Expected env %s = tcp, got %s", tcpproto, env[tcpproto])
}
if env[tcp] != fmt.Sprintf("tcp://172.0.17.2:%d", i) {
t.Fatalf("Expected env %s = tcp://172.0.17.2:%d, got %s", tcp, i, env[tcp])
}
}
link := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, nat.PortSet{
"6379/tcp": struct{}{},
"6380/tcp": struct{}{},
"6381/tcp": struct{}{},
})

expectedEnv := []string{
"DOCKER_ENV_PASSWORD=gordon",
"DOCKER_NAME=/db/docker",
"DOCKER_PORT=tcp://172.0.17.2:6379",
"DOCKER_PORT_6379_TCP=tcp://172.0.17.2:6379",
"DOCKER_PORT_6379_TCP_ADDR=172.0.17.2",
"DOCKER_PORT_6379_TCP_ADDR=172.0.17.2", // FIXME(thaJeztah): duplicate?
"DOCKER_PORT_6379_TCP_END=tcp://172.0.17.2:6381",
"DOCKER_PORT_6379_TCP_PORT=6379",
"DOCKER_PORT_6379_TCP_PORT_END=6381",
"DOCKER_PORT_6379_TCP_PORT_START=6379",
"DOCKER_PORT_6379_TCP_PROTO=tcp",
"DOCKER_PORT_6379_TCP_PROTO=tcp", // FIXME(thaJeztah): duplicate?
"DOCKER_PORT_6379_TCP_START=tcp://172.0.17.2:6379",

"DOCKER_PORT_6380_TCP=tcp://172.0.17.2:6380",
"DOCKER_PORT_6380_TCP_ADDR=172.0.17.2",
"DOCKER_PORT_6380_TCP_PORT=6380",
"DOCKER_PORT_6380_TCP_PROTO=tcp",

"DOCKER_PORT_6381_TCP=tcp://172.0.17.2:6381",
"DOCKER_PORT_6381_TCP_ADDR=172.0.17.2",
"DOCKER_PORT_6381_TCP_PORT=6381",
"DOCKER_PORT_6381_TCP_PROTO=tcp",
}

actual := link.ToEnv()
sort.Strings(actual) // order of env-vars is not relevant
assert.DeepEqual(t, expectedEnv, actual)
}

0 comments on commit 53287e4

Please sign in to comment.