mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
12b6083c8f
Closes #14621 This one grew to be much more than I expected so here's the story... :-) - when a bad port string (e.g. xxx80) is passed into container.create() via the API it wasn't being checked until we tried to start the container. - While starting the container we trid to parse 'xxx80' in nat.Int() and would panic on the strconv.ParseUint(). We should (almost) never panic. - In trying to remove the panic I decided to make it so that we, instead, checked the string during the NewPort() constructor. This means that I had to change all casts from 'string' to 'Port' to use NewPort() instead. Which is a good thing anyway, people shouldn't assume they know the internal format of types like that, in general. - This meant I had to go and add error checks on all calls to NewPort(). To avoid changing the testcases too much I create newPortNoError() **JUST** for the testcase uses where we know the port string is ok. - After all of that I then went back and added a check during container.create() to check the port string so we'll report the error as soon as we get the data. - If, somehow, the bad string does get into the metadata we will generate an error during container.start() but I can't test for that because the container.create() catches it now. But I did add a testcase for that. Signed-off-by: Doug Davis <dug@us.ibm.com>
234 lines
6.8 KiB
Go
234 lines
6.8 KiB
Go
package links
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/pkg/nat"
|
|
)
|
|
|
|
// 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, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, ports)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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.Fatalf("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"])
|
|
}
|
|
}
|
|
|
|
func TestLinkNew(t *testing.T) {
|
|
ports := make(nat.PortSet)
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
|
|
|
link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if link == nil {
|
|
t.FailNow()
|
|
}
|
|
if link.Name != "/db/docker" {
|
|
t.Fail()
|
|
}
|
|
if link.Alias() != "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()
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLinkEnv(t *testing.T) {
|
|
ports := make(nat.PortSet)
|
|
ports[newPortNoError("tcp", "6379")] = struct{}{}
|
|
|
|
link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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"])
|
|
}
|
|
}
|
|
|
|
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, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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] == fmt.Sprintf("%d", 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])
|
|
}
|
|
}
|
|
}
|