Debian Codegraph @

plugins/ipam/host-local/backend/allocator/allocator_test.go

raw symbols SCIP
1 // Copyright 2017 CNI authors
2 //
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 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package allocator
16
17 import (
18 "fmt"
19 "net"
20
21 . "github.com/onsi/ginkgo/v2"
22 . "github.com/onsi/gomega"
23
24 "github.com/containernetworking/cni/pkg/types"
25 current "github.com/containernetworking/cni/pkg/types/100"
26 fakestore "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/testing"
27 )
28
29 type AllocatorTestCase struct {
30 subnets []string
31 ipmap map[string]string
32 expectResult string
33 lastIP string
34 }
35
36 func mkalloc() IPAllocator {
37 p := RangeSet{
38 Range{Subnet: mustSubnet("192.168.1.0/29")},
39 }
40 p.Canonicalize()
41 store := fakestore.NewFakeStore(map[string]string{}, map[string]net.IP{})
42
43 alloc := IPAllocator{
44 rangeset: &p,
45 store: store,
46 rangeID: "rangeid",
47 }
48
49 return alloc
50 }
51
52 func newAllocatorWithMultiRanges() IPAllocator {
53 p := RangeSet{
54 Range{RangeStart: net.IP{192, 168, 1, 0}, RangeEnd: net.IP{192, 168, 1, 3}, Subnet: mustSubnet("192.168.1.1/30")},
55 Range{RangeStart: net.IP{192, 168, 2, 0}, RangeEnd: net.IP{192, 168, 2, 3}, Subnet: mustSubnet("192.168.2.1/30")},
56 }
57 _ = p.Canonicalize()
58 store := fakestore.NewFakeStore(map[string]string{}, map[string]net.IP{})
59
60 alloc := IPAllocator{
61 rangeset: &p,
62 store: store,
63 rangeID: "rangeid",
64 }
65
66 return alloc
67 }
68
69 func (t AllocatorTestCase) run(idx int) (*current.IPConfig, error) {
70 fmt.Fprintln(GinkgoWriter, "Index:", idx)
71 p := RangeSet{}
72 for _, s := range t.subnets {
73 subnet, err := types.ParseCIDR(s)
74 if err != nil {
75 return nil, err
76 }
77 p = append(p, Range{Subnet: types.IPNet(*subnet)})
78 }
79
80 Expect(p.Canonicalize()).To(Succeed())
81
82 store := fakestore.NewFakeStore(t.ipmap, map[string]net.IP{"rangeid": net.ParseIP(t.lastIP)})
83
84 alloc := IPAllocator{
85 rangeset: &p,
86 store: store,
87 rangeID: "rangeid",
88 }
89
90 return alloc.Get("ID", "eth0", nil)
91 }
92
93 var _ = Describe("host-local ip allocator", func() {
94 Context("RangeIter", func() {
95 It("should loop correctly from the beginning", func() {
96 a := mkalloc()
97 r, _ := a.GetIter()
98 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 2}))
99 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 3}))
100 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 4}))
101 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 5}))
102 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 6}))
103 Expect(r.nextip()).To(BeNil())
104 })
105
106 It("should loop correctly from the end", func() {
107 a := mkalloc()
108 a.store.Reserve("ID", "eth0", net.IP{192, 168, 1, 6}, a.rangeID)
109 a.store.ReleaseByID("ID", "eth0")
110 r, _ := a.GetIter()
111 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 2}))
112 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 3}))
113 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 4}))
114 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 5}))
115 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 6}))
116 Expect(r.nextip()).To(BeNil())
117 })
118 It("should loop correctly from the middle", func() {
119 a := mkalloc()
120 a.store.Reserve("ID", "eth0", net.IP{192, 168, 1, 3}, a.rangeID)
121 a.store.ReleaseByID("ID", "eth0")
122 r, _ := a.GetIter()
123 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 4}))
124 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 5}))
125 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 6}))
126 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 2}))
127 Expect(r.nextip()).To(Equal(net.IP{192, 168, 1, 3}))
128 Expect(r.nextip()).To(BeNil())
129 })
130 })
131
132 Context("when has free ip", func() {
133 It("should allocate ips in round robin", func() {
134 testCases := []AllocatorTestCase{
135 // fresh start
136 {
137 subnets: []string{"10.0.0.0/29"},
138 ipmap: map[string]string{},
139 expectResult: "10.0.0.2",
140 lastIP: "",
141 },
142 {
143 subnets: []string{"2001:db8:1::0/64"},
144 ipmap: map[string]string{},
145 expectResult: "2001:db8:1::2",
146 lastIP: "",
147 },
148 {
149 subnets: []string{"10.0.0.0/30"},
150 ipmap: map[string]string{},
151 expectResult: "10.0.0.2",
152 lastIP: "",
153 },
154 {
155 subnets: []string{"10.0.0.0/29"},
156 ipmap: map[string]string{
157 "10.0.0.2": "id",
158 },
159 expectResult: "10.0.0.3",
160 lastIP: "",
161 },
162 // next ip of last reserved ip
163 {
164 subnets: []string{"10.0.0.0/29"},
165 ipmap: map[string]string{},
166 expectResult: "10.0.0.6",
167 lastIP: "10.0.0.5",
168 },
169 {
170 subnets: []string{"10.0.0.0/29"},
171 ipmap: map[string]string{
172 "10.0.0.4": "id",
173 "10.0.0.5": "id",
174 },
175 expectResult: "10.0.0.6",
176 lastIP: "10.0.0.3",
177 },
178 // round robin to the beginning
179 {
180 subnets: []string{"10.0.0.0/29"},
181 ipmap: map[string]string{
182 "10.0.0.6": "id",
183 },
184 expectResult: "10.0.0.2",
185 lastIP: "10.0.0.5",
186 },
187 // lastIP is out of range
188 {
189 subnets: []string{"10.0.0.0/29"},
190 ipmap: map[string]string{
191 "10.0.0.2": "id",
192 },
193 expectResult: "10.0.0.3",
194 lastIP: "10.0.0.128",
195 },
196 // subnet is completely full except for lastip
197 // wrap around and reserve lastIP
198 {
199 subnets: []string{"10.0.0.0/29"},
200 ipmap: map[string]string{
201 "10.0.0.2": "id",
202 "10.0.0.4": "id",
203 "10.0.0.5": "id",
204 "10.0.0.6": "id",
205 },
206 expectResult: "10.0.0.3",
207 lastIP: "10.0.0.3",
208 },
209 // alocate from multiple subnets
210 {
211 subnets: []string{"10.0.0.0/30", "10.0.1.0/30"},
212 expectResult: "10.0.0.2",
213 ipmap: map[string]string{},
214 },
215 // advance to next subnet
216 {
217 subnets: []string{"10.0.0.0/30", "10.0.1.0/30"},
218 lastIP: "10.0.0.2",
219 expectResult: "10.0.1.2",
220 ipmap: map[string]string{},
221 },
222 // Roll to start subnet
223 {
224 subnets: []string{"10.0.0.0/30", "10.0.1.0/30", "10.0.2.0/30"},
225 lastIP: "10.0.2.2",
226 expectResult: "10.0.0.2",
227 ipmap: map[string]string{},
228 },
229 }
230
231 for idx, tc := range testCases {
232 res, err := tc.run(idx)
233 Expect(err).ToNot(HaveOccurred())
234 Expect(res.Address.IP.String()).To(Equal(tc.expectResult))
235 }
236 })
237
238 It("should not allocate the broadcast address", func() {
239 alloc := mkalloc()
240 for i := 2; i < 7; i++ {
241 res, err := alloc.Get(fmt.Sprintf("ID%d", i), "eth0", nil)
242 Expect(err).ToNot(HaveOccurred())
243 s := fmt.Sprintf("192.168.1.%d/29", i)
244 Expect(s).To(Equal(res.Address.String()))
245 fmt.Fprintln(GinkgoWriter, "got ip", res.Address.String())
246 }
247
248 x, err := alloc.Get("ID8", "eth0", nil)
249 fmt.Fprintln(GinkgoWriter, "got ip", x)
250 Expect(err).To(HaveOccurred())
251 })
252
253 It("should allocate in a round-robin fashion", func() {
254 alloc := mkalloc()
255 res, err := alloc.Get("ID", "eth0", nil)
256 Expect(err).ToNot(HaveOccurred())
257 Expect(res.Address.String()).To(Equal("192.168.1.2/29"))
258
259 err = alloc.Release("ID", "eth0")
260 Expect(err).ToNot(HaveOccurred())
261
262 res, err = alloc.Get("ID", "eth0", nil)
263 Expect(err).ToNot(HaveOccurred())
264 Expect(res.Address.String()).To(Equal("192.168.1.3/29"))
265 })
266
267 Context("when requesting a specific IP", func() {
268 It("must allocate the requested IP", func() {
269 alloc := mkalloc()
270 requestedIP := net.IP{192, 168, 1, 5}
271 res, err := alloc.Get("ID", "eth0", requestedIP)
272 Expect(err).ToNot(HaveOccurred())
273 Expect(res.Address.IP.String()).To(Equal(requestedIP.String()))
274 })
275
276 It("must fail when the requested IP is allocated", func() {
277 alloc := mkalloc()
278 requestedIP := net.IP{192, 168, 1, 5}
279 res, err := alloc.Get("ID", "eth0", requestedIP)
280 Expect(err).ToNot(HaveOccurred())
281 Expect(res.Address.IP.String()).To(Equal(requestedIP.String()))
282
283 _, err = alloc.Get("ID", "eth0", requestedIP)
284 Expect(err).To(MatchError(`requested IP address 192.168.1.5 is not available in range set 192.168.1.1-192.168.1.6`))
285 })
286
287 It("must return an error when the requested IP is after RangeEnd", func() {
288 alloc := mkalloc()
289 (*alloc.rangeset)[0].RangeEnd = net.IP{192, 168, 1, 4}
290 requestedIP := net.IP{192, 168, 1, 5}
291 _, err := alloc.Get("ID", "eth0", requestedIP)
292 Expect(err).To(HaveOccurred())
293 })
294
295 It("must return an error when the requested IP is before RangeStart", func() {
296 alloc := mkalloc()
297 (*alloc.rangeset)[0].RangeStart = net.IP{192, 168, 1, 3}
298 requestedIP := net.IP{192, 168, 1, 2}
299 _, err := alloc.Get("ID", "eth0", requestedIP)
300 Expect(err).To(HaveOccurred())
301 })
302 })
303 })
304 Context("when out of ips", func() {
305 It("returns a meaningful error", func() {
306 testCases := []AllocatorTestCase{
307 {
308 subnets: []string{"10.0.0.0/30"},
309 ipmap: map[string]string{
310 "10.0.0.2": "id",
311 },
312 },
313 {
314 subnets: []string{"10.0.0.0/29"},
315 ipmap: map[string]string{
316 "10.0.0.2": "id",
317 "10.0.0.3": "id",
318 "10.0.0.4": "id",
319 "10.0.0.5": "id",
320 "10.0.0.6": "id",
321 },
322 },
323 {
324 subnets: []string{"10.0.0.0/30", "10.0.1.0/30"},
325 ipmap: map[string]string{
326 "10.0.0.2": "id",
327 "10.0.1.2": "id",
328 },
329 },
330 }
331 for idx, tc := range testCases {
332 _, err := tc.run(idx)
333 Expect(err).To(HaveOccurred())
334 Expect(err.Error()).To(HavePrefix("no IP addresses available in range set"))
335 }
336 })
337 })
338
339 Context("when lastReservedIP is at the end of one of multi ranges", func() {
340 It("should use the first IP of next range as startIP after Next", func() {
341 a := newAllocatorWithMultiRanges()
342
343 // reserve the last IP of the first range
344 reserved, err := a.store.Reserve("ID", "eth0", net.IP{192, 168, 1, 3}, a.rangeID)
345 Expect(reserved).To(BeTrue())
346 Expect(err).NotTo(HaveOccurred())
347
348 // get range iterator and do the first Next
349 r, err := a.GetIter()
350 Expect(err).NotTo(HaveOccurred())
351 ip := r.nextip()
352 Expect(ip).NotTo(BeNil())
353
354 Expect(r.startIP).To(Equal(net.IP{192, 168, 2, 0}))
355 })
356 })
357
358 Context("when no lastReservedIP", func() {
359 It("should use the first IP of the first range as startIP after Next", func() {
360 a := newAllocatorWithMultiRanges()
361
362 // get range iterator and do the first Next
363 r, err := a.GetIter()
364 Expect(err).NotTo(HaveOccurred())
365 ip := r.nextip()
366 Expect(ip).NotTo(BeNil())
367
368 Expect(r.startIP).To(Equal(net.IP{192, 168, 1, 0}))
369 })
370 })
371 })
372
373 // nextip is a convenience function used for testing
374 func (i *RangeIter) nextip() net.IP {
375 c, _ := i.Next()
376 if c == nil {
377 return nil
378 }
379
380 return c.IP
381 }