go.ligato.io/vpp-agent/v3@v3.5.0/pkg/idxvpp/idxvpp_test.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 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 idxvpp 16 17 import ( 18 "strconv" 19 "testing" 20 21 "github.com/onsi/gomega" 22 "go.ligato.io/cn-infra/v2/logging/logrus" 23 ) 24 25 const ( 26 idx1 = 1 27 idx2 = 2 28 idx3 = 3 29 ) 30 31 var ( 32 eth0 = "eth0" 33 eth1 = "eth1" 34 eth2 = "eth2" 35 ) 36 37 func IndexFactory() (NameToIndexRW, error) { 38 return NewNameToIndex(logrus.DefaultLogger(), "test", nil), nil 39 } 40 41 func Test01UnregisteredMapsToNothing(t *testing.T) { 42 Given(t).NameToIdx(IndexFactory, nil). 43 When().Name(eth1).IsDeleted(). 44 Then().Name(eth1).MapsToNothing(). 45 And().Notification(eth1, Write).IsNotExpected() 46 } 47 48 func Test02RegisteredReturnsIdx(t *testing.T) { 49 Given(t).NameToIdx(IndexFactory, nil). 50 When().Name(eth1).IsAdded(idx1). 51 Then().Name(eth1).MapsTo(idx1). 52 And().Notification(eth1, Write).IsExpectedFor(idx1) 53 } 54 55 func Test03RegFirstThenUnreg(t *testing.T) { 56 Given(t).NameToIdx(IndexFactory, map[string]uint32{eth1: idx1}). 57 When().Name(eth1).IsDeleted(). 58 Then().Name(eth1).MapsToNothing(). 59 And().Notification(eth1, Del).IsExpectedFor(idx1) 60 } 61 62 func Test03Eth0RegPlusEth1Unreg(t *testing.T) { 63 Given(t).NameToIdx(IndexFactory, map[string]uint32{eth0: idx1, eth1: idx2}). 64 When().Name(eth1).IsDeleted(). 65 Then().Name(eth1).MapsToNothing(). 66 And().Notification(eth1, Del).IsExpectedFor(idx2). 67 And().Name(eth0).MapsTo(idx1). 68 And().Notification(eth0, Write).IsNotExpected() //because watch is registered after given keyword 69 } 70 71 func Test04RegTwiceSameNameWithDifferentIdx(t *testing.T) { 72 Given(t).NameToIdx(IndexFactory, nil). 73 When().Name(eth1).IsAdded(idx1). 74 Then().Name(eth1).MapsTo(idx1). //Notif eth1, idx1 75 And().Notification(eth1, Write).IsExpectedFor(idx1). 76 When().Name(eth1).IsAdded(idx2). 77 Then().Name(eth1).MapsTo(idx2). //Notif eth1, idx1 78 And().Notification(eth1, Write).IsExpectedFor(idx2) 79 } 80 81 const ( 82 flagKey = "flag" 83 valsKey = "vals" 84 ) 85 86 type Item struct { 87 index uint32 88 flag bool 89 vals []string 90 } 91 92 func (item *Item) GetIndex() uint32 { 93 return item.index 94 } 95 96 func createIdx(item interface{}) map[string][]string { 97 typed, ok := item.(*Item) 98 if !ok { 99 return nil 100 } 101 102 return map[string][]string{ 103 flagKey: {strconv.FormatBool(typed.flag)}, 104 valsKey: typed.vals, 105 } 106 } 107 108 func TestIndexedMetadata(t *testing.T) { 109 gomega.RegisterTestingT(t) 110 idxm := NewNameToIndex(logrus.DefaultLogger(), "title", createIdx) 111 112 res := idxm.ListNames(flagKey, "true") 113 gomega.Expect(res).To(gomega.BeNil()) 114 115 item1 := &Item{ 116 index: idx1, 117 flag: true, 118 vals: []string{"abc", "def", "xyz"}, 119 } 120 item2 := &Item{ 121 index: idx2, 122 flag: false, 123 vals: []string{"abc", "klm", "opq"}, 124 } 125 item3 := &Item{ 126 index: idx3, 127 flag: true, 128 vals: []string{"jkl"}, 129 } 130 131 idxm.Put(eth0, item1) 132 idxm.Put(eth1, item2) 133 idxm.Put(eth2, item3) 134 135 res = idxm.ListNames(flagKey, "false") 136 gomega.Expect(res).NotTo(gomega.BeNil()) 137 gomega.Expect(res[0]).To(gomega.BeEquivalentTo(eth1)) 138 139 res = idxm.ListNames(flagKey, "true") 140 gomega.Expect(len(res)).To(gomega.BeEquivalentTo(2)) 141 gomega.Expect(res).To(gomega.ContainElement(string(eth0))) 142 gomega.Expect(res).To(gomega.ContainElement(string(eth2))) 143 144 res = idxm.ListNames(valsKey, "abc") 145 gomega.Expect(len(res)).To(gomega.BeEquivalentTo(2)) 146 gomega.Expect(res).To(gomega.ContainElement(string(eth0))) 147 gomega.Expect(res).To(gomega.ContainElement(string(eth1))) 148 149 res = idxm.ListNames(valsKey, "jkl") 150 gomega.Expect(len(res)).To(gomega.BeEquivalentTo(1)) 151 gomega.Expect(res[0]).To(gomega.BeEquivalentTo(eth2)) 152 153 idxm.Delete(eth0) 154 res = idxm.ListNames(flagKey, "true") 155 gomega.Expect(len(res)).To(gomega.BeEquivalentTo(1)) 156 gomega.Expect(res[0]).To(gomega.BeEquivalentTo(eth2)) 157 158 } 159 160 func TestOldIndexRemove(t *testing.T) { 161 gomega.RegisterTestingT(t) 162 idxm := NewNameToIndex(logrus.DefaultLogger(), "title", nil) 163 164 idxm.Put(eth0, &OnlyIndex{idx1}) 165 166 item, found := idxm.LookupByName(eth0) 167 gomega.Expect(found).To(gomega.BeTrue()) 168 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx1)) 169 170 name, _, found := idxm.LookupByIndex(idx1) 171 gomega.Expect(found).To(gomega.BeTrue()) 172 gomega.Expect(name).To(gomega.BeEquivalentTo(eth0)) 173 174 idxm.Put(eth0, &OnlyIndex{idx2}) 175 176 item, found = idxm.LookupByName(eth0) 177 gomega.Expect(found).To(gomega.BeTrue()) 178 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx2)) 179 180 name, item, found = idxm.LookupByIndex(idx2) 181 gomega.Expect(found).To(gomega.BeTrue()) 182 gomega.Expect(name).To(gomega.BeEquivalentTo(string(eth0))) 183 gomega.Expect(item).ToNot(gomega.BeNil()) 184 185 name, item, found = idxm.LookupByIndex(idx1) 186 gomega.Expect(found).To(gomega.BeFalse()) 187 gomega.Expect(name).To(gomega.BeEquivalentTo("")) 188 gomega.Expect(item).To(gomega.BeNil()) 189 } 190 191 func TestUpdateIndex(t *testing.T) { 192 gomega.RegisterTestingT(t) 193 idxm := NewNameToIndex(logrus.DefaultLogger(), "title", nil) 194 195 idxm.Put(eth0, &OnlyIndex{idx1}) 196 197 item, found := idxm.LookupByName(eth0) 198 gomega.Expect(found).To(gomega.BeTrue()) 199 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx1)) 200 201 success := idxm.Update(eth0, &OnlyIndex{idx2}) 202 gomega.Expect(success).To(gomega.BeTrue()) 203 204 item, found = idxm.LookupByName(eth0) 205 gomega.Expect(found).To(gomega.BeTrue()) 206 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx2)) 207 } 208 209 func TestClearMapping(t *testing.T) { 210 gomega.RegisterTestingT(t) 211 idxm := NewNameToIndex(logrus.DefaultLogger(), "title", nil) 212 213 idxm.Put(eth0, &OnlyIndex{idx1}) 214 idxm.Put(eth1, &OnlyIndex{idx2}) 215 idxm.Put(eth2, &OnlyIndex{idx3}) 216 217 item, found := idxm.LookupByName(eth0) 218 gomega.Expect(found).To(gomega.BeTrue()) 219 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx1)) 220 221 item, found = idxm.LookupByName(eth1) 222 gomega.Expect(found).To(gomega.BeTrue()) 223 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx2)) 224 225 item, found = idxm.LookupByName(eth2) 226 gomega.Expect(found).To(gomega.BeTrue()) 227 gomega.Expect(item.GetIndex()).To(gomega.BeEquivalentTo(idx3)) 228 229 idxm.Clear() 230 231 _, found = idxm.LookupByName(eth0) 232 gomega.Expect(found).To(gomega.BeFalse()) 233 234 _, found = idxm.LookupByName(eth1) 235 gomega.Expect(found).To(gomega.BeFalse()) 236 237 _, found = idxm.LookupByName(eth2) 238 gomega.Expect(found).To(gomega.BeFalse()) 239 }