go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/aclplugin/aclidx/aclidx_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 aclidx_test 16 17 /* 18 import ( 19 "testing" 20 21 "github.com/ligato/vpp-agent/pkg/idxvpp" 22 "github.com/ligato/vpp-agent/pkg/idxvpp/nametoidx" 23 . "github.com/onsi/gomega" 24 ) 25 26 27 func aclIndexTestInitialization(t *testing.T) (idxvpp.NameToIdxRW, aclidx.ACLIndexRW) { 28 RegisterTestingT(t) 29 30 // initialize index 31 nameToIdx := nametoidx.NewNameToIdx(logrus.DefaultLogger(), "index_test", nil) 32 index := aclidx.NewACLIndex(nameToIdx) 33 names := nameToIdx.ListNames() 34 35 // check if names were empty 36 Expect(names).To(BeEmpty()) 37 38 return index.GetMapping(), index 39 } 40 41 var acldata = acl.AccessLists_Acl{ 42 AclName: "acl1", 43 Rules: []*acl.AccessLists_Acl_Rule{{AclAction: acl.AclAction_PERMIT}}, 44 Interfaces: &acl.AccessLists_Acl_Interfaces{}, 45 } 46 47 // Tests registering and unregistering name to index 48 func TestRegisterAndUnregisterName(t *testing.T) { 49 mapping, index := aclIndexTestInitialization(t) 50 51 // Register entry 52 index.RegisterName("acl1", 0, &acldata) 53 names := mapping.ListNames() 54 Expect(names).To(HaveLen(1)) 55 Expect(names).To(ContainElement("acl1")) 56 57 // Unregister entry 58 index.UnregisterName("acl1") 59 names = mapping.ListNames() 60 Expect(names).To(BeEmpty()) 61 } 62 63 // Tests index mapping clear 64 func TestClear(t *testing.T) { 65 mapping, index := aclIndexTestInitialization(t) 66 67 // Register entries 68 index.RegisterName("acl1", 0, nil) 69 index.RegisterName("acl2", 1, nil) 70 index.RegisterName("acl3", 2, nil) 71 names := mapping.ListNames() 72 Expect(names).To(HaveLen(3)) 73 74 // Clear 75 index.Clear() 76 names = mapping.ListNames() 77 Expect(names).To(BeEmpty()) 78 } 79 80 func TestLookupIndex(t *testing.T) { 81 RegisterTestingT(t) 82 83 _, aclIndex := aclIndexTestInitialization(t) 84 85 aclIndex.RegisterName("acl", 0, &acldata) 86 87 foundName, acl, exist := aclIndex.LookupName(0) 88 Expect(exist).To(BeTrue()) 89 Expect(foundName).To(Equal("acl")) 90 Expect(acl.AclName).To(Equal("acl1")) 91 } 92 93 func TestLookupName(t *testing.T) { 94 RegisterTestingT(t) 95 96 _, aclIndex := aclIndexTestInitialization(t) 97 98 aclIndex.RegisterName("acl", 0, &acldata) 99 100 foundName, acl, exist := aclIndex.LookupIdx("acl") 101 Expect(exist).To(BeTrue()) 102 Expect(foundName).To(Equal(uint32(0))) 103 Expect(acl.AclName).To(Equal("acl1")) 104 } 105 106 func TestWatchNameToIdx(t *testing.T) { 107 RegisterTestingT(t) 108 109 _, aclIndex := aclIndexTestInitialization(t) 110 111 c := make(chan aclidx.IdxDto) 112 aclIndex.WatchNameToIdx("testName", c) 113 114 aclIndex.RegisterName("aclX", 0, &acldata) 115 116 var dto aclidx.IdxDto 117 Eventually(c).Should(Receive(&dto)) 118 Expect(dto.Name).To(Equal("aclX")) 119 Expect(dto.NameToIdxDtoWithoutMeta.Idx).To(Equal(uint32(0))) 120 } 121 */