github.com/goern/docker@v1.9.0-rc1/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 TestParsePortRange(t *testing.T) { 45 var ( 46 begin int 47 end int 48 err error 49 ) 50 51 type TestRange struct { 52 Range string 53 Begin int 54 End int 55 } 56 validRanges := []TestRange{ 57 {"1234", 1234, 1234}, 58 {"1234-1234", 1234, 1234}, 59 {"1234-1235", 1234, 1235}, 60 {"8000-9000", 8000, 9000}, 61 {"0", 0, 0}, 62 {"0-0", 0, 0}, 63 } 64 65 for _, r := range validRanges { 66 begin, end, err = ParsePortRange(r.Range) 67 68 if err != nil || begin != r.Begin { 69 t.Fatalf("Parsing port range '%s' did not succeed. Expected begin %d, got %d", r.Range, r.Begin, begin) 70 } 71 if err != nil || end != r.End { 72 t.Fatalf("Parsing port range '%s' did not succeed. Expected end %d, got %d", r.Range, r.End, end) 73 } 74 } 75 76 invalidRanges := []string{ 77 "asdf", 78 "1asdf", 79 "9000-8000", 80 "9000-", 81 "-8000", 82 "-8000-", 83 } 84 85 for _, r := range invalidRanges { 86 begin, end, err = ParsePortRange(r) 87 88 if err == nil || begin != 0 || end != 0 { 89 t.Fatalf("Parsing port range '%s' succeeded", r) 90 } 91 } 92 } 93 94 func TestPort(t *testing.T) { 95 p, err := NewPort("tcp", "1234") 96 97 if err != nil { 98 t.Fatalf("tcp, 1234 had a parsing issue: %v", err) 99 } 100 101 if string(p) != "1234/tcp" { 102 t.Fatal("tcp, 1234 did not result in the string 1234/tcp") 103 } 104 105 if p.Proto() != "tcp" { 106 t.Fatal("protocol was not tcp") 107 } 108 109 if p.Port() != "1234" { 110 t.Fatal("port string value was not 1234") 111 } 112 113 if p.Int() != 1234 { 114 t.Fatal("port int value was not 1234") 115 } 116 117 p, err = NewPort("tcp", "asd1234") 118 if err == nil { 119 t.Fatal("tcp, asd1234 was supposed to fail") 120 } 121 122 p, err = NewPort("tcp", "1234-1230") 123 if err == nil { 124 t.Fatal("tcp, 1234-1230 was supposed to fail") 125 } 126 127 p, err = NewPort("tcp", "1234-1242") 128 if err != nil { 129 t.Fatalf("tcp, 1234-1242 had a parsing issue: %v", err) 130 } 131 132 if string(p) != "1234-1242/tcp" { 133 t.Fatal("tcp, 1234-1242 did not result in the string 1234-1242/tcp") 134 } 135 } 136 137 func TestSplitProtoPort(t *testing.T) { 138 var ( 139 proto string 140 port string 141 ) 142 143 proto, port = SplitProtoPort("1234/tcp") 144 145 if proto != "tcp" || port != "1234" { 146 t.Fatal("Could not split 1234/tcp properly") 147 } 148 149 proto, port = SplitProtoPort("") 150 151 if proto != "" || port != "" { 152 t.Fatal("parsing an empty string yielded surprising results", proto, port) 153 } 154 155 proto, port = SplitProtoPort("1234") 156 157 if proto != "tcp" || port != "1234" { 158 t.Fatal("tcp is not the default protocol for portspec '1234'", proto, port) 159 } 160 161 proto, port = SplitProtoPort("1234/") 162 163 if proto != "tcp" || port != "1234" { 164 t.Fatal("parsing '1234/' yielded:" + port + "/" + proto) 165 } 166 167 proto, port = SplitProtoPort("/tcp") 168 169 if proto != "" || port != "" { 170 t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto) 171 } 172 } 173 174 func TestParsePortSpecs(t *testing.T) { 175 var ( 176 portMap map[Port]struct{} 177 bindingMap map[Port][]PortBinding 178 err error 179 ) 180 181 portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"}) 182 183 if err != nil { 184 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 185 } 186 187 if _, ok := portMap[Port("1234/tcp")]; !ok { 188 t.Fatal("1234/tcp was not parsed properly") 189 } 190 191 if _, ok := portMap[Port("2345/udp")]; !ok { 192 t.Fatal("2345/udp was not parsed properly") 193 } 194 195 for portspec, bindings := range bindingMap { 196 if len(bindings) != 1 { 197 t.Fatalf("%s should have exactly one binding", portspec) 198 } 199 200 if bindings[0].HostIP != "" { 201 t.Fatalf("HostIP should not be set for %s", portspec) 202 } 203 204 if bindings[0].HostPort != "" { 205 t.Fatalf("HostPort should not be set for %s", portspec) 206 } 207 } 208 209 portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"}) 210 211 if err != nil { 212 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 213 } 214 215 if _, ok := portMap[Port("1234/tcp")]; !ok { 216 t.Fatal("1234/tcp was not parsed properly") 217 } 218 219 if _, ok := portMap[Port("2345/udp")]; !ok { 220 t.Fatal("2345/udp was not parsed properly") 221 } 222 223 for portspec, bindings := range bindingMap { 224 _, port := SplitProtoPort(string(portspec)) 225 226 if len(bindings) != 1 { 227 t.Fatalf("%s should have exactly one binding", portspec) 228 } 229 230 if bindings[0].HostIP != "" { 231 t.Fatalf("HostIP should not be set for %s", portspec) 232 } 233 234 if bindings[0].HostPort != port { 235 t.Fatalf("HostPort should be %s for %s", port, portspec) 236 } 237 } 238 239 portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"}) 240 241 if err != nil { 242 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 243 } 244 245 if _, ok := portMap[Port("1234/tcp")]; !ok { 246 t.Fatal("1234/tcp was not parsed properly") 247 } 248 249 if _, ok := portMap[Port("2345/udp")]; !ok { 250 t.Fatal("2345/udp was not parsed properly") 251 } 252 253 for portspec, bindings := range bindingMap { 254 _, port := SplitProtoPort(string(portspec)) 255 256 if len(bindings) != 1 { 257 t.Fatalf("%s should have exactly one binding", portspec) 258 } 259 260 if bindings[0].HostIP != "0.0.0.0" { 261 t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec) 262 } 263 264 if bindings[0].HostPort != port { 265 t.Fatalf("HostPort should be %s for %s", port, portspec) 266 } 267 } 268 269 _, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"}) 270 271 if err == nil { 272 t.Fatal("Received no error while trying to parse a hostname instead of ip") 273 } 274 } 275 276 func TestParsePortSpecsWithRange(t *testing.T) { 277 var ( 278 portMap map[Port]struct{} 279 bindingMap map[Port][]PortBinding 280 err error 281 ) 282 283 portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp"}) 284 285 if err != nil { 286 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 287 } 288 289 if _, ok := portMap[Port("1235/tcp")]; !ok { 290 t.Fatal("1234/tcp was not parsed properly") 291 } 292 293 if _, ok := portMap[Port("2346/udp")]; !ok { 294 t.Fatal("2345/udp was not parsed properly") 295 } 296 297 for portspec, bindings := range bindingMap { 298 if len(bindings) != 1 { 299 t.Fatalf("%s should have exactly one binding", portspec) 300 } 301 302 if bindings[0].HostIP != "" { 303 t.Fatalf("HostIP should not be set for %s", portspec) 304 } 305 306 if bindings[0].HostPort != "" { 307 t.Fatalf("HostPort should not be set for %s", portspec) 308 } 309 } 310 311 portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp"}) 312 313 if err != nil { 314 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 315 } 316 317 if _, ok := portMap[Port("1235/tcp")]; !ok { 318 t.Fatal("1234/tcp was not parsed properly") 319 } 320 321 if _, ok := portMap[Port("2346/udp")]; !ok { 322 t.Fatal("2345/udp was not parsed properly") 323 } 324 325 for portspec, bindings := range bindingMap { 326 _, port := SplitProtoPort(string(portspec)) 327 if len(bindings) != 1 { 328 t.Fatalf("%s should have exactly one binding", portspec) 329 } 330 331 if bindings[0].HostIP != "" { 332 t.Fatalf("HostIP should not be set for %s", portspec) 333 } 334 335 if bindings[0].HostPort != port { 336 t.Fatalf("HostPort should be %s for %s", port, portspec) 337 } 338 } 339 340 portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp"}) 341 342 if err != nil { 343 t.Fatalf("Error while processing ParsePortSpecs: %s", err) 344 } 345 346 if _, ok := portMap[Port("1235/tcp")]; !ok { 347 t.Fatal("1234/tcp was not parsed properly") 348 } 349 350 if _, ok := portMap[Port("2346/udp")]; !ok { 351 t.Fatal("2345/udp was not parsed properly") 352 } 353 354 for portspec, bindings := range bindingMap { 355 _, port := SplitProtoPort(string(portspec)) 356 if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port { 357 t.Fatalf("Expect single binding to port %s but found %s", port, bindings) 358 } 359 } 360 361 _, _, err = ParsePortSpecs([]string{"localhost:1234-1236:1234-1236/tcp"}) 362 363 if err == nil { 364 t.Fatal("Received no error while trying to parse a hostname instead of ip") 365 } 366 } 367 368 func TestParseNetworkOptsPrivateOnly(t *testing.T) { 369 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::80"}) 370 if err != nil { 371 t.Fatal(err) 372 } 373 if len(ports) != 1 { 374 t.Logf("Expected 1 got %d", len(ports)) 375 t.FailNow() 376 } 377 if len(bindings) != 1 { 378 t.Logf("Expected 1 got %d", len(bindings)) 379 t.FailNow() 380 } 381 for k := range ports { 382 if k.Proto() != "tcp" { 383 t.Logf("Expected tcp got %s", k.Proto()) 384 t.Fail() 385 } 386 if k.Port() != "80" { 387 t.Logf("Expected 80 got %s", k.Port()) 388 t.Fail() 389 } 390 b, exists := bindings[k] 391 if !exists { 392 t.Log("Binding does not exist") 393 t.FailNow() 394 } 395 if len(b) != 1 { 396 t.Logf("Expected 1 got %d", len(b)) 397 t.FailNow() 398 } 399 s := b[0] 400 if s.HostPort != "" { 401 t.Logf("Expected \"\" got %s", s.HostPort) 402 t.Fail() 403 } 404 if s.HostIP != "192.168.1.100" { 405 t.Fail() 406 } 407 } 408 } 409 410 func TestParseNetworkOptsPublic(t *testing.T) { 411 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:8080:80"}) 412 if err != nil { 413 t.Fatal(err) 414 } 415 if len(ports) != 1 { 416 t.Logf("Expected 1 got %d", len(ports)) 417 t.FailNow() 418 } 419 if len(bindings) != 1 { 420 t.Logf("Expected 1 got %d", len(bindings)) 421 t.FailNow() 422 } 423 for k := range ports { 424 if k.Proto() != "tcp" { 425 t.Logf("Expected tcp got %s", k.Proto()) 426 t.Fail() 427 } 428 if k.Port() != "80" { 429 t.Logf("Expected 80 got %s", k.Port()) 430 t.Fail() 431 } 432 b, exists := bindings[k] 433 if !exists { 434 t.Log("Binding does not exist") 435 t.FailNow() 436 } 437 if len(b) != 1 { 438 t.Logf("Expected 1 got %d", len(b)) 439 t.FailNow() 440 } 441 s := b[0] 442 if s.HostPort != "8080" { 443 t.Logf("Expected 8080 got %s", s.HostPort) 444 t.Fail() 445 } 446 if s.HostIP != "192.168.1.100" { 447 t.Fail() 448 } 449 } 450 } 451 452 func TestParseNetworkOptsPublicNoPort(t *testing.T) { 453 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100"}) 454 455 if err == nil { 456 t.Logf("Expected error Invalid containerPort") 457 t.Fail() 458 } 459 if ports != nil { 460 t.Logf("Expected nil got %s", ports) 461 t.Fail() 462 } 463 if bindings != nil { 464 t.Logf("Expected nil got %s", bindings) 465 t.Fail() 466 } 467 } 468 469 func TestParseNetworkOptsNegativePorts(t *testing.T) { 470 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:-1:-1"}) 471 472 if err == nil { 473 t.Fail() 474 } 475 if len(ports) != 0 { 476 t.Logf("Expected nil got %d", len(ports)) 477 t.Fail() 478 } 479 if len(bindings) != 0 { 480 t.Logf("Expected 0 got %d", len(bindings)) 481 t.Fail() 482 } 483 } 484 485 func TestParseNetworkOptsUdp(t *testing.T) { 486 ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::6000/udp"}) 487 if err != nil { 488 t.Fatal(err) 489 } 490 if len(ports) != 1 { 491 t.Logf("Expected 1 got %d", len(ports)) 492 t.FailNow() 493 } 494 if len(bindings) != 1 { 495 t.Logf("Expected 1 got %d", len(bindings)) 496 t.FailNow() 497 } 498 for k := range ports { 499 if k.Proto() != "udp" { 500 t.Logf("Expected udp got %s", k.Proto()) 501 t.Fail() 502 } 503 if k.Port() != "6000" { 504 t.Logf("Expected 6000 got %s", k.Port()) 505 t.Fail() 506 } 507 b, exists := bindings[k] 508 if !exists { 509 t.Log("Binding does not exist") 510 t.FailNow() 511 } 512 if len(b) != 1 { 513 t.Logf("Expected 1 got %d", len(b)) 514 t.FailNow() 515 } 516 s := b[0] 517 if s.HostPort != "" { 518 t.Logf("Expected \"\" got %s", s.HostPort) 519 t.Fail() 520 } 521 if s.HostIP != "192.168.1.100" { 522 t.Fail() 523 } 524 } 525 }