blob: a8353d65df0adcb8eeb9361bb8a27a97b87b110b [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"
25 cntlr "voltha-go-controller/internal/pkg/controller"
26 "voltha-go-controller/internal/pkg/of"
27 "voltha-go-controller/internal/pkg/util"
28 "voltha-go-controller/internal/test/mocks"
29
30 "github.com/golang/mock/gomock"
Akash Soni230e6212023-10-16 10:46:07 +053031 "github.com/google/gopacket/layers"
vinokuma04dc9f82023-07-31 15:47:49 +053032 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
vinokuma703a70b2023-07-17 10:06:43 +053033 "github.com/stretchr/testify/assert"
Akash Soni230e6212023-10-16 10:46:07 +053034 "go.uber.org/atomic"
vinokuma703a70b2023-07-17 10:06:43 +053035)
36
37func TestVoltPortVnet_JSONMarshal(t *testing.T) {
38 tests := []struct {
39 name string
40 want []byte
41 wantErr bool
42 }{
43 {
44 name: "VoltPortVnet_JSONMarshal",
45 },
46 }
47 for _, tt := range tests {
48 t.Run(tt.name, func(t *testing.T) {
49 vpv := &VoltPortVnet{}
50 _, err := vpv.JSONMarshal()
51 if (err != nil) != tt.wantErr {
52 t.Errorf("VoltPortVnet.JSONMarshal() error = %v, wantErr %v", err, tt.wantErr)
53 return
54 }
55 })
56 }
57}
58
59func TestVoltPortVnet_IsServiceActivated(t *testing.T) {
60 type args struct {
61 cntx context.Context
62 }
63 tests := []struct {
64 name string
65 args args
66 want bool
67 }{
68 {
69 name: "VoltPortVnet_IsServiceActivated",
70 args: args{
71 cntx: context.Background(),
72 },
73 },
74 }
75 for _, tt := range tests {
76 t.Run(tt.name, func(t *testing.T) {
77 vpv := &VoltPortVnet{}
78 voltServ := &VoltService{
79 VoltServiceOper: VoltServiceOper{
80 Device: test_device,
81 ForceDelete: true,
82 },
83 }
84 vpv.services.Store(test_device, voltServ)
85 if got := vpv.IsServiceActivated(tt.args.cntx); got != tt.want {
86 t.Errorf("VoltPortVnet.IsServiceActivated() = %v, want %v", got, tt.want)
87 }
88 })
89 }
90}
91
92func TestVoltVnet_JSONMarshal(t *testing.T) {
93 tests := []struct {
94 name string
95 want []byte
96 wantErr bool
97 }{
98 {
99 name: "VoltVnet_JSONMarshal",
100 },
101 }
102 for _, tt := range tests {
103 t.Run(tt.name, func(t *testing.T) {
104 vv := &VoltVnet{}
105 _, err := vv.JSONMarshal()
106 if (err != nil) != tt.wantErr {
107 t.Errorf("VoltVnet.JSONMarshal() error = %v, wantErr %v", err, tt.wantErr)
108 return
109 }
110 })
111 }
112}
113
114func TestVoltVnet_TriggerAssociatedFlowDelete(t *testing.T) {
115 type args struct {
116 cntx context.Context
117 device string
118 }
119 tests := []struct {
120 name string
121 args args
122 want bool
123 }{
124 {
125 name: "VoltVnet_TriggerAssociatedFlowDelete",
126 args: args{
127 cntx: context.Background(),
128 device: test_device,
129 },
130 want: true,
131 },
132 {
133 name: "cookieList_empty",
134 args: args{
135 cntx: context.Background(),
136 device: test_device,
137 },
138 want: false,
139 },
140 }
141 for _, tt := range tests {
142 t.Run(tt.name, func(t *testing.T) {
143 vv := &VoltVnet{}
144 switch tt.name {
145 case "VoltVnet_TriggerAssociatedFlowDelete":
146 cookie := map[string]bool{}
147 cookie["1234"] = true
148 pendingDeleteFlow := map[string]map[string]bool{}
149 pendingDeleteFlow[test_device] = cookie
150 vv.PendingDeleteFlow = pendingDeleteFlow
151 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
152 db = dbintf
153 dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
154 if got := vv.TriggerAssociatedFlowDelete(tt.args.cntx, tt.args.device); got != tt.want {
155 t.Errorf("VoltVnet.TriggerAssociatedFlowDelete() = %v, want %v", got, tt.want)
156 }
157 case "cookieList_empty":
158 if got := vv.TriggerAssociatedFlowDelete(tt.args.cntx, tt.args.device); got != tt.want {
159 t.Errorf("VoltVnet.TriggerAssociatedFlowDelete() = %v, want %v", got, tt.want)
160 }
161 }
162 })
163 }
164}
165
166func TestVoltApplication_GetMatchingMcastService(t *testing.T) {
167 type args struct {
168 port string
169 device string
170 cvlan of.VlanType
171 }
172 tests := []struct {
173 name string
174 args args
175 want *VoltService
176 }{
177 {
178 name: "VoltApplication_GetMatchingMcastService",
179 args: args{
180 port: "test_port",
181 device: test_device,
182 cvlan: of.VlanAny,
183 },
184 },
185 {
186 name: "dIntf_error",
187 args: args{
188 port: "test_port",
189 device: test_device,
190 cvlan: of.VlanAny,
191 },
192 },
193 {
194 name: "port == d.NniPort",
195 args: args{
196 port: "test_port",
197 device: test_device,
198 cvlan: of.VlanAny,
199 },
200 },
201 {
202 name: "vnets_error",
203 args: args{
204 port: "",
205 device: test_device,
206 cvlan: of.VlanAny,
207 },
208 },
209 }
210 for _, tt := range tests {
211 t.Run(tt.name, func(t *testing.T) {
212 va := &VoltApplication{}
213 switch tt.name {
214 case "VoltApplication_GetMatchingMcastService":
215 va.DevicesDisc.Store(test_device, voltDevice)
216 va.VnetsByPort.Store("test_port", voltPortVnet1)
217 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
218 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
219 }
220 case "dIntf_error":
221 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
222 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
223 }
224 case "port == d.NniPort":
225 va.DevicesDisc.Store(test_device, voltDevice)
226 voltDevice.NniPort = "test_port"
227 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
228 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
229 }
230 case "vnets_error":
231 va.DevicesDisc.Store(test_device, voltDevice)
232 va.VnetsByPort.Store("test_port1", voltPortVnet1)
233 if got := va.GetMatchingMcastService(tt.args.port, tt.args.device, tt.args.cvlan); !reflect.DeepEqual(got, tt.want) {
234 t.Errorf("VoltApplication.GetMatchingMcastService() = %v, want %v", got, tt.want)
235 }
236 }
237 })
238 }
239}
240
241func TestVoltPortVnet_IgmpFlowInstallFailure(t *testing.T) {
242 type args struct {
243 cookie string
244 errorCode uint32
245 errReason string
246 }
247 tests := []struct {
248 name string
249 args args
250 }{
251 {
252 name: "VoltPortVnet_IgmpFlowInstallFailure",
253 args: args{
254 cookie: "test_cookie",
255 errorCode: uint32(1),
256 errReason: "errReason",
257 },
258 },
259 }
260 for _, tt := range tests {
261 t.Run(tt.name, func(t *testing.T) {
262 vpv := &VoltPortVnet{}
263 switch tt.name {
264 case "VoltPortVnet_IgmpFlowInstallFailure":
265 voltService.IgmpEnabled = true
266 vpv.services.Store("test_cookie", voltService)
267 vpv.IgmpFlowInstallFailure(tt.args.cookie, tt.args.errorCode, tt.args.errReason)
268 }
269 })
270 }
271}
272
273func TestVoltVnet_FlowRemoveFailure(t *testing.T) {
274 type args struct {
275 cntx context.Context
276 cookie string
277 device string
278 errorCode uint32
279 errReason string
280 }
281 tests := []struct {
282 name string
283 args args
284 }{
285 {
286 name: "VoltVnet_FlowRemoveFailure",
287 args: args{
288 cntx: context.Background(),
289 cookie: "1234",
290 device: test_device,
291 },
292 },
293 {
294 name: "mismatch_cookie",
295 args: args{
296 cntx: context.Background(),
297 cookie: "1234",
298 device: test_device,
299 },
300 },
301 }
302 for _, tt := range tests {
303 t.Run(tt.name, func(t *testing.T) {
304 vv := &VoltVnet{}
305 switch tt.name {
306 case "VoltVnet_FlowRemoveFailure":
307 cookie := map[string]bool{}
308 cookie["1234"] = true
309 pendingDeleteFlow := map[string]map[string]bool{}
310 pendingDeleteFlow[test_device] = cookie
311 vv.PendingDeleteFlow = pendingDeleteFlow
312 vv.DeleteInProgress = true
313 vv.Name = "test_name"
314 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
315 db = dbintf
316 dbintf.EXPECT().DelVnet(tt.args.cntx, "test_name").Return(nil).Times(1)
317 vv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
318 case "mismatch_cookie":
319 cookie := map[string]bool{}
320 cookie["12345"] = true
321 pendingDeleteFlow := map[string]map[string]bool{}
322 pendingDeleteFlow[test_device] = cookie
323 vv.PendingDeleteFlow = pendingDeleteFlow
324 vv.DeleteInProgress = true
325 vv.Name = "test_name"
326 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
327 db = dbintf
328 dbintf.EXPECT().DelVnet(tt.args.cntx, "test_name").Return(nil).Times(1)
329 vv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
330 }
331 })
332 }
333}
334
335func TestVoltVnet_FlowRemoveSuccess(t *testing.T) {
336 type args struct {
337 cntx context.Context
338 cookie string
339 device string
340 }
341 tests := []struct {
342 name string
343 args args
344 }{
345 {
346 name: "VoltVnet_FlowRemoveSuccess",
347 args: args{
348 cntx: context.Background(),
349 cookie: "1234",
350 device: test_device,
351 },
352 },
353 }
354 for _, tt := range tests {
355 t.Run(tt.name, func(t *testing.T) {
356 vv := &VoltVnet{}
357 cookie := map[string]bool{}
358 cookie["1234"] = true
359 pendingDeleteFlow := map[string]map[string]bool{}
360 pendingDeleteFlow[test_device] = cookie
361 vv.PendingDeleteFlow = pendingDeleteFlow
362 ga := GetApplication()
363 voltDevice.ConfiguredVlanForDeviceFlows = util.NewConcurrentMap()
364 ga.DevicesDisc.Store(test_device, voltDevice)
365 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
366 db = dbintf
367 dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
368 vv.FlowRemoveSuccess(tt.args.cntx, tt.args.cookie, tt.args.device)
369 })
370 }
371}
372
373func TestVoltPortVnet_FlowRemoveFailure(t *testing.T) {
374 type args struct {
375 cntx context.Context
376 cookie string
377 device string
378 errorCode uint32
379 errReason string
380 }
381 tests := []struct {
382 name string
383 args args
384 }{
385 {
386 name: "VoltPortVnet_FlowRemoveFailure",
387 args: args{
388 cntx: context.Background(),
389 cookie: "1234",
390 device: test_device,
391 },
392 },
393 {
394 name: "DeleteInProgress_false",
395 args: args{
396 cntx: context.Background(),
397 cookie: "1234",
398 device: test_device,
399 },
400 },
401 }
402 for _, tt := range tests {
403 t.Run(tt.name, func(t *testing.T) {
404 vpv := &VoltPortVnet{}
405 switch tt.name {
406 case "VoltPortVnet_FlowRemoveFailure":
407 vpv.services.Store("1234", voltService)
408 vpv.DeleteInProgress = true
409 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
410 db = dbintf
411 dbintf.EXPECT().DelVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
412 vpv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
413 case "DeleteInProgress_false":
414 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
415 db = dbintf
416 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
417 vpv.FlowRemoveFailure(tt.args.cntx, tt.args.cookie, tt.args.device, tt.args.errorCode, tt.args.errReason)
418 }
419 })
420 }
421}
422
423func TestVoltPortVnet_PushFlows(t *testing.T) {
424 type args struct {
425 cntx context.Context
426 device *VoltDevice
427 flow *of.VoltFlow
428 }
429 vsf := make(map[uint64]*of.VoltSubFlow)
430 vsf[uint64(1)] = &of.VoltSubFlow{
431 Cookie: uint64(1234),
432 }
433 tests := []struct {
434 name string
435 args args
436 wantErr bool
437 }{
438 {
439 name: "VoltPortVnet_PushFlows",
440 args: args{
441 cntx: context.Background(),
442 device: voltDevice,
443 flow: &of.VoltFlow{
444 PortName: "test_port",
445 SubFlows: vsf,
446 },
447 },
448 },
449 }
450 for _, tt := range tests {
451 t.Run(tt.name, func(t *testing.T) {
452 vpv := &VoltPortVnet{}
453 _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t)))
454 err := vpv.PushFlows(tt.args.cntx, tt.args.device, tt.args.flow)
455 assert.NotNil(t, err)
456 })
457 }
458}
459
460func TestVoltPortVnet_isVlanMatching(t *testing.T) {
461 type args struct {
462 cvlan of.VlanType
463 svlan of.VlanType
464 }
465 tests := []struct {
466 name string
467 args args
468 want bool
469 }{
470 {
471 name: "VoltPortVnet_isVlanMatching",
472 args: args{
473 cvlan: of.VlanAny,
474 svlan: of.VlanAny,
475 },
476 want: true,
477 },
478 {
479 name: "vpv.VlanControl_nil",
480 args: args{
481 cvlan: of.VlanAny,
482 svlan: of.VlanAny,
483 },
484 want: true,
485 },
486 }
487 for _, tt := range tests {
488 t.Run(tt.name, func(t *testing.T) {
489 vpv := &VoltPortVnet{}
490 switch tt.name {
491 case "VoltPortVnet_isVlanMatching":
492 vpv.VlanControl = ONUCVlanOLTSVlan
493 vpv.SVlan = of.VlanAny
494 vpv.CVlan = of.VlanAny
495 if got := vpv.isVlanMatching(tt.args.cvlan, tt.args.svlan); got != tt.want {
496 t.Errorf("VoltPortVnet.isVlanMatching() = %v, want %v", got, tt.want)
497 }
498 }
499 })
500 }
501}
502
503func TestProcessIcmpv6McGroup(t *testing.T) {
504 type args struct {
505 device string
506 delete bool
507 }
508 tests := []struct {
509 name string
510 args args
511 wantErr bool
512 }{
513 {
514 name: "TestProcessIcmpv6McGroup",
515 args: args{
516 device: test_device,
517 delete: false,
518 },
519 },
520 }
521 for _, tt := range tests {
522 t.Run(tt.name, func(t *testing.T) {
523 err := ProcessIcmpv6McGroup(tt.args.device, tt.args.delete)
524 assert.NotNil(t, err)
525 })
526 }
527}
528
529func TestVoltVnet_setPbitRemarking(t *testing.T) {
530 tests := []struct {
531 name string
532 want uint32
533 }{
534 {
535 name: "VoltVnet_setPbitRemarking",
536 },
537 }
538 for _, tt := range tests {
539 t.Run(tt.name, func(t *testing.T) {
540 vv := &VoltVnet{}
541 a := make(map[of.PbitType]of.PbitType)
542 a[of.PbitMatchAll] = of.PbitMatchAll
543 vv.CtrlPktPbitRemark = a
544 if got := vv.setPbitRemarking(); got != tt.want {
545 t.Errorf("VoltVnet.setPbitRemarking() = %v, want %v", got, tt.want)
546 }
547 })
548 }
549}
550
551func TestBuildDSArpFlow(t *testing.T) {
552 type args struct {
553 inport uint32
554 vnet *VoltVnet
555 }
556 tests := []struct {
557 name string
558 args args
559 want *of.VoltFlow
560 }{
561 {
562 name: "BuildDSArpFlow",
563 args: args{
564 inport: uint32(1),
565 vnet: &VoltVnet{
566 Version: "test_version",
567 },
568 },
569 },
570 }
571 for _, tt := range tests {
572 t.Run(tt.name, func(t *testing.T) {
573 switch tt.name {
574 case "BuildDSArpFlow":
575 got := BuildDSArpFlow(tt.args.inport, tt.args.vnet)
576 assert.NotNil(t, got)
577 }
578 })
579 }
580}
581
582func TestBuildICMPv6Flow(t *testing.T) {
583 type args struct {
584 inport uint32
585 vnet *VoltVnet
586 }
587 tests := []struct {
588 name string
589 args args
590 want *of.VoltFlow
591 }{
592 {
593 name: "BuildICMPv6Flow",
594 args: args{
595 inport: uint32(1),
596 vnet: &VoltVnet{
597 Version: "test_version",
598 },
599 },
600 },
601 }
602 for _, tt := range tests {
603 t.Run(tt.name, func(t *testing.T) {
604 got := BuildICMPv6Flow(tt.args.inport, tt.args.vnet)
605 assert.NotNil(t, got)
606 })
607 }
608}
609
610func TestVoltApplication_DeleteDevFlowForVlanFromDevice(t *testing.T) {
611 type args struct {
612 cntx context.Context
613 vnet *VoltVnet
614 deviceSerialNum string
615 }
616 tests := []struct {
617 name string
618 args args
619 }{
620 {
621 name: "device.SerialNum != deviceSerialNum",
622 args: args{
623 cntx: context.Background(),
624 vnet: &VoltVnet{
625 Version: "test_version",
626 },
627 },
628 },
629 {
630 name: "VoltApplication_DeleteDevFlowForVlanFromDevice",
631 args: args{
632 cntx: context.Background(),
633 vnet: &VoltVnet{
634 Version: "test_version",
635 },
636 deviceSerialNum: "test_serial_number",
637 },
638 },
639 }
640 for _, tt := range tests {
641 t.Run(tt.name, func(t *testing.T) {
642 va := &VoltApplication{}
643 switch tt.name {
644 case "device.SerialNum != deviceSerialNum":
645 va.DevicesDisc.Store(test_device, voltDevice)
646 va.DeleteDevFlowForVlanFromDevice(tt.args.cntx, tt.args.vnet, tt.args.deviceSerialNum)
647 case "VoltApplication_DeleteDevFlowForVlanFromDevice":
648 va.DevicesDisc.Store(test_device, voltDevice)
649 va.DeleteDevFlowForVlanFromDevice(tt.args.cntx, tt.args.vnet, tt.args.deviceSerialNum)
650 }
651 })
652 }
653}
vinokuma04dc9f82023-07-31 15:47:49 +0530654
655func TestVoltApplication_RestoreVnetsFromDb(t *testing.T) {
656 type args struct {
657 cntx context.Context
658 }
659 tests := []struct {
660 name string
661 args args
662 }{
663 {
664 name: "VoltApplication_RestoreVnetsFromDb",
665 args: args{
666 cntx: context.Background(),
667 },
668 },
669 }
670 for _, tt := range tests {
671 t.Run(tt.name, func(t *testing.T) {
672 vnetsToDelete := map[string]bool{}
673 vnetsToDelete["test_name"] = true
674 va := &VoltApplication{
675 VnetsBySvlan: util.NewConcurrentMap(),
676 VnetsToDelete: vnetsToDelete,
677 }
678 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
679 db = dbintf
680 vnets := map[string]*kvstore.KVPair{}
681 voltVnet.SVlan = of.VlanAny
682 b, err := json.Marshal(voltVnet)
683 if err != nil {
684 panic(err)
685 }
686 vnets["test_device_id"] = &kvstore.KVPair{
687 Key: "test_device_id",
688 Value: b,
689 }
690 dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
691 dbintf.EXPECT().GetVnets(tt.args.cntx).Return(vnets, nil)
692 va.RestoreVnetsFromDb(tt.args.cntx)
693 })
694 }
695}
696
697func TestVoltApplication_DeleteDevFlowForDevice(t *testing.T) {
698 type args struct {
699 cntx context.Context
700 device *VoltDevice
701 }
702 tests := []struct {
703 name string
704 args args
705 }{
706 {
707 name: "VoltApplication_DeleteDevFlowForDevice",
708 args: args{
709 cntx: context.Background(),
710 device: &VoltDevice{
711 Name: test_device,
712 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
713 },
714 },
715 },
716 }
717 var voltVnet_DeleteDevFlowForDevice = &VoltVnet{
718 Version: "test_version",
719 VnetConfig: VnetConfig{
720 Name: "test_name",
721 SVlan: of.VlanAny,
722 CVlan: of.VlanAny,
723 },
724 }
725 for _, tt := range tests {
726 t.Run(tt.name, func(t *testing.T) {
727 va := &VoltApplication{}
728 va.VnetsByName.Store("4096-4096-0", voltVnet_DeleteDevFlowForDevice)
729 //tt.args.device.ConfiguredVlanForDeviceFlows.SyncMap.Store("4096-4069-0", util.NewConcurrentMap())
730 va.DeleteDevFlowForDevice(tt.args.cntx, tt.args.device)
731 })
732 }
733}
734
735func TestVoltApplication_DelVnetFromPort(t *testing.T) {
736 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
737 vpv_test := []*VoltPortVnet{
738 {
739 Device: test_device,
740 Port: "test_port",
741 MacAddr: macAdd,
742 VnetName: "test_vnet_name",
743 },
744 }
745 type args struct {
746 cntx context.Context
747 port string
748 vpv *VoltPortVnet
749 }
750 tests := []struct {
751 name string
752 args args
753 }{
754 {
755 name: "VoltApplication_DelVnetFromPort",
756 args: args{
757 cntx: context.Background(),
758 port: "test_port",
759 vpv: &VoltPortVnet{
760 Device: test_device,
761 Port: "test_port",
762 MacAddr: macAdd,
763 VnetName: "test_vnet_name",
764 },
765 },
766 },
767 }
768 for _, tt := range tests {
769 t.Run(tt.name, func(t *testing.T) {
770 va := &VoltApplication{}
771 va.VnetsByPort.Store("test_port", vpv_test)
772 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
773 db = dbintf
774 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
775 dbintf.EXPECT().DelVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
776 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
777 Version: "test_version",
778 })
779 va.DelVnetFromPort(tt.args.cntx, tt.args.port, tt.args.vpv)
780 })
781 }
782}
783
784func TestVoltApplication_PushDevFlowForVlan(t *testing.T) {
785 type args struct {
786 cntx context.Context
787 vnet *VoltVnet
788 }
789 tests := []struct {
790 name string
791 args args
792 }{
793 {
794 name: "VoltApplication_PushDevFlowForVlan",
795 args: args{
796 cntx: context.Background(),
797 vnet: &VoltVnet{
798 Version: "test_version",
799 VnetConfig: VnetConfig{
800 DevicesList: []string{"test_serialNum"},
801 SVlan: of.VlanAny,
802 },
803 },
804 },
805 },
806 }
807 for _, tt := range tests {
808 t.Run(tt.name, func(t *testing.T) {
809 va := &VoltApplication{}
810 voltDevice.SerialNum = "test_serialNum"
811 voltDevice.VlanPortStatus.Store(uint16(of.VlanAny), true)
812 voltDevice.Name = test_device
813 va.DevicesDisc.Store(test_device, voltDevice)
814 ga := GetApplication()
815 ga.DevicesDisc.Store(test_device, voltDevice)
816 _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t)))
817 va.PushDevFlowForVlan(tt.args.cntx, tt.args.vnet)
818 })
819 }
820}
821
822func TestVoltApplication_PushDevFlowForDevice(t *testing.T) {
823 type args struct {
824 cntx context.Context
825 device *VoltDevice
826 }
827 tests := []struct {
828 name string
829 args args
830 }{
831 {
832 name: "device.ConfiguredVlanForDeviceFlows is ok",
833 args: args{
834 cntx: context.Background(),
835 device: &VoltDevice{
836 Name: test_device,
837 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
838 },
839 },
840 },
841 {
842 name: "device.VlanPortStatus is false",
843 args: args{
844 cntx: context.Background(),
845 device: &VoltDevice{
846 Name: test_device,
847 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
848 NniPort: "test_nni_port",
849 },
850 },
851 },
852 {
853 name: "device.VlanPortStatus is true",
854 args: args{
855 cntx: context.Background(),
856 device: &VoltDevice{
857 Name: test_device,
858 ConfiguredVlanForDeviceFlows: util.NewConcurrentMap(),
859 NniPort: "test_nni_port",
860 VlanPortStatus: sync.Map{},
861 },
862 },
863 },
864 }
865 for _, tt := range tests {
866 t.Run(tt.name, func(t *testing.T) {
867 va := &VoltApplication{}
868 switch tt.name {
869 case "device.ConfiguredVlanForDeviceFlows is ok":
870 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
871 Version: "test_version",
872 })
873 tt.args.device.ConfiguredVlanForDeviceFlows.Set("0-0-0", util.NewConcurrentMap())
874 va.PushDevFlowForDevice(tt.args.cntx, tt.args.device)
875 case "device.VlanPortStatus is false":
876 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
877 Version: "test_version",
878 })
879 va.PortsDisc.Store("test_nni_port", &VoltPort{
880 Name: "test_name",
881 })
882 va.PushDevFlowForDevice(tt.args.cntx, tt.args.device)
883 case "device.VlanPortStatus is true":
884 va.VnetsByName.Store("test_vnet_name", &VoltVnet{
885 Version: "test_version",
886 VnetConfig: VnetConfig{
887 SVlan: of.VlanAny,
888 },
889 })
890 va.PortsDisc.Store("test_nni_port", &VoltPort{
891 Name: "test_name",
892 })
893 tt.args.device.VlanPortStatus.Store(uint16(of.VlanAny), true)
894 va.PushDevFlowForDevice(tt.args.cntx, tt.args.device)
895 }
896 })
897 }
898}
Akash Soni230e6212023-10-16 10:46:07 +0530899
900func TestNewVoltPortVnet(t *testing.T) {
901 type args struct {
902 vnet *VoltVnet
903 }
904 usDhcpPbit := []of.PbitType{}
905 usDhcpPbit = append(usDhcpPbit, PbitMatchNone)
906 tests := []struct {
907 name string
908 args args
909 want *VoltPortVnet
910 }{
911 {
912 name: "NewVoltPortVnet",
913 args: args{
914 vnet: &VoltVnet{
915 VnetConfig: VnetConfig{
916 UsDhcpPbit: usDhcpPbit,
917 },
918 },
919 },
920 want: &VoltPortVnet{},
921 },
922 }
923 for _, tt := range tests {
924 t.Run(tt.name, func(t *testing.T) {
925 got := NewVoltPortVnet(tt.args.vnet)
926 assert.NotNil(t, got)
927 })
928 }
929}
930
931func TestVoltPortVnet_GetCircuitID(t *testing.T) {
932 vpv := &VoltPortVnet{}
933 got := vpv.GetCircuitID()
934 assert.Nil(t, got)
935 got1 := vpv.GetRemoteID()
936 assert.Nil(t, got1)
937 got3 := vpv.GetDhcpState()
938 assert.NotNil(t, got3)
939 got4 := vpv.GetPppoeIaState()
940 assert.NotNil(t, got4)
941 got5 := vpv.GetDhcpv6State()
942 assert.NotNil(t, got5)
943}
944
945func TestVoltPortVnet_GetNniVlans(t *testing.T) {
946 tests := []struct {
947 name string
948 want uint16
949 want1 uint16
950 }{
951 {
952 name: "GetNniVlans",
953 want: uint16(of.VlanAny),
954 want1: uint16(of.VlanAny),
955 },
956 {
957 name: "GetNniVlans_OLTSVlan",
958 want: uint16(of.VlanAny),
959 want1: uint16(of.VlanNone),
960 },
961 {
962 name: "GetNniVlans_Default",
963 want: uint16(of.VlanNone),
964 want1: uint16(of.VlanNone),
965 },
966 }
967 for _, tt := range tests {
968 t.Run(tt.name, func(t *testing.T) {
969 vpv := &VoltPortVnet{
970 VlanControl: ONUCVlanOLTSVlan,
971 SVlan: of.VlanAny,
972 CVlan: of.VlanAny,
973 }
974 switch tt.name {
975 case "GetNniVlans":
976 got, got1 := vpv.GetNniVlans()
977 if got != tt.want {
978 t.Errorf("VoltPortVnet.GetNniVlans() got = %v, want %v", got, tt.want)
979 }
980 if got1 != tt.want1 {
981 t.Errorf("VoltPortVnet.GetNniVlans() got1 = %v, want %v", got1, tt.want1)
982 }
983 case "GetNniVlans_OLTSVlan":
984 vpv.VlanControl = OLTSVlan
985 got, got1 := vpv.GetNniVlans()
986 if got != tt.want {
987 t.Errorf("VoltPortVnet.GetNniVlans() got = %v, want %v", got, tt.want)
988 }
989 if got1 != tt.want1 {
990 t.Errorf("VoltPortVnet.GetNniVlans() got1 = %v, want %v", got1, tt.want1)
991 }
992 case "GetNniVlans_Default":
993 vpv.VlanControl = opt82
994 got, got1 := vpv.GetNniVlans()
995 if got != tt.want {
996 t.Errorf("VoltPortVnet.GetNniVlans() got = %v, want %v", got, tt.want)
997 }
998 if got1 != tt.want1 {
999 t.Errorf("VoltPortVnet.GetNniVlans() got1 = %v, want %v", got1, tt.want1)
1000 }
1001 }
1002 })
1003 }
1004}
1005
1006func TestVoltPortVnet_GetService(t *testing.T) {
1007 type args struct {
1008 name string
1009 }
1010 voltServ := &VoltService{
1011 VoltServiceOper: VoltServiceOper{
1012 Device: "SDX6320031",
1013 },
1014 VoltServiceCfg: VoltServiceCfg{
1015 IsActivated: true,
1016 },
1017 }
1018 tests := []struct {
1019 name string
1020 args args
1021 want *VoltService
1022 want1 bool
1023 }{
1024 {
1025 name: "GetService",
1026 args: args{
1027 name: "SDX6320031-1_SDX6320031-1-4096-2310-4096-65",
1028 },
1029 want: voltServ,
1030 want1: true,
1031 },
1032 }
1033 for _, tt := range tests {
1034 t.Run(tt.name, func(t *testing.T) {
1035 vpv := &VoltPortVnet{}
1036 vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
1037 got, got1 := vpv.GetService(tt.args.name)
1038 if !reflect.DeepEqual(got, tt.want) {
1039 t.Errorf("VoltPortVnet.GetService() got = %v, want %v", got, tt.want)
1040 }
1041 if got1 != tt.want1 {
1042 t.Errorf("VoltPortVnet.GetService() got1 = %v, want %v", got1, tt.want1)
1043 }
1044 })
1045 }
1046}
1047
1048func TestVoltPortVnet_ProcessDhcpSuccess(t *testing.T) {
1049 type args struct {
1050 cntx context.Context
1051 res *layers.DHCPv4
1052 }
1053 tests := []struct {
1054 name string
1055 args args
1056 }{
1057 {
1058 name: "ProcessDhcpSuccess",
1059 args: args{
1060 cntx: context.Background(),
1061 res: &layers.DHCPv4{},
1062 },
1063 },
1064 }
1065 for _, tt := range tests {
1066 t.Run(tt.name, func(t *testing.T) {
1067 vpv := &VoltPortVnet{
1068 servicesCount: atomic.NewUint64(0),
1069 }
1070 vpv.ProcessDhcpSuccess(tt.args.cntx, tt.args.res)
1071 })
1072 }
1073}
1074
1075func TestVoltPortVnet_ProcessDhcpResult(t *testing.T) {
1076 type args struct {
1077 cntx context.Context
1078 res *layers.DHCPv4
1079 }
1080 tests := []struct {
1081 name string
1082 args args
1083 }{
1084 {
1085 name: "ProcessDhcpResult",
1086 args: args{
1087 cntx: context.Background(),
1088 res: &layers.DHCPv4{},
1089 },
1090 },
1091 }
1092 for _, tt := range tests {
1093 t.Run(tt.name, func(t *testing.T) {
1094 vpv := &VoltPortVnet{}
1095 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1096 db = dbintf
1097 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
1098 vpv.ProcessDhcpResult(tt.args.cntx, tt.args.res)
1099 })
1100 }
1101}
1102
1103func TestVoltVnet_associatePortToVnet(t *testing.T) {
1104 type args struct {
1105 port string
1106 }
1107 tests := []struct {
1108 name string
1109 args args
1110 }{
1111 {
1112 name: "ProcessDhcpResult",
1113 args: args{
1114 port: "SDX6320031-1",
1115 },
1116 },
1117 }
1118 for _, tt := range tests {
1119 t.Run(tt.name, func(t *testing.T) {
1120 vv := &VoltVnet{}
1121 vv.associatePortToVnet(tt.args.port)
1122 })
1123 }
1124}
1125
1126func TestVoltPortVnet_ProcessDhcpv6Result(t *testing.T) {
1127 type args struct {
1128 cntx context.Context
1129 ipv6Addr net.IP
1130 leaseTime uint32
1131 }
1132 tests := []struct {
1133 name string
1134 args args
1135 }{
1136 {
1137 name: "ProcessDhcpResult",
1138 args: args{
1139 cntx: context.Background(),
1140 ipv6Addr: AllSystemsMulticastGroupIP,
1141 leaseTime: uint32(128),
1142 },
1143 },
1144 }
1145 for _, tt := range tests {
1146 t.Run(tt.name, func(t *testing.T) {
1147 vpv := &VoltPortVnet{}
1148 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1149 db = dbintf
1150 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
1151 vpv.ProcessDhcpv6Result(tt.args.cntx, tt.args.ipv6Addr, tt.args.leaseTime)
1152 })
1153 }
1154}
1155
1156func TestAddSvcUsMeterToDevice(t *testing.T) {
1157 type args struct {
1158 cntx context.Context
1159 key interface{}
1160 value interface{}
1161 flag bool
1162 }
1163 vpv := &VoltApplication{}
1164 voltServ := &VoltService{
1165 VoltServiceOper: VoltServiceOper{
1166 Device: test_device,
1167 ForceDelete: true,
1168 },
1169 }
1170 vpv.ServiceByName.Store(test_device, voltServ)
1171 tests := []struct {
1172 name string
1173 args args
1174 want bool
1175 }{
1176 {
1177 name: "ProcessDhcpResult",
1178 args: args{
1179 cntx: context.Background(),
1180 key: test_device,
1181 value: voltServ,
1182 },
1183 },
1184 }
1185 for _, tt := range tests {
1186 t.Run(tt.name, func(t *testing.T) {
1187 if got := AddSvcUsMeterToDevice(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag); got != tt.want {
1188 t.Errorf("AddSvcUsMeterToDevice() = %v, want %v", got, tt.want)
1189 }
1190 })
1191 }
1192}
1193
1194func TestClearFlagsInService(t *testing.T) {
1195 type args struct {
1196 cntx context.Context
1197 key interface{}
1198 value interface{}
1199 flag bool
1200 }
1201 vpv := &VoltPortVnet{}
1202 voltServ := &VoltService{
1203 VoltServiceOper: VoltServiceOper{
1204 Device: "SDX6320031",
1205 },
1206 VoltServiceCfg: VoltServiceCfg{
1207 IsActivated: true,
1208 },
1209 }
1210 vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
1211 tests := []struct {
1212 name string
1213 args args
1214 want bool
1215 }{
1216 {
1217 name: "ClearFlagsInService",
1218 args: args{
1219 cntx: context.Background(),
1220 key: test_device,
1221 value: voltServ,
1222 },
1223 },
1224 }
1225 for _, tt := range tests {
1226 t.Run(tt.name, func(t *testing.T) {
1227 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1228 db = dbintf
1229 dbintf.EXPECT().PutService(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
1230 got := ClearFlagsInService(tt.args.cntx, tt.args.key, tt.args.value, tt.args.flag)
1231 assert.NotNil(t, got)
1232 })
1233 }
1234}
1235
1236func TestVoltPortVnet_DelDhcpFlows(t *testing.T) {
1237 type args struct {
1238 cntx context.Context
1239 }
1240 tests := []struct {
1241 name string
1242 args args
1243 }{
1244 {
1245 name: "DelDhcpFlows",
1246 args: args{
1247 cntx: context.Background(),
1248 },
1249 },
1250 }
1251 for _, tt := range tests {
1252 t.Run(tt.name, func(t *testing.T) {
1253 vpv := &VoltPortVnet{}
1254 vpv.DelDhcpFlows(tt.args.cntx)
1255 })
1256 }
1257}
1258
1259func TestVoltPortVnet_AddDsDhcpFlows(t *testing.T) {
1260 type args struct {
1261 cntx context.Context
1262 }
1263 va := GetApplication()
1264 voltDev := &VoltDevice{
1265 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
1266 SerialNum: "SDX6320031",
1267 NniDhcpTrapVid: 123,
1268 State: cntlr.DeviceStateUP,
1269 FlowAddEventMap: util.NewConcurrentMap(),
1270 }
1271 va.DevicesDisc.Store("SDX6320031", voltDev)
1272 voltPort := &VoltPort{
1273 Name: "49686e2d-618f-4e8e-bca0-442ab850a63a",
1274 Device: "SDX6320031",
1275 ID: 16777472,
1276 State: PortStateDown,
1277 ChannelPerSubAlarmRaised: false,
1278 Type: VoltPortTypeNni,
1279 }
1280 tests := []struct {
1281 name string
1282 args args
1283 wantErr bool
1284 }{
1285 {
1286 name: "AddDsDhcpFlows",
1287 args: args{
1288 cntx: context.Background(),
1289 },
1290 },
1291 {
1292 name: "AddDsDhcpFlows_DeviceNotFound",
1293 args: args{
1294 cntx: context.Background(),
1295 },
1296 wantErr: true,
1297 },
1298 {
1299 name: "AddDsDhcpFlows_StateDown",
1300 args: args{
1301 cntx: context.Background(),
1302 },
1303 },
1304 {
1305 name: "AddDsDhcpFlows_GlobalDhcpFlowAdded",
1306 args: args{
1307 cntx: context.Background(),
1308 },
1309 },
1310 {
1311 name: "AddDsDhcpFlows_PositiveSenario",
1312 args: args{
1313 cntx: context.Background(),
1314 },
1315 },
1316 }
1317 for _, tt := range tests {
1318 t.Run(tt.name, func(t *testing.T) {
1319 vpv := &VoltPortVnet{
1320 Device: "SDX6320031",
1321 }
1322 switch tt.name {
1323 case "AddDsDhcpFlows":
1324 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1325 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1326 }
1327 case "AddDsDhcpFlows_DeviceNotFound":
1328 vpv.Device = ""
1329 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1330 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1331 }
1332 case "AddDsDhcpFlows_StateDown":
1333 voltDev.State = cntlr.DeviceStateDOWN
1334 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1335 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1336 }
1337 case "AddDsDhcpFlows_GlobalDhcpFlowAdded":
1338 vpv.Device = "SDX6320031"
1339 voltDev.State = cntlr.DeviceStateUP
1340 voltDev.GlobalDhcpFlowAdded = true
1341 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1342 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1343 }
1344 case "AddDsDhcpFlows_PositiveSenario":
1345 vpv.Device = "SDX6320031"
1346 voltDev.State = cntlr.DeviceStateUP
1347 voltDev.GlobalDhcpFlowAdded = false
1348 voltDev.NniPort = "16777472"
1349 va.PortsDisc.Store("16777472", voltPort)
1350 appMock := mocks.NewMockApp(gomock.NewController(t))
1351 cntlr.NewController(ctx, appMock)
1352 vc := cntlr.GetController()
1353 device := &cntlr.Device{
1354 ID: "SDX6320031",
1355 }
1356 dev := map[string]*cntlr.Device{}
1357 dev["SDX6320031"] = device
1358 vc.Devices = dev
1359 if err := vpv.AddDsDhcpFlows(tt.args.cntx); (err != nil) != tt.wantErr {
1360 t.Errorf("VoltPortVnet.AddDsDhcpFlows() error = %v, wantErr %v", err, tt.wantErr)
1361 }
1362 }
1363 })
1364 }
1365}