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