github.com/erriapo/docker@v1.6.0-rc2/daemon/networkdriver/portallocator/portallocator_test.go (about)

     1  package portallocator
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  )
     7  
     8  func init() {
     9  	beginPortRange = DefaultPortRangeStart
    10  	endPortRange = DefaultPortRangeEnd
    11  }
    12  
    13  func reset() {
    14  	ReleaseAll()
    15  }
    16  
    17  func TestRequestNewPort(t *testing.T) {
    18  	defer reset()
    19  
    20  	port, err := RequestPort(defaultIP, "tcp", 0)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	if expected := beginPortRange; port != expected {
    26  		t.Fatalf("Expected port %d got %d", expected, port)
    27  	}
    28  }
    29  
    30  func TestRequestSpecificPort(t *testing.T) {
    31  	defer reset()
    32  
    33  	port, err := RequestPort(defaultIP, "tcp", 5000)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	if port != 5000 {
    38  		t.Fatalf("Expected port 5000 got %d", port)
    39  	}
    40  }
    41  
    42  func TestReleasePort(t *testing.T) {
    43  	defer reset()
    44  
    45  	port, err := RequestPort(defaultIP, "tcp", 5000)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	if port != 5000 {
    50  		t.Fatalf("Expected port 5000 got %d", port)
    51  	}
    52  
    53  	if err := ReleasePort(defaultIP, "tcp", 5000); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  }
    57  
    58  func TestReuseReleasedPort(t *testing.T) {
    59  	defer reset()
    60  
    61  	port, err := RequestPort(defaultIP, "tcp", 5000)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if port != 5000 {
    66  		t.Fatalf("Expected port 5000 got %d", port)
    67  	}
    68  
    69  	if err := ReleasePort(defaultIP, "tcp", 5000); err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	port, err = RequestPort(defaultIP, "tcp", 5000)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  }
    78  
    79  func TestReleaseUnreadledPort(t *testing.T) {
    80  	defer reset()
    81  
    82  	port, err := RequestPort(defaultIP, "tcp", 5000)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	if port != 5000 {
    87  		t.Fatalf("Expected port 5000 got %d", port)
    88  	}
    89  
    90  	port, err = RequestPort(defaultIP, "tcp", 5000)
    91  
    92  	switch err.(type) {
    93  	case ErrPortAlreadyAllocated:
    94  	default:
    95  		t.Fatalf("Expected port allocation error got %s", err)
    96  	}
    97  }
    98  
    99  func TestUnknowProtocol(t *testing.T) {
   100  	defer reset()
   101  
   102  	if _, err := RequestPort(defaultIP, "tcpp", 0); err != ErrUnknownProtocol {
   103  		t.Fatalf("Expected error %s got %s", ErrUnknownProtocol, err)
   104  	}
   105  }
   106  
   107  func TestAllocateAllPorts(t *testing.T) {
   108  	defer reset()
   109  
   110  	for i := 0; i <= endPortRange-beginPortRange; i++ {
   111  		port, err := RequestPort(defaultIP, "tcp", 0)
   112  		if err != nil {
   113  			t.Fatal(err)
   114  		}
   115  
   116  		if expected := beginPortRange + i; port != expected {
   117  			t.Fatalf("Expected port %d got %d", expected, port)
   118  		}
   119  	}
   120  
   121  	if _, err := RequestPort(defaultIP, "tcp", 0); err != ErrAllPortsAllocated {
   122  		t.Fatalf("Expected error %s got %s", ErrAllPortsAllocated, err)
   123  	}
   124  
   125  	_, err := RequestPort(defaultIP, "udp", 0)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	// release a port in the middle and ensure we get another tcp port
   131  	port := beginPortRange + 5
   132  	if err := ReleasePort(defaultIP, "tcp", port); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	newPort, err := RequestPort(defaultIP, "tcp", 0)
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	if newPort != port {
   140  		t.Fatalf("Expected port %d got %d", port, newPort)
   141  	}
   142  
   143  	// now pm.last == newPort, release it so that it's the only free port of
   144  	// the range, and ensure we get it back
   145  	if err := ReleasePort(defaultIP, "tcp", newPort); err != nil {
   146  		t.Fatal(err)
   147  	}
   148  	port, err = RequestPort(defaultIP, "tcp", 0)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	if newPort != port {
   153  		t.Fatalf("Expected port %d got %d", newPort, port)
   154  	}
   155  }
   156  
   157  func BenchmarkAllocatePorts(b *testing.B) {
   158  	defer reset()
   159  
   160  	for i := 0; i < b.N; i++ {
   161  		for i := 0; i <= endPortRange-beginPortRange; i++ {
   162  			port, err := RequestPort(defaultIP, "tcp", 0)
   163  			if err != nil {
   164  				b.Fatal(err)
   165  			}
   166  
   167  			if expected := beginPortRange + i; port != expected {
   168  				b.Fatalf("Expected port %d got %d", expected, port)
   169  			}
   170  		}
   171  		reset()
   172  	}
   173  }
   174  
   175  func TestPortAllocation(t *testing.T) {
   176  	defer reset()
   177  
   178  	ip := net.ParseIP("192.168.0.1")
   179  	ip2 := net.ParseIP("192.168.0.2")
   180  	if port, err := RequestPort(ip, "tcp", 80); err != nil {
   181  		t.Fatal(err)
   182  	} else if port != 80 {
   183  		t.Fatalf("Acquire(80) should return 80, not %d", port)
   184  	}
   185  	port, err := RequestPort(ip, "tcp", 0)
   186  	if err != nil {
   187  		t.Fatal(err)
   188  	}
   189  	if port <= 0 {
   190  		t.Fatalf("Acquire(0) should return a non-zero port")
   191  	}
   192  
   193  	if _, err := RequestPort(ip, "tcp", port); err == nil {
   194  		t.Fatalf("Acquiring a port already in use should return an error")
   195  	}
   196  
   197  	if newPort, err := RequestPort(ip, "tcp", 0); err != nil {
   198  		t.Fatal(err)
   199  	} else if newPort == port {
   200  		t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
   201  	}
   202  
   203  	if _, err := RequestPort(ip, "tcp", 80); err == nil {
   204  		t.Fatalf("Acquiring a port already in use should return an error")
   205  	}
   206  	if _, err := RequestPort(ip2, "tcp", 80); err != nil {
   207  		t.Fatalf("It should be possible to allocate the same port on a different interface")
   208  	}
   209  	if _, err := RequestPort(ip2, "tcp", 80); err == nil {
   210  		t.Fatalf("Acquiring a port already in use should return an error")
   211  	}
   212  	if err := ReleasePort(ip, "tcp", 80); err != nil {
   213  		t.Fatal(err)
   214  	}
   215  	if _, err := RequestPort(ip, "tcp", 80); err != nil {
   216  		t.Fatal(err)
   217  	}
   218  
   219  	port, err = RequestPort(ip, "tcp", 0)
   220  	if err != nil {
   221  		t.Fatal(err)
   222  	}
   223  	port2, err := RequestPort(ip, "tcp", port+1)
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  	port3, err := RequestPort(ip, "tcp", 0)
   228  	if err != nil {
   229  		t.Fatal(err)
   230  	}
   231  	if port3 == port2 {
   232  		t.Fatal("Requesting a dynamic port should never allocate a used port")
   233  	}
   234  }
   235  
   236  func TestNoDuplicateBPR(t *testing.T) {
   237  	defer reset()
   238  
   239  	if port, err := RequestPort(defaultIP, "tcp", beginPortRange); err != nil {
   240  		t.Fatal(err)
   241  	} else if port != beginPortRange {
   242  		t.Fatalf("Expected port %d got %d", beginPortRange, port)
   243  	}
   244  
   245  	if port, err := RequestPort(defaultIP, "tcp", 0); err != nil {
   246  		t.Fatal(err)
   247  	} else if port == beginPortRange {
   248  		t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
   249  	}
   250  }