blob: 503fb00ae9bec52dd9b0c35daf9416424f2e8ee4 [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"
20 "encoding/json"
21 "errors"
22 "testing"
23 "voltha-go-controller/internal/test/mocks"
24
25 "github.com/golang/mock/gomock"
26 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
27)
28
29var Del_error = "Del_error"
30
31func TestUpdateDbData(t *testing.T) {
32 type args struct {
33 cntx context.Context
34 dbPath string
35 hash string
36 value interface{}
37 }
38 val := &VoltVnet{
39 Version: "test_version",
40 VnetConfig: VnetConfig{
41 Name: "test_name",
42 VnetType: "test_vnet_type",
43 },
44 VnetOper: VnetOper{
45 PendingDeviceToDelete: "test_PendingDeviceToDelete",
46 },
47 }
48 tests := []struct {
49 name string
50 args args
51 wantErr bool
52 }{
53 {
54 name: "Update_Db_Data",
55 args: args{
56 cntx: context.Background(),
57 dbPath: "vnets/",
58 hash: "test_hash",
59 value: val,
60 },
61 },
62 }
63 for _, tt := range tests {
64 t.Run(tt.name, func(t *testing.T) {
65 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
66 db = dbintf
67 dbintf.EXPECT().DelVnet(tt.args.cntx, tt.args.hash).Return(nil).Times(1)
68 if err := UpdateDbData(tt.args.cntx, tt.args.dbPath, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
69 t.Errorf("UpdateDbData() error = %v, wantErr %v", err, tt.wantErr)
70 }
71 })
72 }
73}
74
75func Test_updateServices(t *testing.T) {
76 type args struct {
77 cntx context.Context
78 hash string
79 value interface{}
80 }
81 val := &VoltService{
82 VoltServiceOper: VoltServiceOper{
83 Device: test_device,
84 },
85 Version: "test_version",
86 VoltServiceCfg: VoltServiceCfg{
87 Name: "test_name",
88 },
89 }
90 tests := []struct {
91 name string
92 args args
93 wantErr bool
94 }{
95 {
96 name: "updateServices",
97 args: args{
98 cntx: context.Background(),
99 hash: "test_hash",
100 value: val,
101 },
102 },
103 }
104 for _, tt := range tests {
105 t.Run(tt.name, func(t *testing.T) {
106 if err := updateServices(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
107 t.Errorf("updateServices() error = %v, wantErr %v", err, tt.wantErr)
108 }
109 })
110 }
111}
112
113func Test_updateVpvs(t *testing.T) {
114 type args struct {
115 cntx context.Context
116 hash string
117 value interface{}
118 }
119 val := &VoltPortVnet{
120 Device: test_device,
121 }
122 tests := []struct {
123 name string
124 args args
125 wantErr bool
126 }{
127 {
128 name: "updateVpvs",
129 args: args{
130 cntx: context.Background(),
131 hash: "test_hash",
132 value: val,
133 },
134 },
135 {
136 name: "Del error",
137 args: args{
138 cntx: context.Background(),
139 hash: "hash-hash1",
140 value: val,
141 },
142 },
143 }
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 switch tt.name {
147 case "updateVpvs":
148 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
149 db = dbintf
150 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
151 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(nil).Times(1)
152 if err := updateVpvs(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
153 t.Errorf("updateVpvs() error = %v, wantErr %v", err, tt.wantErr)
154 }
155 case "Del error":
156 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
157 db = dbintf
158 dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
159 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(errors.New("error")).Times(1)
160 if err := updateVpvs(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
161 t.Errorf("updateVpvs() error = %v, wantErr %v", err, tt.wantErr)
162 }
163 }
164 })
165 }
166}
167
168func Test_updateMvlans(t *testing.T) {
169 type args struct {
170 cntx context.Context
171 hash string
172 value interface{}
173 }
174 grp := make(map[string]*MvlanGroup)
175 grp["static"] = &MvlanGroup{
176 Name: "test_name",
177 }
178 val := &MvlanProfile{
179 Version: "test_version",
180 Name: "test_name",
181 Groups: grp,
182 }
183 tests := []struct {
184 name string
185 args args
186 wantErr bool
187 }{
188 {
189 name: "updateMvlans",
190 args: args{
191 cntx: context.Background(),
192 hash: "test_hash",
193 value: val,
194 },
195 },
196 {
197 name: write_to_db_error,
198 args: args{
199 cntx: context.Background(),
200 hash: "test_hash",
201 value: val,
202 },
203 },
204 }
205 for _, tt := range tests {
206 t.Run(tt.name, func(t *testing.T) {
207 switch tt.name {
208 case "updateMvlans":
209 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
210 db = dbintf
211 dbintf.EXPECT().PutMvlan(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
212 if err := updateMvlans(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
213 t.Errorf("updateMvlans() error = %v, wantErr %v", err, tt.wantErr)
214 }
215 case write_to_db_error:
216 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
217 db = dbintf
218 dbintf.EXPECT().PutMvlan(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
219 if err := updateMvlans(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
220 t.Errorf("updateMvlans() error = %v, wantErr %v", err, tt.wantErr)
221 }
222 }
223 })
224 }
225}
226
227func Test_updateIgmpGroups(t *testing.T) {
228 type args struct {
229 cntx context.Context
230 hash string
231 value interface{}
232 }
233 val := &IgmpGroup{
234 Version: "test_version",
235 }
236 tests := []struct {
237 name string
238 args args
239 wantErr bool
240 }{
241 {
242 name: "updateIgmpGroups",
243 args: args{
244 cntx: context.Background(),
245 hash: "test_hash",
246 value: val,
247 },
248 },
249 {
250 name: "PutIgmpGroup_error",
251 args: args{
252 cntx: context.Background(),
253 hash: "test_hash",
254 value: val,
255 },
256 },
257 }
258 for _, tt := range tests {
259 t.Run(tt.name, func(t *testing.T) {
260 switch tt.name {
261 case "updateIgmpGroups":
262 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
263 db = dbintf
264 dbintf.EXPECT().PutIgmpGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
265 if err := updateIgmpGroups(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
266 t.Errorf("updateIgmpGroups() error = %v, wantErr %v", err, tt.wantErr)
267 }
268 case "PutIgmpGroup_error":
269 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
270 db = dbintf
271 dbintf.EXPECT().PutIgmpGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
272 if err := updateIgmpGroups(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
273 t.Errorf("updateIgmpGroups() error = %v, wantErr %v", err, tt.wantErr)
274 }
275 }
276 })
277 }
278}
279
280func Test_updateIgmpDevices(t *testing.T) {
281 type args struct {
282 cntx context.Context
283 hash string
284 value interface{}
285 }
286 val := &IgmpGroupDevice{
287 Device: test_device,
288 }
289 tests := []struct {
290 name string
291 args args
292 wantErr bool
293 }{
294 {
295 name: "updateIgmpDevices",
296 args: args{
297 cntx: context.Background(),
298 hash: "test_hash",
299 value: val,
300 },
301 },
302 {
303 name: "PutIgmpDevice_error",
304 args: args{
305 cntx: context.Background(),
306 hash: "test_hash",
307 value: val,
308 },
309 },
310 }
311 for _, tt := range tests {
312 t.Run(tt.name, func(t *testing.T) {
313 switch tt.name {
314 case "updateIgmpDevices":
315 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
316 db = dbintf
317 dbintf.EXPECT().PutIgmpDevice(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
318 if err := updateIgmpDevices(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
319 t.Errorf("updateIgmpDevices() error = %v, wantErr %v", err, tt.wantErr)
320 }
321 case "PutIgmpDevice_error":
322 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
323 db = dbintf
324 dbintf.EXPECT().PutIgmpDevice(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
325 if err := updateIgmpDevices(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
326 t.Errorf("updateIgmpDevices() error = %v, wantErr %v", err, tt.wantErr)
327 }
328 }
329 })
330 }
331}
332
333func Test_updateIgmpProfiles(t *testing.T) {
334 type args struct {
335 cntx context.Context
336 hash string
337 value interface{}
338 }
339 val := &IgmpProfile{
340 ProfileID: "test_profile_id",
341 }
342 tests := []struct {
343 name string
344 args args
345 wantErr bool
346 }{
347 {
348 name: "updateIgmpProfiles",
349 args: args{
350 cntx: context.Background(),
351 hash: "test_hash",
352 value: val,
353 },
354 },
355 }
356 for _, tt := range tests {
357 t.Run(tt.name, func(t *testing.T) {
358 if err := updateIgmpProfiles(tt.args.cntx, tt.args.hash, tt.args.value); (err != nil) != tt.wantErr {
359 t.Errorf("updateIgmpProfiles() error = %v, wantErr %v", err, tt.wantErr)
360 }
361 })
362 }
363}
364
365func TestIgmpGroup_migrateIgmpDevices(t *testing.T) {
366 type args struct {
367 cntx context.Context
368 }
369 tests := []struct {
370 name string
371 args args
372 }{
373 {
374 name: "IgmpGroup_migrateIgmpDevices",
375 args: args{
376 cntx: context.Background(),
377 },
378 },
379 {
380 name: invalid_value,
381 args: args{
382 cntx: context.Background(),
383 },
384 },
385 {
386 name: Del_error,
387 args: args{
388 cntx: context.Background(),
389 },
390 },
391 {
392 name: "NewIgmpGroupDeviceFromBytes_error",
393 args: args{
394 cntx: context.Background(),
395 },
396 },
397 }
398 for _, tt := range tests {
399 t.Run(tt.name, func(t *testing.T) {
400 ig := &IgmpGroup{}
401 switch tt.name {
402 case "IgmpGroup_migrateIgmpDevices":
403 val := &IgmpGroupDevice{
404 Device: test_device,
405 }
406 b, err := json.Marshal(val)
407 if err != nil {
408 panic(err)
409 }
410 test := map[string]*kvstore.KVPair{}
411 test["test_device_id"] = &kvstore.KVPair{
412 Key: "test_device_id",
413 Value: b,
414 }
415 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
416 db = dbintf
417 dbintf.EXPECT().GetPrevIgmpDevices(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
418 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(nil).Times(1)
419 dbintf.EXPECT().PutIgmpDevice(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
420 ig.migrateIgmpDevices(tt.args.cntx)
421 case invalid_value:
422 test := map[string]*kvstore.KVPair{}
423 test["test_device_id"] = &kvstore.KVPair{
424 Key: "test_device_id",
425 Value: "invalid",
426 }
427 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
428 db = dbintf
429 dbintf.EXPECT().GetPrevIgmpDevices(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
430 ig.migrateIgmpDevices(tt.args.cntx)
431 case Del_error:
432 val := &IgmpGroupDevice{
433 Device: test_device,
434 }
435 b, err := json.Marshal(val)
436 if err != nil {
437 panic(err)
438 }
439 test := map[string]*kvstore.KVPair{}
440 test["test_device_id"] = &kvstore.KVPair{
441 Key: "test_device_id",
442 Value: b,
443 }
444 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
445 db = dbintf
446 dbintf.EXPECT().GetPrevIgmpDevices(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
447 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(errors.New("error")).Times(1)
448 dbintf.EXPECT().PutIgmpDevice(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
449 ig.migrateIgmpDevices(tt.args.cntx)
450 case "NewIgmpGroupDeviceFromBytes_error":
451 b, err := json.Marshal("test")
452 if err != nil {
453 panic(err)
454 }
455 test := map[string]*kvstore.KVPair{}
456 test["test_device_id"] = &kvstore.KVPair{
457 Key: "test_device_id",
458 Value: b,
459 }
460 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
461 db = dbintf
462 dbintf.EXPECT().GetPrevIgmpDevices(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
463 ig.migrateIgmpDevices(tt.args.cntx)
464 }
465 })
466 }
467}
468
469func TestIgmpGroupDevice_migrateIgmpChannels(t *testing.T) {
470 type args struct {
471 cntx context.Context
472 }
473 tests := []struct {
474 name string
475 args args
476 }{
477 {
478 name: "IgmpGroupDevice_migrateIgmpChannels",
479 args: args{
480 cntx: context.Background(),
481 },
482 },
483 {
484 name: invalid_value,
485 args: args{
486 cntx: context.Background(),
487 },
488 },
489 {
490 name: Del_error,
491 args: args{
492 cntx: context.Background(),
493 },
494 },
495 {
496 name: "NewIgmpGroupChannelFromBytes_error",
497 args: args{
498 cntx: context.Background(),
499 },
500 },
501 }
502 for _, tt := range tests {
503 t.Run(tt.name, func(t *testing.T) {
504 igd := &IgmpGroupDevice{}
505 switch tt.name {
506 case "IgmpGroupDevice_migrateIgmpChannels":
507 val := IgmpGroupChannel{
508 Device: test_device,
509 }
510 b, err := json.Marshal(val)
511 if err != nil {
512 panic(err)
513 }
514 test := map[string]*kvstore.KVPair{}
515 test["test_device_id"] = &kvstore.KVPair{
516 Key: "test_device_id",
517 Value: b,
518 }
519 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
520 db = dbintf
521 dbintf.EXPECT().GetPrevIgmpChannels(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
522 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(nil).Times(1)
523 dbintf.EXPECT().PutIgmpChannel(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
524 igd.migrateIgmpChannels(tt.args.cntx)
525 case invalid_value:
526 test := map[string]*kvstore.KVPair{}
527 test["test_device_id"] = &kvstore.KVPair{
528 Key: "test_device_id",
529 Value: "invalid",
530 }
531 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
532 db = dbintf
533 dbintf.EXPECT().GetPrevIgmpChannels(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
534 igd.migrateIgmpChannels(tt.args.cntx)
535 case Del_error:
536 val := IgmpGroupChannel{
537 Device: test_device,
538 }
539 b, err := json.Marshal(val)
540 if err != nil {
541 panic(err)
542 }
543 test := map[string]*kvstore.KVPair{}
544 test["test_device_id"] = &kvstore.KVPair{
545 Key: "test_device_id",
546 Value: b,
547 }
548 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
549 db = dbintf
550 dbintf.EXPECT().GetPrevIgmpChannels(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
551 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(errors.New("error")).Times(1)
552 dbintf.EXPECT().PutIgmpChannel(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
553 igd.migrateIgmpChannels(tt.args.cntx)
554 case "NewIgmpGroupChannelFromBytes_error":
555 b, err := json.Marshal("test")
556 if err != nil {
557 panic(err)
558 }
559 test := map[string]*kvstore.KVPair{}
560 test["test_device_id"] = &kvstore.KVPair{
561 Key: "test_device_id",
562 Value: b,
563 }
564 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
565 db = dbintf
566 dbintf.EXPECT().GetPrevIgmpChannels(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
567 igd.migrateIgmpChannels(tt.args.cntx)
568 }
569 })
570 }
571}
572
573func TestIgmpGroupChannel_migrateIgmpPorts(t *testing.T) {
574 type args struct {
575 cntx context.Context
576 }
577 tests := []struct {
578 name string
579 args args
580 }{
581 {
582 name: "IgmpGroupChannel_migrateIgmpPorts",
583 args: args{
584 cntx: context.Background(),
585 },
586 },
587 {
588 name: invalid_value,
589 args: args{
590 cntx: context.Background(),
591 },
592 },
593 {
594 name: Del_error,
595 args: args{
596 cntx: context.Background(),
597 },
598 },
599 {
600 name: "NewIgmpGroupPortFromBytes_error",
601 args: args{
602 cntx: context.Background(),
603 },
604 },
605 }
606 for _, tt := range tests {
607 t.Run(tt.name, func(t *testing.T) {
608 igc := &IgmpGroupChannel{}
609 switch tt.name {
610 case "IgmpGroupChannel_migrateIgmpPorts":
611 val := IgmpGroupPort{
612 Port: "test_port",
613 }
614 b, err := json.Marshal(val)
615 if err != nil {
616 panic(err)
617 }
618 test := map[string]*kvstore.KVPair{}
619 test["test_device_id"] = &kvstore.KVPair{
620 Key: "test_device_id",
621 Value: b,
622 }
623 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
624 db = dbintf
625 dbintf.EXPECT().GetPrevIgmpRcvrs(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
626 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(nil).Times(1)
627 dbintf.EXPECT().PutIgmpRcvr(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
628 igc.migrateIgmpPorts(tt.args.cntx)
629 case invalid_value:
630 test := map[string]*kvstore.KVPair{}
631 test["test_device_id"] = &kvstore.KVPair{
632 Key: "test_device_id",
633 Value: "invalid",
634 }
635 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
636 db = dbintf
637 dbintf.EXPECT().GetPrevIgmpRcvrs(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
638 igc.migrateIgmpPorts(tt.args.cntx)
639 case Del_error:
640 val := IgmpGroupPort{
641 Port: "test_port",
642 }
643 b, err := json.Marshal(val)
644 if err != nil {
645 panic(err)
646 }
647 test := map[string]*kvstore.KVPair{}
648 test["test_device_id"] = &kvstore.KVPair{
649 Key: "test_device_id",
650 Value: b,
651 }
652 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
653 db = dbintf
654 dbintf.EXPECT().GetPrevIgmpRcvrs(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
655 dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(errors.New("error")).Times(1)
656 dbintf.EXPECT().PutIgmpRcvr(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("error")).Times(1)
657 igc.migrateIgmpPorts(tt.args.cntx)
658 case "NewIgmpGroupPortFromBytes_error":
659 b, err := json.Marshal("invalid")
660 if err != nil {
661 panic(err)
662 }
663 test := map[string]*kvstore.KVPair{}
664 test["test_device_id"] = &kvstore.KVPair{
665 Key: "test_device_id",
666 Value: b,
667 }
668 dbintf := mocks.NewMockDBIntf(gomock.NewController(t))
669 db = dbintf
670 dbintf.EXPECT().GetPrevIgmpRcvrs(gomock.Any(), gomock.Any(), gomock.Any()).Return(test, nil).Times(1)
671 // dbintf.EXPECT().Del(tt.args.cntx, gomock.Any()).Return(nil).Times(1)
672 // dbintf.EXPECT().PutIgmpRcvr(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)
673 igc.migrateIgmpPorts(tt.args.cntx)
674 }
675 })
676 }
677}