blob: f224278da5db521e7f45d5cd0fb3289afa96f0be [file] [log] [blame]
vinokuma703a70b2023-07-17 10:06:43 +05301/*
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
16package application
17
18import (
19 "context"
vinokuma04dc9f82023-07-31 15:47:49 +053020 "encoding/json"
21 "net"
vinokuma703a70b2023-07-17 10:06:43 +053022 "reflect"
vinokuma04dc9f82023-07-31 15:47:49 +053023 "sync"
vinokuma703a70b2023-07-17 10:06:43 +053024 "testing"
Akash Sonib03636c2023-10-31 12:30:59 +053025 "voltha-go-controller/internal/pkg/controller"
vinokuma703a70b2023-07-17 10:06:43 +053026 cntlr "voltha-go-controller/internal/pkg/controller"
27 "voltha-go-controller/internal/pkg/of"
28 "voltha-go-controller/internal/pkg/util"
29 "voltha-go-controller/internal/test/mocks"
30
31 "github.com/golang/mock/gomock"
Akash Soni230e6212023-10-16 10:46:07 +053032 "github.com/google/gopacket/layers"
vinokuma04dc9f82023-07-31 15:47:49 +053033 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
vinokuma703a70b2023-07-17 10:06:43 +053034 "github.com/stretchr/testify/assert"
Akash Soni230e6212023-10-16 10:46:07 +053035 "go.uber.org/atomic"
vinokuma703a70b2023-07-17 10:06:43 +053036)
37
Akash Sonib03636c2023-10-31 12:30:59 +053038const deviceName = "SDX6320031"
39
vinokuma703a70b2023-07-17 10:06:43 +053040func TestVoltPortVnet_JSONMarshal(t *testing.T) {
41 tests := []struct {
42 name string
43 want []byte
44 wantErr bool
45 }{
46 {
47 name: "VoltPortVnet_JSONMarshal",
48 },
49 }
50 for _, tt := range tests {
51 t.Run(tt.name, func(t *testing.T) {
52 vpv := &VoltPortVnet{}
53 _, err := vpv.JSONMarshal()
54 if (err != nil) != tt.wantErr {
55 t.Errorf("VoltPortVnet.JSONMarshal() error = %v, wantErr %v", err, tt.wantErr)
56 return
57 }
58 })
59 }
60}
61
62func TestVoltPortVnet_IsServiceActivated(t *testing.T) {
63 type args struct {
64 cntx context.Context
65 }
66 tests := []struct {
67 name string
68 args args
69 want bool
70 }{
71 {
72 name: "VoltPortVnet_IsServiceActivated",
73 args: args{
74 cntx: context.Background(),
75 },
76 },
77 }
78 for _, tt := range tests {
79 t.Run(tt.name, func(t *testing.T) {
80 vpv := &VoltPortVnet{}
81 voltServ := &VoltService{
82 VoltServiceOper: VoltServiceOper{
83 Device: test_device,
84 ForceDelete: true,
85 },
86 }
87 vpv.services.Store(test_device, voltServ)
88 if got := vpv.IsServiceActivated(tt.args.cntx); got != tt.want {
89 t.Errorf("VoltPortVnet.IsServiceActivated() = %v, want %v", got, tt.want)
90 }
91 })
92 }
93}
94
95func TestVoltVnet_JSONMarshal(t *testing.T) {
96 tests := []struct {
97 name string
98 want []byte
99 wantErr bool
100 }{
101 {
102 name: "VoltVnet_JSONMarshal",
103 },
104 }
105 for _, tt := range tests {
106 t.Run(tt.name, func(t *testing.T) {
107 vv := &VoltVnet{}
108 _, err := vv.JSONMarshal()
109 if (err != nil) != tt.wantErr {
110 t.Errorf("VoltVnet.JSONMarshal() error = %v, wantErr %v", err, tt.wantErr)
111 return
112 }
113 })
114 }
115}
116
117func TestVoltVnet_TriggerAssociatedFlowDelete(t *testing.T) {
118 type args struct {
119 cntx context.Context
120 device string
121 }
122 tests := []struct {
123 name string
124 args args
125 want bool
126 }{
127 {
128 name: "VoltVnet_TriggerAssociatedFlowDelete",
129 args: args{
130 cntx: context.Background(),
131 device: test_device,
132 },
133 want: true,
134 },
135 {
136 name: "cookieList_empty",
137 args: args{
138 cntx: context.Background(),
139 device: test_device,
140 },
141 want: false,
142 },
143 }
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 vv := &VoltVnet{}
147 switch tt.name {
148 case "VoltVnet_TriggerAssociatedFlowDelete":
149 cookie := map[string]bool{}
150 cookie["1234"] = true
151 pendingDeleteFlow := map[string]map[string]bool{}
152 pendingDeleteFlow[test_device] = cookie
153 vv.PendingDeleteFlow = pendingDeleteFlow
154 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
155 db = dbintf
156 dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
157 if got := vv.TriggerAssociatedFlowDelete(tt.args.cntx, tt.args.device); got != tt.want {
158 t.Errorf("VoltVnet.TriggerAssociatedFlowDelete() = %v, want %v", got, tt.want)
159 }
160 case "cookieList_empty":
161 if got := vv.TriggerAssociatedFlowDelete(tt.args.cntx, tt.args.device); got != tt.want {
162 t.Errorf("VoltVnet.TriggerAssociatedFlowDelete() = %v, want %v", got, tt.want)
163 }
164 }
165 })
166 }
167}
168
169func TestVoltApplication_GetMatchingMcastService(t *testing.T) {
170 type args struct {
171 port string
172 device string
173 cvlan of.VlanType
174 }
175 tests := []struct {
176 name string
177 args args
178 want *VoltService
179 }{
180 {
181 name: "VoltApplication_GetMatchingMcastService",
182 args: args{
183 port: "test_port",
184 device: test_device,
185 cvlan: of.VlanAny,
186 },
187 },
188 {
189 name: "dIntf_error",
190 args: args{
191 port: "test_port",
192 device: test_device,
193 cvlan: of.VlanAny,
194 },
195 },
196 {
197 name: "port == d.NniPort",
198 args: args{
199 port: "test_port",
200 device: test_device,
201 cvlan: of.VlanAny,
202 },
203 },
204 {
205 name: "vnets_error",
206 args: args{
207 port: "",
208 device: test_device,
209 cvlan: of.VlanAny,
210 },
211 },
212 }
213 for _, tt := range tests {
214 t.Run(tt.name, func(t *testing.T) {
215 va := &VoltApplication{}
216 switch tt.name {
217 case "VoltApplication_GetMatchingMcastService":
218 va.DevicesDisc.Store(test_device, voltDevice)
219 va.VnetsByPort.Store("test_port", voltPortVnet1)
220 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
221 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
222 }
223 case "dIntf_error":
224 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
225 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
226 }
227 case "port == d.NniPort":
228 va.DevicesDisc.Store(test_device, voltDevice)
229 voltDevice.NniPort = "test_port"
230 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
231 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
232 }
233 case "vnets_error":
234 va.DevicesDisc.Store(test_device, voltDevice)
235 va.VnetsByPort.Store("test_port1", voltPortVnet1)
236 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
237 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
238 }
239 }
240 })
241 }
242}
243
244func TestVoltPortVnet_IgmpFlowInstallFailure(t *testing.T) {
245 type args struct {
246 cookie string
247 errorCode uint32
248 errReason string
249 }
250 tests := []struct {
251 name string
252 args args
253 }{
254 {
255 name: "VoltPortVnet_IgmpFlowInstallFailure",
256 args: args{
257 cookie: "test_cookie",
258 errorCode: uint32(1),
259 errReason: "errReason",
260 },
261 },
262 }
263 for _, tt := range tests {
264 t.Run(tt.name, func(t *testing.T) {
265 vpv := &VoltPortVnet{}
266 switch tt.name {
267 case "VoltPortVnet_IgmpFlowInstallFailure":
268 voltService.IgmpEnabled = true
269 vpv.services.Store("test_cookie", voltService)
270 vpv.IgmpFlowInstallFailure(tt.args.cookie, tt.args.errorCode, tt.args.errReason)
271 }
272 })
273 }
274}
275
276func TestVoltVnet_FlowRemoveFailure(t *testing.T) {
277 type args struct {
278 cntx context.Context
279 cookie string
280 device string
281 errorCode uint32
282 errReason string
283 }
284 tests := []struct {
285 name string
286 args args
287 }{
288 {
289 name: "VoltVnet_FlowRemoveFailure",
290 args: args{
291 cntx: context.Background(),
292 cookie: "1234",
293 device: test_device,
294 },
295 },
296 {
297 name: "mismatch_cookie",
298 args: args{
299 cntx: context.Background(),
300 cookie: "1234",
301 device: test_device,
302 },
303 },
304 }
305 for _, tt := range tests {
306 t.Run(tt.name, func(t *testing.T) {
307 vv := &VoltVnet{}
308 switch tt.name {
309 case "VoltVnet_FlowRemoveFailure":
310 cookie := map[string]bool{}
311 cookie["1234"] = true
312 pendingDeleteFlow := map[string]map[string]bool{}
313 pendingDeleteFlow[test_device] = cookie
314 vv.PendingDeleteFlow = pendingDeleteFlow
315 vv.DeleteInProgress = true
316 vv.Name = "test_name"
317 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
318 db = dbintf
319 dbintf.EXPECT().DelVnet(tt.args.cntx, "test_name").Return(nil).Times(1)
320 vv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
321 case "mismatch_cookie":
322 cookie := map[string]bool{}
323 cookie["12345"] = true
324 pendingDeleteFlow := map[string]map[string]bool{}
325 pendingDeleteFlow[test_device] = cookie
326 vv.PendingDeleteFlow = pendingDeleteFlow
327 vv.DeleteInProgress = true
328 vv.Name = "test_name"
329 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
330 db = dbintf
331 dbintf.EXPECT().DelVnet(tt.args.cntx, "test_name").Return(nil).Times(1)
332 vv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
333 }
334 })
335 }
336}
337
338func TestVoltVnet_FlowRemoveSuccess(t *testing.T) {
339 type args struct {
340 cntx context.Context
341 cookie string
342 device string
343 }
344 tests := []struct {
345 name string
346 args args
347 }{
348 {
349 name: "VoltVnet_FlowRemoveSuccess",
350 args: args{
351 cntx: context.Background(),
352 cookie: "1234",
353 device: test_device,
354 },
355 },
356 }
357 for _, tt := range tests {
358 t.Run(tt.name, func(t *testing.T) {
359 vv := &VoltVnet{}
360 cookie := map[string]bool{}
361 cookie["1234"] = true
362 pendingDeleteFlow := map[string]map[string]bool{}
363 pendingDeleteFlow[test_device] = cookie
364 vv.PendingDeleteFlow = pendingDeleteFlow
365 ga := GetApplication()
366 voltDevice.ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
367 ga.DevicesDisc.Store(test_device, voltDevice)
368 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
369 db = dbintf
370 dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
371 vv.FlowRemoveSuccess(tt.args.cntx, tt.args.cookie, tt.args.device)
372 })
373 }
374}
375
376func TestVoltPortVnet_FlowRemoveFailure(t *testing.T) {
377 type args struct {
378 cntx context.Context
379 cookie string
380 device string
381 errorCode uint32
382 errReason string
383 }
384 tests := []struct {
385 name string
386 args args
387 }{
388 {
389 name: "VoltPortVnet_FlowRemoveFailure",
390 args: args{
391 cntx: context.Background(),
392 cookie: "1234",
393 device: test_device,
394 },
395 },
396 {
397 name: "DeleteInProgress_false",
398 args: args{
399 cntx: context.Background(),
400 cookie: "1234",
401 device: test_device,
402 },
403 },
404 }
405 for _, tt := range tests {
406 t.Run(tt.name, func(t *testing.T) {
407 vpv := &VoltPortVnet{}
408 switch tt.name {
409 case "VoltPortVnet_FlowRemoveFailure":
410 vpv.services.Store("1234", voltService)
411 vpv.DeleteInProgress = true
412 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
413 db = dbintf
414 dbintf.EXPECT().DelVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
415 vpv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
416 case "DeleteInProgress_false":
417 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
418 db = dbintf
419 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
420 vpv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
421 }
422 })
423 }
424}
425
426func TestVoltPortVnet_PushFlows(t *testing.T) {
427 type args struct {
428 cntx context.Context
429 device *VoltDevice
430 flow *of.VoltFlow
431 }
432 vsf := make(map[uint64]*of.VoltSubFlow)
433 vsf[uint64(1)] = &of.VoltSubFlow{
434 Cookie: uint64(1234),
435 }
436 tests := []struct {
437 name string
438 args args
439 wantErr bool
440 }{
441 {
442 name: "VoltPortVnet_PushFlows",
443 args: args{
444 cntx: context.Background(),
445 device: voltDevice,
446 flow: &of.VoltFlow{
447 PortName: "test_port",
448 SubFlows: vsf,
449 },
450 },
451 },
452 }
453 for _, tt := range tests {
454 t.Run(tt.name, func(t *testing.T) {
455 vpv := &VoltPortVnet{}
456 _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t)))
457 err := vpv.PushFlows(tt.args.cntx, tt.args.device, tt.args.flow)
458 assert.NotNil(t, err)
459 })
460 }
461}
462
463func TestVoltPortVnet_isVlanMatching(t *testing.T) {
464 type args struct {
465 cvlan of.VlanType
466 svlan of.VlanType
467 }
468 tests := []struct {
469 name string
470 args args
471 want bool
472 }{
473 {
474 name: "VoltPortVnet_isVlanMatching",
475 args: args{
476 cvlan: of.VlanAny,
477 svlan: of.VlanAny,
478 },
479 want: true,
480 },
481 {
482 name: "vpv.VlanControl_nil",
483 args: args{
484 cvlan: of.VlanAny,
485 svlan: of.VlanAny,
486 },
487 want: true,
488 },
489 }
490 for _, tt := range tests {
491 t.Run(tt.name, func(t *testing.T) {
492 vpv := &VoltPortVnet{}
493 switch tt.name {
494 case "VoltPortVnet_isVlanMatching":
495 vpv.VlanControl = ONUCVlanOLTSVlan
496 vpv.SVlan = of.VlanAny
497 vpv.CVlan = of.VlanAny
498 if got := vpv.isVlanMatching(tt.args.cvlan, tt.args.svlan); got != tt.want {
499 t.Errorf("VoltPortVnet.isVlanMatching() = %v, want %v", got, tt.want)
500 }
501 }
502 })
503 }
504}
505
506func TestProcessIcmpv6McGroup(t *testing.T) {
507 type args struct {
508 device string
509 delete bool
510 }
511 tests := []struct {
512 name string
513 args args
514 wantErr bool
515 }{
516 {
517 name: "TestProcessIcmpv6McGroup",
518 args: args{
519 device: test_device,
520 delete: false,
521 },
522 },
523 }
524 for _, tt := range tests {
525 t.Run(tt.name, func(t *testing.T) {
526 err := ProcessIcmpv6McGroup(tt.args.device, tt.args.delete)
527 assert.NotNil(t, err)
528 })
529 }
530}
531
532func TestVoltVnet_setPbitRemarking(t *testing.T) {
533 tests := []struct {
534 name string
535 want uint32
536 }{
537 {
538 name: "VoltVnet_setPbitRemarking",
539 },
540 }
541 for _, tt := range tests {
542 t.Run(tt.name, func(t *testing.T) {
543 vv := &VoltVnet{}
544 a := make(map[of.PbitType]of.PbitType)
545 a[of.PbitMatchAll] = of.PbitMatchAll
546 vv.CtrlPktPbitRemark = a
547 if got := vv.setPbitRemarking(); got != tt.want {
548 t.Errorf("VoltVnet.setPbitRemarking() = %v, want %v", got, tt.want)
549 }
550 })
551 }
552}
553
554func TestBuildDSArpFlow(t *testing.T) {
555 type args struct {
556 inport uint32
557 vnet *VoltVnet
558 }
559 tests := []struct {
560 name string
561 args args
562 want *of.VoltFlow
563 }{
564 {
565 name: "BuildDSArpFlow",
566 args: args{
567 inport: uint32(1),
568 vnet: &VoltVnet{
569 Version: "test_version",
570 },
571 },
572 },
573 }
574 for _, tt := range tests {
575 t.Run(tt.name, func(t *testing.T) {
576 switch tt.name {
577 case "BuildDSArpFlow":
578 got := BuildDSArpFlow(tt.args.inport, tt.args.vnet)
579 assert.NotNil(t, got)
580 }
581 })
582 }
583}
584
585func TestBuildICMPv6Flow(t *testing.T) {
586 type args struct {
587 inport uint32
588 vnet *VoltVnet
589 }
590 tests := []struct {
591 name string
592 args args
593 want *of.VoltFlow
594 }{
595 {
596 name: "BuildICMPv6Flow",
597 args: args{
598 inport: uint32(1),
599 vnet: &VoltVnet{
600 Version: "test_version",
601 },
602 },
603 },
604 }
605 for _, tt := range tests {
606 t.Run(tt.name, func(t *testing.T) {
607 got := BuildICMPv6Flow(tt.args.inport, tt.args.vnet)
608 assert.NotNil(t, got)
609 })
610 }
611}
612
613func TestVoltApplication_DeleteDevFlowForVlanFromDevice(t *testing.T) {
614 type args struct {
615 cntx context.Context
616 vnet *VoltVnet
617 deviceSerialNum string
618 }
Akash Sonib03636c2023-10-31 12:30:59 +0530619 voltDev := &VoltDevice{
620 Name: "SDX6320031",
621 SerialNum: "SDX6320031",
622 NniDhcpTrapVid: 123,
623 State: cntlr.DeviceStateUP,
624 NniPort: "16777472",
625 Ports: sync.Map{},
626 FlowDelEventMap: util.NewConcurrentMap(),
627 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
628 }
629 voltVnet = &VoltVnet{
630 Version: "v3",
631 VnetConfig: VnetConfig{
632 Name: "2310-4096-4096",
633 VnetType: "Encapsulation",
634 },
635 VnetOper: VnetOper{
636 PendingDeviceToDelete: "SDX6320031",
637 DeleteInProgress: true,
638 PendingDeleteFlow: make(map[string]map[string]bool),
639 },
640 }
641 voltPort := &VoltPort{
642 Name: "16777472",
643 Device: "SDX6320031",
644 ID: 16777472,
645 State: PortStateUp,
646 ChannelPerSubAlarmRaised: false,
647 }
vinokuma703a70b2023-07-17 10:06:43 +0530648 tests := []struct {
649 name string
650 args args
651 }{
652 {
653 name: "device.SerialNum != deviceSerialNum",
654 args: args{
655 cntx: context.Background(),
Akash Sonib03636c2023-10-31 12:30:59 +0530656 vnet: voltVnet,
vinokuma703a70b2023-07-17 10:06:43 +0530657 },
658 },
659 {
Akash Sonib03636c2023-10-31 12:30:59 +0530660 name: "DeleteDevFlowForVlanFromDevice",
vinokuma703a70b2023-07-17 10:06:43 +0530661 args: args{
Akash Sonib03636c2023-10-31 12:30:59 +0530662 cntx: context.Background(),
663 deviceSerialNum: "SDX6320031",
664 vnet: voltVnet,
665 },
666 },
667 {
668 name: "DeleteDevFlowForVlanFromDevice_PortStateDown",
669 args: args{
670 cntx: context.Background(),
671 deviceSerialNum: "SDX6320031",
672 vnet: voltVnet,
vinokuma703a70b2023-07-17 10:06:43 +0530673 },
674 },
675 }
676 for _, tt := range tests {
677 t.Run(tt.name, func(t *testing.T) {
Akash Sonib03636c2023-10-31 12:30:59 +0530678 va := &VoltApplication{
679 DevicesDisc: sync.Map{},
680 }
vinokuma703a70b2023-07-17 10:06:43 +0530681 switch tt.name {
682 case "device.SerialNum != deviceSerialNum":
683 va.DevicesDisc.Store(test_device, voltDevice)
684 va.DeleteDevFlowForVlanFromDevice(tt.args.cntx, tt.args.vnet, tt.args.deviceSerialNum)
Akash Sonib03636c2023-10-31 12:30:59 +0530685 case "DeleteDevFlowForVlanFromDevice":
686 va.DevicesDisc.Store("SDX6320031", voltDev)
687 va.VnetsByName.Store("2310-4096-4096", voltVnet)
688 voltDev.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
689 va.PortsDisc.Store("16777472", voltPort)
690 appMock := mocks.NewMockApp(gomock.NewController(t))
691 cntlr.NewController(ctx, appMock)
692 vc := cntlr.GetController()
693 dev := map[string]*cntlr.Device{}
694 portsByName := map[string]*cntlr.DevicePort{}
695 portsByName["16777472"] = &cntlr.DevicePort{
696 Name: "16777472",
697 ID: 256,
698 State: cntlr.PortStateUp,
699 }
700 device := &cntlr.Device{
701 ID: "SDX6320031",
702 PortsByName: portsByName,
703 }
704 dev["SDX6320031"] = device
705 vc.Devices = dev
706 va.DeleteDevFlowForVlanFromDevice(tt.args.cntx, tt.args.vnet, tt.args.deviceSerialNum)
707 case "DeleteDevFlowForVlanFromDevice_PortStateDown":
708 voltDev.Name = ""
709 va.DevicesDisc.Store("SDX6320031", voltDev)
710 va.VnetsByName.Store("2310-4096-4096", voltVnet)
711 voltDev.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
712 va.PortsDisc.Store("16777472", voltPort)
713 appMock := mocks.NewMockApp(gomock.NewController(t))
714 cntlr.NewController(ctx, appMock)
715 vc := cntlr.GetController()
716 dev := map[string]*cntlr.Device{}
717 portsByName := map[string]*cntlr.DevicePort{}
718 portsByName["16777472"] = &cntlr.DevicePort{
719 Name: "16777472",
720 ID: 256,
721 State: cntlr.PortStateUp,
722 }
723 device := &cntlr.Device{
724 ID: "SDX6320031",
725 PortsByName: portsByName,
726 }
727 dev["SDX6320031"] = device
728 vc.Devices = dev
vinokuma703a70b2023-07-17 10:06:43 +0530729 va.DeleteDevFlowForVlanFromDevice(tt.args.cntx, tt.args.vnet, tt.args.deviceSerialNum)
730 }
731 })
732 }
733}
vinokuma04dc9f82023-07-31 15:47:49 +0530734
735func TestVoltApplication_RestoreVnetsFromDb(t *testing.T) {
736 type args struct {
737 cntx context.Context
738 }
739 tests := []struct {
740 name string
741 args args
742 }{
743 {
744 name: "VoltApplication_RestoreVnetsFromDb",
745 args: args{
746 cntx: context.Background(),
747 },
748 },
749 }
750 for _, tt := range tests {
751 t.Run(tt.name, func(t *testing.T) {
752 vnetsToDelete := map[string]bool{}
753 vnetsToDelete["test_name"] = true
754 va := &VoltApplication{
755 VnetsBySvlan: util.NewConcurrentMap(),
756 VnetsToDelete: vnetsToDelete,
757 }
758 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
759 db = dbintf
760 vnets := map[string]*kvstore.KVPair{}
761 voltVnet.SVlan = of.VlanAny
762 b, err := json.Marshal(voltVnet)
763 if err != nil {
764 panic(err)
765 }
766 vnets["test_device_id"] = &kvstore.KVPair{
767 Key: "test_device_id",
768 Value: b,
769 }
770 dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
771 dbintf.EXPECT().GetVnets(tt.args.cntx).Return(vnets, nil)
772 va.RestoreVnetsFromDb(tt.args.cntx)
773 })
774 }
775}
776
777func TestVoltApplication_DeleteDevFlowForDevice(t *testing.T) {
778 type args struct {
779 cntx context.Context
780 device *VoltDevice
781 }
Akash Sonib03636c2023-10-31 12:30:59 +0530782 voltDev := &VoltDevice{
783 Name: "SDX6320031",
784 SerialNum: "SDX6320031",
785 NniDhcpTrapVid: 123,
786 State: cntlr.DeviceStateUP,
787 NniPort: "16777472",
788 FlowDelEventMap: util.NewConcurrentMap(),
789 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
790 icmpv6GroupAdded: true,
791 }
792 voltVnet = &VoltVnet{
793 Version: "v3",
794 VnetConfig: VnetConfig{
795 Name: "2310-4096-4096",
796 VnetType: "Encapsulation",
797 },
798 VnetOper: VnetOper{
799 PendingDeviceToDelete: "SDX6320031",
800 DeleteInProgress: true,
801 PendingDeleteFlow: make(map[string]map[string]bool),
802 },
803 }
804 voltPort := &VoltPort{
805 Name: "16777472",
806 Device: "SDX6320031",
807 ID: 16777472,
808 State: PortStateUp,
809 ChannelPerSubAlarmRaised: false,
810 }
vinokuma04dc9f82023-07-31 15:47:49 +0530811 tests := []struct {
812 name string
813 args args
814 }{
815 {
Akash Sonib03636c2023-10-31 12:30:59 +0530816 name: "DeleteDevFlowForDevice",
vinokuma04dc9f82023-07-31 15:47:49 +0530817 args: args{
Akash Sonib03636c2023-10-31 12:30:59 +0530818 cntx: context.Background(),
819 device: voltDev,
vinokuma04dc9f82023-07-31 15:47:49 +0530820 },
821 },
822 }
vinokuma04dc9f82023-07-31 15:47:49 +0530823 for _, tt := range tests {
824 t.Run(tt.name, func(t *testing.T) {
825 va := &VoltApplication{}
Akash Sonib03636c2023-10-31 12:30:59 +0530826 switch tt.name {
827 case "DeleteDevFlowForDevice":
828 va.DevicesDisc.Store("SDX6320031", voltDev)
829 va.VnetsByName.Store("2310-4096-4096", voltVnet)
830 voltDev.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
831 va.PortsDisc.Store("16777472", voltPort)
832 voltApp := GetApplication()
833 voltApp.DevicesDisc.Store("SDX6320031", voltDev)
834 appMock := mocks.NewMockApp(gomock.NewController(t))
835 cntlr.NewController(ctx, appMock)
836 vc := cntlr.GetController()
837 dev := map[string]*cntlr.Device{}
838 portsByName := map[string]*cntlr.DevicePort{}
839 portsByName["16777472"] = &cntlr.DevicePort{
840 Name: "16777472",
841 ID: 256,
842 State: cntlr.PortStateUp,
843 }
844 device := &cntlr.Device{
845 ID: "SDX6320031",
846 PortsByName: portsByName,
847 }
848 dev["SDX6320031"] = device
849 vc.Devices = dev
850 va.DeleteDevFlowForDevice(tt.args.cntx, tt.args.device)
851 }
vinokuma04dc9f82023-07-31 15:47:49 +0530852 })
853 }
854}
855
856func TestVoltApplication_DelVnetFromPort(t *testing.T) {
857 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
858 vpv_test := []*VoltPortVnet{
859 {
860 Device: test_device,
861 Port: "test_port",
862 MacAddr: macAdd,
863 VnetName: "test_vnet_name",
864 },
865 }
866 type args struct {
867 cntx context.Context
868 port string
869 vpv *VoltPortVnet
870 }
871 tests := []struct {
872 name string
873 args args
874 }{
875 {
876 name: "VoltApplication_DelVnetFromPort",
877 args: args{
878 cntx: context.Background(),
879 port: "test_port",
880 vpv: &VoltPortVnet{
881 Device: test_device,
882 Port: "test_port",
883 MacAddr: macAdd,
884 VnetName: "test_vnet_name",
885 },
886 },
887 },
888 }
889 for _, tt := range tests {
890 t.Run(tt.name, func(t *testing.T) {
891 va := &VoltApplication{}
892 va.VnetsByPort.Store("test_port", vpv_test)
893 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
894 db = dbintf
895 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
896 dbintf.EXPECT().DelVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
897 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
898 Version: "test_version",
899 })
900 va.DelVnetFromPort(tt.args.cntx, tt.args.port, tt.args.vpv)
901 })
902 }
903}
904
905func TestVoltApplication_PushDevFlowForVlan(t *testing.T) {
906 type args struct {
907 cntx context.Context
908 vnet *VoltVnet
909 }
Akash Sonib03636c2023-10-31 12:30:59 +0530910 voltDev := &VoltDevice{
911 Name: "SDX6320031",
912 SerialNum: "SDX6320031",
913 NniDhcpTrapVid: 123,
914 State: cntlr.DeviceStateUP,
915 NniPort: "16777472",
916 FlowDelEventMap: util.NewConcurrentMap(),
917 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
918 icmpv6GroupAdded: true,
919 VlanPortStatus: sync.Map{},
920 }
921 voltVnet := &VoltVnet{
922 Version: "v3",
923 VnetConfig: VnetConfig{
924 Name: "2310-4096-4096",
925 VnetType: "Encapsulation",
926 DevicesList: []string{"SDX6320031"},
927 SVlan: 0,
928 },
929 VnetOper: VnetOper{
930 PendingDeviceToDelete: "SDX6320031",
931 DeleteInProgress: true,
932 PendingDeleteFlow: make(map[string]map[string]bool),
933 },
934 }
935 voltPort := &VoltPort{
936 Name: "16777216",
937 Device: "SDX6320031",
938 ID: 16777216,
939 State: PortStateUp,
940 ChannelPerSubAlarmRaised: false,
941 }
vinokuma04dc9f82023-07-31 15:47:49 +0530942 tests := []struct {
943 name string
944 args args
945 }{
946 {
947 name: "VoltApplication_PushDevFlowForVlan",
948 args: args{
949 cntx: context.Background(),
950 vnet: &VoltVnet{
951 Version: "test_version",
952 VnetConfig: VnetConfig{
953 DevicesList: []string{"test_serialNum"},
954 SVlan: of.VlanAny,
955 },
956 },
957 },
958 },
Akash Sonib03636c2023-10-31 12:30:59 +0530959 // {
960 // name: "PushDevFlowForVlan",
961 // args: args{
962 // cntx: context.Background(),
963 // vnet: voltVnet,
964 // },
965 // },
vinokuma04dc9f82023-07-31 15:47:49 +0530966 }
967 for _, tt := range tests {
968 t.Run(tt.name, func(t *testing.T) {
969 va := &VoltApplication{}
Akash Sonib03636c2023-10-31 12:30:59 +0530970 switch tt.name {
971 case "VoltApplication_PushDevFlowForVlan":
972 voltDevice.SerialNum = "test_serialNum"
973 voltDevice.VlanPortStatus.Store(uint16(of.VlanAny), true)
974 voltDevice.Name = test_device
975 va.DevicesDisc.Store(test_device, voltDevice)
976 va.PortsDisc.Store("16777216", voltPort)
977 ga := GetApplication()
978 ga.DevicesDisc.Store(test_device, voltDevice)
979 _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t)))
980 va.PushDevFlowForVlan(tt.args.cntx, tt.args.vnet)
981 case "PushDevFlowForVlan":
982 va.DevicesDisc.Store("SDX6320031", voltDev)
983 voltDevice.VlanPortStatus.Store(uint16(0), true)
984 va.VnetsByName.Store("2310-4096-4096", voltVnet)
985 voltDev.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
986 va.PortsDisc.Store("16777472", voltPort)
987 voltApp := GetApplication()
988 voltApp.DevicesDisc.Store("SDX6320031", voltDev)
989 _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t)))
990 va.PushDevFlowForVlan(tt.args.cntx, tt.args.vnet)
991 }
vinokuma04dc9f82023-07-31 15:47:49 +0530992 })
993 }
994}
995
996func TestVoltApplication_PushDevFlowForDevice(t *testing.T) {
997 type args struct {
998 cntx context.Context
999 device *VoltDevice
1000 }
1001 tests := []struct {
1002 name string
1003 args args
1004 }{
1005 {
1006 name: "device.ConfiguredVlanForDeviceFlows is ok",
1007 args: args{
1008 cntx: context.Background(),
1009 device: &VoltDevice{
1010 Name: test_device,
1011 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
1012 },
1013 },
1014 },
1015 {
1016 name: "device.VlanPortStatus is false",
1017 args: args{
1018 cntx: context.Background(),
1019 device: &VoltDevice{
1020 Name: test_device,
1021 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
1022 NniPort: "test_nni_port",
1023 },
1024 },
1025 },
1026 {
1027 name: "device.VlanPortStatus is true",
1028 args: args{
1029 cntx: context.Background(),
1030 device: &VoltDevice{
1031 Name: test_device,
1032 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
1033 NniPort: "test_nni_port",
1034 VlanPortStatus: sync.Map{},
1035 },
1036 },
1037 },
1038 }
1039 for _, tt := range tests {
1040 t.Run(tt.name, func(t *testing.T) {
1041 va := &VoltApplication{}
1042 switch tt.name {
1043 case "device.ConfiguredVlanForDeviceFlows is ok":
1044 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
1045 Version: "test_version",
1046 })
1047 tt.args.device.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
1048 va.PushDevFlowForDevice(tt.args.cntx, tt.args.device)
1049 case "device.VlanPortStatus is false":
1050 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
1051 Version: "test_version",
1052 })
1053 va.PortsDisc.Store("test_nni_port", &VoltPort{
1054 Name: "test_name",
1055 })
1056 va.PushDevFlowForDevice(tt.args.cntx, tt.args.device)
1057 case "device.VlanPortStatus is true":
1058 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
1059 Version: "test_version",
1060 VnetConfig: VnetConfig{
1061 SVlan: of.VlanAny,
1062 },
1063 })
1064 va.PortsDisc.Store("test_nni_port", &VoltPort{
1065 Name: "test_name",
1066 })
1067 tt.args.device.VlanPortStatus.Store(uint16(of.VlanAny), true)
1068 va.PushDevFlowForDevice(tt.args.cntx, tt.args.device)
1069 }
1070 })
1071 }
1072}
Akash Soni230e6212023-10-16 10:46:07 +05301073
1074func TestNewVoltPortVnet(t *testing.T) {
1075 type args struct {
1076 vnet *VoltVnet
1077 }
1078 usDhcpPbit := []of.PbitType{}
1079 usDhcpPbit = append(usDhcpPbit, PbitMatchNone)
1080 tests := []struct {
1081 name string
1082 args args
1083 want *VoltPortVnet
1084 }{
1085 {
1086 name: "NewVoltPortVnet",
1087 args: args{
1088 vnet: &VoltVnet{
1089 VnetConfig: VnetConfig{
1090 UsDhcpPbit: usDhcpPbit,
1091 },
1092 },
1093 },
1094 want: &VoltPortVnet{},
1095 },
1096 }
1097 for _, tt := range tests {
1098 t.Run(tt.name, func(t *testing.T) {
1099 got := NewVoltPortVnet(tt.args.vnet)
1100 assert.NotNil(t, got)
1101 })
1102 }
1103}
1104
1105func TestVoltPortVnet_GetCircuitID(t *testing.T) {
1106 vpv := &VoltPortVnet{}
1107 got := vpv.GetCircuitID()
1108 assert.Nil(t, got)
1109 got1 := vpv.GetRemoteID()
1110 assert.Nil(t, got1)
1111 got3 := vpv.GetDhcpState()
1112 assert.NotNil(t, got3)
1113 got4 := vpv.GetPppoeIaState()
1114 assert.NotNil(t, got4)
1115 got5 := vpv.GetDhcpv6State()
1116 assert.NotNil(t, got5)
1117}
1118
1119func TestVoltPortVnet_GetNniVlans(t *testing.T) {
1120 tests := []struct {
1121 name string
1122 want uint16
1123 want1 uint16
1124 }{
1125 {
1126 name: "GetNniVlans",
1127 want: uint16(of.VlanAny),
1128 want1: uint16(of.VlanAny),
1129 },
1130 {
1131 name: "GetNniVlans_OLTSVlan",
1132 want: uint16(of.VlanAny),
1133 want1: uint16(of.VlanNone),
1134 },
1135 {
1136 name: "GetNniVlans_Default",
1137 want: uint16(of.VlanNone),
1138 want1: uint16(of.VlanNone),
1139 },
1140 }
1141 for _, tt := range tests {
1142 t.Run(tt.name, func(t *testing.T) {
1143 vpv := &VoltPortVnet{
1144 VlanControl: ONUCVlanOLTSVlan,
1145 SVlan: of.VlanAny,
1146 CVlan: of.VlanAny,
1147 }
1148 switch tt.name {
1149 case "GetNniVlans":
1150 got, got1 := vpv.GetNniVlans()
1151 if got != tt.want {
1152 t.Errorf("VoltPortVnet.GetNniVlans() got = %v, want %v", got, tt.want)
1153 }
1154 if got1 != tt.want1 {
1155 t.Errorf("VoltPortVnet.GetNniVlans() got1 = %v, want %v", got1, tt.want1)
1156 }
1157 case "GetNniVlans_OLTSVlan":
1158 vpv.VlanControl = OLTSVlan
1159 got, got1 := vpv.GetNniVlans()
1160 if got != tt.want {
1161 t.Errorf("VoltPortVnet.GetNniVlans() got = %v, want %v", got, tt.want)
1162 }
1163 if got1 != tt.want1 {
1164 t.Errorf("VoltPortVnet.GetNniVlans() got1 = %v, want %v", got1, tt.want1)
1165 }
1166 case "GetNniVlans_Default":
1167 vpv.VlanControl = opt82
1168 got, got1 := vpv.GetNniVlans()
1169 if got != tt.want {
1170 t.Errorf("VoltPortVnet.GetNniVlans() got = %v, want %v", got, tt.want)
1171 }
1172 if got1 != tt.want1 {
1173 t.Errorf("VoltPortVnet.GetNniVlans() got1 = %v, want %v", got1, tt.want1)
1174 }
1175 }
1176 })
1177 }
1178}
1179
1180func TestVoltPortVnet_GetService(t *testing.T) {
1181 type args struct {
1182 name string
1183 }
1184 voltServ := &VoltService{
1185 VoltServiceOper: VoltServiceOper{
1186 Device: "SDX6320031",
1187 },
1188 VoltServiceCfg: VoltServiceCfg{
1189 IsActivated: true,
1190 },
1191 }
1192 tests := []struct {
1193 name string
1194 args args
1195 want *VoltService
1196 want1 bool
1197 }{
1198 {
1199 name: "GetService",
1200 args: args{
1201 name: "SDX6320031-1_SDX6320031-1-4096-2310-4096-65",
1202 },
1203 want: voltServ,
1204 want1: true,
1205 },
1206 }
1207 for _, tt := range tests {
1208 t.Run(tt.name, func(t *testing.T) {
1209 vpv := &VoltPortVnet{}
1210 vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
1211 got, got1 := vpv.GetService(tt.args.name)
1212 if !reflect.DeepEqual(got, tt.want) {
1213 t.Errorf("VoltPortVnet.GetService() got = %v, want %v", got, tt.want)
1214 }
1215 if got1 != tt.want1 {
1216 t.Errorf("VoltPortVnet.GetService() got1 = %v, want %v", got1, tt.want1)
1217 }
1218 })
1219 }
1220}
1221
1222func TestVoltPortVnet_ProcessDhcpSuccess(t *testing.T) {
1223 type args struct {
1224 cntx context.Context
1225 res *layers.DHCPv4
1226 }
1227 tests := []struct {
1228 name string
1229 args args
1230 }{
1231 {
1232 name: "ProcessDhcpSuccess",
1233 args: args{
1234 cntx: context.Background(),
1235 res: &layers.DHCPv4{},
1236 },
1237 },
1238 }
1239 for _, tt := range tests {
1240 t.Run(tt.name, func(t *testing.T) {
1241 vpv := &VoltPortVnet{
1242 servicesCount: atomic.NewUint64(0),
1243 }
1244 vpv.ProcessDhcpSuccess(tt.args.cntx, tt.args.res)
1245 })
1246 }
1247}
1248
1249func TestVoltPortVnet_ProcessDhcpResult(t *testing.T) {
1250 type args struct {
1251 cntx context.Context
1252 res *layers.DHCPv4
1253 }
1254 tests := []struct {
1255 name string
1256 args args
1257 }{
1258 {
1259 name: "ProcessDhcpResult",
1260 args: args{
1261 cntx: context.Background(),
1262 res: &layers.DHCPv4{},
1263 },
1264 },
1265 }
1266 for _, tt := range tests {
1267 t.Run(tt.name, func(t *testing.T) {
1268 vpv := &VoltPortVnet{}
1269 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1270 db = dbintf
1271 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
1272 vpv.ProcessDhcpResult(tt.args.cntx, tt.args.res)
1273 })
1274 }
1275}
1276
1277func TestVoltVnet_associatePortToVnet(t *testing.T) {
1278 type args struct {
1279 port string
1280 }
1281 tests := []struct {
1282 name string
1283 args args
1284 }{
1285 {
1286 name: "ProcessDhcpResult",
1287 args: args{
1288 port: "SDX6320031-1",
1289 },
1290 },
1291 }
1292 for _, tt := range tests {
1293 t.Run(tt.name, func(t *testing.T) {
1294 vv := &VoltVnet{}
1295 vv.associatePortToVnet(tt.args.port)
1296 })
1297 }
1298}
1299
1300func TestVoltPortVnet_ProcessDhcpv6Result(t *testing.T) {
1301 type args struct {
1302 cntx context.Context
1303 ipv6Addr net.IP
1304 leaseTime uint32
1305 }
1306 tests := []struct {
1307 name string
1308 args args
1309 }{
1310 {
1311 name: "ProcessDhcpResult",
1312 args: args{
1313 cntx: context.Background(),
1314 ipv6Addr: AllSystemsMulticastGroupIP,
1315 leaseTime: uint32(128),
1316 },
1317 },
1318 }
1319 for _, tt := range tests {
1320 t.Run(tt.name, func(t *testing.T) {
1321 vpv := &VoltPortVnet{}
1322 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1323 db = dbintf
1324 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
1325 vpv.ProcessDhcpv6Result(tt.args.cntx, tt.args.ipv6Addr, tt.args.leaseTime)
1326 })
1327 }
1328}
1329
1330func TestAddSvcUsMeterToDevice(t *testing.T) {
1331 type args struct {
1332 cntx context.Context
1333 key interface{}
1334 value interface{}
1335 flag bool
1336 }
1337 vpv := &VoltApplication{}
1338 voltServ := &VoltService{
1339 VoltServiceOper: VoltServiceOper{
1340 Device: test_device,
1341 ForceDelete: true,
1342 },
1343 }
1344 vpv.ServiceByName.Store(test_device, voltServ)
1345 tests := []struct {
1346 name string
1347 args args
1348 want bool
1349 }{
1350 {
1351 name: "ProcessDhcpResult",
1352 args: args{
1353 cntx: context.Background(),
1354 key: test_device,
1355 value: voltServ,
1356 },
1357 },
1358 }
1359 for _, tt := range tests {
1360 t.Run(tt.name, func(t *testing.T) {
1361 if got := AddSvcUsMeterToDevice(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag); got != tt.want {
1362 t.Errorf("AddSvcUsMeterToDevice() = %v, want %v", got, tt.want)
1363 }
1364 })
1365 }
1366}
1367
1368func TestClearFlagsInService(t *testing.T) {
1369 type args struct {
1370 cntx context.Context
1371 key interface{}
1372 value interface{}
1373 flag bool
1374 }
1375 vpv := &VoltPortVnet{}
1376 voltServ := &VoltService{
1377 VoltServiceOper: VoltServiceOper{
1378 Device: "SDX6320031",
1379 },
1380 VoltServiceCfg: VoltServiceCfg{
1381 IsActivated: true,
1382 },
1383 }
1384 vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
1385 tests := []struct {
1386 name string
1387 args args
1388 want bool
1389 }{
1390 {
1391 name: "ClearFlagsInService",
1392 args: args{
1393 cntx: context.Background(),
1394 key: test_device,
1395 value: voltServ,
1396 },
1397 },
1398 }
1399 for _, tt := range tests {
1400 t.Run(tt.name, func(t *testing.T) {
1401 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1402 db = dbintf
1403 dbintf.EXPECT().PutService(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
1404 got := ClearFlagsInService(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag)
1405 assert.NotNil(t, got)
1406 })
1407 }
1408}
1409
1410func TestVoltPortVnet_DelDhcpFlows(t *testing.T) {
1411 type args struct {
1412 cntx context.Context
1413 }
1414 tests := []struct {
1415 name string
1416 args args
1417 }{
1418 {
1419 name: "DelDhcpFlows",
1420 args: args{
1421 cntx: context.Background(),
1422 },
1423 },
1424 }
1425 for _, tt := range tests {
1426 t.Run(tt.name, func(t *testing.T) {
1427 vpv := &VoltPortVnet{}
1428 vpv.DelDhcpFlows(tt.args.cntx)
1429 })
1430 }
1431}
1432
Akash Sonib03636c2023-10-31 12:30:59 +05301433func TestVoltPortVnet_PushFlowsForPortVnet(t *testing.T) {
1434 type args struct {
1435 cntx context.Context
1436 d *VoltDevice
1437 }
1438 va := GetApplication()
1439 voltDev := &VoltDevice{
1440 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
1441 SerialNum: "SDX6320031",
1442 NniDhcpTrapVid: 123,
1443 State: cntlr.DeviceStateUP,
1444 FlowAddEventMap: util.NewConcurrentMap(),
1445 Ports: sync.Map{},
1446 }
1447 va.DevicesDisc.Store("SDX6320031", voltDev)
1448 voltPort := &VoltPort{
1449 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
1450 Device: "SDX6320031",
1451 ID: 16777472,
1452 State: PortStateUp,
1453 Type: VoltPortTypeNni,
1454 }
1455 voltDev.Ports.Store("16777472", voltPort)
1456 tests := []struct {
1457 name string
1458 args args
1459 }{
1460 {
1461 name: "PushFlowsForPortVnet",
1462 args: args{
1463 cntx: context.Background(),
1464 d: voltDev,
1465 },
1466 },
1467 {
1468 name: "PushFlowsForPortVnet_PortDown",
1469 args: args{
1470 cntx: context.Background(),
1471 d: voltDev,
1472 },
1473 },
1474 }
1475 for _, tt := range tests {
1476 t.Run(tt.name, func(t *testing.T) {
1477 vpv := &VoltPortVnet{}
1478 switch tt.name {
1479 case "PushFlowsForPortVnet_PortDown":
1480 vpv.PushFlowsForPortVnet(tt.args.cntx, tt.args.d)
1481 case "PushFlowsForPortVnet":
1482 vpv.Port = "16777472"
1483 vpv.PushFlowsForPortVnet(tt.args.cntx, tt.args.d)
1484 }
1485 })
1486 }
1487}
1488
1489func TestVoltPortVnet_setLearntMAC(t *testing.T) {
1490 type args struct {
1491 cntx context.Context
1492 key interface{}
1493 value interface{}
1494 flag bool
1495 }
1496 voltServ := &VoltService{
1497 VoltServiceOper: VoltServiceOper{
1498 Device: test_device,
1499 ForceDelete: true,
1500 },
1501 }
1502 tests := []struct {
1503 name string
1504 args args
1505 want bool
1506 }{
1507 {
1508 name: "setLearntMAC",
1509 args: args{
1510 cntx: context.Background(),
1511 key: test_device,
1512 value: voltServ,
1513 },
1514 want: true,
1515 },
1516 {
1517 name: "updateIPv4AndProvisionFlows",
1518 args: args{
1519 cntx: context.Background(),
1520 key: test_device,
1521 value: voltServ,
1522 },
1523 want: true,
1524 },
1525 {
1526 name: "updateIPv6AndProvisionFlows",
1527 args: args{
1528 cntx: context.Background(),
1529 key: test_device,
1530 value: voltServ,
1531 },
1532 want: true,
1533 },
1534 }
1535 for _, tt := range tests {
1536 t.Run(tt.name, func(t *testing.T) {
1537 vpv := &VoltPortVnet{
1538 MacAddr: net.HardwareAddr(pendingPoolTimer),
1539 Ipv4Addr: AllSystemsMulticastGroupIP,
1540 Ipv6Addr: AllSystemsMulticastGroupIP,
1541 }
1542 vpv.services.Store(test_device, voltServ)
1543 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1544 db = dbintf
1545 dbintf.EXPECT().PutService(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
1546 switch tt.name {
1547 case "setLearntMAC":
1548 if got := vpv.setLearntMAC(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag); got != tt.want {
1549 t.Errorf("VoltPortVnet.setLearntMAC() = %v, want %v", got, tt.want)
1550 }
1551 case "updateIPv4AndProvisionFlows":
1552 if got := vpv.updateIPv4AndProvisionFlows(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag); got != tt.want {
1553 t.Errorf("VoltPortVnet.updateIPv4AndProvisionFlows() = %v, want %v", got, tt.want)
1554 }
1555 case "updateIPv6AndProvisionFlows":
1556 if got := vpv.updateIPv6AndProvisionFlows(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag); got != tt.want {
1557 t.Errorf("VoltPortVnet.updateIPv6AndProvisionFlows() = %v, want %v", got, tt.want)
1558 }
1559 }
1560 })
1561 }
1562}
1563
1564func TestAddMeterToDevice(t *testing.T) {
1565 type args struct {
1566 cntx context.Context
1567 key interface{}
1568 value interface{}
1569 flag bool
1570 }
1571 voltServ := &VoltService{
1572 VoltServiceOper: VoltServiceOper{
1573 Device: test_device,
1574 ForceDelete: true,
1575 },
1576 }
1577 tests := []struct {
1578 name string
1579 args args
1580 want bool
1581 }{
1582 {
1583 name: "TestAddMeterToDevice",
1584 args: args{
1585 cntx: context.Background(),
1586 key: test_device,
1587 value: voltServ,
1588 },
1589 want: true,
1590 },
1591 }
1592 for _, tt := range tests {
1593 t.Run(tt.name, func(t *testing.T) {
1594 if got := AddMeterToDevice(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag); got != tt.want {
1595 t.Errorf("AddMeterToDevice() = %v, want %v", got, tt.want)
1596 }
1597 })
1598 }
1599}
1600
1601func TestVoltPortVnet_AddUsArpFlows(t *testing.T) {
1602 type args struct {
1603 cntx context.Context
1604 }
1605 va := GetApplication()
1606 voltDev := &VoltDevice{
1607 Name: "SDX6320031",
1608 SerialNum: "SDX6320031",
1609 NniDhcpTrapVid: 123,
1610 State: cntlr.DeviceStateUP,
1611 FlowAddEventMap: util.NewConcurrentMap(),
1612 Ports: sync.Map{},
1613 }
1614 voltPort := &VoltPort{
1615 Name: "16777472",
1616 Device: "SDX6320031",
1617 ID: 16777472,
1618 State: PortStateUp,
1619 ChannelPerSubAlarmRaised: false,
1620 }
1621 tests := []struct {
1622 name string
1623 args args
1624 wantErr bool
1625 }{
1626 {
1627 name: "AddUsArpFlows",
1628 args: args{
1629 cntx: context.Background(),
1630 },
1631 wantErr: true,
1632 },
1633 {
1634 name: "AddUsArpFlows_DeviceNotFound",
1635 args: args{
1636 cntx: context.Background(),
1637 },
1638 wantErr: true,
1639 },
1640 {
1641 name: "AddUsArpFlows_DeviceStateDOWN",
1642 args: args{
1643 cntx: context.Background(),
1644 },
1645 },
1646 }
1647 for _, tt := range tests {
1648 t.Run(tt.name, func(t *testing.T) {
1649 vpv := &VoltPortVnet{
1650 Device: deviceName,
1651 MacLearning: MacLearningNone,
1652 MacAddr: BroadcastMAC,
1653 Port: "16777472",
1654 }
1655 va.DevicesDisc.Store(deviceName, voltDev)
1656 va.PortsDisc.Store("16777472", voltPort)
1657 appMock := mocks.NewMockApp(gomock.NewController(t))
1658 cntlr.NewController(ctx, appMock)
1659 vc := cntlr.GetController()
1660 dev := map[string]*cntlr.Device{}
1661 portsByName := map[string]*cntlr.DevicePort{}
1662 portsByName["16777472"] = &cntlr.DevicePort{
1663 Name: "16777472",
1664 ID: 256,
1665 }
1666 device := &cntlr.Device{
1667 ID: deviceName,
1668 PortsByName: portsByName,
1669 }
1670 dev["SDX6320031"] = device
1671 vc.Devices = dev
1672 switch tt.name {
1673 case "AddUsArpFlows":
1674 if err := vpv.AddUsArpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1675 t.Errorf("VoltPortVnet.AddUsArpFlows() error = %v, wantErr %v", err, tt.wantErr)
1676 }
1677 case "AddUsArpFlows_DeviceNotFound":
1678 vpv.Device = ""
1679 if err := vpv.AddUsArpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1680 t.Errorf("VoltPortVnet.AddUsArpFlows() error = %v, wantErr %v", err, tt.wantErr)
1681 }
1682 case "AddUsArpFlows_DeviceStateDOWN":
1683 vpv.Device = deviceName
1684 voltDev.State = cntlr.DeviceStateDOWN
1685 if err := vpv.AddUsArpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1686 t.Errorf("VoltPortVnet.AddUsArpFlows() error = %v, wantErr %v", err, tt.wantErr)
1687 }
1688 }
1689 })
1690 }
1691}
1692
Akash Soni230e6212023-10-16 10:46:07 +05301693func TestVoltPortVnet_AddDsDhcpFlows(t *testing.T) {
1694 type args struct {
1695 cntx context.Context
1696 }
1697 va := GetApplication()
1698 voltDev := &VoltDevice{
1699 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
1700 SerialNum: "SDX6320031",
1701 NniDhcpTrapVid: 123,
1702 State: cntlr.DeviceStateUP,
1703 FlowAddEventMap: util.NewConcurrentMap(),
1704 }
1705 va.DevicesDisc.Store("SDX6320031", voltDev)
1706 voltPort := &VoltPort{
1707 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
1708 Device: "SDX6320031",
1709 ID: 16777472,
1710 State: PortStateDown,
1711 ChannelPerSubAlarmRaised: false,
1712 Type: VoltPortTypeNni,
1713 }
1714 tests := []struct {
1715 name string
1716 args args
1717 wantErr bool
1718 }{
1719 {
1720 name: "AddDsDhcpFlows",
1721 args: args{
1722 cntx: context.Background(),
1723 },
1724 },
1725 {
1726 name: "AddDsDhcpFlows_DeviceNotFound",
1727 args: args{
1728 cntx: context.Background(),
1729 },
1730 wantErr: true,
1731 },
1732 {
1733 name: "AddDsDhcpFlows_StateDown",
1734 args: args{
1735 cntx: context.Background(),
1736 },
1737 },
1738 {
1739 name: "AddDsDhcpFlows_GlobalDhcpFlowAdded",
1740 args: args{
1741 cntx: context.Background(),
1742 },
1743 },
1744 {
1745 name: "AddDsDhcpFlows_PositiveSenario",
1746 args: args{
1747 cntx: context.Background(),
1748 },
1749 },
1750 }
1751 for _, tt := range tests {
1752 t.Run(tt.name, func(t *testing.T) {
1753 vpv := &VoltPortVnet{
1754 Device: "SDX6320031",
1755 }
1756 switch tt.name {
1757 case "AddDsDhcpFlows":
1758 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1759 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1760 }
1761 case "AddDsDhcpFlows_DeviceNotFound":
1762 vpv.Device = ""
1763 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1764 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1765 }
1766 case "AddDsDhcpFlows_StateDown":
1767 voltDev.State = cntlr.DeviceStateDOWN
1768 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1769 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1770 }
1771 case "AddDsDhcpFlows_GlobalDhcpFlowAdded":
Akash Sonib03636c2023-10-31 12:30:59 +05301772 vpv.Device = deviceName
Akash Soni230e6212023-10-16 10:46:07 +05301773 voltDev.State = cntlr.DeviceStateUP
1774 voltDev.GlobalDhcpFlowAdded = true
1775 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1776 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1777 }
1778 case "AddDsDhcpFlows_PositiveSenario":
Akash Sonib03636c2023-10-31 12:30:59 +05301779 vpv.Device = deviceName
Akash Soni230e6212023-10-16 10:46:07 +05301780 voltDev.State = cntlr.DeviceStateUP
1781 voltDev.GlobalDhcpFlowAdded = false
1782 voltDev.NniPort = "16777472"
1783 va.PortsDisc.Store("16777472", voltPort)
1784 appMock := mocks.NewMockApp(gomock.NewController(t))
1785 cntlr.NewController(ctx, appMock)
1786 vc := cntlr.GetController()
1787 device := &cntlr.Device{
1788 ID: "SDX6320031",
1789 }
1790 dev := map[string]*cntlr.Device{}
1791 dev["SDX6320031"] = device
1792 vc.Devices = dev
1793 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1794 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1795 }
1796 }
1797 })
1798 }
1799}
Akash Sonib03636c2023-10-31 12:30:59 +05301800
1801func TestVoltPortVnet_AddUsDhcpFlows(t *testing.T) {
1802 type args struct {
1803 cntx context.Context
1804 }
1805 va := GetApplication()
1806 voltDev := &VoltDevice{
1807 Name: "SDX6320031",
1808 SerialNum: "SDX6320031",
1809 NniDhcpTrapVid: 123,
1810 State: cntlr.DeviceStateUP,
1811 NniPort: "16777472",
1812 FlowAddEventMap: util.NewConcurrentMap(),
1813 }
1814 va.DevicesDisc.Store("SDX6320031", voltDev)
1815 voltPort := &VoltPort{
1816 Name: "16777472",
1817 Device: "SDX6320031",
1818 ID: 16777472,
1819 State: PortStateDown,
1820 ChannelPerSubAlarmRaised: false,
1821 Type: VoltPortTypeNni,
1822 }
1823 tests := []struct {
1824 name string
1825 args args
1826 wantErr bool
1827 }{
1828 {
1829 name: "AddUsDhcpFlows_PositiveSenario",
1830 args: args{
1831 cntx: context.Background(),
1832 },
1833 },
1834 {
1835 name: "AddUsDhcpFlows_StateDown",
1836 args: args{
1837 cntx: context.Background(),
1838 },
1839 },
1840 {
1841 name: "AddUsDhcpFlows_DeviceNotFound",
1842 args: args{
1843 cntx: context.Background(),
1844 },
1845 wantErr: true,
1846 },
1847 {
1848 name: "AddUsDhcpFlows_GlobalDhcpFlowAdded",
1849 args: args{
1850 cntx: context.Background(),
1851 },
1852 },
1853 }
1854 for _, tt := range tests {
1855 t.Run(tt.name, func(t *testing.T) {
1856 vpv := &VoltPortVnet{
1857 Device: "SDX6320031",
1858 VnetType: DpuMgmtTraffic,
1859 Port: "16777472",
1860 }
1861 switch tt.name {
1862 case "AddUsDhcpFlows_PositiveSenario":
1863 va.PortsDisc.Store("16777472", voltPort)
1864 appMock := mocks.NewMockApp(gomock.NewController(t))
1865 cntlr.NewController(ctx, appMock)
1866 vc := cntlr.GetController()
1867 device := &cntlr.Device{
1868 ID: "SDX6320031",
1869 }
1870 dev := map[string]*cntlr.Device{}
1871 dev["SDX6320031"] = device
1872 vc.Devices = dev
1873 if err := vpv.AddUsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1874 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1875 }
1876 case "AddUsDhcpFlows_DeviceNotFound":
1877 vpv.Device = ""
1878 if err := vpv.AddUsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1879 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1880 }
1881 case "AddUsDhcpFlows_StateDown":
1882 voltDev.State = cntlr.DeviceStateDOWN
1883 if err := vpv.AddUsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1884 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1885 }
1886 case "AddUsDhcpFlows_GlobalDhcpFlowAdded":
1887 vpv.Device = "SDX6320031"
1888 voltDev.State = cntlr.DeviceStateUP
1889 vpv.Port = ""
1890 if err := vpv.AddUsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1891 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1892 }
1893 }
1894 })
1895 }
1896}
1897
1898func TestVoltPortVnet_AddUsPppoeFlows(t *testing.T) {
1899 type args struct {
1900 cntx context.Context
1901 }
1902 va := GetApplication()
1903 voltDev := &VoltDevice{
1904 Name: "SDX6320031",
1905 SerialNum: "SDX6320031",
1906 NniDhcpTrapVid: 123,
1907 State: cntlr.DeviceStateUP,
1908 NniPort: "16777472",
1909 FlowAddEventMap: util.NewConcurrentMap(),
1910 }
1911 voltPort := &VoltPort{
1912 Name: "16777472",
1913 Device: "SDX6320031",
1914 ID: 16777472,
1915 State: PortStateUp,
1916 ChannelPerSubAlarmRaised: false,
1917 }
1918 va.DevicesDisc.Store("SDX6320031", voltDev)
1919 va.PortsDisc.Store("16777472", voltPort)
1920 tests := []struct {
1921 name string
1922 args args
1923 wantErr bool
1924 }{
1925 {
1926 name: "AddUsPppoeFlows",
1927 args: args{
1928 cntx: context.Background(),
1929 },
1930 wantErr: true,
1931 },
1932 {
1933 name: "AddDsPppoeFlows",
1934 args: args{
1935 cntx: context.Background(),
1936 },
1937 wantErr: true,
1938 },
1939 {
1940 name: "AddUsPppoeFlows_StateDown",
1941 args: args{
1942 cntx: context.Background(),
1943 },
1944 },
1945 {
1946 name: "AddDsPppoeFlows_StateDown",
1947 args: args{
1948 cntx: context.Background(),
1949 },
1950 },
1951 {
1952 name: "AddUsPppoeFlows_DeviceNotFound",
1953 args: args{
1954 cntx: context.Background(),
1955 },
1956 wantErr: true,
1957 },
1958 {
1959 name: "AddDsPppoeFlows_DeviceNotFound",
1960 args: args{
1961 cntx: context.Background(),
1962 },
1963 wantErr: true,
1964 },
1965 }
1966 for _, tt := range tests {
1967 t.Run(tt.name, func(t *testing.T) {
1968 vpv := &VoltPortVnet{
1969 Device: "SDX6320031",
1970 MacLearning: MacLearningNone,
1971 MacAddr: net.HardwareAddr(pendingPoolTimer),
1972 }
1973 switch tt.name {
1974 case "AddUsPppoeFlows":
1975 if err := vpv.AddUsPppoeFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1976 t.Errorf("VoltPortVnet.AddUsPppoeFlows() error = %v, wantErr %v", err, tt.wantErr)
1977 }
1978 case "AddDsPppoeFlows":
1979 appMock := mocks.NewMockApp(gomock.NewController(t))
1980 cntlr.NewController(ctx, appMock)
1981 vc := cntlr.GetController()
1982 dev := map[string]*cntlr.Device{}
1983 portsByName := map[string]*cntlr.DevicePort{}
1984 portsByName["16777472"] = &cntlr.DevicePort{
1985 Name: "16777472",
1986 ID: 256,
1987 }
1988 device := &cntlr.Device{
1989 ID: "SDX6320031",
1990 PortsByName: portsByName,
1991 }
1992 dev["SDX6320031"] = device
1993 vc.Devices = dev
1994 if err := vpv.AddDsPppoeFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1995 t.Errorf("VoltPortVnet.AddUsPppoeFlows() error = %v, wantErr %v", err, tt.wantErr)
1996 }
1997 case "AddUsPppoeFlows_StateDown":
1998 voltDev.State = cntlr.DeviceStateDOWN
1999 if err := vpv.AddUsPppoeFlows(tt.args.cntx); (err != nil) != tt.wantErr {
2000 t.Errorf("VoltPortVnet.AddUsPppoeFlows() error = %v, wantErr %v", err, tt.wantErr)
2001 }
2002 case "AddDsPppoeFlows_StateDown":
2003 voltDev.State = cntlr.DeviceStateDOWN
2004 if err := vpv.AddDsPppoeFlows(tt.args.cntx); (err != nil) != tt.wantErr {
2005 t.Errorf("VoltPortVnet.AddUsPppoeFlows() error = %v, wantErr %v", err, tt.wantErr)
2006 }
2007 case "AddUsPppoeFlows_DeviceNotFound":
2008 vpv.Device = ""
2009 if err := vpv.AddUsPppoeFlows(tt.args.cntx); (err != nil) != tt.wantErr {
2010 t.Errorf("VoltPortVnet.AddUsPppoeFlows() error = %v, wantErr %v", err, tt.wantErr)
2011 }
2012 case "AddDsPppoeFlows_DeviceNotFound":
2013 vpv.Device = ""
2014 if err := vpv.AddDsPppoeFlows(tt.args.cntx); (err != nil) != tt.wantErr {
2015 t.Errorf("VoltPortVnet.AddUsPppoeFlows() error = %v, wantErr %v", err, tt.wantErr)
2016 }
2017 }
2018 })
2019 }
2020}
2021
2022func TestVoltPortVnet_AddIgmpFlows(t *testing.T) {
2023 type args struct {
2024 cntx context.Context
2025 }
2026 var voltPortTest = &VoltPort{
2027 Name: "test_name",
2028 State: PortStateUp,
2029 }
2030 tests := []struct {
2031 name string
2032 args args
2033 wantErr bool
2034 }{
2035 {
2036 name: "AddIgmpFlows",
2037 args: args{
2038 cntx: context.Background(),
2039 },
2040 wantErr: true,
2041 },
2042 }
2043 for _, tt := range tests {
2044 t.Run(tt.name, func(t *testing.T) {
2045 vpv := &VoltPortVnet{
2046 MvlanProfileName: "mvlan_profile",
2047 }
2048 va := GetApplication()
2049 va.PortsDisc.Store("test_port", voltPortTest)
2050 if err := vpv.AddIgmpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
2051 t.Errorf("VoltPortVnet.AddIgmpFlows() error = %v, wantErr %v", err, tt.wantErr)
2052 }
2053 })
2054 }
2055}
2056
2057func TestVoltPortVnet_BuildUsDhcp6Flows(t *testing.T) {
2058 voltPort := &VoltPort{
2059 Name: "16777216",
2060 Device: "SDX6320031",
2061 ID: 16777216,
2062 State: PortStateDown,
2063 ChannelPerSubAlarmRaised: false,
2064 Type: VoltPortTypeNni,
2065 }
2066 voltService := &VoltService{
2067 Version: "test_version",
2068 VoltServiceCfg: VoltServiceCfg{
2069 VnetID: "test_vnet_id",
2070 Port: "16777216",
2071 SVlan: of.VlanAny,
2072 CVlan: of.VlanAny,
2073 UniVlan: of.VlanAny,
2074 },
2075 }
2076 voltDev := &VoltDevice{
2077 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
2078 SerialNum: "SDX6320031",
2079 NniDhcpTrapVid: 123,
2080 State: cntlr.DeviceStateUP,
2081 NniPort: "16777216",
2082 Ports: sync.Map{},
2083 }
2084 tests := []struct {
2085 name string
2086 want *of.VoltFlow
2087 wantErr bool
2088 }{
2089 {
2090 name: "BuildUsDhcp6Flows",
2091 want: &of.VoltFlow{},
2092 },
2093 {
2094 name: "BuildDsDhcp6Flows",
2095 want: &of.VoltFlow{},
2096 },
2097 {
2098 name: "BuildDsDhcp6Flows_DeviceNotFound",
2099 want: &of.VoltFlow{},
2100 wantErr: true,
2101 },
2102 {
2103 name: "BuildUsDhcp6Flows_portnotfound",
2104 want: &of.VoltFlow{},
2105 wantErr: true,
2106 },
2107 {
2108 name: "BuildDsDhcp6Flows_portnotfound",
2109 want: &of.VoltFlow{},
2110 wantErr: true,
2111 },
2112 }
2113 for _, tt := range tests {
2114 t.Run(tt.name, func(t *testing.T) {
2115 vpv := &VoltPortVnet{
2116 Port: "16777216",
2117 services: sync.Map{},
2118 AllowTransparent: true,
2119 Device: "SDX6320031",
2120 }
2121 va := GetApplication()
2122 va.PortsDisc.Store("16777216", voltPort)
2123 vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltService)
2124 switch tt.name {
2125 case "BuildUsDhcp6Flows":
2126 got, err := vpv.BuildUsDhcp6Flows()
2127 if (err != nil) != tt.wantErr {
2128 t.Errorf("VoltPortVnet.BuildUsDhcp6Flows() error = %v, wantErr %v", err, tt.wantErr)
2129 return
2130 }
2131 assert.NotNil(t, got)
2132 case "BuildDsDhcp6Flows":
2133 va.DevicesDisc.Store("SDX6320031", voltDev)
2134 got, err := vpv.BuildDsDhcp6Flows()
2135 if (err != nil) != tt.wantErr {
2136 t.Errorf("VoltPortVnet.BuildDsDhcp6Flows() error = %v, wantErr %v", err, tt.wantErr)
2137 return
2138 }
2139 assert.NotNil(t, got)
2140 case "BuildDsDhcp6Flows_DeviceNotFound":
2141 vpv.Device = ""
2142 got, err := vpv.BuildDsDhcp6Flows()
2143 if (err != nil) != tt.wantErr {
2144 t.Errorf("VoltPortVnet.BuildDsDhcp6Flows() error = %v, wantErr %v", err, tt.wantErr)
2145 return
2146 }
2147 assert.Nil(t, got)
2148 case "BuildUsDhcp6Flows_portnotfound":
2149 vpv.Port = ""
2150 got, err := vpv.BuildUsDhcp6Flows()
2151 if (err != nil) != tt.wantErr {
2152 t.Errorf("VoltPortVnet.BuildUsDhcp6Flows_portnotfound() error = %v, wantErr %v", err, tt.wantErr)
2153 return
2154 }
2155 assert.Nil(t, got)
2156 case "BuildDsDhcp6Flows_portnotfound":
2157 voltDev.NniPort = "abc"
2158 got, err := vpv.BuildDsDhcp6Flows()
2159 if (err != nil) != tt.wantErr {
2160 t.Errorf("VoltPortVnet.BuildDsDhcp6Flows_portnotfound() error = %v, wantErr %v", err, tt.wantErr)
2161 return
2162 }
2163 assert.Nil(t, got)
2164 }
2165 })
2166 }
2167}
2168
2169func TestVoltPortVnet_setUsMatchVlan(t *testing.T) {
2170 type args struct {
2171 flow *of.VoltSubFlow
2172 }
2173 tests := []struct {
2174 name string
2175 args args
2176 wantErr bool
2177 }{
2178 {
2179 name: "setUsMatchVlan",
2180 args: args{
2181 flow: &of.VoltSubFlow{},
2182 },
2183 },
2184 {
2185 name: "OLTCVlanOLTSVlan",
2186 args: args{
2187 flow: &of.VoltSubFlow{},
2188 },
2189 },
2190 {
2191 name: "ONUCVlan",
2192 args: args{
2193 flow: &of.VoltSubFlow{},
2194 },
2195 },
2196 {
2197 name: "OLTSVlan",
2198 args: args{
2199 flow: &of.VoltSubFlow{},
2200 },
2201 },
2202 {
2203 name: "Default",
2204 args: args{
2205 flow: &of.VoltSubFlow{},
2206 },
2207 wantErr: true,
2208 },
2209 {
2210 name: "setDsMatchVlan_OLTCVlanOLTSVlan",
2211 args: args{
2212 flow: &of.VoltSubFlow{},
2213 },
2214 },
2215 {
2216 name: "setDsMatchVlan_Default",
2217 args: args{
2218 flow: &of.VoltSubFlow{},
2219 },
2220 },
2221 }
2222 for _, tt := range tests {
2223 t.Run(tt.name, func(t *testing.T) {
2224 vpv := &VoltPortVnet{
2225 VlanControl: ONUCVlanOLTSVlan,
2226 }
2227 switch tt.name {
2228 case "setUsMatchVlan":
2229 if err := vpv.setUsMatchVlan(tt.args.flow); (err != nil) != tt.wantErr {
2230 t.Errorf("VoltPortVnet.setUsMatchVlan() error = %v, wantErr %v", err, tt.wantErr)
2231 }
2232 case "OLTCVlanOLTSVlan":
2233 vpv.VlanControl = OLTCVlanOLTSVlan
2234 if err := vpv.setUsMatchVlan(tt.args.flow); (err != nil) != tt.wantErr {
2235 t.Errorf("VoltPortVnet.setUsMatchVlan() error = %v, wantErr %v", err, tt.wantErr)
2236 }
2237 case "ONUCVlan":
2238 vpv.VlanControl = ONUCVlan
2239 if err := vpv.setUsMatchVlan(tt.args.flow); (err != nil) != tt.wantErr {
2240 t.Errorf("VoltPortVnet.setUsMatchVlan() error = %v, wantErr %v", err, tt.wantErr)
2241 }
2242 case "OLTSVlan":
2243 vpv.VlanControl = OLTSVlan
2244 if err := vpv.setUsMatchVlan(tt.args.flow); (err != nil) != tt.wantErr {
2245 t.Errorf("VoltPortVnet.setUsMatchVlan() error = %v, wantErr %v", err, tt.wantErr)
2246 }
2247 case "Default":
2248 vpv.VlanControl = opt82
2249 if err := vpv.setUsMatchVlan(tt.args.flow); (err != nil) != tt.wantErr {
2250 t.Errorf("VoltPortVnet.setUsMatchVlan() error = %v, wantErr %v", err, tt.wantErr)
2251 }
2252 case "setDsMatchVlan_OLTCVlanOLTSVlan":
2253 vpv.VlanControl = OLTCVlanOLTSVlan
2254 vpv.setDsMatchVlan(tt.args.flow)
2255 case "setDsMatchVlan_Default":
2256 vpv.VlanControl = opt82
2257 vpv.setDsMatchVlan(tt.args.flow)
2258 }
2259 })
2260 }
2261}
2262
2263func TestVoltPortVnet_BuildIgmpFlows(t *testing.T) {
2264 va := GetApplication()
2265 devicesList := make(map[string]OperInProgress)
2266 devicesList["SDX6320030"] = opt82
2267 mvp := &MvlanProfile{
2268 Name: "mvlan_test",
2269 DevicesList: devicesList,
2270 }
2271 va.MvlanProfilesByName.Store("mvlan_test", mvp)
2272 voltPort := &VoltPort{
2273 Name: "16777472",
2274 Device: "SDX6320031",
2275 ID: 16777472,
2276 State: PortStateUp,
2277 ChannelPerSubAlarmRaised: false,
2278 }
2279 va.PortsDisc.Store("16777472", voltPort)
2280 tests := []struct {
2281 name string
2282 want *of.VoltFlow
2283 wantErr bool
2284 }{
2285 {
2286 name: "BuildIgmpFlows",
2287 want: &of.VoltFlow{},
2288 },
2289 {
2290 name: "BuildIgmpFlows_McastService_False",
2291 want: &of.VoltFlow{},
2292 },
2293 {
2294 name: "BuildIgmpFlows_PortNotFound",
2295 want: nil,
2296 wantErr: true,
2297 },
2298 }
2299 for _, tt := range tests {
2300 t.Run(tt.name, func(t *testing.T) {
2301 vpv := &VoltPortVnet{
2302 Port: "16777472",
2303 MvlanProfileName: "mvlan_test",
2304 MacLearning: MacLearningNone,
2305 MacAddr: util.Uint32ToByte(uint32(23)),
2306 McastService: true,
2307 AllowTransparent: true,
2308 }
2309
2310 switch tt.name {
2311 case "BuildIgmpFlows":
2312 got, err := vpv.BuildIgmpFlows()
2313 if (err != nil) != tt.wantErr {
2314 t.Errorf("VoltPortVnet.BuildIgmpFlows() error = %v, wantErr %v", err, tt.wantErr)
2315 return
2316 }
2317 assert.NotNil(t, got)
2318 case "BuildIgmpFlows_McastService_False":
2319 vpv.McastService = false
2320 vpv.services.Store("16777472", &VoltService{})
2321 got, err := vpv.BuildIgmpFlows()
2322 if (err != nil) != tt.wantErr {
2323 t.Errorf("VoltPortVnet.BuildIgmpFlows() error = %v, wantErr %v", err, tt.wantErr)
2324 return
2325 }
2326 assert.NotNil(t, got)
2327 case "BuildIgmpFlows_PortNotFound":
2328 vpv.Port = ""
2329 got, err := vpv.BuildIgmpFlows()
2330 if (err != nil) != tt.wantErr {
2331 t.Errorf("VoltPortVnet.BuildIgmpFlows() error = %v, wantErr %v", err, tt.wantErr)
2332 return
2333 }
2334 assert.Nil(t, got)
2335 }
2336 })
2337 }
2338}
2339
2340func TestVoltPortVnet_SetMacAddr(t *testing.T) {
2341 type args struct {
2342 cntx context.Context
2343 addr net.HardwareAddr
2344 }
2345 addr, _ := net.ParseMAC("00:00:11:00:00:00")
2346 macAddr, _ := net.ParseMAC("00:00:00:00:00:11")
2347 tests := []struct {
2348 name string
2349 args args
2350 }{
2351 {
2352 name: "SetMacAddr",
2353 args: args{
2354 cntx: context.Background(),
2355 addr: addr,
2356 },
2357 },
2358 }
2359 for _, tt := range tests {
2360 t.Run(tt.name, func(t *testing.T) {
2361 vpv := &VoltPortVnet{
2362 MacAddr: macAddr,
2363 MacLearning: MaxLenDhcpv6DUID,
2364 FlowsApplied: true,
2365 }
2366 switch tt.name {
2367 case "SetMacAddr":
2368 vpv.SetMacAddr(tt.args.cntx, tt.args.addr)
2369 }
2370 })
2371 }
2372}
2373
2374func TestVoltPortVnet_AddTrapFlows(t *testing.T) {
2375 type args struct {
2376 cntx context.Context
2377 }
2378 tests := []struct {
2379 name string
2380 args args
2381 }{
2382 {
2383 name: "AddTrapFlows",
2384 args: args{
2385 cntx: context.Background(),
2386 },
2387 },
2388 {
2389 name: "AddTrapFlows_ArpRelay",
2390 args: args{
2391 cntx: context.Background(),
2392 },
2393 },
2394 {
2395 name: "AddTrapFlows_PppoeIa",
2396 args: args{
2397 cntx: context.Background(),
2398 },
2399 },
2400 }
2401 for _, tt := range tests {
2402 t.Run(tt.name, func(t *testing.T) {
2403 vpv := &VoltPortVnet{
2404 DhcpRelay: true,
2405 DeleteInProgress: true,
2406 }
2407 switch tt.name {
2408 case "AddTrapFlows":
2409 vpv.AddTrapFlows(tt.args.cntx)
2410 case "AddTrapFlows_ArpRelay":
2411 vpv.DhcpRelay = false
2412 vpv.ArpRelay = true
2413 vpv.AddTrapFlows(tt.args.cntx)
2414 case "AddTrapFlows_PppoeIa":
2415 vpv.DhcpRelay = false
2416 vpv.ArpRelay = false
2417 vpv.PppoeIa = true
2418 vpv.AddTrapFlows(tt.args.cntx)
2419 }
2420 })
2421 }
2422}
2423
2424func TestVoltPortVnet_DelTrapFlows(t *testing.T) {
2425 type args struct {
2426 cntx context.Context
2427 }
2428 tests := []struct {
2429 name string
2430 args args
2431 }{
2432 {
2433 name: "DelTrapFlows",
2434 args: args{
2435 cntx: context.Background(),
2436 },
2437 },
2438 }
2439 for _, tt := range tests {
2440 t.Run(tt.name, func(t *testing.T) {
2441 vpv := &VoltPortVnet{
2442 FlowsApplied: true,
2443 DhcpRelay: true,
2444 DeleteInProgress: true,
2445 }
2446 switch tt.name {
2447 case "DelTrapFlows":
2448 vpv.DelTrapFlows(tt.args.cntx)
2449 }
2450 })
2451 }
2452}
2453
2454func TestVoltPortVnet_delDsDhcp4Flows(t *testing.T) {
2455 type args struct {
2456 cntx context.Context
2457 device *VoltDevice
2458 }
2459 voltDev := &VoltDevice{
2460 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
2461 SerialNum: "SDX6320031",
2462 NniDhcpTrapVid: 123,
2463 State: cntlr.DeviceStateUP,
2464 NniPort: "16777472",
2465 Ports: sync.Map{},
2466 FlowDelEventMap: util.NewConcurrentMap(),
2467 }
2468 va := GetApplication()
2469 devicesList := make(map[string]OperInProgress)
2470 devicesList["SDX6320031"] = opt82
2471 mvp := &MvlanProfile{
2472 Name: "mvlan_test",
2473 DevicesList: devicesList,
2474 }
2475 va.MvlanProfilesByName.Store("mvlan_test", mvp)
2476 voltPort := &VoltPort{
2477 Name: "16777472",
2478 Device: "SDX6320031",
2479 ID: 16777472,
2480 State: PortStateUp,
2481 ChannelPerSubAlarmRaised: false,
2482 }
2483 va.DevicesDisc.Store("SDX6320031", voltDev)
2484 va.PortsDisc.Store("16777472", voltPort)
2485 appMock := mocks.NewMockApp(gomock.NewController(t))
2486 controller.NewController(ctx, appMock)
2487 vc := cntlr.GetController()
2488 dev := map[string]*cntlr.Device{}
2489 portsByName := map[string]*cntlr.DevicePort{}
2490 portsByName["16777472"] = &cntlr.DevicePort{
2491 Name: "16777472",
2492 ID: 256,
2493 }
2494 device := &cntlr.Device{
2495 ID: deviceName,
2496 PortsByName: portsByName,
2497 }
2498 dev["SDX6320031"] = device
2499 vc.Devices = dev
2500 tests := []struct {
2501 name string
2502 args args
2503 wantErr bool
2504 }{
2505 {
2506 name: "delDsDhcp4Flows",
2507 args: args{
2508 cntx: context.Background(),
2509 device: voltDev,
2510 },
2511 wantErr: true,
2512 },
2513 }
2514 for _, tt := range tests {
2515 t.Run(tt.name, func(t *testing.T) {
2516 vpv := &VoltPortVnet{
2517 Device: "SDX6320031",
2518 Port: "16777472",
2519 MvlanProfileName: "mvlan_test",
2520 MacLearning: MacLearningNone,
2521 MacAddr: util.Uint32ToByte(uint32(23)),
2522 McastService: true,
2523 AllowTransparent: true,
2524 PendingDeleteFlow: make(map[string]bool),
2525 }
2526 if err := vpv.delDsDhcp4Flows(tt.args.cntx, tt.args.device); (err != nil) != tt.wantErr {
2527 t.Errorf("VoltPortVnet.delDsDhcp4Flows() error = %v, wantErr %v", err, tt.wantErr)
2528 }
2529 })
2530 }
2531}
2532
2533func TestVoltApplication_DeleteDevFlowForVlan(t *testing.T) {
2534 type args struct {
2535 cntx context.Context
2536 vnet *VoltVnet
2537 }
2538 voltDev := &VoltDevice{
2539 Name: "SDX6320031",
2540 SerialNum: "SDX6320031",
2541 NniDhcpTrapVid: 123,
2542 State: cntlr.DeviceStateUP,
2543 NniPort: "16777472",
2544 Ports: sync.Map{},
2545 FlowDelEventMap: util.NewConcurrentMap(),
2546 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
2547 }
2548 voltVnet := &VoltVnet{
2549 Version: "v3",
2550 VnetConfig: VnetConfig{
2551 Name: "2310-4096-4096",
2552 VnetType: "Encapsulation",
2553 },
2554 VnetOper: VnetOper{
2555 PendingDeviceToDelete: "SDX6320031",
2556 DeleteInProgress: true,
2557 PendingDeleteFlow: make(map[string]map[string]bool),
2558 },
2559 }
2560 voltPort := &VoltPort{
2561 Name: "16777472",
2562 Device: "SDX6320031",
2563 ID: 16777472,
2564 State: PortStateUp,
2565 ChannelPerSubAlarmRaised: false,
2566 }
2567 tests := []struct {
2568 name string
2569 args args
2570 }{
2571 {
2572 name: "DeleteDevFlowForVlan",
2573 args: args{
2574 cntx: context.Background(),
2575 vnet: voltVnet,
2576 },
2577 },
2578 {
2579 name: "DeleteDevFlowForVlan_PortStateDown",
2580 args: args{
2581 cntx: context.Background(),
2582 vnet: voltVnet,
2583 },
2584 },
2585 }
2586 for _, tt := range tests {
2587 t.Run(tt.name, func(t *testing.T) {
2588 va := &VoltApplication{}
2589 switch tt.name {
2590 case "DeleteDevFlowForVlan":
2591 va.DevicesDisc.Store("SDX6320031", voltDev)
2592 va.VnetsByName.Store("2310-4096-4096", voltVnet)
2593 voltDev.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
2594 va.PortsDisc.Store("16777472", voltPort)
2595 appMock := mocks.NewMockApp(gomock.NewController(t))
2596 cntlr.NewController(ctx, appMock)
2597 vc := cntlr.GetController()
2598 dev := map[string]*cntlr.Device{}
2599 portsByName := map[string]*cntlr.DevicePort{}
2600 portsByName["16777472"] = &cntlr.DevicePort{
2601 Name: "16777472",
2602 ID: 256,
2603 State: cntlr.PortStateUp,
2604 }
2605 device := &cntlr.Device{
2606 ID: "SDX6320031",
2607 PortsByName: portsByName,
2608 }
2609 dev["SDX6320031"] = device
2610 vc.Devices = dev
2611 va.DeleteDevFlowForVlan(tt.args.cntx, tt.args.vnet)
2612 case "DeleteDevFlowForVlan_PortStateDown":
2613 voltDev.Name = ""
2614 va.DevicesDisc.Store("SDX6320031", voltDev)
2615 va.VnetsByName.Store("2310-4096-4096", voltVnet)
2616 voltDev.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
2617 va.PortsDisc.Store("16777472", voltPort)
2618 appMock := mocks.NewMockApp(gomock.NewController(t))
2619 cntlr.NewController(ctx, appMock)
2620 vc := cntlr.GetController()
2621 dev := map[string]*cntlr.Device{}
2622 portsByName := map[string]*cntlr.DevicePort{}
2623 portsByName["16777472"] = &cntlr.DevicePort{
2624 Name: "16777472",
2625 ID: 256,
2626 State: cntlr.PortStateUp,
2627 }
2628 device := &cntlr.Device{
2629 ID: "SDX6320031",
2630 PortsByName: portsByName,
2631 }
2632 dev["SDX6320031"] = device
2633 vc.Devices = dev
2634 va.DeleteDevFlowForVlan(tt.args.cntx, tt.args.vnet)
2635 }
2636 })
2637 }
2638}