cidr_set_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "math/big"
  16. "net"
  17. "reflect"
  18. "testing"
  19. "github.com/golang/glog"
  20. )
  21. func TestCIDRSetFullyAllocated(t *testing.T) {
  22. _, clusterCIDR, _ := net.ParseCIDR("127.123.234.0/30")
  23. a := newCIDRSet(clusterCIDR, 30)
  24. p, err := a.allocateNext()
  25. if err != nil {
  26. t.Fatalf("unexpected error: %v", err)
  27. }
  28. if p.String() != "127.123.234.0/30" {
  29. t.Fatalf("unexpected allocated cidr: %s", p.String())
  30. }
  31. _, err = a.allocateNext()
  32. if err == nil {
  33. t.Fatalf("expected error because of fully-allocated range")
  34. }
  35. a.release(p)
  36. p, err = a.allocateNext()
  37. if err != nil {
  38. t.Fatalf("unexpected error: %v", err)
  39. }
  40. if p.String() != "127.123.234.0/30" {
  41. t.Fatalf("unexpected allocated cidr: %s", p.String())
  42. }
  43. _, err = a.allocateNext()
  44. if err == nil {
  45. t.Fatalf("expected error because of fully-allocated range")
  46. }
  47. }
  48. func TestCIDRSet_RandomishAllocation(t *testing.T) {
  49. _, clusterCIDR, _ := net.ParseCIDR("127.123.234.0/16")
  50. a := newCIDRSet(clusterCIDR, 24)
  51. // allocate all the CIDRs
  52. var err error
  53. cidrs := make([]*net.IPNet, 256)
  54. for i := 0; i < 256; i++ {
  55. cidrs[i], err = a.allocateNext()
  56. if err != nil {
  57. t.Fatalf("unexpected error: %v", err)
  58. }
  59. }
  60. _, err = a.allocateNext()
  61. if err == nil {
  62. t.Fatalf("expected error because of fully-allocated range")
  63. }
  64. // release them all
  65. for i := 0; i < 256; i++ {
  66. a.release(cidrs[i])
  67. }
  68. // allocate the CIDRs again
  69. rcidrs := make([]*net.IPNet, 256)
  70. for i := 0; i < 256; i++ {
  71. rcidrs[i], err = a.allocateNext()
  72. if err != nil {
  73. t.Fatalf("unexpected error: %d, %v", i, err)
  74. }
  75. }
  76. _, err = a.allocateNext()
  77. if err == nil {
  78. t.Fatalf("expected error because of fully-allocated range")
  79. }
  80. if !reflect.DeepEqual(cidrs, rcidrs) {
  81. t.Fatalf("expected re-allocated cidrs are the same collection")
  82. }
  83. }
  84. func TestCIDRSet_AllocationOccupied(t *testing.T) {
  85. _, clusterCIDR, _ := net.ParseCIDR("127.123.234.0/16")
  86. a := newCIDRSet(clusterCIDR, 24)
  87. // allocate all the CIDRs
  88. var err error
  89. cidrs := make([]*net.IPNet, 256)
  90. for i := 0; i < 256; i++ {
  91. cidrs[i], err = a.allocateNext()
  92. if err != nil {
  93. t.Fatalf("unexpected error: %v", err)
  94. }
  95. }
  96. _, err = a.allocateNext()
  97. if err == nil {
  98. t.Fatalf("expected error because of fully-allocated range")
  99. }
  100. // release them all
  101. for i := 0; i < 256; i++ {
  102. a.release(cidrs[i])
  103. }
  104. // occupy the last 128 CIDRs
  105. for i := 128; i < 256; i++ {
  106. a.occupy(cidrs[i])
  107. }
  108. // allocate the first 128 CIDRs again
  109. rcidrs := make([]*net.IPNet, 128)
  110. for i := 0; i < 128; i++ {
  111. rcidrs[i], err = a.allocateNext()
  112. if err != nil {
  113. t.Fatalf("unexpected error: %d, %v", i, err)
  114. }
  115. }
  116. _, err = a.allocateNext()
  117. if err == nil {
  118. t.Fatalf("expected error because of fully-allocated range")
  119. }
  120. // check Occupy() work properly
  121. for i := 128; i < 256; i++ {
  122. rcidrs = append(rcidrs, cidrs[i])
  123. }
  124. if !reflect.DeepEqual(cidrs, rcidrs) {
  125. t.Fatalf("expected re-allocated cidrs are the same collection")
  126. }
  127. }
  128. func TestGetBitforCIDR(t *testing.T) {
  129. cases := []struct {
  130. clusterCIDRStr string
  131. subNetMaskSize int
  132. subNetCIDRStr string
  133. expectedBit int
  134. expectErr bool
  135. }{
  136. {
  137. clusterCIDRStr: "127.0.0.0/8",
  138. subNetMaskSize: 16,
  139. subNetCIDRStr: "127.0.0.0/16",
  140. expectedBit: 0,
  141. expectErr: false,
  142. },
  143. {
  144. clusterCIDRStr: "127.0.0.0/8",
  145. subNetMaskSize: 16,
  146. subNetCIDRStr: "127.123.0.0/16",
  147. expectedBit: 123,
  148. expectErr: false,
  149. },
  150. {
  151. clusterCIDRStr: "127.0.0.0/8",
  152. subNetMaskSize: 16,
  153. subNetCIDRStr: "127.168.0.0/16",
  154. expectedBit: 168,
  155. expectErr: false,
  156. },
  157. {
  158. clusterCIDRStr: "127.0.0.0/8",
  159. subNetMaskSize: 16,
  160. subNetCIDRStr: "127.224.0.0/16",
  161. expectedBit: 224,
  162. expectErr: false,
  163. },
  164. {
  165. clusterCIDRStr: "192.168.0.0/16",
  166. subNetMaskSize: 24,
  167. subNetCIDRStr: "192.168.12.0/24",
  168. expectedBit: 12,
  169. expectErr: false,
  170. },
  171. {
  172. clusterCIDRStr: "192.168.0.0/16",
  173. subNetMaskSize: 24,
  174. subNetCIDRStr: "192.168.151.0/24",
  175. expectedBit: 151,
  176. expectErr: false,
  177. },
  178. {
  179. clusterCIDRStr: "192.168.0.0/16",
  180. subNetMaskSize: 24,
  181. subNetCIDRStr: "127.168.224.0/24",
  182. expectErr: true,
  183. },
  184. }
  185. for _, tc := range cases {
  186. _, clusterCIDR, err := net.ParseCIDR(tc.clusterCIDRStr)
  187. if err != nil {
  188. t.Fatalf("unexpected error: %v", err)
  189. }
  190. cs := newCIDRSet(clusterCIDR, tc.subNetMaskSize)
  191. _, subnetCIDR, err := net.ParseCIDR(tc.subNetCIDRStr)
  192. if err != nil {
  193. t.Fatalf("unexpected error: %v", err)
  194. }
  195. got, err := cs.getIndexForCIDR(subnetCIDR)
  196. if err == nil && tc.expectErr {
  197. glog.Errorf("expected error but got null")
  198. continue
  199. }
  200. if err != nil && !tc.expectErr {
  201. glog.Errorf("unexpected error: %v", err)
  202. continue
  203. }
  204. if got != tc.expectedBit {
  205. glog.Errorf("expected %v, but got %v", tc.expectedBit, got)
  206. }
  207. }
  208. }
  209. func TestOccupy(t *testing.T) {
  210. cases := []struct {
  211. clusterCIDRStr string
  212. subNetMaskSize int
  213. subNetCIDRStr string
  214. expectedUsedBegin int
  215. expectedUsedEnd int
  216. expectErr bool
  217. }{
  218. {
  219. clusterCIDRStr: "127.0.0.0/8",
  220. subNetMaskSize: 16,
  221. subNetCIDRStr: "127.0.0.0/8",
  222. expectedUsedBegin: 0,
  223. expectedUsedEnd: 256,
  224. expectErr: false,
  225. },
  226. {
  227. clusterCIDRStr: "127.0.0.0/8",
  228. subNetMaskSize: 16,
  229. subNetCIDRStr: "127.0.0.0/2",
  230. expectedUsedBegin: 0,
  231. expectedUsedEnd: 256,
  232. expectErr: false,
  233. },
  234. {
  235. clusterCIDRStr: "127.0.0.0/8",
  236. subNetMaskSize: 16,
  237. subNetCIDRStr: "127.0.0.0/16",
  238. expectedUsedBegin: 0,
  239. expectedUsedEnd: 0,
  240. expectErr: false,
  241. },
  242. {
  243. clusterCIDRStr: "127.0.0.0/8",
  244. subNetMaskSize: 32,
  245. subNetCIDRStr: "127.0.0.0/16",
  246. expectedUsedBegin: 0,
  247. expectedUsedEnd: 65535,
  248. expectErr: false,
  249. },
  250. {
  251. clusterCIDRStr: "127.0.0.0/7",
  252. subNetMaskSize: 16,
  253. subNetCIDRStr: "127.0.0.0/15",
  254. expectedUsedBegin: 256,
  255. expectedUsedEnd: 257,
  256. expectErr: false,
  257. },
  258. {
  259. clusterCIDRStr: "127.0.0.0/7",
  260. subNetMaskSize: 15,
  261. subNetCIDRStr: "127.0.0.0/15",
  262. expectedUsedBegin: 128,
  263. expectedUsedEnd: 128,
  264. expectErr: false,
  265. },
  266. {
  267. clusterCIDRStr: "127.0.0.0/7",
  268. subNetMaskSize: 18,
  269. subNetCIDRStr: "127.0.0.0/15",
  270. expectedUsedBegin: 1024,
  271. expectedUsedEnd: 1031,
  272. expectErr: false,
  273. },
  274. }
  275. for _, tc := range cases {
  276. _, clusterCIDR, err := net.ParseCIDR(tc.clusterCIDRStr)
  277. if err != nil {
  278. t.Fatalf("unexpected error: %v", err)
  279. }
  280. cs := newCIDRSet(clusterCIDR, tc.subNetMaskSize)
  281. _, subnetCIDR, err := net.ParseCIDR(tc.subNetCIDRStr)
  282. if err != nil {
  283. t.Fatalf("unexpected error: %v", err)
  284. }
  285. err = cs.occupy(subnetCIDR)
  286. if err == nil && tc.expectErr {
  287. t.Errorf("expected error but got none")
  288. continue
  289. }
  290. if err != nil && !tc.expectErr {
  291. t.Errorf("unexpected error: %v", err)
  292. continue
  293. }
  294. expectedUsed := big.Int{}
  295. for i := tc.expectedUsedBegin; i <= tc.expectedUsedEnd; i++ {
  296. expectedUsed.SetBit(&expectedUsed, i, 1)
  297. }
  298. if expectedUsed.Cmp(&cs.used) != 0 {
  299. t.Errorf("error")
  300. }
  301. }
  302. }