github.com/datadog/cilium@v1.6.12/pkg/ipam/ipam_test.go (about) 1 // Copyright 2016-2017 Authors of Cilium 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 // +build !privileged_tests 16 17 package ipam 18 19 import ( 20 "net" 21 "testing" 22 23 "github.com/cilium/cilium/common/addressing" 24 "github.com/cilium/cilium/pkg/checker" 25 "github.com/cilium/cilium/pkg/datapath/fake" 26 27 . "gopkg.in/check.v1" 28 ) 29 30 func Test(t *testing.T) { 31 TestingT(t) 32 } 33 34 type IPAMSuite struct{} 35 36 var _ = Suite(&IPAMSuite{}) 37 38 func (s *IPAMSuite) TestLock(c *C) { 39 fakeAddressing := fake.NewNodeAddressing() 40 ipam := NewIPAM(fakeAddressing, Configuration{EnableIPv4: true, EnableIPv6: true}, &ownerMock{}) 41 42 // Since the IPs we have allocated to the endpoints might or might not 43 // be in the allocrange specified in cilium, we need to specify them 44 // manually on the endpoint based on the alloc range. 45 ipv4 := fakeAddressing.IPv4().AllocationCIDR().IP 46 nextIP(ipv4) 47 epipv4, err := addressing.NewCiliumIPv4(ipv4.String()) 48 c.Assert(err, IsNil) 49 50 ipv6 := fakeAddressing.IPv6().AllocationCIDR().IP 51 nextIP(ipv6) 52 epipv6, err := addressing.NewCiliumIPv6(ipv6.String()) 53 c.Assert(err, IsNil) 54 55 // Forcefully release possible allocated IPs 56 err = ipam.IPv4Allocator.Release(epipv4.IP()) 57 c.Assert(err, IsNil) 58 err = ipam.IPv6Allocator.Release(epipv6.IP()) 59 c.Assert(err, IsNil) 60 61 // Let's allocate the IP first so we can see the tests failing 62 result, err := ipam.IPv4Allocator.Allocate(epipv4.IP(), "test") 63 c.Assert(err, IsNil) 64 c.Assert(result.IP, checker.DeepEquals, epipv4.IP()) 65 66 err = ipam.IPv4Allocator.Release(epipv4.IP()) 67 c.Assert(err, IsNil) 68 } 69 70 func (s *IPAMSuite) TestBlackList(c *C) { 71 fakeAddressing := fake.NewNodeAddressing() 72 ipam := NewIPAM(fakeAddressing, Configuration{EnableIPv4: true, EnableIPv6: true}, &ownerMock{}) 73 74 // force copy of first possible IP in allocation range 75 ipv4 := net.ParseIP(fakeAddressing.IPv4().AllocationCIDR().IP.String()) 76 nextIP(ipv4) 77 78 ipam.BlacklistIP(ipv4, "test") 79 err := ipam.AllocateIP(ipv4, "test") 80 c.Assert(err, Not(IsNil)) 81 ipam.ReleaseIP(ipv4) 82 83 nextIP(ipv4) 84 _, ipNet, err := net.ParseCIDR(fakeAddressing.IPv4().AllocationCIDR().String()) 85 c.Assert(err, IsNil) 86 ipam.BlacklistIPNet(*ipNet, "test") 87 c.Assert(ipam.blacklist.Contains(ipv4), Equals, true) 88 89 // force copy of first possible IP in allocation range 90 ipv6 := net.ParseIP(fakeAddressing.IPv6().AllocationCIDR().IP.String()) 91 nextIP(ipv6) 92 93 ipam.BlacklistIP(ipv6, "test") 94 err = ipam.AllocateIP(ipv6, "test") 95 c.Assert(err, Not(IsNil)) 96 ipam.ReleaseIP(ipv6) 97 98 nextIP(ipv6) 99 _, ipNet, err = net.ParseCIDR(fakeAddressing.IPv6().AllocationCIDR().String()) 100 c.Assert(err, IsNil) 101 ipam.BlacklistIPNet(*ipNet, "test") 102 c.Assert(ipam.blacklist.Contains(ipv6), Equals, true) 103 } 104 105 func (s *IPAMSuite) TestDeriveFamily(c *C) { 106 c.Assert(DeriveFamily(net.ParseIP("1.1.1.1")), Equals, IPv4) 107 c.Assert(DeriveFamily(net.ParseIP("f00d::1")), Equals, IPv6) 108 } 109 110 func (s *IPAMSuite) TestOwnerRelease(c *C) { 111 fakeAddressing := fake.NewNodeAddressing() 112 ipam := NewIPAM(fakeAddressing, Configuration{EnableIPv4: true, EnableIPv6: true}, &ownerMock{}) 113 114 // force copy of first possible IP in allocation range 115 ipv4 := net.ParseIP(fakeAddressing.IPv4().AllocationCIDR().IP.String()) 116 nextIP(ipv4) 117 err := ipam.AllocateIP(ipv4, "default/test") 118 c.Assert(err, IsNil) 119 120 ipv6 := net.ParseIP(fakeAddressing.IPv6().AllocationCIDR().IP.String()) 121 nextIP(ipv6) 122 err = ipam.AllocateIP(ipv6, "default/test") 123 c.Assert(err, IsNil) 124 125 // unknown owner, must fail 126 err = ipam.ReleaseIPString("default/test2") 127 c.Assert(err, Not(IsNil)) 128 // 1st release by correct owner, must succeed 129 err = ipam.ReleaseIPString("default/test") 130 c.Assert(err, IsNil) 131 // 2nd release by owner, must now fail 132 err = ipam.ReleaseIPString("default/test") 133 c.Assert(err, Not(IsNil)) 134 }