github.com/torfuzx/docker@v1.8.1/pkg/nat/nat_test.go (about) 1 package nat 2 3 import ( 4 "testing" 5 ) 6 7 func TestParsePort(t *testing.T) { 8 var ( 9 p int 10 err error 11 ) 12 13 p, err = ParsePort("1234") 14 15 if err != nil || p != 1234 { 16 t.Fatal("Parsing '1234' did not succeed") 17 } 18 19 // FIXME currently this is a valid port. I don't think it should be. 20 // I'm leaving this test commented out until we make a decision. 21 // - erikh 22 23 /* 24 p, err = ParsePort("0123") 25 26 if err != nil { 27 t.Fatal("Successfully parsed port '0123' to '123'") 28 } 29 */ 30 31 p, err = ParsePort("asdf") 32 33 if err == nil || p != 0 { 34 t.Fatal("Parsing port 'asdf' succeeded") 35 } 36 37 p, err = ParsePort("1asdf") 38 39 if err == nil || p != 0 { 40 t.Fatal("Parsing port '1asdf' succeeded") 41 } 42 } 43 44 func TestPort(t *testing.T) { 45 p, err := NewPort("tcp", "1234") 46 47 if err != nil { 48 t.Fatalf("tcp, 1234 had a parsing issue: %v", err) 49 } 50 51 if string(p) != "1234/tcp" { 52 t.Fatal("tcp, 1234 did not result in the string 1234/tcp") 53 } 54 55 if p.Proto() != "tcp" { 56 t.Fatal("protocol was not tcp") 57 } 58 59 if p.Port() != "1234" { 60 t.Fatal("port string value was not 1234") 61 } 62 63 if p.Int() != 1234 { 64 t.Fatal("port int value was not 1234") 65 } 66 67 p, err = NewPort("tcp", "asd1234") 68 if err == nil { 69 t.Fatal("tcp, asd1234 was supposed to fail") 70 } 71 } 72 73 func TestSplitProtoPort(t *testing.T) { 74 var ( 75 proto string 76 port string 77 ) 78 79 proto, port = SplitProtoPort("1234/tcp") 80 81 if proto != "tcp" || port != "1234" { 82 t.Fatal("Could not split 1234/tcp properly") 83 } 84 85 proto, port = SplitProtoPort("") 86 87 if proto != "" || port != "" { 88 t.Fatal("parsing an empty string yielded surprising results", proto, port) 89 } 90 91 proto, port = SplitProtoPort("1234") 92 93 if proto != "tcp" || port != "1234" { 94 t.Fatal("tcp is not the default protocol for portspec '1234'", proto, port) 95 } 96 97 proto, port = SplitProtoPort("1234/") 98 99 if proto != "tcp" || port != "1234" { 100 t.Fatal("parsing '1234/' yielded:" + port + "/" + proto) 101 } 102 103 proto, port = SplitProtoPort("/tcp") 104 105 if proto != "" || port != "" { 106 t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto) 107 } 108 } 109 110 func TestParsePortSpecs(t *testing.T) { 111 var ( 112 portMap map[Port]struct{} 113 bindingMap map[Port][]PortBinding 114 err error 115 ) 116 117 portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"}) 118 119 if err != nil { 120 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 121 } 122 123 if _, ok := portMap[Port("1234/tcp")]; !ok { 124 t.Fatal("1234/tcp was not parsed properly") 125 } 126 127 if _, ok := portMap[Port("2345/udp")]; !ok { 128 t.Fatal("2345/udp was not parsed properly") 129 } 130 131 for portspec, bindings := range bindingMap { 132 if len(bindings) != 1 { 133 t.Fatalf("%s should have exactly one binding", portspec) 134 } 135 136 if bindings[0].HostIP != "" { 137 t.Fatalf("HostIP should not be set for %s", portspec) 138 } 139 140 if bindings[0].HostPort != "" { 141 t.Fatalf("HostPort should not be set for %s", portspec) 142 } 143 } 144 145 portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"}) 146 147 if err != nil { 148 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 149 } 150 151 if _, ok := portMap[Port("1234/tcp")]; !ok { 152 t.Fatal("1234/tcp was not parsed properly") 153 } 154 155 if _, ok := portMap[Port("2345/udp")]; !ok { 156 t.Fatal("2345/udp was not parsed properly") 157 } 158 159 for portspec, bindings := range bindingMap { 160 _, port := SplitProtoPort(string(portspec)) 161 162 if len(bindings) != 1 { 163 t.Fatalf("%s should have exactly one binding", portspec) 164 } 165 166 if bindings[0].HostIP != "" { 167 t.Fatalf("HostIP should not be set for %s", portspec) 168 } 169 170 if bindings[0].HostPort != port { 171 t.Fatalf("HostPort should be %s for %s", port, portspec) 172 } 173 } 174 175 portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"}) 176 177 if err != nil { 178 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 179 } 180 181 if _, ok := portMap[Port("1234/tcp")]; !ok { 182 t.Fatal("1234/tcp was not parsed properly") 183 } 184 185 if _, ok := portMap[Port("2345/udp")]; !ok { 186 t.Fatal("2345/udp was not parsed properly") 187 } 188 189 for portspec, bindings := range bindingMap { 190 _, port := SplitProtoPort(string(portspec)) 191 192 if len(bindings) != 1 { 193 t.Fatalf("%s should have exactly one binding", portspec) 194 } 195 196 if bindings[0].HostIP != "0.0.0.0" { 197 t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec) 198 } 199 200 if bindings[0].HostPort != port { 201 t.Fatalf("HostPort should be %s for %s", port, portspec) 202 } 203 } 204 205 _, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"}) 206 207 if err == nil { 208 t.Fatal("Received no error while trying to parse a hostname instead of ip") 209 } 210 } 211 212 func TestParsePortSpecsWithRange(t *testing.T) { 213 var ( 214 portMap map[Port]struct{} 215 bindingMap map[Port][]PortBinding 216 err error 217 ) 218 219 portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp"}) 220 221 if err != nil { 222 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 223 } 224 225 if _, ok := portMap[Port("1235/tcp")]; !ok { 226 t.Fatal("1234/tcp was not parsed properly") 227 } 228 229 if _, ok := portMap[Port("2346/udp")]; !ok { 230 t.Fatal("2345/udp was not parsed properly") 231 } 232 233 for portspec, bindings := range bindingMap { 234 if len(bindings) != 1 { 235 t.Fatalf("%s should have exactly one binding", portspec) 236 } 237 238 if bindings[0].HostIP != "" { 239 t.Fatalf("HostIP should not be set for %s", portspec) 240 } 241 242 if bindings[0].HostPort != "" { 243 t.Fatalf("HostPort should not be set for %s", portspec) 244 } 245 } 246 247 portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp"}) 248 249 if err != nil { 250 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 251 } 252 253 if _, ok := portMap[Port("1235/tcp")]; !ok { 254 t.Fatal("1234/tcp was not parsed properly") 255 } 256 257 if _, ok := portMap[Port("2346/udp")]; !ok { 258 t.Fatal("2345/udp was not parsed properly") 259 } 260 261 for portspec, bindings := range bindingMap { 262 _, port := SplitProtoPort(string(portspec)) 263 if len(bindings) != 1 { 264 t.Fatalf("%s should have exactly one binding", portspec) 265 } 266 267 if bindings[0].HostIP != "" { 268 t.Fatalf("HostIP should not be set for %s", portspec) 269 } 270 271 if bindings[0].HostPort != port { 272 t.Fatalf("HostPort should be %s for %s", port, portspec) 273 } 274 } 275 276 portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp"}) 277 278 if err != nil { 279 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 280 } 281 282 if _, ok := portMap[Port("1235/tcp")]; !ok { 283 t.Fatal("1234/tcp was not parsed properly") 284 } 285 286 if _, ok := portMap[Port("2346/udp")]; !ok { 287 t.Fatal("2345/udp was not parsed properly") 288 } 289 290 for portspec, bindings := range bindingMap { 291 _, port := SplitProtoPort(string(portspec)) 292 if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port { 293 t.Fatalf("Expect single binding to port %s but found %s", port, bindings) 294 } 295 } 296 297 _, _, err = ParsePortSpecs([]string{"localhost:1234-1236:1234-1236/tcp"}) 298 299 if err == nil { 300 t.Fatal("Received no error while trying to parse a hostname instead of ip") 301 } 302 } 303 304 func TestParseNetworkOptsPrivateOnly(t *testing.T) { 305 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::80"}) 306 if err != nil { 307 t.Fatal(err) 308 } 309 if len(ports) != 1 { 310 t.Logf("Expected 1 got %d", len(ports)) 311 t.FailNow() 312 } 313 if len(bindings) != 1 { 314 t.Logf("Expected 1 got %d", len(bindings)) 315 t.FailNow() 316 } 317 for k := range ports { 318 if k.Proto() != "tcp" { 319 t.Logf("Expected tcp got %s", k.Proto()) 320 t.Fail() 321 } 322 if k.Port() != "80" { 323 t.Logf("Expected 80 got %s", k.Port()) 324 t.Fail() 325 } 326 b, exists := bindings[k] 327 if !exists { 328 t.Log("Binding does not exist") 329 t.FailNow() 330 } 331 if len(b) != 1 { 332 t.Logf("Expected 1 got %d", len(b)) 333 t.FailNow() 334 } 335 s := b[0] 336 if s.HostPort != "" { 337 t.Logf("Expected \"\" got %s", s.HostPort) 338 t.Fail() 339 } 340 if s.HostIP != "192.168.1.100" { 341 t.Fail() 342 } 343 } 344 } 345 346 func TestParseNetworkOptsPublic(t *testing.T) { 347 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:8080:80"}) 348 if err != nil { 349 t.Fatal(err) 350 } 351 if len(ports) != 1 { 352 t.Logf("Expected 1 got %d", len(ports)) 353 t.FailNow() 354 } 355 if len(bindings) != 1 { 356 t.Logf("Expected 1 got %d", len(bindings)) 357 t.FailNow() 358 } 359 for k := range ports { 360 if k.Proto() != "tcp" { 361 t.Logf("Expected tcp got %s", k.Proto()) 362 t.Fail() 363 } 364 if k.Port() != "80" { 365 t.Logf("Expected 80 got %s", k.Port()) 366 t.Fail() 367 } 368 b, exists := bindings[k] 369 if !exists { 370 t.Log("Binding does not exist") 371 t.FailNow() 372 } 373 if len(b) != 1 { 374 t.Logf("Expected 1 got %d", len(b)) 375 t.FailNow() 376 } 377 s := b[0] 378 if s.HostPort != "8080" { 379 t.Logf("Expected 8080 got %s", s.HostPort) 380 t.Fail() 381 } 382 if s.HostIP != "192.168.1.100" { 383 t.Fail() 384 } 385 } 386 } 387 388 func TestParseNetworkOptsPublicNoPort(t *testing.T) { 389 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100"}) 390 391 if err == nil { 392 t.Logf("Expected error Invalid containerPort") 393 t.Fail() 394 } 395 if ports != nil { 396 t.Logf("Expected nil got %s", ports) 397 t.Fail() 398 } 399 if bindings != nil { 400 t.Logf("Expected nil got %s", bindings) 401 t.Fail() 402 } 403 } 404 405 func TestParseNetworkOptsNegativePorts(t *testing.T) { 406 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:-1:-1"}) 407 408 if err == nil { 409 t.Fail() 410 } 411 if len(ports) != 0 { 412 t.Logf("Expected nil got %s", len(ports)) 413 t.Fail() 414 } 415 if len(bindings) != 0 { 416 t.Logf("Expected 0 got %s", len(bindings)) 417 t.Fail() 418 } 419 } 420 421 func TestParseNetworkOptsUdp(t *testing.T) { 422 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::6000/udp"}) 423 if err != nil { 424 t.Fatal(err) 425 } 426 if len(ports) != 1 { 427 t.Logf("Expected 1 got %d", len(ports)) 428 t.FailNow() 429 } 430 if len(bindings) != 1 { 431 t.Logf("Expected 1 got %d", len(bindings)) 432 t.FailNow() 433 } 434 for k := range ports { 435 if k.Proto() != "udp" { 436 t.Logf("Expected udp got %s", k.Proto()) 437 t.Fail() 438 } 439 if k.Port() != "6000" { 440 t.Logf("Expected 6000 got %s", k.Port()) 441 t.Fail() 442 } 443 b, exists := bindings[k] 444 if !exists { 445 t.Log("Binding does not exist") 446 t.FailNow() 447 } 448 if len(b) != 1 { 449 t.Logf("Expected 1 got %d", len(b)) 450 t.FailNow() 451 } 452 s := b[0] 453 if s.HostPort != "" { 454 t.Logf("Expected \"\" got %s", s.HostPort) 455 t.Fail() 456 } 457 if s.HostIP != "192.168.1.100" { 458 t.Fail() 459 } 460 } 461 }