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) { |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 153 | ProcessUsIgmpFlowAddEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, nil) |
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 | } |
| 165 | |
| 166 | flowPushCountMap := make(map[string]uint32) |
| 167 | vs := &VoltService{ |
| 168 | VoltServiceCfg: VoltServiceCfg{ |
| 169 | FlowPushCount: flowPushCountMap, |
| 170 | }, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | tests := []struct { |
| 174 | name string |
| 175 | args args |
| 176 | }{ |
| 177 | { |
| 178 | name: "ProcessServiceFlowAddEvent", |
| 179 | args: args{ |
| 180 | cntx: context.Background(), |
| 181 | event: &FlowEvent{ |
| 182 | device: "test_device", |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 183 | eventData: vs, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 184 | }, |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 185 | flowEventMap: util.NewConcurrentMap(), |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 186 | }, |
| 187 | }, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 188 | { |
| 189 | name: "ProcessServiceFlowAddEvent_else_condition", |
| 190 | args: args{ |
| 191 | cntx: context.Background(), |
| 192 | event: &FlowEvent{ |
| 193 | device: "test_device", |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 194 | eventData: vs, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 195 | }, |
| 196 | flowStatus: intf.FlowStatus{ |
| 197 | Status: uint32(1001), |
| 198 | }, |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 199 | flowEventMap: util.NewConcurrentMap(), |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 200 | }, |
| 201 | }, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 202 | } |
| 203 | for _, tt := range tests { |
| 204 | t.Run(tt.name, func(t *testing.T) { |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 205 | ProcessServiceFlowAddEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, tt.args.flowEventMap) |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 206 | }) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | func TestProcessControlFlowAddEvent(t *testing.T) { |
| 211 | type args struct { |
| 212 | cntx context.Context |
| 213 | event *FlowEvent |
| 214 | flowStatus intf.FlowStatus |
| 215 | } |
| 216 | tests := []struct { |
| 217 | name string |
| 218 | args args |
| 219 | }{ |
| 220 | { |
| 221 | name: "ProcessControlFlowAddEvent", |
| 222 | args: args{ |
| 223 | cntx: context.Background(), |
| 224 | event: &FlowEvent{ |
| 225 | eventData: voltPortVnet, |
| 226 | }, |
| 227 | }, |
| 228 | }, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 229 | { |
| 230 | name: "ProcessControlFlowAddEvent_else_condition", |
| 231 | args: args{ |
| 232 | cntx: context.Background(), |
| 233 | event: &FlowEvent{ |
| 234 | eventData: voltPortVnet, |
| 235 | }, |
| 236 | flowStatus: intf.FlowStatus{ |
| 237 | Status: uint32(1001), |
| 238 | }, |
| 239 | }, |
| 240 | }, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 241 | } |
| 242 | for _, tt := range tests { |
| 243 | t.Run(tt.name, func(t *testing.T) { |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 244 | ProcessControlFlowAddEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, nil) |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 245 | }) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | func TestProcessServiceFlowDelEvent(t *testing.T) { |
| 250 | type args struct { |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 251 | cntx context.Context |
| 252 | event *FlowEvent |
| 253 | flowStatus intf.FlowStatus |
| 254 | flowEventMap *util.ConcurrentMap |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 255 | } |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 256 | |
| 257 | flowPushCountMap := make(map[string]uint32) |
| 258 | vs := &VoltService{ |
| 259 | VoltServiceCfg: VoltServiceCfg{ |
| 260 | FlowPushCount: flowPushCountMap, |
| 261 | }, |
| 262 | } |
| 263 | |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 264 | tests := []struct { |
| 265 | name string |
| 266 | args args |
| 267 | }{ |
| 268 | { |
| 269 | name: "ProcessServiceFlowDelEvent", |
| 270 | args: args{ |
| 271 | cntx: context.Background(), |
| 272 | event: &FlowEvent{ |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 273 | eventData: vs, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 274 | }, |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 275 | flowEventMap: util.NewConcurrentMap(), |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 276 | }, |
| 277 | }, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 278 | { |
| 279 | name: "ProcessServiceFlowDelEvent_else_condition", |
| 280 | args: args{ |
| 281 | cntx: context.Background(), |
| 282 | event: &FlowEvent{ |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 283 | eventData: vs, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 284 | }, |
| 285 | flowStatus: intf.FlowStatus{ |
| 286 | Status: uint32(1001), |
| 287 | }, |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 288 | flowEventMap: util.NewConcurrentMap(), |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 289 | }, |
| 290 | }, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 291 | } |
| 292 | for _, tt := range tests { |
| 293 | t.Run(tt.name, func(t *testing.T) { |
| 294 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 295 | db = dbintf |
| 296 | dbintf.EXPECT().PutService(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 297 | ProcessServiceFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, tt.args.flowEventMap) |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 298 | }) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestProcessControlFlowDelEvent(t *testing.T) { |
| 303 | type args struct { |
| 304 | cntx context.Context |
| 305 | event *FlowEvent |
| 306 | flowStatus intf.FlowStatus |
| 307 | } |
| 308 | tests := []struct { |
| 309 | name string |
| 310 | args args |
| 311 | }{ |
| 312 | { |
| 313 | name: "ProcessControlFlowDelEvent", |
| 314 | args: args{ |
| 315 | cntx: context.Background(), |
| 316 | event: &FlowEvent{ |
| 317 | eventData: voltPortVnet, |
| 318 | }, |
| 319 | }, |
| 320 | }, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 321 | { |
| 322 | name: "ProcessControlFlowDelEvent_else_condition", |
| 323 | args: args{ |
| 324 | cntx: context.Background(), |
| 325 | event: &FlowEvent{ |
| 326 | eventData: voltPortVnet, |
| 327 | }, |
| 328 | flowStatus: intf.FlowStatus{ |
| 329 | Status: uint32(1001), |
| 330 | }, |
| 331 | }, |
| 332 | }, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 333 | } |
| 334 | for _, tt := range tests { |
| 335 | t.Run(tt.name, func(t *testing.T) { |
| 336 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 337 | db = dbintf |
| 338 | dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 339 | ProcessControlFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, nil) |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 340 | }) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | func TestProcessMcastFlowDelEvent(t *testing.T) { |
| 345 | type args struct { |
| 346 | cntx context.Context |
| 347 | event *FlowEvent |
| 348 | flowStatus intf.FlowStatus |
| 349 | } |
| 350 | mvlanProfile := &MvlanProfile{ |
| 351 | Version: "test_version", |
| 352 | } |
| 353 | tests := []struct { |
| 354 | name string |
| 355 | args args |
| 356 | }{ |
| 357 | { |
| 358 | name: "ProcessMcastFlowDelEvent", |
| 359 | args: args{ |
| 360 | cntx: context.Background(), |
| 361 | event: &FlowEvent{ |
| 362 | eventData: mvlanProfile, |
| 363 | }, |
| 364 | }, |
| 365 | }, |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 366 | { |
| 367 | name: "ProcessMcastFlowDelEvent_else_condition", |
| 368 | args: args{ |
| 369 | cntx: context.Background(), |
| 370 | event: &FlowEvent{ |
| 371 | eventData: mvlanProfile, |
| 372 | }, |
| 373 | flowStatus: intf.FlowStatus{ |
| 374 | Status: uint32(1001), |
| 375 | }, |
| 376 | }, |
| 377 | }, |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 378 | } |
| 379 | for _, tt := range tests { |
| 380 | t.Run(tt.name, func(t *testing.T) { |
| 381 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 382 | db = dbintf |
| 383 | dbintf.EXPECT().PutMvlan(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 384 | ProcessMcastFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, nil) |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 385 | }) |
| 386 | } |
| 387 | } |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 388 | |
| 389 | func TestProcessDeviceFlowDelEvent(t *testing.T) { |
| 390 | type args struct { |
| 391 | cntx context.Context |
| 392 | event *FlowEvent |
| 393 | flowStatus intf.FlowStatus |
| 394 | } |
| 395 | tests := []struct { |
| 396 | name string |
| 397 | args args |
| 398 | }{ |
| 399 | { |
| 400 | name: "ProcessDeviceFlowDelEvent", |
| 401 | args: args{ |
| 402 | cntx: context.Background(), |
| 403 | event: &FlowEvent{ |
| 404 | device: test_device, |
| 405 | eventData: voltVnet, |
| 406 | }, |
| 407 | flowStatus: intf.FlowStatus{ |
| 408 | Device: test_device, |
| 409 | }, |
| 410 | }, |
| 411 | }, |
| 412 | { |
| 413 | name: "ProcessDeviceFlowDelEvent_else_condition", |
| 414 | args: args{ |
| 415 | cntx: context.Background(), |
| 416 | event: &FlowEvent{ |
| 417 | device: test_device, |
| 418 | eventData: voltVnet, |
| 419 | }, |
| 420 | flowStatus: intf.FlowStatus{ |
| 421 | Device: test_device, |
| 422 | Status: uint32(1001), |
| 423 | }, |
| 424 | }, |
| 425 | }, |
| 426 | } |
| 427 | for _, tt := range tests { |
| 428 | t.Run(tt.name, func(t *testing.T) { |
| 429 | switch tt.name { |
| 430 | case "ProcessDeviceFlowDelEvent": |
| 431 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 432 | db = dbintf |
| 433 | dbintf.EXPECT().PutVnet(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil).AnyTimes() |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 434 | ProcessDeviceFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, nil) |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 435 | case "ProcessDeviceFlowDelEvent_else_condition": |
Sridhar Ravindra | 3ec1423 | 2024-01-01 19:11:48 +0530 | [diff] [blame] | 436 | ProcessDeviceFlowDelEvent(tt.args.cntx, tt.args.event, tt.args.flowStatus, nil) |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 437 | } |
| 438 | }) |
| 439 | } |
| 440 | } |