go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/abfplugin/abfidx/abfidx_test.go (about) 1 // Copyright (c) 2019 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 abfidx_test 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 "go.ligato.io/cn-infra/v2/logging" 22 23 "go.ligato.io/vpp-agent/v3/plugins/vpp/abfplugin/abfidx" 24 ) 25 26 func TestABFIndexLookupByName(t *testing.T) { 27 RegisterTestingT(t) 28 abfIndex := abfidx.NewABFIndex(logging.DefaultLogger, "abf-index") 29 30 abfIndex.Put("val1", &abfidx.ABFMetadata{Index: 10}) 31 abfIndex.Put("val2", &abfidx.ABFMetadata{Index: 20}) 32 abfIndex.Put("val3", 10) 33 34 metadata, exists := abfIndex.LookupByName("val1") 35 Expect(exists).To(BeTrue()) 36 Expect(metadata).ToNot(BeNil()) 37 Expect(metadata.Index).To(Equal(uint32(10))) 38 39 metadata, exists = abfIndex.LookupByName("val2") 40 Expect(exists).To(BeTrue()) 41 Expect(metadata).ToNot(BeNil()) 42 Expect(metadata.Index).To(Equal(uint32(20))) 43 44 metadata, exists = abfIndex.LookupByName("val3") 45 Expect(exists).To(BeFalse()) 46 Expect(metadata).To(BeNil()) 47 48 metadata, exists = abfIndex.LookupByName("val4") 49 Expect(exists).To(BeFalse()) 50 Expect(metadata).To(BeNil()) 51 } 52 53 func TestABFIndexLookupByIndex(t *testing.T) { 54 RegisterTestingT(t) 55 abfIndex := abfidx.NewABFIndex(logging.DefaultLogger, "abf-index") 56 57 abfIndex.Put("val1", &abfidx.ABFMetadata{Index: 10}) 58 abfIndex.Put("val2", &abfidx.ABFMetadata{Index: 20}) 59 60 name, metadata, exists := abfIndex.LookupByIndex(10) 61 Expect(exists).To(BeTrue()) 62 Expect(name).To(Equal("val1")) 63 Expect(metadata).ToNot(BeNil()) 64 Expect(metadata.Index).To(Equal(uint32(10))) 65 66 name, metadata, exists = abfIndex.LookupByIndex(20) 67 Expect(exists).To(BeTrue()) 68 Expect(name).To(Equal("val2")) 69 Expect(metadata).ToNot(BeNil()) 70 Expect(metadata.Index).To(Equal(uint32(20))) 71 72 name, metadata, exists = abfIndex.LookupByIndex(30) 73 Expect(exists).To(BeFalse()) 74 Expect(name).To(Equal("")) 75 Expect(metadata).To(BeNil()) 76 }