blob: 235bffe9198a8b438c07b864229316b6bbfcf21a [file] [log] [blame]
Akash Sonid36d23b2023-08-18 12:51:40 +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 controller
17
18import (
19 "context"
20 "reflect"
21 "sync"
22 "testing"
23 "voltha-go-controller/internal/pkg/intf"
24 "voltha-go-controller/internal/pkg/of"
25 "voltha-go-controller/internal/pkg/tasks"
Akash Soni6f369452023-09-19 11:18:28 +053026 "voltha-go-controller/internal/pkg/util"
Akash Sonid36d23b2023-08-18 12:51:40 +053027 "voltha-go-controller/internal/pkg/vpagent"
28 "voltha-go-controller/internal/test/mocks"
29
30 "github.com/golang/mock/gomock"
31 "github.com/stretchr/testify/assert"
32)
33
34func TestNewController(t *testing.T) {
35 type args struct {
36 ctx context.Context
37 app intf.App
38 }
39 appMock := mocks.NewMockApp(gomock.NewController(t))
40 app := NewController(ctx, appMock)
41 tests := []struct {
42 name string
43 args args
44 want intf.IVPClientAgent
45 }{
46 {
47 name: "TestNewController",
48 args: args{
49 ctx: context.Background(),
50 app: GetController().app,
51 },
52 want: app,
53 },
54 }
55 for _, tt := range tests {
56 t.Run(tt.name, func(t *testing.T) {
57 if got := NewController(tt.args.ctx, tt.args.app); !reflect.DeepEqual(got, tt.want) {
58 t.Errorf("NewController() = %v, want %v", got, tt.want)
59 }
60 })
61 }
62}
63
64func Cancel() {}
65func TestVoltController_DelDevice(t *testing.T) {
66 type args struct {
67 cntx context.Context
68 id string
69 }
70
71 device := &Device{
72 ID: "SDX6320031",
73 cancel: Cancel,
74 }
75 dev := map[string]*Device{}
76 dev["SDX6320031"] = device
77 appMock := mocks.NewMockApp(gomock.NewController(t))
78 NewController(ctx, appMock)
79 appMock.EXPECT().DelDevice(gomock.Any(), gomock.Any()).AnyTimes()
80 tests := []struct {
81 name string
82 args args
83 }{
84 {
85 name: "DelDevice",
86 args: args{
87 cntx: context.Background(),
88 id: "SDX6320031",
89 },
90 },
91 }
92 for _, tt := range tests {
93 t.Run(tt.name, func(t *testing.T) {
94 v := &VoltController{
95 devices: dev,
96 app: GetController().app,
97 }
98 v.DelDevice(tt.args.cntx, tt.args.id)
99 })
100 }
101}
102
103func TestVoltController_AddFlows(t *testing.T) {
104 type args struct {
105 cntx context.Context
106 port string
107 device string
108 flow *of.VoltFlow
109 }
110 subFlows := map[uint64]*of.VoltSubFlow{}
111 vltSubFlow := &of.VoltSubFlow{
112 Priority: 100,
113 Cookie: 103112802816,
114 State: of.FlowAddSuccess,
115 Match: of.Match{
116 InPort: 1573376,
117 MatchVlan: 4096,
118 L4Protocol: 255,
119 },
120 Action: of.Action{
121 Metadata: 279189651712,
122 GoToTableID: 1,
123 MeterID: 1,
124 SetVlan: 4097,
125 Pcp: 8,
126 Output: 4,
127 },
128 }
129 subFlows[0] = vltSubFlow
130 portsByName := map[string]*DevicePort{}
131 portsByName["SDX6320031-1"] = &DevicePort{
132 Name: "SDX6320031-1",
133 ID: 256,
134 }
135 device := &Device{
136 ctx: context.Background(),
137 ID: "SDX6320031",
138 flows: subFlows,
139 PortsByName: portsByName,
140 }
141 dev := map[string]*Device{}
142 dev["SDX6320031"] = device
143 flow := &of.VoltFlow{
144 PortName: "SDX6320031-1",
145 PortID: 256,
146 Command: 0,
147 MigrateCookie: true,
148 }
149 tests := []struct {
150 name string
151 args args
152 wantErr bool
153 }{
154 {
155 name: "AddFlows",
156 args: args{
157 cntx: context.Background(),
158 port: "SDX6320031-1",
159 device: "SDX6320031",
160 flow: flow,
161 },
162 },
163 }
164 for _, tt := range tests {
165 t.Run(tt.name, func(t *testing.T) {
166 v := &VoltController{
167 devices: dev,
168 }
169 if err := v.AddFlows(tt.args.cntx, tt.args.port, tt.args.device, tt.args.flow); (err != nil) != tt.wantErr {
170 t.Errorf("VoltController.AddFlows() error = %v, wantErr %v", err, tt.wantErr)
171 }
172 })
173 }
174}
175
176func TestVoltController_DelFlows(t *testing.T) {
177 type args struct {
178 cntx context.Context
179 port string
180 device string
181 flow *of.VoltFlow
182 }
183 subFlows := map[uint64]*of.VoltSubFlow{}
184 vltSubFlow := &of.VoltSubFlow{
185 Priority: 100,
186 Cookie: 103112802816,
187 State: of.FlowAddSuccess,
188 Match: of.Match{
189 InPort: 1573376,
190 MatchVlan: 4096,
191 L4Protocol: 255,
192 },
193 Action: of.Action{
194 Metadata: 279189651712,
195 GoToTableID: 1,
196 MeterID: 1,
197 SetVlan: 4097,
198 Pcp: 8,
199 Output: 4,
200 },
201 }
202 subFlows[0] = vltSubFlow
203 portsByName := map[string]*DevicePort{}
204 portsByName["SDX6320031-1"] = &DevicePort{
205 Name: "SDX6320031-1",
206 ID: 256,
207 }
208 device := &Device{
209 ctx: context.Background(),
210 ID: "SDX6320031",
211 flows: subFlows,
212 PortsByName: portsByName,
213 }
214 dev := map[string]*Device{}
215 dev["SDX6320031"] = device
216 flow := &of.VoltFlow{
217 PortName: "SDX6320031-1",
218 PortID: 256,
219 Command: 0,
220 MigrateCookie: true,
221 }
222 tests := []struct {
223 name string
224 args args
225 wantErr bool
226 }{
227 {
228 name: "DelFlows",
229 args: args{
230 cntx: context.Background(),
231 port: "SDX6320031-1",
232 device: "SDX6320031",
233 flow: flow,
234 },
235 },
236 }
237 for _, tt := range tests {
238 t.Run(tt.name, func(t *testing.T) {
239 v := &VoltController{
240 devices: dev,
241 }
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530242 if err := v.DelFlows(tt.args.cntx, tt.args.port, tt.args.device, tt.args.flow, false); (err != nil) != tt.wantErr {
Akash Sonid36d23b2023-08-18 12:51:40 +0530243 t.Errorf("VoltController.DelFlows() error = %v, wantErr %v", err, tt.wantErr)
244 }
245 })
246 }
247}
248
249func TestVoltController_GetGroups(t *testing.T) {
250 type args struct {
251 cntx context.Context
252 id uint32
253 }
254 device := &Device{
255 ctx: context.Background(),
256 ID: "SDX6320031",
257 groups: sync.Map{},
258 }
259 grp := &of.Group{
260 Device: "SDX6320031",
261 GroupID: uint32(256),
262 State: 1,
263 SetVlan: of.VlanAny,
264 }
265 dev := map[string]*Device{}
266 dev["SDX6320031"] = device
267 tests := []struct {
268 name string
269 args args
270 want *of.Group
271 wantErr bool
272 }{
273 {
274 name: "VoltController_GetGroups",
275 args: args{
276 cntx: context.Background(),
277 id: uint32(256),
278 },
279 want: grp,
280 wantErr: false,
281 },
282 {
283 name: "GetGroups_Not-Found",
284 args: args{
285 cntx: context.Background(),
286 id: 1,
287 },
288 want: nil,
289 wantErr: true,
290 },
291 }
292
293 for _, tt := range tests {
294 t.Run(tt.name, func(t *testing.T) {
295 v := &VoltController{
296 devices: dev,
297 }
298 switch tt.name {
299 case "VoltController_GetGroups":
300 device.groups.Store(uint32(256), grp)
301 got, err := v.GetGroups(tt.args.cntx, tt.args.id)
302 if (err != nil) != tt.wantErr {
303 t.Errorf("VoltController.GetGroups() error = %v, wantErr %v", err, tt.wantErr)
304 return
305 }
306 if !reflect.DeepEqual(got, tt.want) {
307 t.Errorf("VoltController.GetGroups() = %v, want %v", got, tt.want)
308 }
309 case "GetGroups_Not-Found":
310 got, err := v.GetGroups(tt.args.cntx, tt.args.id)
311 if (err != nil) != tt.wantErr {
312 t.Errorf("VoltController.GetGroups() error = %v, wantErr %v", err, tt.wantErr)
313 return
314 }
315 if !reflect.DeepEqual(got, tt.want) {
316 t.Errorf("VoltController.GetGroups() = %v, want %v", got, tt.want)
317 }
318 }
319 })
320 }
321}
322
323func TestVoltController_GetGroupList(t *testing.T) {
324 device := &Device{
325 ctx: context.Background(),
326 ID: "SDX6320031",
327 groups: sync.Map{},
328 }
329 grpList := []*of.Group{}
330 grp := &of.Group{
331 Device: "SDX6320031",
332 GroupID: uint32(256),
333 State: 1,
334 SetVlan: of.VlanAny,
335 }
336 grpList = append(grpList, grp)
337 dev := map[string]*Device{}
338 dev["SDX6320031"] = device
339 tests := []struct {
340 name string
341 want []*of.Group
342 wantErr bool
343 }{
344 {
345 name: "VoltController_GetGroups",
346 want: grpList,
347 wantErr: false,
348 },
349 }
350 for _, tt := range tests {
351 t.Run(tt.name, func(t *testing.T) {
352 v := &VoltController{
353 devices: dev,
354 }
355 device.groups.Store(uint32(256), grp)
356 got, err := v.GetGroupList()
357 if (err != nil) != tt.wantErr {
358 t.Errorf("VoltController.GetGroupList() error = %v, wantErr %v", err, tt.wantErr)
359 return
360 }
361 if !reflect.DeepEqual(got, tt.want) {
362 t.Errorf("VoltController.GetGroupList() = %v, want %v", got, tt.want)
363 }
364 })
365 }
366}
367
368func TestVoltController_GetMeterInfo(t *testing.T) {
369 type args struct {
370 cntx context.Context
371 id uint32
372 }
373 mtrs := &of.Meter{
374 ID: uint32(256),
375 State: 1,
376 }
377 mtr := map[string]*of.Meter{}
378 mtr["SDX6320031"] = mtrs
379 devMtr := map[uint32]*of.Meter{}
380 devMtr[uint32(256)] = mtrs
381 device := &Device{
382 ctx: context.Background(),
383 ID: "SDX6320031",
384 meters: devMtr,
385 }
386 dev := map[string]*Device{}
387 dev["SDX6320031"] = device
388 tests := []struct {
389 name string
390 args args
391 want map[string]*of.Meter
392 wantErr bool
393 }{
394 {
395 name: "VoltController_GetMeterInfo",
396 args: args{
397 cntx: context.Background(),
398 id: uint32(256),
399 },
400 want: mtr,
401 wantErr: false,
402 },
403 {
404 name: "Not_Found_Error",
405 args: args{
406 cntx: context.Background(),
407 id: 1,
408 },
409 want: nil,
410 wantErr: true,
411 },
412 }
413 for _, tt := range tests {
414 t.Run(tt.name, func(t *testing.T) {
415 v := &VoltController{
416 devices: dev,
417 }
418 switch tt.name {
419 case "VoltController_GetMeterInfo":
420 got, err := v.GetMeterInfo(tt.args.cntx, tt.args.id)
421 if (err != nil) != tt.wantErr {
422 t.Errorf("VoltController.GetMeterInfo() error = %v, wantErr %v", err, tt.wantErr)
423 return
424 }
425 if !reflect.DeepEqual(got, tt.want) {
426 t.Errorf("VoltController.GetMeterInfo() = %v, want %v", got, tt.want)
427 }
428 case "Not_Found_Error":
429 got, err := v.GetMeterInfo(tt.args.cntx, tt.args.id)
430 if (err != nil) != tt.wantErr {
431 t.Errorf("VoltController.GetMeterInfo() error = %v, wantErr %v", err, tt.wantErr)
432 return
433 }
434 if !reflect.DeepEqual(got, tt.want) {
435 t.Errorf("VoltController.GetMeterInfo() = %v, want %v", got, tt.want)
436 }
437 }
438 })
439 }
440}
441
442func TestVoltController_GetAllMeterInfo(t *testing.T) {
443 vltMtr := map[string][]*of.Meter{}
444 mtr := &of.Meter{
445 ID: uint32(256),
446 State: 1,
447 }
448 mtrs := []*of.Meter{}
449 mtrs = append(mtrs, mtr)
450 vltMtr["SDX6320031"] = mtrs
451 devMtr := map[uint32]*of.Meter{}
452 devMtr[uint32(256)] = mtr
453 device := &Device{
454 ctx: context.Background(),
455 ID: "SDX6320031",
456 meters: devMtr,
457 }
458 dev := map[string]*Device{}
459 dev["SDX6320031"] = device
460 tests := []struct {
461 name string
462 want map[string][]*of.Meter
463 wantErr bool
464 }{
465 {
466 name: "VoltController_GetMeterInfo",
467 want: vltMtr,
468 wantErr: false,
469 },
470 }
471 for _, tt := range tests {
472 t.Run(tt.name, func(t *testing.T) {
473 v := &VoltController{
474 devices: dev,
475 }
476 got, err := v.GetAllMeterInfo()
477 if (err != nil) != tt.wantErr {
478 t.Errorf("VoltController.GetAllMeterInfo() error = %v, wantErr %v", err, tt.wantErr)
479 return
480 }
481 if !reflect.DeepEqual(got, tt.want) {
482 t.Errorf("VoltController.GetAllMeterInfo() = %v, want %v", got, tt.want)
483 }
484 })
485 }
486}
487
488func TestVoltController_GetAllPendingFlows(t *testing.T) {
489 subFlowList := []*of.VoltSubFlow{}
490 vltSubFlow := &of.VoltSubFlow{
491 Priority: 100,
492 Cookie: 103112802816,
493 State: of.FlowAddSuccess,
494 Match: of.Match{
495 InPort: 1573376,
496 MatchVlan: 4096,
497 L4Protocol: 255,
498 },
499 Action: of.Action{
500 Metadata: 279189651712,
501 GoToTableID: 1,
502 MeterID: 1,
503 SetVlan: 4097,
504 Pcp: 8,
505 Output: 4,
506 },
507 }
508 subFlowList = append(subFlowList, vltSubFlow)
509 subFlows := map[uint64]*of.VoltSubFlow{}
510 subFlows[0] = vltSubFlow
511 device := &Device{
512 ctx: context.Background(),
513 ID: "SDX6320031",
514 flows: subFlows,
515 }
516 dev := map[string]*Device{}
517 dev["SDX6320031"] = device
518 tests := []struct {
519 name string
520 want []*of.VoltSubFlow
521 wantErr bool
522 }{
523 {
524 name: "GetAllPendingFlows",
525 want: subFlowList,
526 wantErr: false,
527 },
528 }
529 type args1 struct {
530 deviceId string
531 }
532 tests1 := []struct {
533 name string
534 args args1
535 want []*of.VoltSubFlow
536 wantErr bool
537 }{
538 {
539 name: "GetFlows_with_DeviceID",
540 args: args1{
541 deviceId: "SDX6320031",
542 },
543 want: subFlowList,
544 wantErr: false,
545 },
546 {
547 name: "GetFlows_with_DeviceID_NOT_FOUND",
548 args: args1{
549 deviceId: "",
550 },
551 want: subFlowList,
552 wantErr: false,
553 },
554 }
555 type args2 struct {
556 deviceId string
557 cookie uint64
558 }
559 tests2 := []struct {
560 name string
561 args args2
562 want []*of.VoltSubFlow
563 wantErr bool
564 }{
565 {
566 name: "GetFlow_with_DeviceID_and_cookie",
567 args: args2{
568 deviceId: "SDX6320031",
569 cookie: 103112802816,
570 },
571 want: subFlowList,
572 wantErr: false,
573 },
574 {
575 name: "GetFlow_with_DeviceID_and_cookie_NOT_FOUND",
576 args: args2{
577 deviceId: "",
578 },
579 want: subFlowList,
580 wantErr: true,
581 },
582 }
583 for _, tt := range tests {
584 t.Run(tt.name, func(t *testing.T) {
585 v := &VoltController{
586 devices: dev,
587 }
588 got, err := v.GetAllPendingFlows()
589 if (err != nil) != tt.wantErr {
590 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
591 return
592 }
593 assert.Nil(t, got)
594 got1, err1 := v.GetAllFlows()
595 if (err1 != nil) != tt.wantErr {
596 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
597 return
598 }
599 assert.NotNil(t, got1)
600 })
601 }
602 for _, tt := range tests1 {
603 t.Run(tt.name, func(t *testing.T) {
604 v := &VoltController{
605 devices: dev,
606 }
607 switch tt.name {
608 case "GetFlows_with_DeviceID":
609 got, err := v.GetFlows(tt.args.deviceId)
610 if (err != nil) != tt.wantErr {
611 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
612 return
613 }
614 assert.NotNil(t, got)
615 case "GetFlows_with_DeviceID_NOT_FOUND":
616 got, err := v.GetFlows(tt.args.deviceId)
617 if (err != nil) != tt.wantErr {
618 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
619 return
620 }
621 assert.Nil(t, got)
622 }
623 })
624 }
625 for _, tt := range tests2 {
626 t.Run(tt.name, func(t *testing.T) {
627 v := &VoltController{
628 devices: dev,
629 }
630 switch tt.name {
631 case "GetFlow_with_DeviceID_and_cookie":
632 got, err := v.GetFlow(tt.args.deviceId, tt.args.cookie)
633 if (err != nil) != tt.wantErr {
634 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
635 return
636 }
637 assert.Nil(t, got)
638 case "GetFlow_with_DeviceID_and_cookie_NOT_FOUND":
639 got, err := v.GetFlow(tt.args.deviceId, tt.args.cookie)
640 if (err != nil) != tt.wantErr {
641 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
642 return
643 }
644 assert.Nil(t, got)
645 }
646 })
647 }
648}
649
650func TestVoltController_GetTaskList(t *testing.T) {
651 type args struct {
652 device string
653 }
654 device := &Device{
655 ctx: context.Background(),
656 ID: "SDX6320031",
657 }
658 dev := map[string]*Device{}
659 dev["SDX6320031"] = device
660 tests := []struct {
661 name string
662 args args
663 want []tasks.Task
664 }{
665 {
666 name: "GetTaskList",
667 args: args{
668 device: "SDX6320031",
669 },
670 want: []tasks.Task{},
671 },
672 {
673 name: "GetTaskList_Device_Not_found",
674 args: args{
675 device: "SDX632003",
676 },
677 want: []tasks.Task{},
678 },
679 }
680 for _, tt := range tests {
681 t.Run(tt.name, func(t *testing.T) {
682 v := &VoltController{
683 devices: dev,
684 }
685 switch tt.name {
686 case "GetTaskList":
687 if got := v.GetTaskList(tt.args.device); !reflect.DeepEqual(got, tt.want) {
688 t.Errorf("VoltController.GetTaskList() = %v, want %v", got, tt.want)
689 }
690 case "GetTaskList_Device_Not_found":
691 if got := v.GetTaskList(tt.args.device); !reflect.DeepEqual(got, tt.want) {
692 t.Errorf("VoltController.GetTaskList() = %v, want %v", got, tt.want)
693 }
694 }
695 })
696 }
697}
698
699func TestVoltController_GetPortState(t *testing.T) {
700 type args struct {
701 device string
702 name string
703 }
704 portsByName := map[string]*DevicePort{}
705 portsByName["SDX6320031-1"] = &DevicePort{
706 Name: "SDX6320031-1",
707 ID: 256,
708 }
709 device := &Device{
710 ctx: context.Background(),
711 ID: "SDX6320031",
712 PortsByName: portsByName,
713 }
714 dev := map[string]*Device{}
715 dev["SDX6320031"] = device
716 tests := []struct {
717 name string
718 args args
719 want PortState
720 wantErr bool
721 }{
722 {
723 name: "GetPortState",
724 args: args{
725 device: "SDX6320031",
726 name: "SDX6320031-1",
727 },
728 want: PortStateUp,
729 },
730 {
731 name: "GetPortState_Device_Not_found",
732 args: args{
733 device: "SDX6320031-1",
734 name: "SDX6320031",
735 },
736 want: PortStateDown,
737 wantErr: true,
738 },
739 }
740 for _, tt := range tests {
741 t.Run(tt.name, func(t *testing.T) {
742 v := &VoltController{
743 devices: dev,
744 }
745 switch tt.name {
746 case "GetPortState":
747 got, err := v.GetPortState(tt.args.device, tt.args.name)
748 if (err != nil) != tt.wantErr {
749 t.Errorf("VoltController.GetPortState() error = %v, wantErr %v", err, tt.wantErr)
750 return
751 }
752 assert.NotNil(t, got)
753 case "GetPortState_Device_Not_found":
754 got, err := v.GetPortState(tt.args.device, tt.args.name)
755 if (err != nil) != tt.wantErr {
756 t.Errorf("VoltController.GetPortState() error = %v, wantErr %v", err, tt.wantErr)
757 return
758 }
759 if got != tt.want {
760 t.Errorf("VoltController.GetPortState() = %v, want %v", got, tt.want)
761 }
762 }
763 })
764 }
765}
766
767func TestVoltController_ModMeter(t *testing.T) {
768 type args struct {
769 port string
770 device string
771 command of.MeterCommand
772 meter *of.Meter
773 }
774 portsByName := map[string]*DevicePort{}
775 portsByName["SDX6320031-1"] = &DevicePort{
776 Name: "SDX6320031-1",
777 ID: 256,
778 }
779 mtrs := &of.Meter{
780 ID: uint32(256),
781 State: 1,
782 }
783 devMtr := map[uint32]*of.Meter{}
784 devMtr[uint32(256)] = mtrs
785 device := &Device{
786 ctx: context.Background(),
787 ID: "SDX6320031",
788 PortsByName: portsByName,
789 meters: devMtr,
790 }
791 dev := map[string]*Device{}
792 dev["SDX6320031"] = device
793 tests := []struct {
794 name string
795 args args
796 wantErr bool
797 }{
798 {
799 name: "ModMeter",
800 args: args{
801 device: "SDX6320031",
802 port: "SDX6320031-1",
803 command: of.MeterCommandAdd,
804 meter: mtrs,
805 },
806 wantErr: false,
807 },
808 {
809 name: "ModMeter_device_not_found",
810 args: args{
811 command: of.MeterCommandAdd,
812 meter: mtrs,
813 },
814 wantErr: true,
815 },
816 {
817 name: "ModMeter_port_not_found",
818 args: args{
819 device: "SDX6320031",
820 command: of.MeterCommandAdd,
821 meter: mtrs,
822 },
823 wantErr: true,
824 },
825 }
826 for _, tt := range tests {
827 t.Run(tt.name, func(t *testing.T) {
828 v := &VoltController{
829 devices: dev,
830 }
831 switch tt.name {
832 case "ModMeter":
833 if err := v.ModMeter(tt.args.port, tt.args.device, tt.args.command, tt.args.meter); (err != nil) != tt.wantErr {
834 t.Errorf("VoltController.ModMeter() error = %v, wantErr %v", err, tt.wantErr)
835 }
836 case "ModMeter_device_not_found":
837 if err := v.ModMeter(tt.args.port, tt.args.device, tt.args.command, tt.args.meter); (err != nil) != tt.wantErr {
838 t.Errorf("VoltController.ModMeter() error = %v, wantErr %v", err, tt.wantErr)
839 }
840 case "ModMeter_port_not_found":
841 if err := v.ModMeter(tt.args.port, tt.args.device, tt.args.command, tt.args.meter); (err != nil) != tt.wantErr {
842 t.Errorf("VoltController.ModMeter() error = %v, wantErr %v", err, tt.wantErr)
843 }
844 }
845 })
846 }
847}
848
849func TestVoltController_VPAgent(t *testing.T) {
850 type args struct {
851 vep string
852 }
853 vagent := map[string]*vpagent.VPAgent{}
854 vpa := &vpagent.VPAgent{}
855 vagent[""] = vpa
856 tests := []struct {
857 name string
858 args args
859 want *vpagent.VPAgent
860 wantErr bool
861 }{
862 {
863 name: "VPAgent",
864 args: args{},
865 want: vpa,
866 wantErr: false,
867 },
868 {
869 name: "VPAgent_Error",
870 args: args{
871 vep: "ab",
872 },
873 want: nil,
874 wantErr: true,
875 },
876 }
877 for _, tt := range tests {
878 t.Run(tt.name, func(t *testing.T) {
879 v := &VoltController{
880 vagent: vagent,
881 }
882 switch tt.name {
883 case "VPAgent":
884 got, err := v.VPAgent(tt.args.vep)
885 if (err != nil) != tt.wantErr {
886 t.Errorf("VoltController.VPAgent() error = %v, wantErr %v", err, tt.wantErr)
887 return
888 }
889 if !reflect.DeepEqual(got, tt.want) {
890 t.Errorf("VoltController.VPAgent() = %v, want %v", got, tt.want)
891 }
892 case "VPAgent_Error":
893 got, err := v.VPAgent(tt.args.vep)
894 if (err != nil) != tt.wantErr {
895 t.Errorf("VoltController.VPAgent() error = %v, wantErr %v", err, tt.wantErr)
896 return
897 }
898 if !reflect.DeepEqual(got, tt.want) {
899 t.Errorf("VoltController.VPAgent() = %v, want %v", got, tt.want)
900 }
901 }
902 })
903 }
904}
905
906func TestVoltController_DeviceRebootInd(t *testing.T) {
907 type args struct {
908 cntx context.Context
909 dID string
910 srNo string
911 sbID string
912 }
913 appMock := mocks.NewMockApp(gomock.NewController(t))
914 NewController(ctx, appMock)
915 appMock.EXPECT().DeviceRebootInd(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
916 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
917 db = dbintf
918 dbintf.EXPECT().DelAllRoutesForDevice(gomock.Any(), gomock.Any()).AnyTimes()
919 dbintf.EXPECT().DelAllGroup(gomock.Any(), gomock.Any()).AnyTimes()
920 dbintf.EXPECT().DelAllMeter(gomock.Any(), gomock.Any()).AnyTimes()
921 dbintf.EXPECT().DelAllPONCounters(gomock.Any(), gomock.Any()).AnyTimes()
922 tests := []struct {
923 name string
924 args args
925 }{
926 {
927 name: "VPAgent",
928 args: args{
929 dID: "1234",
930 srNo: "SDX6320031",
931 cntx: context.Background(),
932 sbID: "4321",
933 },
934 },
935 }
936 for _, tt := range tests {
937 t.Run(tt.name, func(t *testing.T) {
938 v := &VoltController{
939 app: GetController().app,
940 }
941 v.DeviceRebootInd(tt.args.cntx, tt.args.dID, tt.args.srNo, tt.args.sbID)
942 })
943 }
944}
945
946func TestVoltController_SetRebootInProgressForDevice(t *testing.T) {
947 type args struct {
948 device string
949 }
Akash Soni6f369452023-09-19 11:18:28 +0530950 rebootInProgressdevices := map[string]string{}
Akash Sonid36d23b2023-08-18 12:51:40 +0530951 device := &Device{
952 ctx: context.Background(),
953 ID: "SDX6320031",
954 }
955 dev := map[string]*Device{}
956 dev["SDX6320031"] = device
957 tests := []struct {
958 name string
959 args args
960 want bool
961 }{
962 {
963 name: "SetRebootInProgressForDevice",
964 args: args{
965 device: "SDX6320031",
966 },
967 want: true,
968 },
969 {
970 name: "SetRebootInProgressForDevice_Error",
971 args: args{
972 device: "SDX6320031-1",
973 },
974 want: true,
975 },
976 }
977 for _, tt := range tests {
978 t.Run(tt.name, func(t *testing.T) {
979 v := &VoltController{
Akash Soni6f369452023-09-19 11:18:28 +0530980 rebootInProgressDevices: rebootInProgressdevices,
Akash Sonid36d23b2023-08-18 12:51:40 +0530981 devices: dev,
982 }
983 switch tt.name {
984 case "SetRebootInProgressForDevice":
985 if got := v.SetRebootInProgressForDevice(tt.args.device); got != tt.want {
986 t.Errorf("VoltController.SetRebootInProgressForDevice() = %v, want %v", got, tt.want)
987 }
988 case "SetRebootInProgressForDevice_Error":
989 if got := v.SetRebootInProgressForDevice(tt.args.device); got != tt.want {
990 t.Errorf("VoltController.SetRebootInProgressForDevice() = %v, want %v", got, tt.want)
991 }
992 }
993 })
994 }
995}
996
997func TestVoltController_ReSetRebootInProgressForDevice(t *testing.T) {
998 type args struct {
999 device string
1000 }
Akash Soni6f369452023-09-19 11:18:28 +05301001 rebootInProgressdevices := map[string]string{}
Akash Sonid36d23b2023-08-18 12:51:40 +05301002 device := &Device{
1003 ctx: context.Background(),
1004 ID: "SDX6320031",
1005 }
Akash Soni6f369452023-09-19 11:18:28 +05301006 rebootInProgressdevices["SDX6320031"] = "done"
Akash Sonid36d23b2023-08-18 12:51:40 +05301007 dev := map[string]*Device{}
1008 dev["SDX6320031"] = device
1009 tests := []struct {
1010 name string
1011 args args
1012 want bool
1013 }{
1014 {
1015 name: "ReSetRebootInProgressForDevice",
1016 args: args{
1017 device: "SDX6320031",
1018 },
1019 want: true,
1020 },
1021 }
1022 for _, tt := range tests {
1023 t.Run(tt.name, func(t *testing.T) {
1024 v := &VoltController{
Akash Soni6f369452023-09-19 11:18:28 +05301025 rebootInProgressDevices: rebootInProgressdevices,
Akash Sonid36d23b2023-08-18 12:51:40 +05301026 devices: dev,
1027 }
1028 if got := v.ReSetRebootInProgressForDevice(tt.args.device); got != tt.want {
1029 t.Errorf("VoltController.ReSetRebootInProgressForDevice() = %v, want %v", got, tt.want)
1030 }
1031 })
1032 }
1033}
Akash Soni6f369452023-09-19 11:18:28 +05301034
1035func TestVoltController_IsBlockedDevice(t *testing.T) {
1036 type args struct {
1037 deviceserialNumber string
1038 }
1039 tests := []struct {
1040 name string
1041 args args
1042 want bool
1043 }{
1044 {
1045 name: "IsBlockedDevice",
1046 args: args{
1047 deviceserialNumber: "SDX6320031",
1048 },
1049 want: false,
1050 },
1051 {
1052 name: "deviceserialNumber",
1053 args: args{
1054 deviceserialNumber: "SDX6320031",
1055 },
1056 want: false,
1057 },
1058 {
1059 name: "AddBlockeddevices",
1060 args: args{
1061 deviceserialNumber: "SDX6320031",
1062 },
1063 want: false,
1064 },
1065 }
1066 for _, tt := range tests {
1067 t.Run(tt.name, func(t *testing.T) {
1068 v := &VoltController{
1069 BlockedDeviceList: util.NewConcurrentMap(),
1070 }
1071 switch tt.name {
1072 case "IsBlockedDevice":
1073 if got := v.IsBlockedDevice(tt.args.deviceserialNumber); got != tt.want {
1074 t.Errorf("VoltController.IsBlockedDevice() = %v, want %v", got, tt.want)
1075 }
1076 case "deviceserialNumber":
1077 v.DelBlockedDevices(tt.args.deviceserialNumber)
1078 case "AddBlockeddevices":
1079 v.AddBlockedDevices(tt.args.deviceserialNumber)
1080 }
1081 })
1082 }
1083}
1084
1085func TestVoltController_SetDeviceTableSyncDuration(t *testing.T) {
1086 type args struct {
1087 duration int
1088 }
1089 tests := []struct {
1090 name string
1091 args args
1092 }{
1093 {
1094 name: "SetDeviceTableSyncDuration",
1095 args: args{
1096 duration: 1,
1097 },
1098 },
1099 }
1100 for _, tt := range tests {
1101 t.Run(tt.name, func(t *testing.T) {
1102 v := &VoltController{}
1103 switch tt.name {
1104 case "SetDeviceTableSyncDuration":
1105 v.SetDeviceTableSyncDuration(tt.args.duration)
1106 v.GetDeviceTableSyncDuration()
1107 }
1108 })
1109 }
1110}
1111
1112func TestVoltController_IsRebootInProgressForDevice(t *testing.T) {
1113 type args struct {
1114 device string
1115 }
1116 tests := []struct {
1117 name string
1118 args args
1119 want bool
1120 }{
1121 {
1122 name: "SetDeviceTableSyncDuration",
1123 args: args{
1124 device: "SDX6320031",
1125 },
1126 },
1127 }
1128 for _, tt := range tests {
1129 t.Run(tt.name, func(t *testing.T) {
1130 v := &VoltController{}
1131 if got := v.IsRebootInProgressForDevice(tt.args.device); got != tt.want {
1132 t.Errorf("VoltController.IsRebootInProgressForDevice() = %v, want %v", got, tt.want)
1133 }
1134 })
1135 }
1136}
1137
1138func TestVoltController_GroupUpdate(t *testing.T) {
1139 type args struct {
1140 port string
1141 device string
1142 group *of.Group
1143 }
1144 portsByName := map[string]*DevicePort{}
1145 portsByName["SDX6320031-1"] = &DevicePort{
1146 Name: "SDX6320031-1",
1147 ID: 256,
1148 }
1149 device := &Device{
1150 ctx: context.Background(),
1151 ID: "SDX6320031",
1152 groups: sync.Map{},
1153 PortsByName: portsByName,
1154 }
1155 dev := map[string]*Device{}
1156 dev["SDX6320031"] = device
1157 grp := &of.Group{
1158 Device: "SDX6320031",
1159 GroupID: uint32(256),
1160 State: 1,
1161 SetVlan: of.VlanAny,
1162 }
1163 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1164 db = dbintf
1165 dbintf.EXPECT().PutGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
1166 tests := []struct {
1167 name string
1168 args args
1169 wantErr bool
1170 }{
1171 {
1172 name: "GroupUpdate",
1173 args: args{
1174 port: "SDX6320031-1",
1175 device: "SDX6320031",
1176 group: grp,
1177 },
1178 wantErr: false,
1179 },
1180 {
1181 name: "DeviceNOtFound_Error",
1182 args: args{
1183 device: "SDX632003134",
1184 },
1185 wantErr: true,
1186 },
1187 {
1188 name: "PortNOtFound_Error",
1189 args: args{
1190 device: "SDX6320031",
1191 port: "SDX632003134",
1192 },
1193 wantErr: true,
1194 },
1195 {
1196 name: "ContextNill_Error",
1197 args: args{
1198 device: "SDX6320031",
1199 port: "SDX6320031-1",
1200 },
1201 wantErr: true,
1202 },
1203 }
1204 for _, tt := range tests {
1205 t.Run(tt.name, func(t *testing.T) {
1206 switch tt.name {
1207 case "GroupUpdate", "DeviceNOtFound_Error", "PortNOtFound_Error":
1208 v := &VoltController{
1209 devices: dev,
1210 }
1211 if err := v.GroupUpdate(tt.args.port, tt.args.device, tt.args.group); (err != nil) != tt.wantErr {
1212 t.Errorf("VoltController.GroupUpdate() error = %v, wantErr %v", err, tt.wantErr)
1213 }
1214 case "ContextNill_Error":
1215 device := &Device{
1216 ID: "SDX6320031",
1217 groups: sync.Map{},
1218 PortsByName: portsByName,
1219 }
1220 dev := map[string]*Device{}
1221 dev["SDX6320031"] = device
1222 v := &VoltController{
1223 devices: dev,
1224 }
1225 if err := v.GroupUpdate(tt.args.port, tt.args.device, tt.args.group); (err != nil) != tt.wantErr {
1226 t.Errorf("VoltController.GroupUpdate() error = %v, wantErr %v", err, tt.wantErr)
1227 }
1228 }
1229 })
1230 }
1231}