go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/l2/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_l2 16 17 import ( 18 "testing" 19 ) 20 21 /*func TestBridgeDomainKey(t *testing.T) { 22 tests := []struct { 23 name string 24 bdName string 25 expectedKey string 26 }{ 27 { 28 name: "valid BD name", 29 bdName: "bd1", 30 expectedKey: "vpp/config/v2/bd/bd1", 31 }, 32 { 33 name: "invalid BD name", 34 bdName: "", 35 expectedKey: "vpp/config/v2/bd/<invalid>", 36 }, 37 } 38 for _, test := range tests { 39 t.Run(test.name, func(t *testing.T) { 40 key := BridgeDomainKey(test.bdName) 41 if key != test.expectedKey { 42 t.Errorf("failed for: bdName=%s\n"+ 43 "expected key:\n\t%q\ngot key:\n\t%q", 44 test.bdName, test.expectedKey, key) 45 } 46 }) 47 } 48 } 49 50 func TestParseBDNameFromKey(t *testing.T) { 51 tests := []struct { 52 name string 53 key string 54 expectedBDName string 55 expectedIsBDKey bool 56 }{ 57 { 58 name: "valid BD name", 59 key: "vpp/config/v2/bd/bd1", 60 expectedBDName: "bd1", 61 expectedIsBDKey: true, 62 }, 63 { 64 name: "invalid BD name", 65 key: "vpp/config/v2/bd/<invalid>", 66 expectedBDName: "<invalid>", 67 expectedIsBDKey: true, 68 }, 69 { 70 name: "not BD key", 71 key: "vpp/config/v2/bd/bd1/fib/aa:aa:aa:aa:aa:aa", 72 expectedBDName: "", 73 expectedIsBDKey: false, 74 }, 75 } 76 for _, test := range tests { 77 t.Run(test.name, func(t *testing.T) { 78 bdName, isBDKey := models.Model(&BridgeDomain{}).ParseKey(test.key) 79 if isBDKey != test.expectedIsBDKey { 80 t.Errorf("expected isBDKey: %v\tgot: %v", test.expectedIsBDKey, isBDKey) 81 } 82 if bdName != test.expectedBDName { 83 t.Errorf("expected bdName: %s\tgot: %s", test.expectedBDName, bdName) 84 } 85 }) 86 } 87 }*/ 88 89 func TestBDInterfaceKey(t *testing.T) { 90 tests := []struct { 91 name string 92 bdName string 93 iface string 94 expectedKey string 95 }{ 96 { 97 name: "valid BD & iface names", 98 bdName: "bd1", 99 iface: "tap0", 100 expectedKey: "vpp/bd/bd1/interface/tap0", 101 }, 102 { 103 name: "invalid BD but valid interface", 104 bdName: "", 105 iface: "tap1", 106 expectedKey: "vpp/bd/<invalid>/interface/tap1", 107 }, 108 { 109 name: "invalid BD but valid interface", 110 bdName: "", 111 iface: "tap1", 112 expectedKey: "vpp/bd/<invalid>/interface/tap1", 113 }, 114 { 115 name: "valid BD but invalid interface", 116 bdName: "bd2", 117 iface: "", 118 expectedKey: "vpp/bd/bd2/interface/<invalid>", 119 }, 120 { 121 name: "invalid parameters", 122 bdName: "", 123 iface: "", 124 expectedKey: "vpp/bd/<invalid>/interface/<invalid>", 125 }, 126 { 127 name: "Gbe interface", 128 bdName: "bd5", 129 iface: "GigabitEthernet0/8/0", 130 expectedKey: "vpp/bd/bd5/interface/GigabitEthernet0/8/0", 131 }, 132 } 133 for _, test := range tests { 134 t.Run(test.name, func(t *testing.T) { 135 key := BDInterfaceKey(test.bdName, test.iface) 136 if key != test.expectedKey { 137 t.Errorf("failed for: bdName=%s iface=%s\n"+ 138 "expected key:\n\t%q\ngot key:\n\t%q", 139 test.bdName, test.iface, test.expectedKey, key) 140 } 141 }) 142 } 143 } 144 145 func TestParseBDInterfaceKey(t *testing.T) { 146 tests := []struct { 147 name string 148 key string 149 expectedBDName string 150 expectedIface string 151 expectedIsBDIfaceKey bool 152 }{ 153 { 154 name: "valid BD & iface names", 155 key: "vpp/bd/bd1/interface/tap0", 156 expectedBDName: "bd1", 157 expectedIface: "tap0", 158 expectedIsBDIfaceKey: true, 159 }, 160 { 161 name: "invalid BD but valid interface", 162 key: "vpp/bd/<invalid>/interface/tap1", 163 expectedBDName: "<invalid>", 164 expectedIface: "tap1", 165 expectedIsBDIfaceKey: true, 166 }, 167 { 168 name: "valid BD but invalid interface", 169 key: "vpp/bd/bd2/interface/<invalid>", 170 expectedBDName: "bd2", 171 expectedIface: "<invalid>", 172 expectedIsBDIfaceKey: true, 173 }, 174 { 175 name: "Gbe interface", 176 key: "vpp/bd/bd4/interface/GigabitEthernet0/8/0", 177 expectedBDName: "bd4", 178 expectedIface: "GigabitEthernet0/8/0", 179 expectedIsBDIfaceKey: true, 180 }, 181 { 182 name: "not BD-interface key", 183 key: "vpp/config/v2/bd/bd1", 184 expectedBDName: "", 185 expectedIface: "", 186 expectedIsBDIfaceKey: false, 187 }, 188 } 189 for _, test := range tests { 190 t.Run(test.name, func(t *testing.T) { 191 bdName, iface, isBDIfaceKey := ParseBDInterfaceKey(test.key) 192 if isBDIfaceKey != test.expectedIsBDIfaceKey { 193 t.Errorf("expected isBDIfaceKey: %v\tgot: %v", test.expectedIsBDIfaceKey, isBDIfaceKey) 194 } 195 if bdName != test.expectedBDName { 196 t.Errorf("expected bdName: %s\tgot: %s", test.expectedBDName, bdName) 197 } 198 if iface != test.expectedIface { 199 t.Errorf("expected iface: %s\tgot: %s", test.expectedIface, iface) 200 } 201 }) 202 } 203 } 204 205 /*func TestFIBKey(t *testing.T) { 206 tests := []struct { 207 name string 208 bdName string 209 fibMac string 210 expectedKey string 211 }{ 212 { 213 name: "valid parameters", 214 bdName: "bd1", 215 fibMac: "12:34:56:78:9a:bc", 216 expectedKey: "vpp/config/v2/bd/bd1/fib/12:34:56:78:9a:bc", 217 }, 218 { 219 name: "invalid bd", 220 bdName: "", 221 fibMac: "aa:aa:aa:bb:bb:bb", 222 expectedKey: "vpp/config/v2/bd/<invalid>/fib/aa:aa:aa:bb:bb:bb", 223 }, 224 { 225 name: "invalid hw address", 226 bdName: "bd2", 227 fibMac: "in:va:li:d", 228 expectedKey: "vpp/config/v2/bd/bd2/fib/<invalid>", 229 }, 230 { 231 name: "invalid parameters", 232 bdName: "", 233 fibMac: "192.168.1.1", 234 expectedKey: "vpp/config/v2/bd/<invalid>/fib/<invalid>", 235 }, 236 } 237 for _, test := range tests { 238 t.Run(test.name, func(t *testing.T) { 239 key := FIBKey(test.bdName, test.fibMac) 240 if key != test.expectedKey { 241 t.Errorf("failed for: bdName=%s fibMac=%s\n"+ 242 "expected key:\n\t%q\ngot key:\n\t%q", 243 test.bdName, test.fibMac, test.expectedKey, key) 244 } 245 }) 246 } 247 } 248 249 func TestParseFIBKey(t *testing.T) { 250 tests := []struct { 251 name string 252 key string 253 expectedBDName string 254 expectedfibMac string 255 expectedIsFIBKey bool 256 }{ 257 { 258 name: "valid FIB key", 259 key: "vpp/config/v2/bd/bd1/fib/12:34:56:78:9a:bc", 260 expectedBDName: "bd1", 261 expectedfibMac: "12:34:56:78:9a:bc", 262 expectedIsFIBKey: true, 263 }, 264 { 265 name: "invalid bd", 266 key: "vpp/config/v2/bd/<invalid>/fib/aa:bb:cc:dd:ee:ff", 267 expectedBDName: "<invalid>", 268 expectedfibMac: "aa:bb:cc:dd:ee:ff", 269 expectedIsFIBKey: true, 270 }, 271 { 272 name: "invalid fib", 273 key: "vpp/config/v2/bd/bd2/fib/<invalid>", 274 expectedBDName: "bd2", 275 expectedfibMac: "<invalid>", 276 expectedIsFIBKey: true, 277 }, 278 { 279 name: "invalid params", 280 key: "vpp/config/v2/bd/<invalid>/fib/<invalid>", 281 expectedBDName: "<invalid>", 282 expectedfibMac: "<invalid>", 283 expectedIsFIBKey: true, 284 }, 285 { 286 name: "not FIB key", 287 key: "vpp/bd/bd1/interface/tap0", 288 expectedBDName: "", 289 expectedfibMac: "", 290 expectedIsFIBKey: false, 291 }, 292 { 293 name: "not FIB key", 294 key: "vpp/config/v2/bd/bd1", 295 expectedBDName: "", 296 expectedfibMac: "", 297 expectedIsFIBKey: false, 298 }, 299 } 300 for _, test := range tests { 301 t.Run(test.name, func(t *testing.T) { 302 name, isFIBKey := models.Model(&FIBEntry{}).ParseKey(test.key) 303 nameParts := strings.Split(name, "/") 304 if len(nameParts) != 3 { 305 t.Fatalf("invalid name: %q", name) 306 } 307 bdName, fibMac := nameParts[0], nameParts[2] 308 if isFIBKey != test.expectedIsFIBKey { 309 t.Errorf("expected isFIBKey: %v\tgot: %v", test.expectedIsFIBKey, isFIBKey) 310 } 311 if bdName != test.expectedBDName { 312 t.Errorf("expected bdName: %s\tgot: %s", test.expectedBDName, bdName) 313 } 314 if fibMac != test.expectedfibMac { 315 t.Errorf("expected iface: %s\tgot: %s", test.expectedfibMac, fibMac) 316 } 317 }) 318 } 319 }*/ 320 321 /*func TestXConnectKey(t *testing.T) { 322 tests := []struct { 323 name string 324 rxIface string 325 expectedKey string 326 }{ 327 { 328 name: "valid interface", 329 rxIface: "memif0", 330 expectedKey: "vpp/config/v2/xconnect/memif0", 331 }, 332 { 333 name: "invalid interface", 334 rxIface: "", 335 expectedKey: "vpp/config/v2/xconnect/<invalid>", 336 }, 337 { 338 name: "gbe", 339 rxIface: "GigabitEthernet0/8/0", 340 expectedKey: "vpp/config/v2/xconnect/GigabitEthernet0/8/0", 341 }, 342 } 343 for _, test := range tests { 344 t.Run(test.name, func(t *testing.T) { 345 key := XConnectKey(test.rxIface) 346 if key != test.expectedKey { 347 t.Errorf("failed for: rxIface=%s\n"+ 348 "expected key:\n\t%q\ngot key:\n\t%q", 349 test.rxIface, test.expectedKey, key) 350 } 351 }) 352 } 353 }*/