| vinokuma | f7605fc | 2023-06-02 18:08:01 +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 application |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "testing" |
| 21 | "voltha-go-controller/internal/pkg/intf" |
| 22 | "voltha-go-controller/internal/pkg/of" |
| 23 | "voltha-go-controller/internal/pkg/util" |
| 24 | "voltha-go-controller/internal/test/mocks" |
| 25 | |
| 26 | "github.com/golang/mock/gomock" |
| 27 | ) |
| 28 | |
| 29 | var voltPortVnet = &VoltPortVnet{ |
| 30 | Device: "test_device", |
| 31 | } |
| 32 | var voltService = &VoltService{ |
| 33 | Version: "test_version", |
| 34 | } |
| 35 | |
| 36 | func TestExecuteFlowEvent(t *testing.T) { |
| 37 | type args struct { |
| 38 | cntx context.Context |
| 39 | vd *VoltDevice |
| 40 | cookie string |
| 41 | flowStatus intf.FlowStatus |
| 42 | } |
| 43 | tests := []struct { |
| 44 | name string |
| 45 | args args |
| 46 | want bool |
| 47 | }{ |
| 48 | { |
| 49 | name: "ExecuteFlowEvent_add", |
| 50 | args: args{ |
| 51 | cntx: context.Background(), |
| 52 | vd: &VoltDevice{ |
| 53 | SouthBoundID: "test_device_id", |
| 54 | FlowAddEventMap: util.NewConcurrentMap(), |
| 55 | }, |
| 56 | cookie: "test_cookie", |
| 57 | flowStatus: intf.FlowStatus{ |
| 58 | Device: "test_device", |
| 59 | FlowModType: of.CommandAdd, |
| 60 | }, |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | name: "ExecuteFlowEvent_del", |
| 65 | args: args{ |
| 66 | cntx: context.Background(), |
| 67 | vd: &VoltDevice{ |
| 68 | SouthBoundID: "test_device_id", |
| 69 | FlowDelEventMap: util.NewConcurrentMap(), |
| 70 | }, |
| 71 | cookie: "test_cookie", |
| 72 | flowStatus: intf.FlowStatus{ |
| 73 | Device: "test_device", |
| 74 | FlowModType: of.CommandDel, |
| 75 | }, |
| 76 | }, |
| 77 | }, |
| 78 | } |
| 79 | for _, tt := range tests { |
| 80 | t.Run(tt.name, func(t *testing.T) { |
| 81 | switch tt.name { |
| 82 | case "ExecuteFlowEvent_add": |
| 83 | if got := ExecuteFlowEvent(tt.args.cntx, tt.args.vd, tt.args.cookie, tt.args.flowStatus); got != tt.want { |
| 84 | t.Errorf("ExecuteFlowEvent() = %v, want %v", got, tt.want) |
| 85 | } |
| 86 | case "ExecuteFlowEvent_del": |
| 87 | if got := ExecuteFlowEvent(tt.args.cntx, tt.args.vd, tt.args.cookie, tt.args.flowStatus); got != tt.want { |
| 88 | t.Errorf("ExecuteFlowEvent() = %v, want %v", got, tt.want) |
| 89 | } |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestInitEventFuncMapper(t *testing.T) { |
| 96 | tests := []struct { |
| 97 | name string |
| 98 | }{ |
| 99 | { |
| 100 | name: "InitEventFuncMapper", |
| 101 | }, |
| 102 | } |
| 103 | for _, tt := range tests { |
| 104 | t.Run(tt.name, func(t *testing.T) { |
| 105 | InitEventFuncMapper() |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestProcessUsIgmpFlowAddEvent(t *testing.T) { |
| 111 | type args struct { |
| 112 | cntx context.Context |
| 113 | event *FlowEvent |
| 114 | flowStatus intf.FlowStatus |
| 115 | } |
| 116 | tests := []struct { |
| 117 | name string |
| 118 | args args |
| 119 | }{ |
| 120 | { |
| 121 | name: "ProcessUsIgmpFlowAddEvent", |
| 122 | args: args{ |
| 123 | cntx: context.Background(), |
| 124 | event: &FlowEvent{ |
| 125 | device: "test_device", |
| 126 | eType: EventTypeControlFlowAdded, |
| 127 | eventData: voltPortVnet, |
| 128 | }, |
| 129 | flowStatus: intf.FlowStatus{ |
| 130 | Device: "test_device", |
| 131 | Status: uint32(0), |
| 132 | }, |
| 133 | }, |
| 134 | }, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 135 | { |
| 136 | name: "ProcessUsIgmpFlowAddEvent_else_condition", |
| 137 | args: args{ |
| 138 | cntx: context.Background(), |
| 139 | event: &FlowEvent{ |
| 140 | device: "test_device", |
| 141 | eType: EventTypeControlFlowAdded, |
| 142 | eventData: voltPortVnet, |
| 143 | }, |
| 144 | flowStatus: intf.FlowStatus{ |
| 145 | Device: "test_device", |
| 146 | Status: uint32(1001), |
| 147 | }, |
| 148 | }, |
| 149 | }, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 150 | } |
| 151 | for _, tt := range tests { |
| 152 | t.Run(tt.name, func(t *testing.T) { |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 153 | ProcessUsIgmpFlowAddEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 154 | }) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestProcessServiceFlowAddEvent(t *testing.T) { |
| 159 | type args struct { |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 160 | cntx context.Context |
| 161 | event *FlowEvent |
| 162 | flowStatus intf.FlowStatus |
| 163 | flowEventMap *util.ConcurrentMap |
| 164 | } |
| Akash Reddy Kankanala | 105581b | 2024-09-11 05:20:38 +0530 | [diff] [blame] | 165 | |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 166 | vs := &VoltService{ |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 167 | VoltServiceCfg: VoltServiceCfg{}, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | tests := []struct { |
| 171 | name string |
| 172 | args args |
| 173 | }{ |
| 174 | { |
| 175 | name: "ProcessServiceFlowAddEvent", |
| 176 | args: args{ |
| 177 | cntx: context.Background(), |
| 178 | event: &FlowEvent{ |
| 179 | device: "test_device", |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 180 | eventData: vs, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 181 | }, |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 182 | flowEventMap: util.NewConcurrentMap(), |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 183 | }, |
| 184 | }, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 185 | { |
| 186 | name: "ProcessServiceFlowAddEvent_else_condition", |
| 187 | args: args{ |
| 188 | cntx: context.Background(), |
| 189 | event: &FlowEvent{ |
| 190 | device: "test_device", |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 191 | eventData: vs, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 192 | }, |
| 193 | flowStatus: intf.FlowStatus{ |
| 194 | Status: uint32(1001), |
| 195 | }, |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 196 | flowEventMap: util.NewConcurrentMap(), |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 197 | }, |
| 198 | }, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 199 | } |
| 200 | for _, tt := range tests { |
| 201 | t.Run(tt.name, func(t *testing.T) { |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 202 | ProcessServiceFlowAddEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 203 | }) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func TestProcessControlFlowAddEvent(t *testing.T) { |
| 208 | type args struct { |
| 209 | cntx context.Context |
| 210 | event *FlowEvent |
| 211 | flowStatus intf.FlowStatus |
| 212 | } |
| 213 | tests := []struct { |
| 214 | name string |
| 215 | args args |
| 216 | }{ |
| 217 | { |
| 218 | name: "ProcessControlFlowAddEvent", |
| 219 | args: args{ |
| 220 | cntx: context.Background(), |
| 221 | event: &FlowEvent{ |
| 222 | eventData: voltPortVnet, |
| 223 | }, |
| 224 | }, |
| 225 | }, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 226 | { |
| 227 | name: "ProcessControlFlowAddEvent_else_condition", |
| 228 | args: args{ |
| 229 | cntx: context.Background(), |
| 230 | event: &FlowEvent{ |
| 231 | eventData: voltPortVnet, |
| 232 | }, |
| 233 | flowStatus: intf.FlowStatus{ |
| 234 | Status: uint32(1001), |
| 235 | }, |
| 236 | }, |
| 237 | }, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 238 | } |
| 239 | for _, tt := range tests { |
| 240 | t.Run(tt.name, func(t *testing.T) { |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 241 | ProcessControlFlowAddEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 242 | }) |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func TestProcessServiceFlowDelEvent(t *testing.T) { |
| 247 | type args struct { |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 248 | cntx context.Context |
| 249 | event *FlowEvent |
| 250 | flowStatus intf.FlowStatus |
| 251 | flowEventMap *util.ConcurrentMap |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 252 | } |
| Akash Reddy Kankanala | 105581b | 2024-09-11 05:20:38 +0530 | [diff] [blame] | 253 | |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 254 | vs := &VoltService{ |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 255 | VoltServiceCfg: VoltServiceCfg{}, |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 256 | } |
| 257 | |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 258 | tests := []struct { |
| 259 | name string |
| 260 | args args |
| 261 | }{ |
| 262 | { |
| 263 | name: "ProcessServiceFlowDelEvent", |
| 264 | args: args{ |
| 265 | cntx: context.Background(), |
| 266 | event: &FlowEvent{ |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 267 | eventData: vs, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 268 | }, |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 269 | flowEventMap: util.NewConcurrentMap(), |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 270 | }, |
| 271 | }, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 272 | { |
| 273 | name: "ProcessServiceFlowDelEvent_else_condition", |
| 274 | args: args{ |
| 275 | cntx: context.Background(), |
| 276 | event: &FlowEvent{ |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 277 | eventData: vs, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 278 | }, |
| 279 | flowStatus: intf.FlowStatus{ |
| 280 | Status: uint32(1001), |
| 281 | }, |
| Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 282 | flowEventMap: util.NewConcurrentMap(), |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 283 | }, |
| 284 | }, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 285 | } |
| 286 | for _, tt := range tests { |
| 287 | t.Run(tt.name, func(t *testing.T) { |
| 288 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 289 | db = dbintf |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame^] | 290 | switch tt.name { |
| 291 | case "ProcessServiceFlowDelEvent": |
| 292 | dbintf.EXPECT().PutService(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| 293 | case "ProcessServiceFlowDelEvent_else_condition": |
| 294 | dbintf.EXPECT().PutService(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() |
| 295 | } |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 296 | ProcessServiceFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 297 | }) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | func TestProcessControlFlowDelEvent(t *testing.T) { |
| 302 | type args struct { |
| 303 | cntx context.Context |
| 304 | event *FlowEvent |
| 305 | flowStatus intf.FlowStatus |
| 306 | } |
| 307 | tests := []struct { |
| 308 | name string |
| 309 | args args |
| 310 | }{ |
| 311 | { |
| 312 | name: "ProcessControlFlowDelEvent", |
| 313 | args: args{ |
| 314 | cntx: context.Background(), |
| 315 | event: &FlowEvent{ |
| 316 | eventData: voltPortVnet, |
| 317 | }, |
| 318 | }, |
| 319 | }, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 320 | { |
| 321 | name: "ProcessControlFlowDelEvent_else_condition", |
| 322 | args: args{ |
| 323 | cntx: context.Background(), |
| 324 | event: &FlowEvent{ |
| 325 | eventData: voltPortVnet, |
| 326 | }, |
| 327 | flowStatus: intf.FlowStatus{ |
| 328 | Status: uint32(1001), |
| 329 | }, |
| 330 | }, |
| 331 | }, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 332 | } |
| 333 | for _, tt := range tests { |
| 334 | t.Run(tt.name, func(t *testing.T) { |
| 335 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 336 | db = dbintf |
| 337 | dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 338 | ProcessControlFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 339 | }) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | func TestProcessMcastFlowDelEvent(t *testing.T) { |
| 344 | type args struct { |
| 345 | cntx context.Context |
| 346 | event *FlowEvent |
| 347 | flowStatus intf.FlowStatus |
| 348 | } |
| 349 | mvlanProfile := &MvlanProfile{ |
| 350 | Version: "test_version", |
| 351 | } |
| 352 | tests := []struct { |
| 353 | name string |
| 354 | args args |
| 355 | }{ |
| 356 | { |
| 357 | name: "ProcessMcastFlowDelEvent", |
| 358 | args: args{ |
| 359 | cntx: context.Background(), |
| 360 | event: &FlowEvent{ |
| 361 | eventData: mvlanProfile, |
| 362 | }, |
| 363 | }, |
| 364 | }, |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 365 | { |
| 366 | name: "ProcessMcastFlowDelEvent_else_condition", |
| 367 | args: args{ |
| 368 | cntx: context.Background(), |
| 369 | event: &FlowEvent{ |
| 370 | eventData: mvlanProfile, |
| 371 | }, |
| 372 | flowStatus: intf.FlowStatus{ |
| 373 | Status: uint32(1001), |
| 374 | }, |
| 375 | }, |
| 376 | }, |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 377 | } |
| 378 | for _, tt := range tests { |
| 379 | t.Run(tt.name, func(t *testing.T) { |
| 380 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 381 | db = dbintf |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame^] | 382 | switch tt.name { |
| 383 | case "ProcessMcastFlowDelEvent": |
| 384 | dbintf.EXPECT().PutMvlan(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| 385 | case "ProcessMcastFlowDelEvent_else_condition": |
| 386 | dbintf.EXPECT().PutMvlan(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() |
| 387 | } |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 388 | ProcessMcastFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 389 | }) |
| 390 | } |
| 391 | } |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 392 | |
| 393 | func TestProcessDeviceFlowDelEvent(t *testing.T) { |
| 394 | type args struct { |
| 395 | cntx context.Context |
| 396 | event *FlowEvent |
| 397 | flowStatus intf.FlowStatus |
| 398 | } |
| 399 | tests := []struct { |
| 400 | name string |
| 401 | args args |
| 402 | }{ |
| 403 | { |
| 404 | name: "ProcessDeviceFlowDelEvent", |
| 405 | args: args{ |
| 406 | cntx: context.Background(), |
| 407 | event: &FlowEvent{ |
| 408 | device: test_device, |
| 409 | eventData: voltVnet, |
| 410 | }, |
| 411 | flowStatus: intf.FlowStatus{ |
| 412 | Device: test_device, |
| 413 | }, |
| 414 | }, |
| 415 | }, |
| 416 | { |
| 417 | name: "ProcessDeviceFlowDelEvent_else_condition", |
| 418 | args: args{ |
| 419 | cntx: context.Background(), |
| 420 | event: &FlowEvent{ |
| 421 | device: test_device, |
| 422 | eventData: voltVnet, |
| 423 | }, |
| 424 | flowStatus: intf.FlowStatus{ |
| 425 | Device: test_device, |
| 426 | Status: uint32(1001), |
| 427 | }, |
| 428 | }, |
| 429 | }, |
| 430 | } |
| 431 | for _, tt := range tests { |
| 432 | t.Run(tt.name, func(t *testing.T) { |
| 433 | switch tt.name { |
| 434 | case "ProcessDeviceFlowDelEvent": |
| 435 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 436 | db = dbintf |
| 437 | dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil).AnyTimes() |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 438 | ProcessDeviceFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 439 | case "ProcessDeviceFlowDelEvent_else_condition": |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 440 | ProcessDeviceFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus) |
| vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 441 | } |
| 442 | }) |
| 443 | } |
| 444 | } |