github.com/cwandrews/docker@v1.7.0/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 := NewPort("tcp", "1234")
    46  
    47  	if string(p) != "1234/tcp" {
    48  		t.Fatal("tcp, 1234 did not result in the string 1234/tcp")
    49  	}
    50  
    51  	if p.Proto() != "tcp" {
    52  		t.Fatal("protocol was not tcp")
    53  	}
    54  
    55  	if p.Port() != "1234" {
    56  		t.Fatal("port string value was not 1234")
    57  	}
    58  
    59  	if p.Int() != 1234 {
    60  		t.Fatal("port int value was not 1234")
    61  	}
    62  }
    63  
    64  func TestSplitProtoPort(t *testing.T) {
    65  	var (
    66  		proto string
    67  		port  string
    68  	)
    69  
    70  	proto, port = SplitProtoPort("1234/tcp")
    71  
    72  	if proto != "tcp" || port != "1234" {
    73  		t.Fatal("Could not split 1234/tcp properly")
    74  	}
    75  
    76  	proto, port = SplitProtoPort("")
    77  
    78  	if proto != "" || port != "" {
    79  		t.Fatal("parsing an empty string yielded surprising results", proto, port)
    80  	}
    81  
    82  	proto, port = SplitProtoPort("1234")
    83  
    84  	if proto != "tcp" || port != "1234" {
    85  		t.Fatal("tcp is not the default protocol for portspec '1234'", proto, port)
    86  	}
    87  
    88  	proto, port = SplitProtoPort("1234/")
    89  
    90  	if proto != "tcp" || port != "1234" {
    91  		t.Fatal("parsing '1234/' yielded:" + port + "/" + proto)
    92  	}
    93  
    94  	proto, port = SplitProtoPort("/tcp")
    95  
    96  	if proto != "" || port != "" {
    97  		t.Fatal("parsing '/tcp' yielded:" + port + "/" + proto)
    98  	}
    99  }
   100  
   101  func TestParsePortSpecs(t *testing.T) {
   102  	var (
   103  		portMap    map[Port]struct{}
   104  		bindingMap map[Port][]PortBinding
   105  		err        error
   106  	)
   107  
   108  	portMap, bindingMap, err = ParsePortSpecs([]string{"1234/tcp", "2345/udp"})
   109  
   110  	if err != nil {
   111  		t.Fatalf("Error while processing ParsePortSpecs: %s", err)
   112  	}
   113  
   114  	if _, ok := portMap[Port("1234/tcp")]; !ok {
   115  		t.Fatal("1234/tcp was not parsed properly")
   116  	}
   117  
   118  	if _, ok := portMap[Port("2345/udp")]; !ok {
   119  		t.Fatal("2345/udp was not parsed properly")
   120  	}
   121  
   122  	for portspec, bindings := range bindingMap {
   123  		if len(bindings) != 1 {
   124  			t.Fatalf("%s should have exactly one binding", portspec)
   125  		}
   126  
   127  		if bindings[0].HostIp != "" {
   128  			t.Fatalf("HostIp should not be set for %s", portspec)
   129  		}
   130  
   131  		if bindings[0].HostPort != "" {
   132  			t.Fatalf("HostPort should not be set for %s", portspec)
   133  		}
   134  	}
   135  
   136  	portMap, bindingMap, err = ParsePortSpecs([]string{"1234:1234/tcp", "2345:2345/udp"})
   137  
   138  	if err != nil {
   139  		t.Fatalf("Error while processing ParsePortSpecs: %s", err)
   140  	}
   141  
   142  	if _, ok := portMap[Port("1234/tcp")]; !ok {
   143  		t.Fatal("1234/tcp was not parsed properly")
   144  	}
   145  
   146  	if _, ok := portMap[Port("2345/udp")]; !ok {
   147  		t.Fatal("2345/udp was not parsed properly")
   148  	}
   149  
   150  	for portspec, bindings := range bindingMap {
   151  		_, port := SplitProtoPort(string(portspec))
   152  
   153  		if len(bindings) != 1 {
   154  			t.Fatalf("%s should have exactly one binding", portspec)
   155  		}
   156  
   157  		if bindings[0].HostIp != "" {
   158  			t.Fatalf("HostIp should not be set for %s", portspec)
   159  		}
   160  
   161  		if bindings[0].HostPort != port {
   162  			t.Fatalf("HostPort should be %s for %s", port, portspec)
   163  		}
   164  	}
   165  
   166  	portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234:1234/tcp", "0.0.0.0:2345:2345/udp"})
   167  
   168  	if err != nil {
   169  		t.Fatalf("Error while processing ParsePortSpecs: %s", err)
   170  	}
   171  
   172  	if _, ok := portMap[Port("1234/tcp")]; !ok {
   173  		t.Fatal("1234/tcp was not parsed properly")
   174  	}
   175  
   176  	if _, ok := portMap[Port("2345/udp")]; !ok {
   177  		t.Fatal("2345/udp was not parsed properly")
   178  	}
   179  
   180  	for portspec, bindings := range bindingMap {
   181  		_, port := SplitProtoPort(string(portspec))
   182  
   183  		if len(bindings) != 1 {
   184  			t.Fatalf("%s should have exactly one binding", portspec)
   185  		}
   186  
   187  		if bindings[0].HostIp != "0.0.0.0" {
   188  			t.Fatalf("HostIp is not 0.0.0.0 for %s", portspec)
   189  		}
   190  
   191  		if bindings[0].HostPort != port {
   192  			t.Fatalf("HostPort should be %s for %s", port, portspec)
   193  		}
   194  	}
   195  
   196  	_, _, err = ParsePortSpecs([]string{"localhost:1234:1234/tcp"})
   197  
   198  	if err == nil {
   199  		t.Fatal("Received no error while trying to parse a hostname instead of ip")
   200  	}
   201  }
   202  
   203  func TestParsePortSpecsWithRange(t *testing.T) {
   204  	var (
   205  		portMap    map[Port]struct{}
   206  		bindingMap map[Port][]PortBinding
   207  		err        error
   208  	)
   209  
   210  	portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236/tcp", "2345-2347/udp"})
   211  
   212  	if err != nil {
   213  		t.Fatalf("Error while processing ParsePortSpecs: %s", err)
   214  	}
   215  
   216  	if _, ok := portMap[Port("1235/tcp")]; !ok {
   217  		t.Fatal("1234/tcp was not parsed properly")
   218  	}
   219  
   220  	if _, ok := portMap[Port("2346/udp")]; !ok {
   221  		t.Fatal("2345/udp was not parsed properly")
   222  	}
   223  
   224  	for portspec, bindings := range bindingMap {
   225  		if len(bindings) != 1 {
   226  			t.Fatalf("%s should have exactly one binding", portspec)
   227  		}
   228  
   229  		if bindings[0].HostIp != "" {
   230  			t.Fatalf("HostIp should not be set for %s", portspec)
   231  		}
   232  
   233  		if bindings[0].HostPort != "" {
   234  			t.Fatalf("HostPort should not be set for %s", portspec)
   235  		}
   236  	}
   237  
   238  	portMap, bindingMap, err = ParsePortSpecs([]string{"1234-1236:1234-1236/tcp", "2345-2347:2345-2347/udp"})
   239  
   240  	if err != nil {
   241  		t.Fatalf("Error while processing ParsePortSpecs: %s", err)
   242  	}
   243  
   244  	if _, ok := portMap[Port("1235/tcp")]; !ok {
   245  		t.Fatal("1234/tcp was not parsed properly")
   246  	}
   247  
   248  	if _, ok := portMap[Port("2346/udp")]; !ok {
   249  		t.Fatal("2345/udp was not parsed properly")
   250  	}
   251  
   252  	for portspec, bindings := range bindingMap {
   253  		_, port := SplitProtoPort(string(portspec))
   254  		if len(bindings) != 1 {
   255  			t.Fatalf("%s should have exactly one binding", portspec)
   256  		}
   257  
   258  		if bindings[0].HostIp != "" {
   259  			t.Fatalf("HostIp should not be set for %s", portspec)
   260  		}
   261  
   262  		if bindings[0].HostPort != port {
   263  			t.Fatalf("HostPort should be %s for %s", port, portspec)
   264  		}
   265  	}
   266  
   267  	portMap, bindingMap, err = ParsePortSpecs([]string{"0.0.0.0:1234-1236:1234-1236/tcp", "0.0.0.0:2345-2347:2345-2347/udp"})
   268  
   269  	if err != nil {
   270  		t.Fatalf("Error while processing ParsePortSpecs: %s", err)
   271  	}
   272  
   273  	if _, ok := portMap[Port("1235/tcp")]; !ok {
   274  		t.Fatal("1234/tcp was not parsed properly")
   275  	}
   276  
   277  	if _, ok := portMap[Port("2346/udp")]; !ok {
   278  		t.Fatal("2345/udp was not parsed properly")
   279  	}
   280  
   281  	for portspec, bindings := range bindingMap {
   282  		_, port := SplitProtoPort(string(portspec))
   283  		if len(bindings) != 1 || bindings[0].HostIp != "0.0.0.0" || bindings[0].HostPort != port {
   284  			t.Fatalf("Expect single binding to port %s but found %s", port, bindings)
   285  		}
   286  	}
   287  
   288  	_, _, err = ParsePortSpecs([]string{"localhost:1234-1236:1234-1236/tcp"})
   289  
   290  	if err == nil {
   291  		t.Fatal("Received no error while trying to parse a hostname instead of ip")
   292  	}
   293  }