go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/l3/models_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 vpp_l3 16 17 import ( 18 "testing" 19 20 . "github.com/onsi/gomega" 21 22 "go.ligato.io/vpp-agent/v3/pkg/models" 23 ) 24 25 func TestRouteKey(t *testing.T) { 26 tests := []*struct { 27 name string 28 route Route 29 expectedKey string 30 }{ 31 { 32 "route-ipv4", 33 Route{ 34 VrfId: 0, 35 DstNetwork: "10.10.0.0/24", 36 NextHopAddr: "0.0.0.0", 37 OutgoingInterface: "", 38 }, 39 "config/vpp/v2/route/vrf/0/dst/10.10.0.0/24/gw/0.0.0.0", 40 }, 41 { 42 "route-ipv6", 43 Route{ 44 VrfId: 0, 45 DstNetwork: "2001:DB8::0001/32", 46 NextHopAddr: "0.0.0.0", 47 OutgoingInterface: "", 48 }, 49 "config/vpp/v2/route/vrf/0/dst/2001:db8::/32/gw/0.0.0.0", 50 }, 51 { 52 "route-ipv4-interface", 53 Route{ 54 VrfId: 0, 55 DstNetwork: "10.10.0.0/24", 56 NextHopAddr: "0.0.0.0", 57 OutgoingInterface: "iface1", 58 }, 59 "config/vpp/v2/route/if/iface1/vrf/0/dst/10.10.0.0/24/gw/0.0.0.0", 60 }, 61 { 62 "route-ipv6-interface", 63 Route{ 64 VrfId: 0, 65 DstNetwork: "2001:DB8::0001/32", 66 NextHopAddr: "0.0.0.0", 67 OutgoingInterface: "iface1", 68 }, 69 "config/vpp/v2/route/if/iface1/vrf/0/dst/2001:db8::/32/gw/0.0.0.0", 70 }, 71 { 72 "route-invalid-ip", 73 Route{ 74 VrfId: 0, 75 DstNetwork: "INVALID", 76 NextHopAddr: "0.0.0.0", 77 OutgoingInterface: "", 78 }, 79 "config/vpp/v2/route/vrf/0/dst/<invalid>/0/gw/0.0.0.0", 80 }, 81 { 82 "route-invalid-gw", 83 Route{ 84 VrfId: 0, 85 DstNetwork: "10.10.10.0/32", 86 NextHopAddr: "INVALID", 87 OutgoingInterface: "", 88 }, 89 "config/vpp/v2/route/vrf/0/dst/10.10.10.0/32/gw/INVALID", 90 }, 91 { 92 "route-dstnetwork", 93 Route{ 94 VrfId: 0, 95 DstNetwork: "10.10.0.5/24", 96 NextHopAddr: "0.0.0.0", 97 OutgoingInterface: "", 98 }, 99 "config/vpp/v2/route/vrf/0/dst/10.10.0.0/24/gw/0.0.0.0", 100 }, 101 { 102 "route-gw-empty", 103 Route{ 104 VrfId: 0, 105 DstNetwork: "10.0.0.0/8", 106 NextHopAddr: "", 107 OutgoingInterface: "", 108 }, 109 "config/vpp/v2/route/vrf/0/dst/10.0.0.0/8", 110 }, 111 { 112 "route-vrf", 113 Route{ 114 VrfId: 3, 115 DstNetwork: "10.0.0.0/8", 116 NextHopAddr: "", 117 OutgoingInterface: "", 118 }, 119 "config/vpp/v2/route/vrf/3/dst/10.0.0.0/8", 120 }, 121 } 122 for _, test := range tests { 123 t.Run(test.name, func(t *testing.T) { 124 key := models.Key(&test.route) 125 if key != test.expectedKey { 126 t.Errorf("failed key for route: %+v\n"+ 127 "expected key:\n\t%q\ngot key:\n\t%q", 128 &test.route, test.expectedKey, key) 129 } 130 }) 131 } 132 } 133 134 // TestParseRouteKey test different cases for ParseRouteKey(...) 135 func TestParseRouteKey(t *testing.T) { 136 tests := []struct { 137 name string 138 routeKey string 139 expectedIsRouteKey bool 140 expectedOutIface string 141 expectedVrfIndex string 142 expectedDstNet string 143 expectedNextHopAddr string 144 }{ 145 { 146 name: "route-ipv4", 147 routeKey: "config/vpp/v2/route/vrf/0/dst/10.10.0.0/16/gw/0.0.0.0", 148 expectedIsRouteKey: true, 149 expectedVrfIndex: "0", 150 expectedDstNet: "10.10.0.0/16", 151 expectedNextHopAddr: "0.0.0.0", 152 }, 153 { 154 name: "route-ipv4 with interface", 155 routeKey: "config/vpp/v2/route/if/Gbe0/8/0/vrf/0/dst/10.10.0.0/16/gw/0.0.0.0", 156 expectedIsRouteKey: true, 157 expectedOutIface: "Gbe0/8/0", 158 expectedVrfIndex: "0", 159 expectedDstNet: "10.10.0.0/16", 160 expectedNextHopAddr: "0.0.0.0", 161 }, 162 { 163 name: "route-ipv6", 164 routeKey: "config/vpp/v2/route/vrf/0/dst/2001:db8::/32/gw/::", 165 expectedIsRouteKey: true, 166 expectedVrfIndex: "0", 167 expectedDstNet: "2001:db8::/32", 168 expectedNextHopAddr: "::", 169 }, 170 { 171 name: "undefined interface and GW", 172 routeKey: "config/vpp/v2/route/vrf/0/dst/2001:db8::/32/", 173 expectedIsRouteKey: true, 174 expectedVrfIndex: "0", 175 expectedDstNet: "2001:db8::/32", 176 }, 177 { 178 name: "invalid-key-missing-dst", 179 routeKey: "config/vpp/v2/route/vrf/0/10.10.0.0/16/gw/0.0.0.0", 180 expectedIsRouteKey: false, 181 }, 182 } 183 for _, test := range tests { 184 t.Run(test.name, func(t *testing.T) { 185 RegisterTestingT(t) 186 outIface, vrfIndex, dstNet, nextHopAddr, isRouteKey := ParseRouteKey(test.routeKey) 187 Expect(isRouteKey).To(BeEquivalentTo(test.expectedIsRouteKey), "Route/Non-route key should be properly detected") 188 if isRouteKey { 189 Expect(outIface).To(BeEquivalentTo(test.expectedOutIface), "outgoing interface should be properly extracted by parsing route key") 190 Expect(vrfIndex).To(BeEquivalentTo(test.expectedVrfIndex), "VRF should be properly extracted by parsing route key") 191 Expect(dstNet).To(BeEquivalentTo(test.expectedDstNet), "Destination network should be properly extracted by parsing route key") 192 Expect(nextHopAddr).To(BeEquivalentTo(test.expectedNextHopAddr), "Next hop address should be properly extracted by parsing route key") 193 } 194 }) 195 } 196 }