vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 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 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "encoding/json" |
| 21 | "net" |
| 22 | "reflect" |
| 23 | "sync" |
| 24 | "testing" |
| 25 | "voltha-go-controller/internal/pkg/of" |
| 26 | "voltha-go-controller/internal/test/mocks" |
| 27 | |
| 28 | "github.com/golang/mock/gomock" |
| 29 | "github.com/google/gopacket" |
| 30 | "github.com/google/gopacket/layers" |
| 31 | "github.com/stretchr/testify/assert" |
| 32 | ) |
| 33 | |
| 34 | func TestVoltApplication_GetIgnoredPorts(t *testing.T) { |
| 35 | voltDevice := &VoltDevice{ |
| 36 | Name: "11c3175b-50f3-4220-9555-93df733ded1d", |
| 37 | SerialNum: "SDX6320031", |
| 38 | SouthBoundID: "68580342-6b3e-57cb-9ea4-06125594e330", |
| 39 | NniPort: "16777472", |
| 40 | Ports: sync.Map{}, |
| 41 | PonPortList: sync.Map{}, |
| 42 | } |
| 43 | voltPort := &VoltPort{ |
| 44 | Name: "16777472", |
| 45 | Device: "SDX6320031", |
| 46 | ID: 16777472, |
| 47 | State: PortStateDown, |
| 48 | ChannelPerSubAlarmRaised: false, |
| 49 | Type: VoltPortTypeNni, |
| 50 | } |
| 51 | voltPortVnets := make([]*VoltPortVnet, 0) |
| 52 | voltPortVnet := &VoltPortVnet{ |
| 53 | Device: "SDX6320031", |
| 54 | Port: "16777472", |
| 55 | MacLearning: MacLearningNone, |
| 56 | } |
| 57 | voltPortVnets = append(voltPortVnets, voltPortVnet) |
| 58 | IgnoredPorts := make(map[string][]string) |
| 59 | IgnoredPorts["SDX6320031"] = append(IgnoredPorts["SDX6320031"], "16777472") |
| 60 | tests := []struct { |
| 61 | name string |
| 62 | want map[string][]string |
| 63 | wantErr bool |
| 64 | }{ |
| 65 | { |
| 66 | name: "Positive_Case_GetIgnoredPorts", |
| 67 | want: IgnoredPorts, |
| 68 | wantErr: false, |
| 69 | }, |
| 70 | } |
| 71 | for _, tt := range tests { |
| 72 | t.Run(tt.name, func(t *testing.T) { |
| 73 | va := &VoltApplication{ |
| 74 | DevicesDisc: sync.Map{}, |
| 75 | } |
| 76 | va.DevicesDisc.Store("SDX6320031", voltDevice) |
| 77 | voltDevice.Ports.Store("16777472", voltPort) |
| 78 | voltApp := GetApplication() |
| 79 | voltApp.VnetsByPort.Store("16777472", voltPortVnets) |
| 80 | got, err := va.GetIgnoredPorts() |
| 81 | if (err != nil) != tt.wantErr { |
| 82 | t.Errorf("VoltApplication.GetIgnoredPorts() error = %v, wantErr %v", err, tt.wantErr) |
| 83 | return |
| 84 | } |
| 85 | if !reflect.DeepEqual(got, tt.want) { |
| 86 | t.Errorf("VoltApplication.GetIgnoredPorts() = %v, want %v", got, tt.want) |
| 87 | } |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestDhcpNetworks_AddDhcpSession(t *testing.T) { |
| 93 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 94 | type args struct { |
| 95 | pkt gopacket.Packet |
| 96 | session IDhcpRelaySession |
| 97 | } |
| 98 | tests := []struct { |
| 99 | name string |
| 100 | args args |
| 101 | wantErr bool |
| 102 | }{ |
| 103 | { |
| 104 | name: "DhcpNetworks_AddDhcpSession", |
| 105 | args: args{ |
| 106 | pkt: pkt, |
| 107 | session: &VoltPortVnet{}, |
| 108 | }, |
| 109 | }, |
| 110 | } |
| 111 | for _, tt := range tests { |
| 112 | t.Run(tt.name, func(t *testing.T) { |
| 113 | network := make(map[uint32]*DhcpRelayVnet) |
| 114 | dn := &DhcpNetworks{ |
| 115 | Networks: network, |
| 116 | } |
| 117 | pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1) |
| 118 | if err := dn.AddDhcpSession(tt.args.pkt, tt.args.session); (err != nil) != tt.wantErr { |
| 119 | t.Errorf("DhcpNetworks.AddDhcpSession() error = %v, wantErr %v", err, tt.wantErr) |
| 120 | } |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestDhcpNetworks_DelDhcpSession(t *testing.T) { |
| 126 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 127 | type args struct { |
| 128 | pkt gopacket.Packet |
| 129 | session IDhcpRelaySession |
| 130 | } |
| 131 | tests := []struct { |
| 132 | name string |
| 133 | args args |
| 134 | }{ |
| 135 | { |
| 136 | name: "DhcpNetworks_DelDhcpSession", |
| 137 | args: args{ |
| 138 | pkt: pkt, |
| 139 | session: &VoltPortVnet{}, |
| 140 | }, |
| 141 | }, |
| 142 | } |
| 143 | for _, tt := range tests { |
| 144 | t.Run(tt.name, func(t *testing.T) { |
| 145 | network := make(map[uint32]*DhcpRelayVnet) |
| 146 | dn := &DhcpNetworks{ |
| 147 | Networks: network, |
| 148 | } |
| 149 | pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1) |
| 150 | dn.DelDhcpSession(tt.args.pkt, tt.args.session) |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func TestDhcpNetworks_AddDhcp6Session(t *testing.T) { |
| 156 | type args struct { |
| 157 | key [MaxLenDhcpv6DUID]byte |
| 158 | session IDhcpRelaySession |
| 159 | } |
| 160 | tests := []struct { |
| 161 | name string |
| 162 | args args |
| 163 | wantErr bool |
| 164 | }{ |
| 165 | { |
| 166 | name: "DhcpNetworks_AddDhcp6Session", |
| 167 | args: args{ |
| 168 | session: &VoltPortVnet{}, |
| 169 | }, |
| 170 | }, |
| 171 | } |
| 172 | for _, tt := range tests { |
| 173 | t.Run(tt.name, func(t *testing.T) { |
| 174 | network := make(map[uint32]*DhcpRelayVnet) |
| 175 | dn := &DhcpNetworks{ |
| 176 | Networks: network, |
| 177 | } |
| 178 | if err := dn.AddDhcp6Session(tt.args.key, tt.args.session); (err != nil) != tt.wantErr { |
| 179 | t.Errorf("DhcpNetworks.AddDhcp6Session() error = %v, wantErr %v", err, tt.wantErr) |
| 180 | } |
| 181 | }) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestDhcpNetworks_DelDhcp6Session(t *testing.T) { |
| 186 | type args struct { |
| 187 | key [MaxLenDhcpv6DUID]byte |
| 188 | session IDhcpRelaySession |
| 189 | } |
| 190 | tests := []struct { |
| 191 | name string |
| 192 | args args |
| 193 | }{ |
| 194 | { |
| 195 | name: "DhcpNetworks_DelDhcp6Session", |
| 196 | args: args{ |
| 197 | session: &VoltPortVnet{}, |
| 198 | }, |
| 199 | }, |
| 200 | } |
| 201 | for _, tt := range tests { |
| 202 | t.Run(tt.name, func(t *testing.T) { |
| 203 | network := make(map[uint32]*DhcpRelayVnet) |
| 204 | network[uint32(4097)] = &DhcpRelayVnet{ |
| 205 | InnerVlan: uint16(4097), |
| 206 | } |
| 207 | dn := &DhcpNetworks{ |
| 208 | Networks: network, |
| 209 | } |
| 210 | dn.DelDhcp6Session(tt.args.key, tt.args.session) |
| 211 | }) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func TestDhcpNetworks_GetDhcpSession(t *testing.T) { |
| 216 | type fields struct { |
| 217 | Networks map[uint32]*DhcpRelayVnet |
| 218 | } |
| 219 | type args struct { |
| 220 | outerVlan uint16 |
| 221 | innerVlan uint16 |
| 222 | addr net.HardwareAddr |
| 223 | } |
| 224 | macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff") |
| 225 | tests := []struct { |
| 226 | name string |
| 227 | fields fields |
| 228 | args args |
| 229 | want IDhcpRelaySession |
| 230 | }{ |
| 231 | { |
| 232 | name: "DhcpNetworks_GetDhcpSession", |
| 233 | args: args{ |
| 234 | outerVlan: uint16(0), |
| 235 | innerVlan: uint16(4097), |
| 236 | addr: macAdd, |
| 237 | }, |
| 238 | }, |
| 239 | } |
| 240 | for _, tt := range tests { |
| 241 | t.Run(tt.name, func(t *testing.T) { |
| 242 | network := make(map[uint32]*DhcpRelayVnet) |
| 243 | network[uint32(4097)] = &DhcpRelayVnet{ |
| 244 | InnerVlan: uint16(4097), |
| 245 | } |
| 246 | dn := &DhcpNetworks{ |
| 247 | Networks: network, |
| 248 | } |
| 249 | got, err := dn.GetDhcpSession(tt.args.outerVlan, tt.args.innerVlan, tt.args.addr) |
| 250 | assert.NotNil(t, err) |
| 251 | assert.Nil(t, got) |
| 252 | }) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | func TestDhcpNetworks_GetDhcp6Session(t *testing.T) { |
| 257 | type fields struct { |
| 258 | Networks map[uint32]*DhcpRelayVnet |
| 259 | } |
| 260 | type args struct { |
| 261 | outerVlan uint16 |
| 262 | innerVlan uint16 |
| 263 | key [MaxLenDhcpv6DUID]byte |
| 264 | } |
| 265 | tests := []struct { |
| 266 | name string |
| 267 | fields fields |
| 268 | args args |
| 269 | want IDhcpRelaySession |
| 270 | wantErr bool |
| 271 | }{ |
| 272 | { |
| 273 | name: "DhcpNetworks_GetDhcp6Session", |
| 274 | args: args{ |
| 275 | outerVlan: uint16(0), |
| 276 | innerVlan: uint16(4097), |
| 277 | }, |
| 278 | }, |
| 279 | } |
| 280 | for _, tt := range tests { |
| 281 | t.Run(tt.name, func(t *testing.T) { |
| 282 | network := make(map[uint32]*DhcpRelayVnet) |
| 283 | network[uint32(4097)] = &DhcpRelayVnet{ |
| 284 | InnerVlan: uint16(4097), |
| 285 | } |
| 286 | dn := &DhcpNetworks{ |
| 287 | Networks: network, |
| 288 | } |
| 289 | got, err := dn.GetDhcp6Session(tt.args.outerVlan, tt.args.innerVlan, tt.args.key) |
| 290 | assert.NotNil(t, err) |
| 291 | assert.Nil(t, got) |
| 292 | }) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | func TestGetVnetForV4Nni(t *testing.T) { |
| 297 | type args struct { |
| 298 | dhcp *layers.DHCPv4 |
| 299 | cvlan of.VlanType |
| 300 | svlan of.VlanType |
| 301 | pbit uint8 |
| 302 | } |
| 303 | macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff") |
| 304 | tests := []struct { |
| 305 | name string |
| 306 | args args |
| 307 | want []*VoltPortVnet |
| 308 | wantErr bool |
| 309 | }{ |
| 310 | { |
| 311 | name: "GetVnetForV4Nni", |
| 312 | args: args{ |
| 313 | cvlan: of.VlanAny, |
| 314 | svlan: of.VlanAny, |
| 315 | dhcp: &layers.DHCPv4{ |
| 316 | BaseLayer: dot1Q.BaseLayer, |
| 317 | ClientHWAddr: macAdd, |
| 318 | }, |
| 319 | }, |
| 320 | }, |
| 321 | } |
| 322 | for _, tt := range tests { |
| 323 | t.Run(tt.name, func(t *testing.T) { |
| 324 | got, err := GetVnetForV4Nni(tt.args.dhcp, tt.args.cvlan, tt.args.svlan, tt.args.pbit) |
| 325 | assert.NotNil(t, err) |
| 326 | assert.Nil(t, got) |
| 327 | }) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | func TestGetVnetForV6Nni(t *testing.T) { |
| 332 | type args struct { |
| 333 | dhcp *layers.DHCPv6 |
| 334 | cvlan of.VlanType |
| 335 | svlan of.VlanType |
| 336 | pbit uint8 |
| 337 | clientMAC net.HardwareAddr |
| 338 | } |
| 339 | macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff") |
| 340 | tests := []struct { |
| 341 | name string |
| 342 | args args |
| 343 | want []*VoltPortVnet |
| 344 | want1 net.HardwareAddr |
| 345 | wantErr bool |
| 346 | }{ |
| 347 | { |
| 348 | name: "GetVnetForV6Nni", |
| 349 | args: args{ |
| 350 | dhcp: &layers.DHCPv6{ |
| 351 | BaseLayer: dot1Q.BaseLayer, |
| 352 | Options: layers.DHCPv6Options{ |
| 353 | { |
| 354 | Code: layers.DHCPv6OptClientID, |
| 355 | Data: []byte{2, 3, 4, 2, 3, 4, 2, 3, 4}, |
| 356 | }, |
| 357 | }, |
| 358 | }, |
| 359 | cvlan: of.VlanAny, |
| 360 | svlan: of.VlanAny, |
| 361 | clientMAC: macAdd, |
| 362 | }, |
| 363 | }, |
| 364 | } |
| 365 | for _, tt := range tests { |
| 366 | t.Run(tt.name, func(t *testing.T) { |
| 367 | got, got1, err := GetVnetForV6Nni(tt.args.dhcp, tt.args.cvlan, tt.args.svlan, tt.args.pbit, tt.args.clientMAC) |
| 368 | assert.NotNil(t, err) |
| 369 | assert.Nil(t, got) |
| 370 | assert.NotNil(t, got1) |
| 371 | }) |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | func TestAddDhcpv4Option82(t *testing.T) { |
| 376 | type args struct { |
| 377 | svc *VoltService |
| 378 | rID []byte |
| 379 | dhcpv4 *layers.DHCPv4 |
| 380 | } |
| 381 | tests := []struct { |
| 382 | name string |
| 383 | args args |
| 384 | }{ |
| 385 | { |
| 386 | name: "AddDhcpv4Option82", |
| 387 | args: args{ |
| 388 | svc: &VoltService{ |
| 389 | VoltServiceCfg: VoltServiceCfg{ |
| 390 | CircuitID: "test_circuit_id", |
| 391 | DataRateAttr: DSLAttrEnabled, |
| 392 | }, |
| 393 | }, |
| 394 | rID: []byte{1}, |
| 395 | dhcpv4: &layers.DHCPv4{ |
| 396 | Options: layers.DHCPOptions{ |
| 397 | { |
| 398 | Type: layers.DHCPOptARPTimeout, |
| 399 | }, |
| 400 | }, |
| 401 | }, |
| 402 | }, |
| 403 | }, |
| 404 | } |
| 405 | for _, tt := range tests { |
| 406 | t.Run(tt.name, func(t *testing.T) { |
| 407 | AddDhcpv4Option82(tt.args.svc, tt.args.rID, tt.args.dhcpv4) |
| 408 | }) |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | func TestVoltApplication_ProcessDsDhcpv4Packet(t *testing.T) { |
| 413 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 414 | type args struct { |
| 415 | cntx context.Context |
| 416 | device string |
| 417 | port string |
| 418 | pkt gopacket.Packet |
| 419 | } |
| 420 | tests := []struct { |
| 421 | name string |
| 422 | args args |
| 423 | }{ |
| 424 | { |
| 425 | name: "VoltApplication_ProcessDsDhcpv4Packet", |
| 426 | args: args{ |
| 427 | cntx: context.Background(), |
| 428 | device: test_device, |
| 429 | port: "test_port", |
| 430 | pkt: pkt, |
| 431 | }, |
| 432 | }, |
| 433 | } |
| 434 | for _, tt := range tests { |
| 435 | t.Run(tt.name, func(t *testing.T) { |
| 436 | va := &VoltApplication{} |
| 437 | iPv4 := &layers.IPv4{ |
| 438 | Version: uint8(1), |
| 439 | } |
| 440 | uDP := &layers.UDP{ |
| 441 | Length: uint16(1), |
| 442 | } |
| 443 | dHCPv4 := &layers.DHCPv4{ |
| 444 | HardwareLen: uint8(1), |
| 445 | } |
| 446 | dot1Q_test := &layers.Dot1Q{ |
| 447 | Priority: uint8(1), |
| 448 | } |
| 449 | pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1) |
| 450 | pkt.EXPECT().Layer(layers.LayerTypeIPv4).Return(iPv4).Times(1) |
| 451 | pkt.EXPECT().Layer(layers.LayerTypeUDP).Return(uDP).Times(1) |
| 452 | pkt.EXPECT().Layer(layers.LayerTypeDHCPv4).Return(dHCPv4).Times(1) |
| 453 | pkt.EXPECT().Layer(layers.LayerTypeDot1Q).Return(dot1Q_test).Times(1) |
| 454 | pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(1) |
| 455 | va.ProcessDsDhcpv4Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt) |
| 456 | }) |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | func TestDelOption82(t *testing.T) { |
| 461 | type args struct { |
| 462 | dhcpv4 *layers.DHCPv4 |
| 463 | } |
| 464 | tests := []struct { |
| 465 | name string |
| 466 | args args |
| 467 | }{ |
| 468 | { |
| 469 | name: "DelOption82", |
| 470 | args: args{ |
| 471 | dhcpv4: &layers.DHCPv4{ |
| 472 | Options: layers.DHCPOptions{ |
| 473 | { |
| 474 | Type: opt82, |
| 475 | }, |
| 476 | }, |
| 477 | }, |
| 478 | }, |
| 479 | }, |
| 480 | } |
| 481 | for _, tt := range tests { |
| 482 | t.Run(tt.name, func(t *testing.T) { |
| 483 | DelOption82(tt.args.dhcpv4) |
| 484 | }) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | func TestDhcpMsgType(t *testing.T) { |
| 489 | type args struct { |
| 490 | dhcp *layers.DHCPv4 |
| 491 | } |
| 492 | tests := []struct { |
| 493 | name string |
| 494 | args args |
| 495 | want layers.DHCPMsgType |
| 496 | }{ |
| 497 | { |
| 498 | name: "DhcpMsgType", |
| 499 | args: args{ |
| 500 | dhcp: &layers.DHCPv4{ |
| 501 | Options: layers.DHCPOptions{ |
| 502 | { |
| 503 | Type: layers.DHCPOptMessageType, |
| 504 | Data: []byte{1}, |
| 505 | }, |
| 506 | }, |
| 507 | }, |
| 508 | }, |
| 509 | want: layers.DHCPMsgTypeDiscover, |
| 510 | }, |
| 511 | } |
| 512 | for _, tt := range tests { |
| 513 | t.Run(tt.name, func(t *testing.T) { |
| 514 | if got := DhcpMsgType(tt.args.dhcp); !reflect.DeepEqual(got, tt.want) { |
| 515 | t.Errorf("DhcpMsgType() = %v, want %v", got, tt.want) |
| 516 | } |
| 517 | }) |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | func TestGetIpv4Addr(t *testing.T) { |
| 522 | type args struct { |
| 523 | dhcp *layers.DHCPv4 |
| 524 | } |
| 525 | tests := []struct { |
| 526 | name string |
| 527 | args args |
| 528 | want net.IP |
| 529 | want1 int64 |
| 530 | }{ |
| 531 | { |
| 532 | name: "GetIpv4Addr", |
| 533 | args: args{ |
| 534 | dhcp: &layers.DHCPv4{ |
| 535 | Options: layers.DHCPOptions{ |
| 536 | { |
| 537 | Type: layers.DHCPOptLeaseTime, |
| 538 | Data: []byte{1, 2, 3, 4, 5}, |
| 539 | }, |
| 540 | }, |
| 541 | }, |
| 542 | }, |
| 543 | want1: int64(16909060), |
| 544 | }, |
| 545 | } |
| 546 | for _, tt := range tests { |
| 547 | t.Run(tt.name, func(t *testing.T) { |
| 548 | got, got1 := GetIpv4Addr(tt.args.dhcp) |
| 549 | if !reflect.DeepEqual(got, tt.want) { |
| 550 | t.Errorf("GetIpv4Addr() got = %v, want %v", got, tt.want) |
| 551 | } |
| 552 | if got1 != tt.want1 { |
| 553 | t.Errorf("GetIpv4Addr() got1 = %v, want %v", got1, tt.want1) |
| 554 | } |
| 555 | }) |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | func TestGetIpv6Addr(t *testing.T) { |
| 560 | type args struct { |
| 561 | dhcp6 *layers.DHCPv6 |
| 562 | } |
| 563 | b, err := json.Marshal(layers.DHCPv6OptIAAddr) |
| 564 | if err != nil { |
| 565 | panic(err) |
| 566 | } |
| 567 | tests := []struct { |
| 568 | name string |
| 569 | args args |
| 570 | want net.IP |
| 571 | want1 uint32 |
| 572 | }{ |
| 573 | { |
| 574 | name: "GetIpv6Addr_error", |
| 575 | args: args{ |
| 576 | dhcp6: &layers.DHCPv6{ |
| 577 | MsgType: layers.DHCPv6MsgTypeReply, |
| 578 | Options: layers.DHCPv6Options{ |
| 579 | { |
| 580 | Code: layers.DHCPv6OptIANA, |
| 581 | Data: b, |
| 582 | }, |
| 583 | }, |
| 584 | }, |
| 585 | }, |
| 586 | }, |
| 587 | } |
| 588 | for _, tt := range tests { |
| 589 | t.Run(tt.name, func(t *testing.T) { |
| 590 | got, got1 := GetIpv6Addr(tt.args.dhcp6) |
| 591 | if !reflect.DeepEqual(got, tt.want) { |
| 592 | t.Errorf("GetIpv6Addr() got = %v, want %v", got, tt.want) |
| 593 | } |
| 594 | if got1 != tt.want1 { |
| 595 | t.Errorf("GetIpv6Addr() got1 = %v, want %v", got1, tt.want1) |
| 596 | } |
| 597 | }) |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | func TestVoltApplication_GetMacLearnerInfo(t *testing.T) { |
| 602 | type args struct { |
| 603 | cntx context.Context |
| 604 | deviceID string |
| 605 | portNumber string |
| 606 | vlanID string |
| 607 | } |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 608 | vpv := &VoltPortVnet{ |
| 609 | Device: "SDX6320031", |
| 610 | Port: "SDX6320031-1", |
| 611 | SVlan: of.VlanAny, |
| 612 | MacAddr: BroadcastMAC, |
| 613 | } |
| 614 | sessions := map[[6]byte]IDhcpRelaySession{} |
| 615 | key := [6]byte{1, 2, 3, 4, 5, 6} |
| 616 | sessions[key] = vpv |
| 617 | network := make(map[uint32]*DhcpRelayVnet) |
| 618 | network[uint32(256)] = &DhcpRelayVnet{ |
| 619 | sessions: sessions, |
| 620 | } |
| 621 | dhcpNws.Networks = network |
| 622 | svlan := of.VlanAny |
| 623 | macLearning := MacLearnerInfo{ |
| 624 | DeviceID: "SDX6320031", |
| 625 | PortNumber: "SDX6320031-1", |
| 626 | VlanID: svlan.String(), |
| 627 | MacAddress: BroadcastMAC.String(), |
| 628 | } |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 629 | tests := []struct { |
| 630 | name string |
| 631 | args args |
| 632 | want MacLearnerInfo |
| 633 | wantErr bool |
| 634 | }{ |
| 635 | { |
| 636 | name: "VoltApplication_GetMacLearnerInfo", |
| 637 | args: args{ |
| 638 | cntx: context.Background(), |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 639 | deviceID: "SDX6320031", |
| 640 | portNumber: "SDX6320031-1", |
| 641 | vlanID: svlan.String(), |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 642 | }, |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 643 | want: macLearning, |
| 644 | }, |
| 645 | { |
| 646 | name: "VoltApplication_GetMacLearnerInfo_svlan_empty", |
| 647 | args: args{ |
| 648 | cntx: context.Background(), |
| 649 | deviceID: "SDX6320031", |
| 650 | portNumber: "SDX6320031-1", |
| 651 | vlanID: "", |
| 652 | }, |
| 653 | want: macLearning, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 654 | }, |
| 655 | } |
| 656 | for _, tt := range tests { |
| 657 | t.Run(tt.name, func(t *testing.T) { |
| 658 | va := &VoltApplication{} |
| 659 | got, err := va.GetMacLearnerInfo(tt.args.cntx, tt.args.deviceID, tt.args.portNumber, tt.args.vlanID) |
| 660 | if (err != nil) != tt.wantErr { |
| 661 | t.Errorf("VoltApplication.GetMacLearnerInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 662 | return |
| 663 | } |
| 664 | if !reflect.DeepEqual(got, tt.want) { |
| 665 | t.Errorf("VoltApplication.GetMacLearnerInfo() = %v, want %v", got, tt.want) |
| 666 | } |
| 667 | }) |
| 668 | } |
| 669 | } |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 670 | |
| 671 | func TestVoltApplication_GetAllocations(t *testing.T) { |
| 672 | type args struct { |
| 673 | cntx context.Context |
| 674 | deviceID string |
| 675 | } |
| 676 | allocation := []DhcpAllocation{} |
| 677 | vpv := &VoltPortVnet{ |
| 678 | Device: "SDX6320031", |
| 679 | services: sync.Map{}, |
| 680 | } |
| 681 | voltServ := &VoltService{ |
| 682 | VoltServiceOper: VoltServiceOper{ |
| 683 | Device: "SDX6320031", |
| 684 | }, |
| 685 | VoltServiceCfg: VoltServiceCfg{ |
| 686 | Name: "SDX6320031-1_SDX6320031-1-4096-2310-4096-65", |
| 687 | }, |
| 688 | } |
| 689 | sessions := map[[6]byte]IDhcpRelaySession{} |
| 690 | key := [6]byte{1, 2, 3, 4, 5, 6} |
| 691 | sessions[key] = vpv |
| 692 | network := make(map[uint32]*DhcpRelayVnet) |
| 693 | network[uint32(256)] = &DhcpRelayVnet{ |
| 694 | sessions: sessions, |
| 695 | } |
| 696 | dhcpNws.Networks = network |
| 697 | tests := []struct { |
| 698 | name string |
| 699 | args args |
| 700 | want []DhcpAllocation |
| 701 | wantErr bool |
| 702 | }{ |
| 703 | { |
| 704 | name: "VoltApplication_GetAllocations", |
| 705 | args: args{ |
| 706 | cntx: context.Background(), |
| 707 | deviceID: "SDX6320031", |
| 708 | }, |
| 709 | want: allocation, |
| 710 | }, |
| 711 | { |
| 712 | name: "GetAllocations_with_Services", |
| 713 | args: args{ |
| 714 | cntx: context.Background(), |
| 715 | deviceID: "SDX6320031", |
| 716 | }, |
| 717 | want: allocation, |
| 718 | }, |
| 719 | } |
| 720 | for _, tt := range tests { |
| 721 | t.Run(tt.name, func(t *testing.T) { |
| 722 | va := &VoltApplication{} |
| 723 | switch tt.name { |
| 724 | case "VoltApplication_GetAllocations": |
| 725 | got, err := va.GetAllocations(tt.args.cntx, tt.args.deviceID) |
| 726 | if (err != nil) != tt.wantErr { |
| 727 | t.Errorf("VoltApplication.GetAllocations() error = %v, wantErr %v", err, tt.wantErr) |
| 728 | return |
| 729 | } |
| 730 | assert.NotNil(t, got) |
| 731 | case "GetAllocations_with_Services": |
| 732 | vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ) |
| 733 | got, err := va.GetAllocations(tt.args.cntx, tt.args.deviceID) |
| 734 | if (err != nil) != tt.wantErr { |
| 735 | t.Errorf("VoltApplication.GetAllocations() error = %v, wantErr %v", err, tt.wantErr) |
| 736 | return |
| 737 | } |
| 738 | assert.NotNil(t, got) |
| 739 | } |
| 740 | }) |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | func TestVoltApplication_GetAllMacLearnerInfo(t *testing.T) { |
| 745 | vpv := &VoltPortVnet{ |
| 746 | Device: "SDX6320031", |
| 747 | Port: "SDX6320031-1", |
| 748 | SVlan: of.VlanAny, |
| 749 | MacAddr: BroadcastMAC, |
| 750 | } |
| 751 | sessions := map[[6]byte]IDhcpRelaySession{} |
| 752 | key := [6]byte{1, 2, 3, 4, 5, 6} |
| 753 | sessions[key] = vpv |
| 754 | network := make(map[uint32]*DhcpRelayVnet) |
| 755 | network[uint32(256)] = &DhcpRelayVnet{ |
| 756 | sessions: sessions, |
| 757 | } |
| 758 | dhcpNws.Networks = network |
| 759 | svlan := of.VlanAny |
| 760 | macLearningList := []MacLearnerInfo{} |
| 761 | macLearning := MacLearnerInfo{ |
| 762 | DeviceID: "SDX6320031", |
| 763 | PortNumber: "SDX6320031-1", |
| 764 | VlanID: svlan.String(), |
| 765 | MacAddress: BroadcastMAC.String(), |
| 766 | } |
| 767 | macLearningList = append(macLearningList, macLearning) |
| 768 | tests := []struct { |
| 769 | name string |
| 770 | want []MacLearnerInfo |
| 771 | wantErr bool |
| 772 | }{ |
| 773 | { |
| 774 | name: "VoltApplication_GetAllMacLearnerInfo", |
| 775 | want: macLearningList, |
| 776 | }, |
| 777 | } |
| 778 | for _, tt := range tests { |
| 779 | t.Run(tt.name, func(t *testing.T) { |
| 780 | va := &VoltApplication{} |
| 781 | got, err := va.GetAllMacLearnerInfo() |
| 782 | if (err != nil) != tt.wantErr { |
| 783 | t.Errorf("VoltApplication.GetAllMacLearnerInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 784 | return |
| 785 | } |
| 786 | if !reflect.DeepEqual(got, tt.want) { |
| 787 | t.Errorf("VoltApplication.GetAllMacLearnerInfo() = %v, want %v", got, tt.want) |
| 788 | } |
| 789 | }) |
| 790 | } |
| 791 | } |