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