go.ligato.io/vpp-agent/v3@v3.5.0/tests/integration/vpp/140_teib_test.go (about) 1 // Copyright (c) 2020 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 vpp 16 17 import ( 18 "context" 19 "testing" 20 21 "go.ligato.io/cn-infra/v2/logging/logrus" 22 23 netalloc_mock "go.ligato.io/vpp-agent/v3/plugins/netalloc/mock" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx" 25 ifplugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls" 26 l3plugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls" 27 "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vrfidx" 28 l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3" 29 30 _ "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin" 31 ) 32 33 func TestTeib(t *testing.T) { 34 ctx := setupVPP(t) 35 defer ctx.teardownVPP() 36 37 release := ctx.versionInfo.Release() 38 if release < "20.05" { 39 t.Skipf("TEIB: skipped for VPP < 20.05 (%s)", release) 40 } 41 42 ifIndexes := ifaceidx.NewIfaceIndex(logrus.NewLogger("test-if"), "test-if") 43 vrfIndexes := vrfidx.NewVRFIndex(logrus.NewLogger("test-vrf"), "test-vrf") 44 l3Handler := l3plugin_vppcalls.CompatibleL3VppHandler(ctx.vppClient, ifIndexes, vrfIndexes, 45 netalloc_mock.NewMockNetAlloc(), logrus.NewLogger("test-l3")) 46 47 ifHandler := ifplugin_vppcalls.CompatibleInterfaceVppHandler(ctx.vppClient, logrus.NewLogger("test-if")) 48 49 tests := []struct { 50 name string 51 teib *l3.TeibEntry 52 shouldFail bool 53 }{ 54 { 55 name: "Create TEIB entry (IPv4)", 56 teib: &l3.TeibEntry{ 57 Interface: "if0", 58 PeerAddr: "20.30.40.50", 59 NextHopAddr: "50.40.30.20", 60 }, 61 shouldFail: false, 62 }, 63 { 64 name: "Create TEIB entry (IPv6)", 65 teib: &l3.TeibEntry{ 66 Interface: "if1", 67 PeerAddr: "2001:db8:0:1:1:1:1:1", 68 NextHopAddr: "2002:db8:0:1:1:1:1:1", 69 }, 70 shouldFail: false, 71 }, 72 { 73 name: "Create TEIB entry with no peer IP", 74 teib: &l3.TeibEntry{ 75 Interface: "if2", 76 NextHopAddr: "50.40.30.20", 77 }, 78 shouldFail: true, 79 }, 80 { 81 name: "Create TEIB entry with no next hop IP", 82 teib: &l3.TeibEntry{ 83 Interface: "if3", 84 PeerAddr: "20.30.40.50", 85 }, 86 shouldFail: true, 87 }, 88 } 89 for _, test := range tests { 90 t.Run(test.name, func(t *testing.T) { 91 ifIdx, err := ifHandler.AddLoopbackInterface(test.teib.Interface) 92 if err != nil { 93 t.Fatalf("creating interface failed: %v", err) 94 } 95 ifIndexes.Put(test.teib.Interface, &ifaceidx.IfaceMetadata{SwIfIndex: ifIdx}) 96 97 err = l3Handler.VppAddTeibEntry(context.Background(), test.teib) 98 if err != nil { 99 if test.shouldFail { 100 return 101 } 102 t.Fatalf("create TEIB entry failed: %v\n", err) 103 } else { 104 if test.shouldFail { 105 t.Fatal("create TEIB entry must fail, but it's not") 106 } 107 } 108 109 entries, err := l3Handler.DumpTeib() 110 if err != nil { 111 t.Fatalf("dump TEIB entries failed: %v\n", err) 112 } 113 if len(entries) == 0 { 114 t.Fatalf("no TEIB entries dumped") 115 } 116 117 if entries[0].VrfId != test.teib.VrfId { 118 t.Fatalf("expected VrfId <%v>, got: <%v>", test.teib.VrfId, entries[0].VrfId) 119 } 120 if entries[0].Interface != test.teib.Interface { 121 t.Fatalf("expected Interface <%v>, got: <%v>", test.teib.Interface, entries[0].Interface) 122 } 123 if entries[0].PeerAddr != test.teib.PeerAddr { 124 t.Fatalf("expected PeerAddr <%s>, got: <%s>", test.teib.PeerAddr, entries[0].PeerAddr) 125 } 126 if entries[0].NextHopAddr != test.teib.NextHopAddr { 127 t.Fatalf("expected NextHopAddr <%s>, got: <%s>", test.teib.NextHopAddr, entries[0].NextHopAddr) 128 } 129 130 err = l3Handler.VppDelTeibEntry(context.Background(), test.teib) 131 if err != nil { 132 t.Fatalf("delete TEIB entry failed: %v\n", err) 133 } 134 135 entries, err = l3Handler.DumpTeib() 136 if err != nil { 137 t.Fatalf("dump TEIB entries failed: %v\n", err) 138 } 139 if len(entries) != 0 { 140 t.Fatalf("%d TEIB entries dumped after delete", len(entries)) 141 } 142 143 err = ifHandler.DeleteLoopbackInterface(test.teib.Interface, ifIdx) 144 if err != nil { 145 t.Fatalf("delete interface failed: %v", err) 146 } 147 ifIndexes.Delete(test.teib.Interface) 148 }) 149 } 150 }