github.com/fafucoder/cilium@v1.6.11/pkg/service/id_test.go (about) 1 // Copyright 2016-2018 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 service 18 19 import ( 20 "encoding/json" 21 "net" 22 23 "github.com/cilium/cilium/pkg/checker" 24 "github.com/cilium/cilium/pkg/kvstore" 25 "github.com/cilium/cilium/pkg/loadbalancer" 26 27 . "gopkg.in/check.v1" 28 ) 29 30 var ( 31 l3n4Addr1 = loadbalancer.L3n4Addr{ 32 IP: net.IPv6loopback, 33 L4Addr: loadbalancer.L4Addr{Port: 0, Protocol: "UDP"}, 34 } 35 l3n4Addr2 = loadbalancer.L3n4Addr{ 36 IP: net.IPv6loopback, 37 L4Addr: loadbalancer.L4Addr{Port: 1, Protocol: "TCP"}, 38 } 39 l3n4Addr3 = loadbalancer.L3n4Addr{ 40 IP: net.IPv6loopback, 41 L4Addr: loadbalancer.L4Addr{Port: 1, Protocol: "UDP"}, 42 } 43 wantL3n4AddrID = &loadbalancer.L3n4AddrID{ 44 ID: 123, 45 L3n4Addr: l3n4Addr2, 46 } 47 ) 48 49 func (ds *ServiceTestSuite) TestServices(c *C) { 50 var nilL3n4AddrID *loadbalancer.L3n4AddrID 51 // Set up last free ID with zero 52 id, err := getMaxServiceID() 53 c.Assert(err, Equals, nil) 54 c.Assert(id, Equals, FirstFreeServiceID) 55 56 ffsIDu16 := loadbalancer.ServiceID(uint16(FirstFreeServiceID)) 57 58 l3n4AddrID, err := AcquireID(l3n4Addr1, 0) 59 c.Assert(err, Equals, nil) 60 c.Assert(l3n4AddrID.ID, Equals, loadbalancer.ID(ffsIDu16)) 61 62 l3n4AddrID, err = AcquireID(l3n4Addr1, 0) 63 c.Assert(err, Equals, nil) 64 c.Assert(l3n4AddrID.ID, Equals, loadbalancer.ID(ffsIDu16)) 65 66 l3n4AddrID, err = AcquireID(l3n4Addr2, 0) 67 c.Assert(err, Equals, nil) 68 c.Assert(l3n4AddrID.ID, Equals, loadbalancer.ID(ffsIDu16+1)) 69 70 // l3n4Addr3 should have the same ID as l3n4Addr2 since we are omitting the 71 // protocol type. 72 l3n4AddrID, err = AcquireID(l3n4Addr3, 0) 73 c.Assert(err, Equals, nil) 74 c.Assert(l3n4AddrID.ID, Equals, loadbalancer.ID(ffsIDu16+1)) 75 76 gotL3n4AddrID, err := GetID(FirstFreeServiceID) 77 c.Assert(err, Equals, nil) 78 wantL3n4AddrID.ID = loadbalancer.ID(ffsIDu16) 79 wantL3n4AddrID.L3n4Addr = l3n4Addr1 80 c.Assert(gotL3n4AddrID, checker.DeepEquals, wantL3n4AddrID) 81 82 err = DeleteID(FirstFreeServiceID) 83 c.Assert(err, Equals, nil) 84 gotL3n4AddrID, err = GetID(FirstFreeServiceID) 85 c.Assert(err, Equals, nil) 86 c.Assert(gotL3n4AddrID, Equals, nilL3n4AddrID) 87 88 gotL3n4AddrID, err = GetID(FirstFreeServiceID + 1) 89 c.Assert(err, Equals, nil) 90 wantL3n4AddrID.ID = loadbalancer.ID(FirstFreeServiceID + 1) 91 wantL3n4AddrID.L3n4Addr = l3n4Addr2 92 c.Assert(gotL3n4AddrID, checker.DeepEquals, wantL3n4AddrID) 93 94 err = DeleteID(FirstFreeServiceID) 95 c.Assert(err, Equals, nil) 96 97 err = setIDSpace(FirstFreeServiceID, FirstFreeServiceID) 98 c.Assert(err, Equals, nil) 99 100 err = DeleteID(FirstFreeServiceID) 101 c.Assert(err, Equals, nil) 102 gotL3n4AddrID, err = GetID(FirstFreeServiceID) 103 c.Assert(err, Equals, nil) 104 c.Assert(gotL3n4AddrID, Equals, nilL3n4AddrID) 105 106 gotL3n4AddrID, err = AcquireID(l3n4Addr2, 0) 107 c.Assert(err, Equals, nil) 108 c.Assert(gotL3n4AddrID.ID, Equals, loadbalancer.ID(FirstFreeServiceID+1)) 109 110 err = DeleteID(uint32(gotL3n4AddrID.ID)) 111 c.Assert(err, Equals, nil) 112 err = DeleteID(FirstFreeServiceID + 1) 113 c.Assert(err, Equals, nil) 114 err = DeleteID(FirstFreeServiceID + 1) 115 c.Assert(err, Equals, nil) 116 117 gotL3n4AddrID, err = AcquireID(l3n4Addr2, 0) 118 c.Assert(err, Equals, nil) 119 c.Assert(gotL3n4AddrID.ID, Equals, loadbalancer.ID(ffsIDu16)) 120 121 gotL3n4AddrID, err = AcquireID(l3n4Addr1, 0) 122 c.Assert(err, Equals, nil) 123 c.Assert(gotL3n4AddrID.ID, Equals, loadbalancer.ID(FirstFreeServiceID+1)) 124 125 gotL3n4AddrID, err = AcquireID(l3n4Addr1, 99) 126 c.Assert(err, Equals, nil) 127 c.Assert(gotL3n4AddrID.ID, Equals, loadbalancer.ID(FirstFreeServiceID+1)) 128 129 err = DeleteID(uint32(FirstFreeServiceID + 1)) 130 c.Assert(err, Equals, nil) 131 132 gotL3n4AddrID, err = AcquireID(l3n4Addr1, 99) 133 c.Assert(err, Equals, nil) 134 c.Assert(gotL3n4AddrID.ID, Equals, loadbalancer.ID(99)) 135 } 136 137 func (ds *ServiceTestSuite) TestGetMaxServiceID(c *C) { 138 lastID := uint32(MaxSetOfServiceID - 1) 139 140 marshaledID, err := json.Marshal(lastID) 141 c.Assert(err, IsNil) 142 if enableGlobalServiceIDs { 143 err := kvstore.Client().Set(LastFreeServiceIDKeyPath, marshaledID) 144 c.Assert(err, IsNil) 145 } else { 146 err := setIDSpace(lastID, MaxSetOfServiceID) 147 c.Assert(err, IsNil) 148 } 149 150 id, err := getMaxServiceID() 151 c.Assert(err, Equals, nil) 152 c.Assert(id, Equals, (MaxSetOfServiceID - 1)) 153 } 154 155 func (ds *ServiceTestSuite) TestBackendID(c *C) { 156 firstBackendID := loadbalancer.BackendID(FirstFreeBackendID) 157 158 id1, err := AcquireBackendID(l3n4Addr1) 159 c.Assert(err, Equals, nil) 160 c.Assert(id1, Equals, firstBackendID) 161 162 id1, err = AcquireBackendID(l3n4Addr1) 163 c.Assert(err, Equals, nil) 164 c.Assert(id1, Equals, firstBackendID) 165 166 id2, err := AcquireBackendID(l3n4Addr2) 167 c.Assert(err, Equals, nil) 168 c.Assert(id2, Equals, firstBackendID+1) 169 170 existingID1, err := LookupBackendID(l3n4Addr1) 171 c.Assert(err, Equals, nil) 172 c.Assert(existingID1, Equals, id1) 173 } 174 175 func (ds *ServiceTestSuite) BenchmarkAllocation(c *C) { 176 addr := loadbalancer.L3n4Addr{ 177 IP: net.IPv6loopback, 178 L4Addr: loadbalancer.L4Addr{Port: 0, Protocol: "UDP"}, 179 } 180 181 c.ResetTimer() 182 for i := 0; i < c.N; i++ { 183 addr.L4Addr.Port = uint16(c.N) 184 _, err := AcquireID(addr, 0) 185 c.Assert(err, IsNil) 186 } 187 c.StopTimer() 188 189 }