move nat tests from container's unit test to nat's ones
| ... | ... |
@@ -1,170 +1,6 @@ |
| 1 | 1 |
package daemon |
| 2 | 2 |
|
| 3 |
-import ( |
|
| 4 |
- "github.com/docker/docker/pkg/nat" |
|
| 5 |
- "testing" |
|
| 6 |
-) |
|
| 7 |
- |
|
| 8 |
-func TestParseNetworkOptsPrivateOnly(t *testing.T) {
|
|
| 9 |
- ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100::80"})
|
|
| 10 |
- if err != nil {
|
|
| 11 |
- t.Fatal(err) |
|
| 12 |
- } |
|
| 13 |
- if len(ports) != 1 {
|
|
| 14 |
- t.Logf("Expected 1 got %d", len(ports))
|
|
| 15 |
- t.FailNow() |
|
| 16 |
- } |
|
| 17 |
- if len(bindings) != 1 {
|
|
| 18 |
- t.Logf("Expected 1 got %d", len(bindings))
|
|
| 19 |
- t.FailNow() |
|
| 20 |
- } |
|
| 21 |
- for k := range ports {
|
|
| 22 |
- if k.Proto() != "tcp" {
|
|
| 23 |
- t.Logf("Expected tcp got %s", k.Proto())
|
|
| 24 |
- t.Fail() |
|
| 25 |
- } |
|
| 26 |
- if k.Port() != "80" {
|
|
| 27 |
- t.Logf("Expected 80 got %s", k.Port())
|
|
| 28 |
- t.Fail() |
|
| 29 |
- } |
|
| 30 |
- b, exists := bindings[k] |
|
| 31 |
- if !exists {
|
|
| 32 |
- t.Log("Binding does not exist")
|
|
| 33 |
- t.FailNow() |
|
| 34 |
- } |
|
| 35 |
- if len(b) != 1 {
|
|
| 36 |
- t.Logf("Expected 1 got %d", len(b))
|
|
| 37 |
- t.FailNow() |
|
| 38 |
- } |
|
| 39 |
- s := b[0] |
|
| 40 |
- if s.HostPort != "" {
|
|
| 41 |
- t.Logf("Expected \"\" got %s", s.HostPort)
|
|
| 42 |
- t.Fail() |
|
| 43 |
- } |
|
| 44 |
- if s.HostIp != "192.168.1.100" {
|
|
| 45 |
- t.Fail() |
|
| 46 |
- } |
|
| 47 |
- } |
|
| 48 |
-} |
|
| 49 |
- |
|
| 50 |
-func TestParseNetworkOptsPublic(t *testing.T) {
|
|
| 51 |
- ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100:8080:80"})
|
|
| 52 |
- if err != nil {
|
|
| 53 |
- t.Fatal(err) |
|
| 54 |
- } |
|
| 55 |
- if len(ports) != 1 {
|
|
| 56 |
- t.Logf("Expected 1 got %d", len(ports))
|
|
| 57 |
- t.FailNow() |
|
| 58 |
- } |
|
| 59 |
- if len(bindings) != 1 {
|
|
| 60 |
- t.Logf("Expected 1 got %d", len(bindings))
|
|
| 61 |
- t.FailNow() |
|
| 62 |
- } |
|
| 63 |
- for k := range ports {
|
|
| 64 |
- if k.Proto() != "tcp" {
|
|
| 65 |
- t.Logf("Expected tcp got %s", k.Proto())
|
|
| 66 |
- t.Fail() |
|
| 67 |
- } |
|
| 68 |
- if k.Port() != "80" {
|
|
| 69 |
- t.Logf("Expected 80 got %s", k.Port())
|
|
| 70 |
- t.Fail() |
|
| 71 |
- } |
|
| 72 |
- b, exists := bindings[k] |
|
| 73 |
- if !exists {
|
|
| 74 |
- t.Log("Binding does not exist")
|
|
| 75 |
- t.FailNow() |
|
| 76 |
- } |
|
| 77 |
- if len(b) != 1 {
|
|
| 78 |
- t.Logf("Expected 1 got %d", len(b))
|
|
| 79 |
- t.FailNow() |
|
| 80 |
- } |
|
| 81 |
- s := b[0] |
|
| 82 |
- if s.HostPort != "8080" {
|
|
| 83 |
- t.Logf("Expected 8080 got %s", s.HostPort)
|
|
| 84 |
- t.Fail() |
|
| 85 |
- } |
|
| 86 |
- if s.HostIp != "192.168.1.100" {
|
|
| 87 |
- t.Fail() |
|
| 88 |
- } |
|
| 89 |
- } |
|
| 90 |
-} |
|
| 91 |
- |
|
| 92 |
-func TestParseNetworkOptsPublicNoPort(t *testing.T) {
|
|
| 93 |
- ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100"})
|
|
| 94 |
- |
|
| 95 |
- if err == nil {
|
|
| 96 |
- t.Logf("Expected error Invalid containerPort")
|
|
| 97 |
- t.Fail() |
|
| 98 |
- } |
|
| 99 |
- if ports != nil {
|
|
| 100 |
- t.Logf("Expected nil got %s", ports)
|
|
| 101 |
- t.Fail() |
|
| 102 |
- } |
|
| 103 |
- if bindings != nil {
|
|
| 104 |
- t.Logf("Expected nil got %s", bindings)
|
|
| 105 |
- t.Fail() |
|
| 106 |
- } |
|
| 107 |
-} |
|
| 108 |
- |
|
| 109 |
-func TestParseNetworkOptsNegativePorts(t *testing.T) {
|
|
| 110 |
- ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100:-1:-1"})
|
|
| 111 |
- |
|
| 112 |
- if err == nil {
|
|
| 113 |
- t.Fail() |
|
| 114 |
- } |
|
| 115 |
- t.Logf("%v", len(ports))
|
|
| 116 |
- t.Logf("%v", bindings)
|
|
| 117 |
- if len(ports) != 0 {
|
|
| 118 |
- t.Logf("Expected nil got %s", len(ports))
|
|
| 119 |
- t.Fail() |
|
| 120 |
- } |
|
| 121 |
- if len(bindings) != 0 {
|
|
| 122 |
- t.Logf("Expected 0 got %s", len(bindings))
|
|
| 123 |
- t.Fail() |
|
| 124 |
- } |
|
| 125 |
-} |
|
| 126 |
- |
|
| 127 |
-func TestParseNetworkOptsUdp(t *testing.T) {
|
|
| 128 |
- ports, bindings, err := nat.ParsePortSpecs([]string{"192.168.1.100::6000/udp"})
|
|
| 129 |
- if err != nil {
|
|
| 130 |
- t.Fatal(err) |
|
| 131 |
- } |
|
| 132 |
- if len(ports) != 1 {
|
|
| 133 |
- t.Logf("Expected 1 got %d", len(ports))
|
|
| 134 |
- t.FailNow() |
|
| 135 |
- } |
|
| 136 |
- if len(bindings) != 1 {
|
|
| 137 |
- t.Logf("Expected 1 got %d", len(bindings))
|
|
| 138 |
- t.FailNow() |
|
| 139 |
- } |
|
| 140 |
- for k := range ports {
|
|
| 141 |
- if k.Proto() != "udp" {
|
|
| 142 |
- t.Logf("Expected udp got %s", k.Proto())
|
|
| 143 |
- t.Fail() |
|
| 144 |
- } |
|
| 145 |
- if k.Port() != "6000" {
|
|
| 146 |
- t.Logf("Expected 6000 got %s", k.Port())
|
|
| 147 |
- t.Fail() |
|
| 148 |
- } |
|
| 149 |
- b, exists := bindings[k] |
|
| 150 |
- if !exists {
|
|
| 151 |
- t.Log("Binding does not exist")
|
|
| 152 |
- t.FailNow() |
|
| 153 |
- } |
|
| 154 |
- if len(b) != 1 {
|
|
| 155 |
- t.Logf("Expected 1 got %d", len(b))
|
|
| 156 |
- t.FailNow() |
|
| 157 |
- } |
|
| 158 |
- s := b[0] |
|
| 159 |
- if s.HostPort != "" {
|
|
| 160 |
- t.Logf("Expected \"\" got %s", s.HostPort)
|
|
| 161 |
- t.Fail() |
|
| 162 |
- } |
|
| 163 |
- if s.HostIp != "192.168.1.100" {
|
|
| 164 |
- t.Fail() |
|
| 165 |
- } |
|
| 166 |
- } |
|
| 167 |
-} |
|
| 3 |
+import "testing" |
|
| 168 | 4 |
|
| 169 | 5 |
func TestGetFullName(t *testing.T) {
|
| 170 | 6 |
name, err := GetFullContainerName("testing")
|
| ... | ... |
@@ -291,3 +291,162 @@ func TestParsePortSpecsWithRange(t *testing.T) {
|
| 291 | 291 |
t.Fatal("Received no error while trying to parse a hostname instead of ip")
|
| 292 | 292 |
} |
| 293 | 293 |
} |
| 294 |
+ |
|
| 295 |
+func TestParseNetworkOptsPrivateOnly(t *testing.T) {
|
|
| 296 |
+ ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::80"})
|
|
| 297 |
+ if err != nil {
|
|
| 298 |
+ t.Fatal(err) |
|
| 299 |
+ } |
|
| 300 |
+ if len(ports) != 1 {
|
|
| 301 |
+ t.Logf("Expected 1 got %d", len(ports))
|
|
| 302 |
+ t.FailNow() |
|
| 303 |
+ } |
|
| 304 |
+ if len(bindings) != 1 {
|
|
| 305 |
+ t.Logf("Expected 1 got %d", len(bindings))
|
|
| 306 |
+ t.FailNow() |
|
| 307 |
+ } |
|
| 308 |
+ for k := range ports {
|
|
| 309 |
+ if k.Proto() != "tcp" {
|
|
| 310 |
+ t.Logf("Expected tcp got %s", k.Proto())
|
|
| 311 |
+ t.Fail() |
|
| 312 |
+ } |
|
| 313 |
+ if k.Port() != "80" {
|
|
| 314 |
+ t.Logf("Expected 80 got %s", k.Port())
|
|
| 315 |
+ t.Fail() |
|
| 316 |
+ } |
|
| 317 |
+ b, exists := bindings[k] |
|
| 318 |
+ if !exists {
|
|
| 319 |
+ t.Log("Binding does not exist")
|
|
| 320 |
+ t.FailNow() |
|
| 321 |
+ } |
|
| 322 |
+ if len(b) != 1 {
|
|
| 323 |
+ t.Logf("Expected 1 got %d", len(b))
|
|
| 324 |
+ t.FailNow() |
|
| 325 |
+ } |
|
| 326 |
+ s := b[0] |
|
| 327 |
+ if s.HostPort != "" {
|
|
| 328 |
+ t.Logf("Expected \"\" got %s", s.HostPort)
|
|
| 329 |
+ t.Fail() |
|
| 330 |
+ } |
|
| 331 |
+ if s.HostIp != "192.168.1.100" {
|
|
| 332 |
+ t.Fail() |
|
| 333 |
+ } |
|
| 334 |
+ } |
|
| 335 |
+} |
|
| 336 |
+ |
|
| 337 |
+func TestParseNetworkOptsPublic(t *testing.T) {
|
|
| 338 |
+ ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:8080:80"})
|
|
| 339 |
+ if err != nil {
|
|
| 340 |
+ t.Fatal(err) |
|
| 341 |
+ } |
|
| 342 |
+ if len(ports) != 1 {
|
|
| 343 |
+ t.Logf("Expected 1 got %d", len(ports))
|
|
| 344 |
+ t.FailNow() |
|
| 345 |
+ } |
|
| 346 |
+ if len(bindings) != 1 {
|
|
| 347 |
+ t.Logf("Expected 1 got %d", len(bindings))
|
|
| 348 |
+ t.FailNow() |
|
| 349 |
+ } |
|
| 350 |
+ for k := range ports {
|
|
| 351 |
+ if k.Proto() != "tcp" {
|
|
| 352 |
+ t.Logf("Expected tcp got %s", k.Proto())
|
|
| 353 |
+ t.Fail() |
|
| 354 |
+ } |
|
| 355 |
+ if k.Port() != "80" {
|
|
| 356 |
+ t.Logf("Expected 80 got %s", k.Port())
|
|
| 357 |
+ t.Fail() |
|
| 358 |
+ } |
|
| 359 |
+ b, exists := bindings[k] |
|
| 360 |
+ if !exists {
|
|
| 361 |
+ t.Log("Binding does not exist")
|
|
| 362 |
+ t.FailNow() |
|
| 363 |
+ } |
|
| 364 |
+ if len(b) != 1 {
|
|
| 365 |
+ t.Logf("Expected 1 got %d", len(b))
|
|
| 366 |
+ t.FailNow() |
|
| 367 |
+ } |
|
| 368 |
+ s := b[0] |
|
| 369 |
+ if s.HostPort != "8080" {
|
|
| 370 |
+ t.Logf("Expected 8080 got %s", s.HostPort)
|
|
| 371 |
+ t.Fail() |
|
| 372 |
+ } |
|
| 373 |
+ if s.HostIp != "192.168.1.100" {
|
|
| 374 |
+ t.Fail() |
|
| 375 |
+ } |
|
| 376 |
+ } |
|
| 377 |
+} |
|
| 378 |
+ |
|
| 379 |
+func TestParseNetworkOptsPublicNoPort(t *testing.T) {
|
|
| 380 |
+ ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100"})
|
|
| 381 |
+ |
|
| 382 |
+ if err == nil {
|
|
| 383 |
+ t.Logf("Expected error Invalid containerPort")
|
|
| 384 |
+ t.Fail() |
|
| 385 |
+ } |
|
| 386 |
+ if ports != nil {
|
|
| 387 |
+ t.Logf("Expected nil got %s", ports)
|
|
| 388 |
+ t.Fail() |
|
| 389 |
+ } |
|
| 390 |
+ if bindings != nil {
|
|
| 391 |
+ t.Logf("Expected nil got %s", bindings)
|
|
| 392 |
+ t.Fail() |
|
| 393 |
+ } |
|
| 394 |
+} |
|
| 395 |
+ |
|
| 396 |
+func TestParseNetworkOptsNegativePorts(t *testing.T) {
|
|
| 397 |
+ ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100:-1:-1"})
|
|
| 398 |
+ |
|
| 399 |
+ if err == nil {
|
|
| 400 |
+ t.Fail() |
|
| 401 |
+ } |
|
| 402 |
+ if len(ports) != 0 {
|
|
| 403 |
+ t.Logf("Expected nil got %s", len(ports))
|
|
| 404 |
+ t.Fail() |
|
| 405 |
+ } |
|
| 406 |
+ if len(bindings) != 0 {
|
|
| 407 |
+ t.Logf("Expected 0 got %s", len(bindings))
|
|
| 408 |
+ t.Fail() |
|
| 409 |
+ } |
|
| 410 |
+} |
|
| 411 |
+ |
|
| 412 |
+func TestParseNetworkOptsUdp(t *testing.T) {
|
|
| 413 |
+ ports, bindings, err := ParsePortSpecs([]string{"192.168.1.100::6000/udp"})
|
|
| 414 |
+ if err != nil {
|
|
| 415 |
+ t.Fatal(err) |
|
| 416 |
+ } |
|
| 417 |
+ if len(ports) != 1 {
|
|
| 418 |
+ t.Logf("Expected 1 got %d", len(ports))
|
|
| 419 |
+ t.FailNow() |
|
| 420 |
+ } |
|
| 421 |
+ if len(bindings) != 1 {
|
|
| 422 |
+ t.Logf("Expected 1 got %d", len(bindings))
|
|
| 423 |
+ t.FailNow() |
|
| 424 |
+ } |
|
| 425 |
+ for k := range ports {
|
|
| 426 |
+ if k.Proto() != "udp" {
|
|
| 427 |
+ t.Logf("Expected udp got %s", k.Proto())
|
|
| 428 |
+ t.Fail() |
|
| 429 |
+ } |
|
| 430 |
+ if k.Port() != "6000" {
|
|
| 431 |
+ t.Logf("Expected 6000 got %s", k.Port())
|
|
| 432 |
+ t.Fail() |
|
| 433 |
+ } |
|
| 434 |
+ b, exists := bindings[k] |
|
| 435 |
+ if !exists {
|
|
| 436 |
+ t.Log("Binding does not exist")
|
|
| 437 |
+ t.FailNow() |
|
| 438 |
+ } |
|
| 439 |
+ if len(b) != 1 {
|
|
| 440 |
+ t.Logf("Expected 1 got %d", len(b))
|
|
| 441 |
+ t.FailNow() |
|
| 442 |
+ } |
|
| 443 |
+ s := b[0] |
|
| 444 |
+ if s.HostPort != "" {
|
|
| 445 |
+ t.Logf("Expected \"\" got %s", s.HostPort)
|
|
| 446 |
+ t.Fail() |
|
| 447 |
+ } |
|
| 448 |
+ if s.HostIp != "192.168.1.100" {
|
|
| 449 |
+ t.Fail() |
|
| 450 |
+ } |
|
| 451 |
+ } |
|
| 452 |
+} |