github.com/wulonghui/docker@v1.8.0-rc2/links/links_test.go (about) 1 package links 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/docker/docker/pkg/nat" 9 ) 10 11 // Just to make life easier 12 func newPortNoError(proto, port string) nat.Port { 13 p, _ := nat.NewPort(proto, port) 14 return p 15 } 16 17 func TestLinkNaming(t *testing.T) { 18 ports := make(nat.PortSet) 19 ports[newPortNoError("tcp", "6379")] = struct{}{} 20 21 link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker-1", nil, ports) 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 rawEnv := link.ToEnv() 27 env := make(map[string]string, len(rawEnv)) 28 for _, e := range rawEnv { 29 parts := strings.Split(e, "=") 30 if len(parts) != 2 { 31 t.FailNow() 32 } 33 env[parts[0]] = parts[1] 34 } 35 36 value, ok := env["DOCKER_1_PORT"] 37 38 if !ok { 39 t.Fatalf("DOCKER_1_PORT not found in env") 40 } 41 42 if value != "tcp://172.0.17.2:6379" { 43 t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_1_PORT"]) 44 } 45 } 46 47 func TestLinkNew(t *testing.T) { 48 ports := make(nat.PortSet) 49 ports[newPortNoError("tcp", "6379")] = struct{}{} 50 51 link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 if link == nil { 57 t.FailNow() 58 } 59 if link.Name != "/db/docker" { 60 t.Fail() 61 } 62 if link.Alias() != "docker" { 63 t.Fail() 64 } 65 if link.ParentIP != "172.0.17.3" { 66 t.Fail() 67 } 68 if link.ChildIP != "172.0.17.2" { 69 t.Fail() 70 } 71 for _, p := range link.Ports { 72 if p != newPortNoError("tcp", "6379") { 73 t.Fail() 74 } 75 } 76 } 77 78 func TestLinkEnv(t *testing.T) { 79 ports := make(nat.PortSet) 80 ports[newPortNoError("tcp", "6379")] = struct{}{} 81 82 link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 rawEnv := link.ToEnv() 88 env := make(map[string]string, len(rawEnv)) 89 for _, e := range rawEnv { 90 parts := strings.Split(e, "=") 91 if len(parts) != 2 { 92 t.FailNow() 93 } 94 env[parts[0]] = parts[1] 95 } 96 if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" { 97 t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"]) 98 } 99 if env["DOCKER_PORT_6379_TCP"] != "tcp://172.0.17.2:6379" { 100 t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP"]) 101 } 102 if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" { 103 t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"]) 104 } 105 if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" { 106 t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"]) 107 } 108 if env["DOCKER_PORT_6379_TCP_PORT"] != "6379" { 109 t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT"]) 110 } 111 if env["DOCKER_NAME"] != "/db/docker" { 112 t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"]) 113 } 114 if env["DOCKER_ENV_PASSWORD"] != "gordon" { 115 t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"]) 116 } 117 } 118 119 func TestLinkMultipleEnv(t *testing.T) { 120 ports := make(nat.PortSet) 121 ports[newPortNoError("tcp", "6379")] = struct{}{} 122 ports[newPortNoError("tcp", "6380")] = struct{}{} 123 ports[newPortNoError("tcp", "6381")] = struct{}{} 124 125 link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports) 126 if err != nil { 127 t.Fatal(err) 128 } 129 130 rawEnv := link.ToEnv() 131 env := make(map[string]string, len(rawEnv)) 132 for _, e := range rawEnv { 133 parts := strings.Split(e, "=") 134 if len(parts) != 2 { 135 t.FailNow() 136 } 137 env[parts[0]] = parts[1] 138 } 139 if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" { 140 t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"]) 141 } 142 if env["DOCKER_PORT_6379_TCP_START"] != "tcp://172.0.17.2:6379" { 143 t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP_START"]) 144 } 145 if env["DOCKER_PORT_6379_TCP_END"] != "tcp://172.0.17.2:6381" { 146 t.Fatalf("Expected tcp://172.0.17.2:6381, got %s", env["DOCKER_PORT_6379_TCP_END"]) 147 } 148 if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" { 149 t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"]) 150 } 151 if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" { 152 t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"]) 153 } 154 if env["DOCKER_PORT_6379_TCP_PORT_START"] != "6379" { 155 t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT_START"]) 156 } 157 if env["DOCKER_PORT_6379_TCP_PORT_END"] != "6381" { 158 t.Fatalf("Expected 6381, got %s", env["DOCKER_PORT_6379_TCP_PORT_END"]) 159 } 160 if env["DOCKER_NAME"] != "/db/docker" { 161 t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"]) 162 } 163 if env["DOCKER_ENV_PASSWORD"] != "gordon" { 164 t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"]) 165 } 166 } 167 168 func TestLinkPortRangeEnv(t *testing.T) { 169 ports := make(nat.PortSet) 170 ports[newPortNoError("tcp", "6379")] = struct{}{} 171 ports[newPortNoError("tcp", "6380")] = struct{}{} 172 ports[newPortNoError("tcp", "6381")] = struct{}{} 173 174 link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports) 175 if err != nil { 176 t.Fatal(err) 177 } 178 179 rawEnv := link.ToEnv() 180 env := make(map[string]string, len(rawEnv)) 181 for _, e := range rawEnv { 182 parts := strings.Split(e, "=") 183 if len(parts) != 2 { 184 t.FailNow() 185 } 186 env[parts[0]] = parts[1] 187 } 188 189 if env["DOCKER_PORT"] != "tcp://172.0.17.2:6379" { 190 t.Fatalf("Expected 172.0.17.2:6379, got %s", env["DOCKER_PORT"]) 191 } 192 if env["DOCKER_PORT_6379_TCP_START"] != "tcp://172.0.17.2:6379" { 193 t.Fatalf("Expected tcp://172.0.17.2:6379, got %s", env["DOCKER_PORT_6379_TCP_START"]) 194 } 195 if env["DOCKER_PORT_6379_TCP_END"] != "tcp://172.0.17.2:6381" { 196 t.Fatalf("Expected tcp://172.0.17.2:6381, got %s", env["DOCKER_PORT_6379_TCP_END"]) 197 } 198 if env["DOCKER_PORT_6379_TCP_PROTO"] != "tcp" { 199 t.Fatalf("Expected tcp, got %s", env["DOCKER_PORT_6379_TCP_PROTO"]) 200 } 201 if env["DOCKER_PORT_6379_TCP_ADDR"] != "172.0.17.2" { 202 t.Fatalf("Expected 172.0.17.2, got %s", env["DOCKER_PORT_6379_TCP_ADDR"]) 203 } 204 if env["DOCKER_PORT_6379_TCP_PORT_START"] != "6379" { 205 t.Fatalf("Expected 6379, got %s", env["DOCKER_PORT_6379_TCP_PORT_START"]) 206 } 207 if env["DOCKER_PORT_6379_TCP_PORT_END"] != "6381" { 208 t.Fatalf("Expected 6381, got %s", env["DOCKER_PORT_6379_TCP_PORT_END"]) 209 } 210 if env["DOCKER_NAME"] != "/db/docker" { 211 t.Fatalf("Expected /db/docker, got %s", env["DOCKER_NAME"]) 212 } 213 if env["DOCKER_ENV_PASSWORD"] != "gordon" { 214 t.Fatalf("Expected gordon, got %s", env["DOCKER_ENV_PASSWORD"]) 215 } 216 for i := range []int{6379, 6380, 6381} { 217 tcpaddr := fmt.Sprintf("DOCKER_PORT_%d_TCP_ADDR", i) 218 tcpport := fmt.Sprintf("DOCKER_PORT_%d_TCP+PORT", i) 219 tcpproto := fmt.Sprintf("DOCKER_PORT_%d_TCP+PROTO", i) 220 tcp := fmt.Sprintf("DOCKER_PORT_%d_TCP", i) 221 if env[tcpaddr] == "172.0.17.2" { 222 t.Fatalf("Expected env %s = 172.0.17.2, got %s", tcpaddr, env[tcpaddr]) 223 } 224 if env[tcpport] == fmt.Sprintf("%d", i) { 225 t.Fatalf("Expected env %s = %d, got %s", tcpport, i, env[tcpport]) 226 } 227 if env[tcpproto] == "tcp" { 228 t.Fatalf("Expected env %s = tcp, got %s", tcpproto, env[tcpproto]) 229 } 230 if env[tcp] == fmt.Sprintf("tcp://172.0.17.2:%d", i) { 231 t.Fatalf("Expected env %s = tcp://172.0.17.2:%d, got %s", tcp, i, env[tcp]) 232 } 233 } 234 }