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