File: range_set_test.go

package info (click to toggle)
golang-github-containernetworking-plugins 0.9.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,512 kB
  • sloc: sh: 124; makefile: 18
file content (70 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2017 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package allocator

import (
	"net"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("range sets", func() {
	It("should detect set membership correctly", func() {
		p := RangeSet{
			Range{Subnet: mustSubnet("192.168.0.0/24")},
			Range{Subnet: mustSubnet("172.16.1.0/24")},
		}

		err := p.Canonicalize()
		Expect(err).NotTo(HaveOccurred())

		Expect(p.Contains(net.IP{192, 168, 0, 55})).To(BeTrue())

		r, err := p.RangeFor(net.IP{192, 168, 0, 55})
		Expect(err).NotTo(HaveOccurred())
		Expect(r).To(Equal(&p[0]))

		r, err = p.RangeFor(net.IP{192, 168, 99, 99})
		Expect(r).To(BeNil())
		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"))

	})

	It("should discover overlaps within a set", func() {
		p := RangeSet{
			{Subnet: mustSubnet("192.168.0.0/20")},
			{Subnet: mustSubnet("192.168.2.0/24")},
		}

		err := p.Canonicalize()
		Expect(err).To(MatchError("subnets 192.168.0.1-192.168.15.254 and 192.168.2.1-192.168.2.254 overlap"))
	})

	It("should discover overlaps outside a set", func() {
		p1 := RangeSet{
			{Subnet: mustSubnet("192.168.0.0/20")},
		}
		p2 := RangeSet{
			{Subnet: mustSubnet("192.168.2.0/24")},
		}

		p1.Canonicalize()
		p2.Canonicalize()

		Expect(p1.Overlaps(&p2)).To(BeTrue())
		Expect(p2.Overlaps(&p1)).To(BeTrue())
	})
})