network_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package docker
  2. import (
  3. "net"
  4. "os"
  5. "testing"
  6. )
  7. func TestIptables(t *testing.T) {
  8. if err := iptables("-L"); err != nil {
  9. t.Fatal(err)
  10. }
  11. path := os.Getenv("PATH")
  12. os.Setenv("PATH", "")
  13. defer os.Setenv("PATH", path)
  14. if err := iptables("-L"); err == nil {
  15. t.Fatal("Not finding iptables in the PATH should cause an error")
  16. }
  17. }
  18. func TestPortAllocation(t *testing.T) {
  19. allocator, err := newPortAllocator()
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if port, err := allocator.Acquire(80); err != nil {
  24. t.Fatal(err)
  25. } else if port != 80 {
  26. t.Fatalf("Acquire(80) should return 80, not %d", port)
  27. }
  28. port, err := allocator.Acquire(0)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if port <= 0 {
  33. t.Fatalf("Acquire(0) should return a non-zero port")
  34. }
  35. if _, err := allocator.Acquire(port); err == nil {
  36. t.Fatalf("Acquiring a port already in use should return an error")
  37. }
  38. if newPort, err := allocator.Acquire(0); err != nil {
  39. t.Fatal(err)
  40. } else if newPort == port {
  41. t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
  42. }
  43. if _, err := allocator.Acquire(80); err == nil {
  44. t.Fatalf("Acquiring a port already in use should return an error")
  45. }
  46. if err := allocator.Release(80); err != nil {
  47. t.Fatal(err)
  48. }
  49. if _, err := allocator.Acquire(80); err != nil {
  50. t.Fatal(err)
  51. }
  52. }
  53. func TestNetworkRange(t *testing.T) {
  54. // Simple class C test
  55. _, network, _ := net.ParseCIDR("192.168.0.1/24")
  56. first, last := networkRange(network)
  57. if !first.Equal(net.ParseIP("192.168.0.0")) {
  58. t.Error(first.String())
  59. }
  60. if !last.Equal(net.ParseIP("192.168.0.255")) {
  61. t.Error(last.String())
  62. }
  63. if size := networkSize(network.Mask); size != 256 {
  64. t.Error(size)
  65. }
  66. // Class A test
  67. _, network, _ = net.ParseCIDR("10.0.0.1/8")
  68. first, last = networkRange(network)
  69. if !first.Equal(net.ParseIP("10.0.0.0")) {
  70. t.Error(first.String())
  71. }
  72. if !last.Equal(net.ParseIP("10.255.255.255")) {
  73. t.Error(last.String())
  74. }
  75. if size := networkSize(network.Mask); size != 16777216 {
  76. t.Error(size)
  77. }
  78. // Class A, random IP address
  79. _, network, _ = net.ParseCIDR("10.1.2.3/8")
  80. first, last = networkRange(network)
  81. if !first.Equal(net.ParseIP("10.0.0.0")) {
  82. t.Error(first.String())
  83. }
  84. if !last.Equal(net.ParseIP("10.255.255.255")) {
  85. t.Error(last.String())
  86. }
  87. // 32bit mask
  88. _, network, _ = net.ParseCIDR("10.1.2.3/32")
  89. first, last = networkRange(network)
  90. if !first.Equal(net.ParseIP("10.1.2.3")) {
  91. t.Error(first.String())
  92. }
  93. if !last.Equal(net.ParseIP("10.1.2.3")) {
  94. t.Error(last.String())
  95. }
  96. if size := networkSize(network.Mask); size != 1 {
  97. t.Error(size)
  98. }
  99. // 31bit mask
  100. _, network, _ = net.ParseCIDR("10.1.2.3/31")
  101. first, last = networkRange(network)
  102. if !first.Equal(net.ParseIP("10.1.2.2")) {
  103. t.Error(first.String())
  104. }
  105. if !last.Equal(net.ParseIP("10.1.2.3")) {
  106. t.Error(last.String())
  107. }
  108. if size := networkSize(network.Mask); size != 2 {
  109. t.Error(size)
  110. }
  111. // 26bit mask
  112. _, network, _ = net.ParseCIDR("10.1.2.3/26")
  113. first, last = networkRange(network)
  114. if !first.Equal(net.ParseIP("10.1.2.0")) {
  115. t.Error(first.String())
  116. }
  117. if !last.Equal(net.ParseIP("10.1.2.63")) {
  118. t.Error(last.String())
  119. }
  120. if size := networkSize(network.Mask); size != 64 {
  121. t.Error(size)
  122. }
  123. }
  124. func TestConversion(t *testing.T) {
  125. ip := net.ParseIP("127.0.0.1")
  126. i := ipToInt(ip)
  127. if i == 0 {
  128. t.Fatal("converted to zero")
  129. }
  130. conv := intToIp(i)
  131. if !ip.Equal(conv) {
  132. t.Error(conv.String())
  133. }
  134. }
  135. func TestIPAllocator(t *testing.T) {
  136. expectedIPs := []net.IP{
  137. 0: net.IPv4(127, 0, 0, 2),
  138. 1: net.IPv4(127, 0, 0, 3),
  139. 2: net.IPv4(127, 0, 0, 4),
  140. 3: net.IPv4(127, 0, 0, 5),
  141. 4: net.IPv4(127, 0, 0, 6),
  142. }
  143. gwIP, n, _ := net.ParseCIDR("127.0.0.1/29")
  144. alloc := newIPAllocator(&net.IPNet{IP: gwIP, Mask: n.Mask})
  145. // Pool after initialisation (f = free, u = used)
  146. // 2(f) - 3(f) - 4(f) - 5(f) - 6(f)
  147. // ↑
  148. // Check that we get 5 IPs, from 127.0.0.2–127.0.0.6, in that
  149. // order.
  150. for i := 0; i < 5; i++ {
  151. ip, err := alloc.Acquire()
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. assertIPEquals(t, expectedIPs[i], ip)
  156. }
  157. // Before loop begin
  158. // 2(f) - 3(f) - 4(f) - 5(f) - 6(f)
  159. // ↑
  160. // After i = 0
  161. // 2(u) - 3(f) - 4(f) - 5(f) - 6(f)
  162. // ↑
  163. // After i = 1
  164. // 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
  165. // ↑
  166. // After i = 2
  167. // 2(u) - 3(u) - 4(u) - 5(f) - 6(f)
  168. // ↑
  169. // After i = 3
  170. // 2(u) - 3(u) - 4(u) - 5(u) - 6(f)
  171. // ↑
  172. // After i = 4
  173. // 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
  174. // ↑
  175. // Check that there are no more IPs
  176. _, err := alloc.Acquire()
  177. if err == nil {
  178. t.Fatal("There shouldn't be any IP addresses at this point")
  179. }
  180. // Release some IPs in non-sequential order
  181. alloc.Release(expectedIPs[3])
  182. // 2(u) - 3(u) - 4(u) - 5(f) - 6(u)
  183. // ↑
  184. alloc.Release(expectedIPs[2])
  185. // 2(u) - 3(u) - 4(f) - 5(f) - 6(u)
  186. // ↑
  187. alloc.Release(expectedIPs[4])
  188. // 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
  189. // ↑
  190. // Make sure that IPs are reused in sequential order, starting
  191. // with the first released IP
  192. newIPs := make([]net.IP, 3)
  193. for i := 0; i < 3; i++ {
  194. ip, err := alloc.Acquire()
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. newIPs[i] = ip
  199. }
  200. // Before loop begin
  201. // 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
  202. // ↑
  203. // After i = 0
  204. // 2(u) - 3(u) - 4(f) - 5(u) - 6(f)
  205. // ↑
  206. // After i = 1
  207. // 2(u) - 3(u) - 4(f) - 5(u) - 6(u)
  208. // ↑
  209. // After i = 2
  210. // 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
  211. // ↑
  212. assertIPEquals(t, expectedIPs[3], newIPs[0])
  213. assertIPEquals(t, expectedIPs[4], newIPs[1])
  214. assertIPEquals(t, expectedIPs[2], newIPs[2])
  215. _, err = alloc.Acquire()
  216. if err == nil {
  217. t.Fatal("There shouldn't be any IP addresses at this point")
  218. }
  219. }
  220. func assertIPEquals(t *testing.T, ip1, ip2 net.IP) {
  221. if !ip1.Equal(ip2) {
  222. t.Fatalf("Expected IP %s, got %s", ip1, ip2)
  223. }
  224. }
  225. func AssertOverlap(CIDRx string, CIDRy string, t *testing.T) {
  226. _, netX, _ := net.ParseCIDR(CIDRx)
  227. _, netY, _ := net.ParseCIDR(CIDRy)
  228. if !networkOverlaps(netX, netY) {
  229. t.Errorf("%v and %v should overlap", netX, netY)
  230. }
  231. }
  232. func AssertNoOverlap(CIDRx string, CIDRy string, t *testing.T) {
  233. _, netX, _ := net.ParseCIDR(CIDRx)
  234. _, netY, _ := net.ParseCIDR(CIDRy)
  235. if networkOverlaps(netX, netY) {
  236. t.Errorf("%v and %v should not overlap", netX, netY)
  237. }
  238. }
  239. func TestNetworkOverlaps(t *testing.T) {
  240. //netY starts at same IP and ends within netX
  241. AssertOverlap("172.16.0.1/24", "172.16.0.1/25", t)
  242. //netY starts within netX and ends at same IP
  243. AssertOverlap("172.16.0.1/24", "172.16.0.128/25", t)
  244. //netY starts and ends within netX
  245. AssertOverlap("172.16.0.1/24", "172.16.0.64/25", t)
  246. //netY starts at same IP and ends outside of netX
  247. AssertOverlap("172.16.0.1/24", "172.16.0.1/23", t)
  248. //netY starts before and ends at same IP of netX
  249. AssertOverlap("172.16.1.1/24", "172.16.0.1/23", t)
  250. //netY starts before and ends outside of netX
  251. AssertOverlap("172.16.1.1/24", "172.16.0.1/23", t)
  252. //netY starts and ends before netX
  253. AssertNoOverlap("172.16.1.1/25", "172.16.0.1/24", t)
  254. //netX starts and ends before netY
  255. AssertNoOverlap("172.16.1.1/25", "172.16.2.1/24", t)
  256. }