go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l3plugin/vppcalls/vpp2202/vrf_vppcalls_test.go (about) 1 // Copyright (c) 2022 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 vpp2202_test 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 "go.ligato.io/cn-infra/v2/logging/logrus" 22 23 vpp_ip "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls" 25 vpp2202 "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls/vpp2202" 26 "go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock" 27 l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3" 28 ) 29 30 var vrfTables = []*l3.VrfTable{ 31 { 32 Id: 1, 33 Protocol: l3.VrfTable_IPV4, 34 Label: "table1", 35 }, 36 { 37 Id: 1, 38 Protocol: l3.VrfTable_IPV6, 39 Label: "table1", 40 }, 41 { 42 Id: 2, 43 Protocol: l3.VrfTable_IPV6, 44 Label: "table2", 45 }, 46 } 47 48 // Test adding routes 49 func TestAddVrfTable(t *testing.T) { 50 ctx, vtHandler := vrfTableTestSetup(t) 51 defer ctx.TeardownTestCtx() 52 53 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{}) 54 err := vtHandler.AddVrfTable(vrfTables[0]) 55 Expect(err).To(Succeed()) 56 57 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPTableAddDel) 58 Expect(ok).To(BeTrue()) 59 Expect(vppMsg.Table.TableID).To(BeEquivalentTo(1)) 60 Expect(vppMsg.Table.IsIP6).To(BeFalse()) 61 Expect(vppMsg.IsAdd).To(BeTrue()) 62 Expect(vppMsg.Table.Name).To(BeEquivalentTo([]byte("table1"))) 63 64 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{}) 65 err = vtHandler.AddVrfTable(vrfTables[1]) 66 Expect(err).To(Succeed()) 67 68 vppMsg, ok = ctx.MockChannel.Msg.(*vpp_ip.IPTableAddDel) 69 Expect(ok).To(BeTrue()) 70 Expect(vppMsg.Table.TableID).To(BeEquivalentTo(1)) 71 Expect(vppMsg.Table.IsIP6).To(BeTrue()) 72 Expect(vppMsg.IsAdd).To(BeTrue()) 73 Expect(vppMsg.Table.Name).To(BeEquivalentTo([]byte("table1"))) 74 75 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{}) 76 err = vtHandler.AddVrfTable(vrfTables[2]) 77 Expect(err).To(Succeed()) 78 79 vppMsg, ok = ctx.MockChannel.Msg.(*vpp_ip.IPTableAddDel) 80 Expect(ok).To(BeTrue()) 81 Expect(vppMsg.Table.TableID).To(BeEquivalentTo(2)) 82 Expect(vppMsg.Table.IsIP6).To(BeTrue()) 83 Expect(vppMsg.IsAdd).To(BeTrue()) 84 Expect(vppMsg.Table.Name).To(BeEquivalentTo([]byte("table2"))) 85 86 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{Retval: 1}) 87 err = vtHandler.AddVrfTable(vrfTables[0]) 88 Expect(err).To(Not(BeNil())) 89 } 90 91 // Test deleting routes 92 func TestDeleteVrfTable(t *testing.T) { 93 ctx, vtHandler := vrfTableTestSetup(t) 94 defer ctx.TeardownTestCtx() 95 96 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{}) 97 err := vtHandler.DelVrfTable(vrfTables[0]) 98 Expect(err).To(Succeed()) 99 100 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPTableAddDel) 101 Expect(ok).To(BeTrue()) 102 Expect(vppMsg.Table.TableID).To(BeEquivalentTo(1)) 103 Expect(vppMsg.Table.IsIP6).To(BeFalse()) 104 Expect(vppMsg.IsAdd).To(BeFalse()) 105 Expect(vppMsg.Table.Name).To(BeEquivalentTo([]byte("table1"))) 106 107 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{}) 108 err = vtHandler.DelVrfTable(vrfTables[1]) 109 Expect(err).To(Succeed()) 110 111 vppMsg, ok = ctx.MockChannel.Msg.(*vpp_ip.IPTableAddDel) 112 Expect(ok).To(BeTrue()) 113 Expect(vppMsg.Table.TableID).To(BeEquivalentTo(1)) 114 Expect(vppMsg.Table.IsIP6).To(BeTrue()) 115 Expect(vppMsg.IsAdd).To(BeFalse()) 116 Expect(vppMsg.Table.Name).To(BeEquivalentTo([]byte("table1"))) 117 118 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{}) 119 err = vtHandler.DelVrfTable(vrfTables[2]) 120 Expect(err).To(Succeed()) 121 122 vppMsg, ok = ctx.MockChannel.Msg.(*vpp_ip.IPTableAddDel) 123 Expect(ok).To(BeTrue()) 124 Expect(vppMsg.Table.TableID).To(BeEquivalentTo(2)) 125 Expect(vppMsg.Table.IsIP6).To(BeTrue()) 126 Expect(vppMsg.IsAdd).To(BeFalse()) 127 Expect(vppMsg.Table.Name).To(BeEquivalentTo([]byte("table2"))) 128 129 ctx.MockVpp.MockReply(&vpp_ip.IPTableAddDelReply{Retval: 1}) 130 err = vtHandler.DelVrfTable(vrfTables[0]) 131 Expect(err).To(Not(BeNil())) 132 } 133 134 // Test VRF flow hash settings 135 func TestVrfFlowHashSettings(t *testing.T) { 136 ctx, vtHandler := vrfTableTestSetup(t) 137 defer ctx.TeardownTestCtx() 138 139 ctx.MockVpp.MockReply(&vpp_ip.SetIPFlowHashReply{}) 140 err := vtHandler.SetVrfFlowHashSettings(5, true, 141 &l3.VrfTable_FlowHashSettings{ 142 UseSrcIp: true, 143 UseSrcPort: true, 144 Symmetric: true, 145 }) 146 Expect(err).To(Succeed()) 147 148 vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.SetIPFlowHash) 149 Expect(ok).To(BeTrue()) 150 Expect(vppMsg.VrfID).To(BeEquivalentTo(5)) 151 Expect(vppMsg.IsIPv6).To(BeTrue()) 152 Expect(vppMsg.Src).To(BeTrue()) 153 Expect(vppMsg.Dst).To(BeFalse()) 154 Expect(vppMsg.Sport).To(BeTrue()) 155 Expect(vppMsg.Dport).To(BeFalse()) 156 Expect(vppMsg.Proto).To(BeFalse()) 157 Expect(vppMsg.Symmetric).To(BeTrue()) 158 Expect(vppMsg.Reverse).To(BeFalse()) 159 } 160 161 func vrfTableTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.VrfTableVppAPI) { 162 ctx := vppmock.SetupTestCtx(t) 163 log := logrus.NewLogger("test-log") 164 vtHandler := vpp2202.NewVrfTableVppHandler(ctx.MockChannel, log) 165 return ctx, vtHandler 166 }