Debian Codegraph @

plugins/ipam/host-local/backend/allocator/range_set_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 "net"
19
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22 )
23
24 var _ = Describe("range sets", func() {
25 It("should detect set membership correctly", func() {
26 p := RangeSet{
27 Range{Subnet: mustSubnet("192.168.0.0/24")},
28 Range{Subnet: mustSubnet("172.16.1.0/24")},
29 }
30
31 err := p.Canonicalize()
32 Expect(err).NotTo(HaveOccurred())
33
34 Expect(p.Contains(net.IP{192, 168, 0, 55})).To(BeTrue())
35
36 r, err := p.RangeFor(net.IP{192, 168, 0, 55})
37 Expect(err).NotTo(HaveOccurred())
38 Expect(r).To(Equal(&p[0]))
39
40 r, err = p.RangeFor(net.IP{192, 168, 99, 99})
41 Expect(r).To(BeNil())
42 Expect(err).To(MatchError("192.168.99.99 not in range set 192.168.0.1-192.168.0.254,172.16.1.1-172.16.1.254"))
43 })
44
45 It("should discover overlaps within a set", func() {
46 p := RangeSet{
47 {Subnet: mustSubnet("192.168.0.0/20")},
48 {Subnet: mustSubnet("192.168.2.0/24")},
49 }
50
51 err := p.Canonicalize()
52 Expect(err).To(MatchError("subnets 192.168.0.1-192.168.15.254 and 192.168.2.1-192.168.2.254 overlap"))
53 })
54
55 It("should discover overlaps outside a set", func() {
56 p1 := RangeSet{
57 {Subnet: mustSubnet("192.168.0.0/20")},
58 }
59 p2 := RangeSet{
60 {Subnet: mustSubnet("192.168.2.0/24")},
61 }
62
63 p1.Canonicalize()
64 p2.Canonicalize()
65
66 Expect(p1.Overlaps(&p2)).To(BeTrue())
67 Expect(p2.Overlaps(&p1)).To(BeTrue())
68 })
69 })