blob: 18b16bcb85706d384306a23d67fbb14c10178e79 [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) {
Akash Soni230e6212023-10-16 10:46:07 +053057 got := NewController(tt.args.ctx, tt.args.app)
58 assert.NotNil(t, got)
Akash Sonid36d23b2023-08-18 12:51:40 +053059 })
60 }
61}
62
63func Cancel() {}
64func TestVoltController_DelDevice(t *testing.T) {
65 type args struct {
66 cntx context.Context
67 id string
68 }
69
70 device := &Device{
71 ID: "SDX6320031",
72 cancel: Cancel,
73 }
bseenivaa8cb94c2024-12-16 13:37:17 +053074 var dev sync.Map
75 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +053076 appMock := mocks.NewMockApp(gomock.NewController(t))
77 NewController(ctx, appMock)
78 appMock.EXPECT().DelDevice(gomock.Any(), gomock.Any()).AnyTimes()
79 tests := []struct {
80 name string
81 args args
82 }{
83 {
84 name: "DelDevice",
85 args: args{
86 cntx: context.Background(),
87 id: "SDX6320031",
88 },
89 },
90 }
91 for _, tt := range tests {
92 t.Run(tt.name, func(t *testing.T) {
93 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +053094 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +053095 app: GetController().app,
96 }
bseenivaa8cb94c2024-12-16 13:37:17 +053097 dev.Range(func(key, value interface{}) bool {
98 v.Devices.Store(key, value)
99 return true
100 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530101 v.DelDevice(tt.args.cntx, tt.args.id)
102 })
103 }
104}
105
106func TestVoltController_AddFlows(t *testing.T) {
107 type args struct {
108 cntx context.Context
109 port string
110 device string
111 flow *of.VoltFlow
112 }
113 subFlows := map[uint64]*of.VoltSubFlow{}
114 vltSubFlow := &of.VoltSubFlow{
115 Priority: 100,
116 Cookie: 103112802816,
117 State: of.FlowAddSuccess,
118 Match: of.Match{
119 InPort: 1573376,
120 MatchVlan: 4096,
121 L4Protocol: 255,
122 },
123 Action: of.Action{
124 Metadata: 279189651712,
125 GoToTableID: 1,
126 MeterID: 1,
127 SetVlan: 4097,
128 Pcp: 8,
129 Output: 4,
130 },
131 }
132 subFlows[0] = vltSubFlow
133 portsByName := map[string]*DevicePort{}
134 portsByName["SDX6320031-1"] = &DevicePort{
135 Name: "SDX6320031-1",
136 ID: 256,
137 }
138 device := &Device{
139 ctx: context.Background(),
140 ID: "SDX6320031",
141 flows: subFlows,
142 PortsByName: portsByName,
143 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530144 var dev sync.Map
145 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530146 flow := &of.VoltFlow{
147 PortName: "SDX6320031-1",
148 PortID: 256,
149 Command: 0,
150 MigrateCookie: true,
151 }
152 tests := []struct {
153 name string
154 args args
155 wantErr bool
156 }{
157 {
158 name: "AddFlows",
159 args: args{
160 cntx: context.Background(),
161 port: "SDX6320031-1",
162 device: "SDX6320031",
163 flow: flow,
164 },
165 },
166 }
167 for _, tt := range tests {
168 t.Run(tt.name, func(t *testing.T) {
169 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530170 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530171 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530172 dev.Range(func(key, value interface{}) bool {
173 v.Devices.Store(key, value)
174 return true
175 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530176 if err := v.AddFlows(tt.args.cntx, tt.args.port, tt.args.device, tt.args.flow); (err != nil) != tt.wantErr {
177 t.Errorf("VoltController.AddFlows() error = %v, wantErr %v", err, tt.wantErr)
178 }
179 })
180 }
181}
182
183func TestVoltController_DelFlows(t *testing.T) {
184 type args struct {
185 cntx context.Context
186 port string
187 device string
188 flow *of.VoltFlow
189 }
190 subFlows := map[uint64]*of.VoltSubFlow{}
191 vltSubFlow := &of.VoltSubFlow{
192 Priority: 100,
193 Cookie: 103112802816,
194 State: of.FlowAddSuccess,
195 Match: of.Match{
196 InPort: 1573376,
197 MatchVlan: 4096,
198 L4Protocol: 255,
199 },
200 Action: of.Action{
201 Metadata: 279189651712,
202 GoToTableID: 1,
203 MeterID: 1,
204 SetVlan: 4097,
205 Pcp: 8,
206 Output: 4,
207 },
208 }
209 subFlows[0] = vltSubFlow
210 portsByName := map[string]*DevicePort{}
211 portsByName["SDX6320031-1"] = &DevicePort{
212 Name: "SDX6320031-1",
213 ID: 256,
214 }
215 device := &Device{
216 ctx: context.Background(),
217 ID: "SDX6320031",
218 flows: subFlows,
219 PortsByName: portsByName,
220 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530221 var dev sync.Map
222 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530223 flow := &of.VoltFlow{
224 PortName: "SDX6320031-1",
225 PortID: 256,
226 Command: 0,
227 MigrateCookie: true,
228 }
229 tests := []struct {
230 name string
231 args args
232 wantErr bool
233 }{
234 {
235 name: "DelFlows",
236 args: args{
237 cntx: context.Background(),
238 port: "SDX6320031-1",
239 device: "SDX6320031",
240 flow: flow,
241 },
242 },
243 }
244 for _, tt := range tests {
245 t.Run(tt.name, func(t *testing.T) {
246 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530247 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530248 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530249 dev.Range(func(key, value interface{}) bool {
250 v.Devices.Store(key, value)
251 return true
252 })
Sridhar Ravindra03aa0bf2023-09-12 17:46:40 +0530253 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 +0530254 t.Errorf("VoltController.DelFlows() error = %v, wantErr %v", err, tt.wantErr)
255 }
256 })
257 }
258}
259
260func TestVoltController_GetGroups(t *testing.T) {
261 type args struct {
262 cntx context.Context
263 id uint32
264 }
265 device := &Device{
266 ctx: context.Background(),
267 ID: "SDX6320031",
268 groups: sync.Map{},
269 }
270 grp := &of.Group{
271 Device: "SDX6320031",
272 GroupID: uint32(256),
273 State: 1,
274 SetVlan: of.VlanAny,
275 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530276 var dev sync.Map
277 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530278 tests := []struct {
279 name string
280 args args
281 want *of.Group
282 wantErr bool
283 }{
284 {
285 name: "VoltController_GetGroups",
286 args: args{
287 cntx: context.Background(),
288 id: uint32(256),
289 },
290 want: grp,
291 wantErr: false,
292 },
293 {
294 name: "GetGroups_Not-Found",
295 args: args{
296 cntx: context.Background(),
297 id: 1,
298 },
299 want: nil,
300 wantErr: true,
301 },
302 }
303
304 for _, tt := range tests {
305 t.Run(tt.name, func(t *testing.T) {
306 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530307 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530308 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530309 dev.Range(func(key, value interface{}) bool {
310 v.Devices.Store(key, value)
311 return true
312 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530313 switch tt.name {
314 case "VoltController_GetGroups":
315 device.groups.Store(uint32(256), grp)
316 got, err := v.GetGroups(tt.args.cntx, tt.args.id)
317 if (err != nil) != tt.wantErr {
318 t.Errorf("VoltController.GetGroups() error = %v, wantErr %v", err, tt.wantErr)
319 return
320 }
321 if !reflect.DeepEqual(got, tt.want) {
322 t.Errorf("VoltController.GetGroups() = %v, want %v", got, tt.want)
323 }
324 case "GetGroups_Not-Found":
325 got, err := v.GetGroups(tt.args.cntx, tt.args.id)
326 if (err != nil) != tt.wantErr {
327 t.Errorf("VoltController.GetGroups() error = %v, wantErr %v", err, tt.wantErr)
328 return
329 }
330 if !reflect.DeepEqual(got, tt.want) {
331 t.Errorf("VoltController.GetGroups() = %v, want %v", got, tt.want)
332 }
333 }
334 })
335 }
336}
337
338func TestVoltController_GetGroupList(t *testing.T) {
339 device := &Device{
340 ctx: context.Background(),
341 ID: "SDX6320031",
342 groups: sync.Map{},
343 }
344 grpList := []*of.Group{}
345 grp := &of.Group{
346 Device: "SDX6320031",
347 GroupID: uint32(256),
348 State: 1,
349 SetVlan: of.VlanAny,
350 }
351 grpList = append(grpList, grp)
bseenivaa8cb94c2024-12-16 13:37:17 +0530352 var dev sync.Map
353 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530354 tests := []struct {
355 name string
356 want []*of.Group
357 wantErr bool
358 }{
359 {
360 name: "VoltController_GetGroups",
361 want: grpList,
362 wantErr: false,
363 },
364 }
365 for _, tt := range tests {
366 t.Run(tt.name, func(t *testing.T) {
367 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530368 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530369 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530370 dev.Range(func(key, value interface{}) bool {
371 v.Devices.Store(key, value)
372 return true
373 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530374 device.groups.Store(uint32(256), grp)
375 got, err := v.GetGroupList()
376 if (err != nil) != tt.wantErr {
377 t.Errorf("VoltController.GetGroupList() error = %v, wantErr %v", err, tt.wantErr)
378 return
379 }
380 if !reflect.DeepEqual(got, tt.want) {
381 t.Errorf("VoltController.GetGroupList() = %v, want %v", got, tt.want)
382 }
383 })
384 }
385}
386
387func TestVoltController_GetMeterInfo(t *testing.T) {
388 type args struct {
389 cntx context.Context
390 id uint32
391 }
392 mtrs := &of.Meter{
393 ID: uint32(256),
394 State: 1,
395 }
396 mtr := map[string]*of.Meter{}
397 mtr["SDX6320031"] = mtrs
398 devMtr := map[uint32]*of.Meter{}
399 devMtr[uint32(256)] = mtrs
400 device := &Device{
401 ctx: context.Background(),
402 ID: "SDX6320031",
403 meters: devMtr,
404 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530405 var dev sync.Map
406 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530407 tests := []struct {
408 name string
409 args args
410 want map[string]*of.Meter
411 wantErr bool
412 }{
413 {
414 name: "VoltController_GetMeterInfo",
415 args: args{
416 cntx: context.Background(),
417 id: uint32(256),
418 },
419 want: mtr,
420 wantErr: false,
421 },
422 {
423 name: "Not_Found_Error",
424 args: args{
425 cntx: context.Background(),
426 id: 1,
427 },
428 want: nil,
429 wantErr: true,
430 },
431 }
432 for _, tt := range tests {
433 t.Run(tt.name, func(t *testing.T) {
434 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530435 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530436 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530437 dev.Range(func(key, value interface{}) bool {
438 v.Devices.Store(key, value)
439 return true
440 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530441 switch tt.name {
442 case "VoltController_GetMeterInfo":
443 got, err := v.GetMeterInfo(tt.args.cntx, tt.args.id)
444 if (err != nil) != tt.wantErr {
445 t.Errorf("VoltController.GetMeterInfo() error = %v, wantErr %v", err, tt.wantErr)
446 return
447 }
448 if !reflect.DeepEqual(got, tt.want) {
449 t.Errorf("VoltController.GetMeterInfo() = %v, want %v", got, tt.want)
450 }
451 case "Not_Found_Error":
452 got, err := v.GetMeterInfo(tt.args.cntx, tt.args.id)
453 if (err != nil) != tt.wantErr {
454 t.Errorf("VoltController.GetMeterInfo() error = %v, wantErr %v", err, tt.wantErr)
455 return
456 }
457 if !reflect.DeepEqual(got, tt.want) {
458 t.Errorf("VoltController.GetMeterInfo() = %v, want %v", got, tt.want)
459 }
460 }
461 })
462 }
463}
464
465func TestVoltController_GetAllMeterInfo(t *testing.T) {
466 vltMtr := map[string][]*of.Meter{}
467 mtr := &of.Meter{
468 ID: uint32(256),
469 State: 1,
470 }
471 mtrs := []*of.Meter{}
472 mtrs = append(mtrs, mtr)
473 vltMtr["SDX6320031"] = mtrs
474 devMtr := map[uint32]*of.Meter{}
475 devMtr[uint32(256)] = mtr
476 device := &Device{
477 ctx: context.Background(),
478 ID: "SDX6320031",
479 meters: devMtr,
480 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530481 var dev sync.Map
482 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530483 tests := []struct {
484 name string
485 want map[string][]*of.Meter
486 wantErr bool
487 }{
488 {
489 name: "VoltController_GetMeterInfo",
490 want: vltMtr,
491 wantErr: false,
492 },
493 }
494 for _, tt := range tests {
495 t.Run(tt.name, func(t *testing.T) {
496 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530497 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530498 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530499 dev.Range(func(key, value interface{}) bool {
500 v.Devices.Store(key, value)
501 return true
502 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530503 got, err := v.GetAllMeterInfo()
504 if (err != nil) != tt.wantErr {
505 t.Errorf("VoltController.GetAllMeterInfo() error = %v, wantErr %v", err, tt.wantErr)
506 return
507 }
508 if !reflect.DeepEqual(got, tt.want) {
509 t.Errorf("VoltController.GetAllMeterInfo() = %v, want %v", got, tt.want)
510 }
511 })
512 }
513}
514
515func TestVoltController_GetAllPendingFlows(t *testing.T) {
516 subFlowList := []*of.VoltSubFlow{}
517 vltSubFlow := &of.VoltSubFlow{
518 Priority: 100,
519 Cookie: 103112802816,
520 State: of.FlowAddSuccess,
521 Match: of.Match{
522 InPort: 1573376,
523 MatchVlan: 4096,
524 L4Protocol: 255,
525 },
526 Action: of.Action{
527 Metadata: 279189651712,
528 GoToTableID: 1,
529 MeterID: 1,
530 SetVlan: 4097,
531 Pcp: 8,
532 Output: 4,
533 },
534 }
535 subFlowList = append(subFlowList, vltSubFlow)
536 subFlows := map[uint64]*of.VoltSubFlow{}
537 subFlows[0] = vltSubFlow
538 device := &Device{
539 ctx: context.Background(),
540 ID: "SDX6320031",
541 flows: subFlows,
542 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530543 var dev sync.Map
544 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530545 tests := []struct {
546 name string
547 want []*of.VoltSubFlow
548 wantErr bool
549 }{
550 {
551 name: "GetAllPendingFlows",
552 want: subFlowList,
553 wantErr: false,
554 },
555 }
556 type args1 struct {
557 deviceId string
558 }
559 tests1 := []struct {
560 name string
561 args args1
562 want []*of.VoltSubFlow
563 wantErr bool
564 }{
565 {
566 name: "GetFlows_with_DeviceID",
567 args: args1{
568 deviceId: "SDX6320031",
569 },
570 want: subFlowList,
571 wantErr: false,
572 },
573 {
574 name: "GetFlows_with_DeviceID_NOT_FOUND",
575 args: args1{
576 deviceId: "",
577 },
578 want: subFlowList,
579 wantErr: false,
580 },
581 }
582 type args2 struct {
583 deviceId string
584 cookie uint64
585 }
586 tests2 := []struct {
587 name string
588 args args2
589 want []*of.VoltSubFlow
590 wantErr bool
591 }{
592 {
593 name: "GetFlow_with_DeviceID_and_cookie",
594 args: args2{
595 deviceId: "SDX6320031",
596 cookie: 103112802816,
597 },
598 want: subFlowList,
599 wantErr: false,
600 },
601 {
602 name: "GetFlow_with_DeviceID_and_cookie_NOT_FOUND",
603 args: args2{
604 deviceId: "",
605 },
606 want: subFlowList,
607 wantErr: true,
608 },
609 }
610 for _, tt := range tests {
611 t.Run(tt.name, func(t *testing.T) {
612 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530613 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530614 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530615 dev.Range(func(key, value interface{}) bool {
616 v.Devices.Store(key, value)
617 return true
618 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530619 got, err := v.GetAllPendingFlows()
620 if (err != nil) != tt.wantErr {
621 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
622 return
623 }
624 assert.Nil(t, got)
625 got1, err1 := v.GetAllFlows()
626 if (err1 != nil) != tt.wantErr {
627 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
628 return
629 }
630 assert.NotNil(t, got1)
631 })
632 }
633 for _, tt := range tests1 {
634 t.Run(tt.name, func(t *testing.T) {
635 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530636 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530637 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530638 dev.Range(func(key, value interface{}) bool {
639 v.Devices.Store(key, value)
640 return true
641 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530642 switch tt.name {
643 case "GetFlows_with_DeviceID":
644 got, err := v.GetFlows(tt.args.deviceId)
645 if (err != nil) != tt.wantErr {
646 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
647 return
648 }
649 assert.NotNil(t, got)
650 case "GetFlows_with_DeviceID_NOT_FOUND":
651 got, err := v.GetFlows(tt.args.deviceId)
652 if (err != nil) != tt.wantErr {
653 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
654 return
655 }
656 assert.Nil(t, got)
657 }
658 })
659 }
660 for _, tt := range tests2 {
661 t.Run(tt.name, func(t *testing.T) {
662 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530663 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530664 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530665 dev.Range(func(key, value interface{}) bool {
666 v.Devices.Store(key, value)
667 return true
668 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530669 switch tt.name {
670 case "GetFlow_with_DeviceID_and_cookie":
671 got, err := v.GetFlow(tt.args.deviceId, tt.args.cookie)
672 if (err != nil) != tt.wantErr {
673 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
674 return
675 }
676 assert.Nil(t, got)
677 case "GetFlow_with_DeviceID_and_cookie_NOT_FOUND":
678 got, err := v.GetFlow(tt.args.deviceId, tt.args.cookie)
679 if (err != nil) != tt.wantErr {
680 t.Errorf("VoltController.GetAllPendingFlows() error = %v, wantErr %v", err, tt.wantErr)
681 return
682 }
683 assert.Nil(t, got)
684 }
685 })
686 }
687}
688
689func TestVoltController_GetTaskList(t *testing.T) {
690 type args struct {
691 device string
692 }
693 device := &Device{
694 ctx: context.Background(),
695 ID: "SDX6320031",
696 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530697 var dev sync.Map
698 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530699 tests := []struct {
700 name string
701 args args
702 want []tasks.Task
703 }{
704 {
705 name: "GetTaskList",
706 args: args{
707 device: "SDX6320031",
708 },
709 want: []tasks.Task{},
710 },
711 {
712 name: "GetTaskList_Device_Not_found",
713 args: args{
714 device: "SDX632003",
715 },
716 want: []tasks.Task{},
717 },
718 }
719 for _, tt := range tests {
720 t.Run(tt.name, func(t *testing.T) {
721 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530722 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530723 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530724 dev.Range(func(key, value interface{}) bool {
725 v.Devices.Store(key, value)
726 return true
727 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530728 switch tt.name {
729 case "GetTaskList":
730 if got := v.GetTaskList(tt.args.device); !reflect.DeepEqual(got, tt.want) {
731 t.Errorf("VoltController.GetTaskList() = %v, want %v", got, tt.want)
732 }
733 case "GetTaskList_Device_Not_found":
734 if got := v.GetTaskList(tt.args.device); !reflect.DeepEqual(got, tt.want) {
735 t.Errorf("VoltController.GetTaskList() = %v, want %v", got, tt.want)
736 }
737 }
738 })
739 }
740}
741
742func TestVoltController_GetPortState(t *testing.T) {
743 type args struct {
744 device string
745 name string
746 }
747 portsByName := map[string]*DevicePort{}
748 portsByName["SDX6320031-1"] = &DevicePort{
749 Name: "SDX6320031-1",
750 ID: 256,
751 }
752 device := &Device{
753 ctx: context.Background(),
754 ID: "SDX6320031",
755 PortsByName: portsByName,
756 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530757 var dev sync.Map
758 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530759 tests := []struct {
760 name string
761 args args
762 want PortState
763 wantErr bool
764 }{
765 {
766 name: "GetPortState",
767 args: args{
768 device: "SDX6320031",
769 name: "SDX6320031-1",
770 },
771 want: PortStateUp,
772 },
773 {
774 name: "GetPortState_Device_Not_found",
775 args: args{
776 device: "SDX6320031-1",
777 name: "SDX6320031",
778 },
779 want: PortStateDown,
780 wantErr: true,
781 },
782 }
783 for _, tt := range tests {
784 t.Run(tt.name, func(t *testing.T) {
785 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530786 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530787 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530788 dev.Range(func(key, value interface{}) bool {
789 v.Devices.Store(key, value)
790 return true
791 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530792 switch tt.name {
793 case "GetPortState":
794 got, err := v.GetPortState(tt.args.device, tt.args.name)
795 if (err != nil) != tt.wantErr {
796 t.Errorf("VoltController.GetPortState() error = %v, wantErr %v", err, tt.wantErr)
797 return
798 }
799 assert.NotNil(t, got)
800 case "GetPortState_Device_Not_found":
801 got, err := v.GetPortState(tt.args.device, tt.args.name)
802 if (err != nil) != tt.wantErr {
803 t.Errorf("VoltController.GetPortState() error = %v, wantErr %v", err, tt.wantErr)
804 return
805 }
806 if got != tt.want {
807 t.Errorf("VoltController.GetPortState() = %v, want %v", got, tt.want)
808 }
809 }
810 })
811 }
812}
813
814func TestVoltController_ModMeter(t *testing.T) {
815 type args struct {
816 port string
817 device string
818 command of.MeterCommand
819 meter *of.Meter
820 }
821 portsByName := map[string]*DevicePort{}
822 portsByName["SDX6320031-1"] = &DevicePort{
823 Name: "SDX6320031-1",
824 ID: 256,
825 }
826 mtrs := &of.Meter{
827 ID: uint32(256),
828 State: 1,
829 }
830 devMtr := map[uint32]*of.Meter{}
831 devMtr[uint32(256)] = mtrs
832 device := &Device{
833 ctx: context.Background(),
834 ID: "SDX6320031",
835 PortsByName: portsByName,
836 meters: devMtr,
837 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530838 var dev sync.Map
839 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +0530840 tests := []struct {
841 name string
842 args args
843 wantErr bool
844 }{
845 {
846 name: "ModMeter",
847 args: args{
848 device: "SDX6320031",
849 port: "SDX6320031-1",
850 command: of.MeterCommandAdd,
851 meter: mtrs,
852 },
853 wantErr: false,
854 },
855 {
856 name: "ModMeter_device_not_found",
857 args: args{
858 command: of.MeterCommandAdd,
859 meter: mtrs,
860 },
861 wantErr: true,
862 },
863 {
864 name: "ModMeter_port_not_found",
865 args: args{
866 device: "SDX6320031",
867 command: of.MeterCommandAdd,
868 meter: mtrs,
869 },
870 wantErr: true,
871 },
872 }
873 for _, tt := range tests {
874 t.Run(tt.name, func(t *testing.T) {
875 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +0530876 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +0530877 }
bseenivaa8cb94c2024-12-16 13:37:17 +0530878 dev.Range(func(key, value interface{}) bool {
879 v.Devices.Store(key, value)
880 return true
881 })
Akash Sonid36d23b2023-08-18 12:51:40 +0530882 switch tt.name {
883 case "ModMeter":
884 if err := v.ModMeter(tt.args.port, tt.args.device, tt.args.command, tt.args.meter); (err != nil) != tt.wantErr {
885 t.Errorf("VoltController.ModMeter() error = %v, wantErr %v", err, tt.wantErr)
886 }
887 case "ModMeter_device_not_found":
888 if err := v.ModMeter(tt.args.port, tt.args.device, tt.args.command, tt.args.meter); (err != nil) != tt.wantErr {
889 t.Errorf("VoltController.ModMeter() error = %v, wantErr %v", err, tt.wantErr)
890 }
891 case "ModMeter_port_not_found":
892 if err := v.ModMeter(tt.args.port, tt.args.device, tt.args.command, tt.args.meter); (err != nil) != tt.wantErr {
893 t.Errorf("VoltController.ModMeter() error = %v, wantErr %v", err, tt.wantErr)
894 }
895 }
896 })
897 }
898}
899
900func TestVoltController_VPAgent(t *testing.T) {
901 type args struct {
902 vep string
903 }
904 vagent := map[string]*vpagent.VPAgent{}
905 vpa := &vpagent.VPAgent{}
906 vagent[""] = vpa
907 tests := []struct {
908 name string
909 args args
910 want *vpagent.VPAgent
911 wantErr bool
912 }{
913 {
914 name: "VPAgent",
915 args: args{},
916 want: vpa,
917 wantErr: false,
918 },
919 {
920 name: "VPAgent_Error",
921 args: args{
922 vep: "ab",
923 },
924 want: nil,
925 wantErr: true,
926 },
927 }
928 for _, tt := range tests {
929 t.Run(tt.name, func(t *testing.T) {
930 v := &VoltController{
931 vagent: vagent,
932 }
933 switch tt.name {
934 case "VPAgent":
935 got, err := v.VPAgent(tt.args.vep)
936 if (err != nil) != tt.wantErr {
937 t.Errorf("VoltController.VPAgent() error = %v, wantErr %v", err, tt.wantErr)
938 return
939 }
940 if !reflect.DeepEqual(got, tt.want) {
941 t.Errorf("VoltController.VPAgent() = %v, want %v", got, tt.want)
942 }
943 case "VPAgent_Error":
944 got, err := v.VPAgent(tt.args.vep)
945 if (err != nil) != tt.wantErr {
946 t.Errorf("VoltController.VPAgent() error = %v, wantErr %v", err, tt.wantErr)
947 return
948 }
949 if !reflect.DeepEqual(got, tt.want) {
950 t.Errorf("VoltController.VPAgent() = %v, want %v", got, tt.want)
951 }
952 }
953 })
954 }
955}
956
957func TestVoltController_DeviceRebootInd(t *testing.T) {
958 type args struct {
959 cntx context.Context
960 dID string
961 srNo string
962 sbID string
963 }
964 appMock := mocks.NewMockApp(gomock.NewController(t))
965 NewController(ctx, appMock)
966 appMock.EXPECT().DeviceRebootInd(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
967 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
968 db = dbintf
969 dbintf.EXPECT().DelAllRoutesForDevice(gomock.Any(), gomock.Any()).AnyTimes()
970 dbintf.EXPECT().DelAllGroup(gomock.Any(), gomock.Any()).AnyTimes()
971 dbintf.EXPECT().DelAllMeter(gomock.Any(), gomock.Any()).AnyTimes()
972 dbintf.EXPECT().DelAllPONCounters(gomock.Any(), gomock.Any()).AnyTimes()
973 tests := []struct {
974 name string
975 args args
976 }{
977 {
978 name: "VPAgent",
979 args: args{
980 dID: "1234",
981 srNo: "SDX6320031",
982 cntx: context.Background(),
983 sbID: "4321",
984 },
985 },
986 }
987 for _, tt := range tests {
988 t.Run(tt.name, func(t *testing.T) {
989 v := &VoltController{
990 app: GetController().app,
991 }
992 v.DeviceRebootInd(tt.args.cntx, tt.args.dID, tt.args.srNo, tt.args.sbID)
993 })
994 }
995}
996
997func TestVoltController_SetRebootInProgressForDevice(t *testing.T) {
998 type args struct {
999 device string
1000 }
Akash Soni230e6212023-10-16 10:46:07 +05301001 rebootInProgressDevices := map[string]string{}
Akash Sonid36d23b2023-08-18 12:51:40 +05301002 device := &Device{
1003 ctx: context.Background(),
1004 ID: "SDX6320031",
1005 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301006 var dev sync.Map
1007 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +05301008 tests := []struct {
1009 name string
1010 args args
1011 want bool
1012 }{
1013 {
1014 name: "SetRebootInProgressForDevice",
1015 args: args{
1016 device: "SDX6320031",
1017 },
1018 want: true,
1019 },
1020 {
1021 name: "SetRebootInProgressForDevice_Error",
1022 args: args{
1023 device: "SDX6320031-1",
1024 },
1025 want: true,
1026 },
1027 }
1028 for _, tt := range tests {
1029 t.Run(tt.name, func(t *testing.T) {
1030 v := &VoltController{
Akash Soni230e6212023-10-16 10:46:07 +05301031 rebootInProgressDevices: rebootInProgressDevices,
bseenivaa8cb94c2024-12-16 13:37:17 +05301032 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +05301033 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301034 dev.Range(func(key, value interface{}) bool {
1035 v.Devices.Store(key, value)
1036 return true
1037 })
Akash Sonid36d23b2023-08-18 12:51:40 +05301038 switch tt.name {
1039 case "SetRebootInProgressForDevice":
1040 if got := v.SetRebootInProgressForDevice(tt.args.device); got != tt.want {
1041 t.Errorf("VoltController.SetRebootInProgressForDevice() = %v, want %v", got, tt.want)
1042 }
1043 case "SetRebootInProgressForDevice_Error":
1044 if got := v.SetRebootInProgressForDevice(tt.args.device); got != tt.want {
1045 t.Errorf("VoltController.SetRebootInProgressForDevice() = %v, want %v", got, tt.want)
1046 }
1047 }
1048 })
1049 }
1050}
1051
1052func TestVoltController_ReSetRebootInProgressForDevice(t *testing.T) {
1053 type args struct {
1054 device string
1055 }
Akash Soni230e6212023-10-16 10:46:07 +05301056 rebootInProgressDevices := map[string]string{}
Akash Sonid36d23b2023-08-18 12:51:40 +05301057 device := &Device{
1058 ctx: context.Background(),
1059 ID: "SDX6320031",
1060 }
Akash Soni230e6212023-10-16 10:46:07 +05301061 rebootInProgressDevices["SDX6320031"] = "done"
bseenivaa8cb94c2024-12-16 13:37:17 +05301062 var dev sync.Map
1063 dev.Store("SDX6320031", device)
Akash Sonid36d23b2023-08-18 12:51:40 +05301064 tests := []struct {
1065 name string
1066 args args
1067 want bool
1068 }{
1069 {
1070 name: "ReSetRebootInProgressForDevice",
1071 args: args{
1072 device: "SDX6320031",
1073 },
1074 want: true,
1075 },
1076 }
1077 for _, tt := range tests {
1078 t.Run(tt.name, func(t *testing.T) {
1079 v := &VoltController{
Akash Soni230e6212023-10-16 10:46:07 +05301080 rebootInProgressDevices: rebootInProgressDevices,
bseenivaa8cb94c2024-12-16 13:37:17 +05301081 Devices: sync.Map{},
Akash Sonid36d23b2023-08-18 12:51:40 +05301082 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301083 dev.Range(func(key, value interface{}) bool {
1084 v.Devices.Store(key, value)
1085 return true
1086 })
Akash Sonid36d23b2023-08-18 12:51:40 +05301087 if got := v.ReSetRebootInProgressForDevice(tt.args.device); got != tt.want {
1088 t.Errorf("VoltController.ReSetRebootInProgressForDevice() = %v, want %v", got, tt.want)
1089 }
1090 })
1091 }
1092}
Akash Soni6f369452023-09-19 11:18:28 +05301093
1094func TestVoltController_IsBlockedDevice(t *testing.T) {
1095 type args struct {
Akash Soni230e6212023-10-16 10:46:07 +05301096 DeviceserialNumber string
Akash Soni6f369452023-09-19 11:18:28 +05301097 }
1098 tests := []struct {
1099 name string
1100 args args
1101 want bool
1102 }{
1103 {
1104 name: "IsBlockedDevice",
1105 args: args{
Akash Soni230e6212023-10-16 10:46:07 +05301106 DeviceserialNumber: "SDX6320031",
Akash Soni6f369452023-09-19 11:18:28 +05301107 },
1108 want: false,
1109 },
1110 {
Akash Soni230e6212023-10-16 10:46:07 +05301111 name: "DeviceserialNumber",
Akash Soni6f369452023-09-19 11:18:28 +05301112 args: args{
Akash Soni230e6212023-10-16 10:46:07 +05301113 DeviceserialNumber: "SDX6320031",
Akash Soni6f369452023-09-19 11:18:28 +05301114 },
1115 want: false,
1116 },
1117 {
Akash Soni230e6212023-10-16 10:46:07 +05301118 name: "AddBlockedDevices",
Akash Soni6f369452023-09-19 11:18:28 +05301119 args: args{
Akash Soni230e6212023-10-16 10:46:07 +05301120 DeviceserialNumber: "SDX6320031",
Akash Soni6f369452023-09-19 11:18:28 +05301121 },
1122 want: false,
1123 },
1124 }
1125 for _, tt := range tests {
1126 t.Run(tt.name, func(t *testing.T) {
1127 v := &VoltController{
1128 BlockedDeviceList: util.NewConcurrentMap(),
1129 }
1130 switch tt.name {
1131 case "IsBlockedDevice":
Akash Soni230e6212023-10-16 10:46:07 +05301132 if got := v.IsBlockedDevice(tt.args.DeviceserialNumber); got != tt.want {
Akash Soni6f369452023-09-19 11:18:28 +05301133 t.Errorf("VoltController.IsBlockedDevice() = %v, want %v", got, tt.want)
1134 }
Akash Soni230e6212023-10-16 10:46:07 +05301135 case "DeviceserialNumber":
1136 v.DelBlockedDevices(tt.args.DeviceserialNumber)
1137 case "AddBlockedDevices":
1138 v.AddBlockedDevices(tt.args.DeviceserialNumber)
Akash Soni6f369452023-09-19 11:18:28 +05301139 }
1140 })
1141 }
1142}
1143
1144func TestVoltController_SetDeviceTableSyncDuration(t *testing.T) {
1145 type args struct {
1146 duration int
1147 }
1148 tests := []struct {
1149 name string
1150 args args
1151 }{
1152 {
1153 name: "SetDeviceTableSyncDuration",
1154 args: args{
1155 duration: 1,
1156 },
1157 },
1158 }
1159 for _, tt := range tests {
1160 t.Run(tt.name, func(t *testing.T) {
1161 v := &VoltController{}
1162 switch tt.name {
1163 case "SetDeviceTableSyncDuration":
1164 v.SetDeviceTableSyncDuration(tt.args.duration)
1165 v.GetDeviceTableSyncDuration()
1166 }
1167 })
1168 }
1169}
1170
1171func TestVoltController_IsRebootInProgressForDevice(t *testing.T) {
1172 type args struct {
1173 device string
1174 }
1175 tests := []struct {
1176 name string
1177 args args
1178 want bool
1179 }{
1180 {
1181 name: "SetDeviceTableSyncDuration",
1182 args: args{
1183 device: "SDX6320031",
1184 },
1185 },
1186 }
1187 for _, tt := range tests {
1188 t.Run(tt.name, func(t *testing.T) {
1189 v := &VoltController{}
1190 if got := v.IsRebootInProgressForDevice(tt.args.device); got != tt.want {
1191 t.Errorf("VoltController.IsRebootInProgressForDevice() = %v, want %v", got, tt.want)
1192 }
1193 })
1194 }
1195}
1196
1197func TestVoltController_GroupUpdate(t *testing.T) {
1198 type args struct {
1199 port string
1200 device string
1201 group *of.Group
1202 }
1203 portsByName := map[string]*DevicePort{}
1204 portsByName["SDX6320031-1"] = &DevicePort{
1205 Name: "SDX6320031-1",
1206 ID: 256,
1207 }
1208 device := &Device{
1209 ctx: context.Background(),
1210 ID: "SDX6320031",
1211 groups: sync.Map{},
1212 PortsByName: portsByName,
1213 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301214 var dev sync.Map
1215 dev.Store("SDX6320031", device)
Akash Soni6f369452023-09-19 11:18:28 +05301216 grp := &of.Group{
1217 Device: "SDX6320031",
1218 GroupID: uint32(256),
1219 State: 1,
1220 SetVlan: of.VlanAny,
1221 }
1222 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
1223 db = dbintf
1224 dbintf.EXPECT().PutGroup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
1225 tests := []struct {
1226 name string
1227 args args
1228 wantErr bool
1229 }{
1230 {
1231 name: "GroupUpdate",
1232 args: args{
1233 port: "SDX6320031-1",
1234 device: "SDX6320031",
1235 group: grp,
1236 },
1237 wantErr: false,
1238 },
1239 {
1240 name: "DeviceNOtFound_Error",
1241 args: args{
1242 device: "SDX632003134",
1243 },
1244 wantErr: true,
1245 },
1246 {
1247 name: "PortNOtFound_Error",
1248 args: args{
1249 device: "SDX6320031",
1250 port: "SDX632003134",
1251 },
1252 wantErr: true,
1253 },
1254 {
1255 name: "ContextNill_Error",
1256 args: args{
1257 device: "SDX6320031",
1258 port: "SDX6320031-1",
1259 },
1260 wantErr: true,
1261 },
1262 }
1263 for _, tt := range tests {
1264 t.Run(tt.name, func(t *testing.T) {
1265 switch tt.name {
1266 case "GroupUpdate", "DeviceNOtFound_Error", "PortNOtFound_Error":
1267 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +05301268 Devices: sync.Map{},
Akash Soni6f369452023-09-19 11:18:28 +05301269 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301270 dev.Range(func(key, value interface{}) bool {
1271 v.Devices.Store(key, value)
1272 return true
1273 })
Akash Soni6f369452023-09-19 11:18:28 +05301274 if err := v.GroupUpdate(tt.args.port, tt.args.device, tt.args.group); (err != nil) != tt.wantErr {
1275 t.Errorf("VoltController.GroupUpdate() error = %v, wantErr %v", err, tt.wantErr)
1276 }
1277 case "ContextNill_Error":
1278 device := &Device{
1279 ID: "SDX6320031",
1280 groups: sync.Map{},
1281 PortsByName: portsByName,
1282 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301283 var dev sync.Map
1284 dev.Store("SDX6320031", device)
Akash Soni6f369452023-09-19 11:18:28 +05301285 v := &VoltController{
bseenivaa8cb94c2024-12-16 13:37:17 +05301286 Devices: sync.Map{},
Akash Soni6f369452023-09-19 11:18:28 +05301287 }
bseenivaa8cb94c2024-12-16 13:37:17 +05301288 dev.Range(func(key, value interface{}) bool {
1289 v.Devices.Store(key, value)
1290 return true
1291 })
Akash Soni6f369452023-09-19 11:18:28 +05301292 if err := v.GroupUpdate(tt.args.port, tt.args.device, tt.args.group); (err != nil) != tt.wantErr {
1293 t.Errorf("VoltController.GroupUpdate() error = %v, wantErr %v", err, tt.wantErr)
1294 }
1295 }
1296 })
1297 }
1298}