cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package resourcemanager |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
| 21 | "errors" |
Scott Baker | f8424cc | 2019-10-18 11:26:50 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/pkg/db/kvstore" |
| 23 | "github.com/opencord/voltha-lib-go/pkg/db/model" |
| 24 | "github.com/opencord/voltha-lib-go/pkg/log" |
| 25 | ponrmgr "github.com/opencord/voltha-lib-go/pkg/ponresourcemanager" |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 26 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/go/openolt" |
| 28 | "reflect" |
| 29 | "strconv" |
| 30 | "strings" |
| 31 | "testing" |
| 32 | ) |
| 33 | |
| 34 | func init() { |
| 35 | log.SetDefaultLogger(log.JSON, log.DebugLevel, nil) |
| 36 | } |
| 37 | |
| 38 | const ( |
| 39 | // MeterConfig meter to extract meter |
| 40 | MeterConfig = "meter_id" |
| 41 | // TpIDSuffixPath to extract Techprofile |
| 42 | TpIDSuffixPath = "tp_id" |
| 43 | // FlowIDInfo to extract flows |
| 44 | FlowIDInfo = "flow_id_info" |
| 45 | // FlowIds to extract flows |
| 46 | FlowIDs = "flow_ids" |
| 47 | // GemportIDs to gemport_ids |
| 48 | GemportIDs = "gemport_ids" |
| 49 | // AllocIDs to extract alloc_ids |
| 50 | AllocIDs = "alloc_ids" |
| 51 | // GemportIDPool to extract gemport |
| 52 | GemportIDPool = "gemport_id_pool" |
| 53 | // AllocIDPool to extract allocid |
| 54 | AllocIDPool = "alloc_id_pool" |
| 55 | // FlowIDpool to extract Flow ids |
| 56 | FlowIDpool = "flow_id_pool" |
| 57 | ) |
| 58 | |
| 59 | type fields struct { |
| 60 | DeviceID string |
| 61 | HostAndPort string |
| 62 | Args string |
| 63 | KVStore *model.Backend |
| 64 | DeviceType string |
| 65 | Host string |
| 66 | Port int |
| 67 | DevInfo *openolt.DeviceInfo |
| 68 | ResourceMgrs map[uint32]*ponrmgr.PONResourceManager |
| 69 | } |
| 70 | type MockResKVClient struct { |
| 71 | } |
| 72 | |
| 73 | func getResMgr() *fields { |
| 74 | var resMgr fields |
| 75 | resMgr.KVStore = &model.Backend{ |
| 76 | Client: &MockResKVClient{}, |
| 77 | } |
| 78 | resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager) |
| 79 | ranges := make(map[string]interface{}) |
| 80 | sharedIdxByType := make(map[string]string) |
| 81 | sharedIdxByType["ALLOC_ID"] = "ALLOC_ID" |
| 82 | sharedIdxByType["ONU_ID"] = "ONU_ID" |
| 83 | sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID" |
| 84 | sharedIdxByType["FLOW_ID"] = "FLOW_ID" |
| 85 | ranges["ONU_ID"] = uint32(0) |
| 86 | ranges["GEMPORT_ID"] = uint32(0) |
| 87 | ranges["ALLOC_ID"] = uint32(0) |
| 88 | ranges["FLOW_ID"] = uint32(0) |
| 89 | ranges["onu_id_shared"] = uint32(0) |
| 90 | ranges["alloc_id_shared"] = uint32(0) |
| 91 | ranges["gemport_id_shared"] = uint32(0) |
| 92 | ranges["flow_id_shared"] = uint32(0) |
| 93 | ponMgr := &ponrmgr.PONResourceManager{ |
| 94 | DeviceID: "onu-1", |
| 95 | IntfIDs: []uint32{1, 2}, |
| 96 | KVStore: &model.Backend{ |
| 97 | Client: &MockResKVClient{}, |
| 98 | }, |
| 99 | PonResourceRanges: ranges, |
| 100 | SharedIdxByType: sharedIdxByType, |
| 101 | } |
| 102 | resMgr.ResourceMgrs[1] = ponMgr |
| 103 | resMgr.ResourceMgrs[2] = ponMgr |
| 104 | return &resMgr |
| 105 | } |
| 106 | func (kvclient *MockResKVClient) List(key string, timeout int, lock ...bool) (map[string]*kvstore.KVPair, error) { |
| 107 | return nil, errors.New("key didn't find") |
| 108 | } |
| 109 | |
| 110 | // Get mock function implementation for KVClient |
| 111 | func (kvclient *MockResKVClient) Get(key string, timeout int, lock ...bool) (*kvstore.KVPair, error) { |
| 112 | log.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key}) |
| 113 | if key != "" { |
| 114 | if strings.Contains(key, MeterConfig) { |
| 115 | var bands []*ofp.OfpMeterBandHeader |
| 116 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 117 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}}) |
| 118 | |
| 119 | bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK, |
| 120 | Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}}) |
| 121 | |
| 122 | sep := strings.Split(key, "/")[2] |
| 123 | val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32) |
| 124 | if uint32(val) > 1 { |
| 125 | meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands} |
| 126 | str, _ := json.Marshal(meterConfig) |
| 127 | |
| 128 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 129 | } |
| 130 | return nil, errors.New("invalid meter") |
| 131 | } |
| 132 | if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) { |
| 133 | log.Debug("Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool) |
| 134 | data := make(map[string]interface{}) |
| 135 | data["pool"] = "1024" |
| 136 | data["start_idx"] = 1 |
| 137 | data["end_idx"] = 1024 |
| 138 | str, _ := json.Marshal(data) |
| 139 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 140 | } |
| 141 | if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) { |
| 142 | log.Debug("Error Error Error Key:", FlowIDs, FlowIDInfo) |
| 143 | str, _ := json.Marshal([]uint32{1, 2}) |
| 144 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 145 | } |
| 146 | if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) { |
| 147 | log.Debug("Error Error Error Key:", AllocIDs, GemportIDs) |
| 148 | str, _ := json.Marshal(1) |
| 149 | return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil |
| 150 | } |
| 151 | maps := make(map[string]*kvstore.KVPair) |
| 152 | maps[key] = &kvstore.KVPair{Key: key} |
| 153 | return maps[key], nil |
| 154 | } |
| 155 | return nil, errors.New("key didn't find") |
| 156 | } |
| 157 | |
| 158 | // Put mock function implementation for KVClient |
| 159 | func (kvclient *MockResKVClient) Put(key string, value interface{}, timeout int, lock ...bool) error { |
| 160 | if key != "" { |
| 161 | return nil |
| 162 | } |
| 163 | return errors.New("key didn't find") |
| 164 | } |
| 165 | |
| 166 | // Delete mock function implementation for KVClient |
| 167 | func (kvclient *MockResKVClient) Delete(key string, timeout int, lock ...bool) error { |
| 168 | return nil |
| 169 | } |
| 170 | |
| 171 | // Reserve mock function implementation for KVClient |
| 172 | func (kvclient *MockResKVClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) { |
| 173 | return nil, errors.New("key didn't find") |
| 174 | } |
| 175 | |
| 176 | // ReleaseReservation mock function implementation for KVClient |
| 177 | func (kvclient *MockResKVClient) ReleaseReservation(key string) error { |
| 178 | return nil |
| 179 | } |
| 180 | |
| 181 | // ReleaseAllReservations mock function implementation for KVClient |
| 182 | func (kvclient *MockResKVClient) ReleaseAllReservations() error { |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // RenewReservation mock function implementation for KVClient |
| 187 | func (kvclient *MockResKVClient) RenewReservation(key string) error { |
| 188 | return nil |
| 189 | } |
| 190 | |
| 191 | // Watch mock function implementation for KVClient |
| 192 | func (kvclient *MockResKVClient) Watch(key string) chan *kvstore.Event { |
| 193 | return nil |
| 194 | } |
| 195 | |
| 196 | // AcquireLock mock function implementation for KVClient |
| 197 | func (kvclient *MockResKVClient) AcquireLock(lockName string, timeout int) error { |
| 198 | return nil |
| 199 | } |
| 200 | |
| 201 | // ReleaseLock mock function implementation for KVClient |
| 202 | func (kvclient *MockResKVClient) ReleaseLock(lockName string) error { |
| 203 | return nil |
| 204 | } |
| 205 | |
| 206 | // IsConnectionUp mock function implementation for KVClient |
| 207 | func (kvclient *MockResKVClient) IsConnectionUp(timeout int) bool { // timeout in second |
| 208 | return true |
| 209 | } |
| 210 | |
| 211 | // CloseWatch mock function implementation for KVClient |
| 212 | func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) { |
| 213 | } |
| 214 | |
| 215 | // Close mock function implementation for KVClient |
| 216 | func (kvclient *MockResKVClient) Close() { |
| 217 | } |
| 218 | |
| 219 | func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr { |
| 220 | return &OpenOltResourceMgr{ |
| 221 | DeviceID: testResMgr.DeviceID, |
| 222 | HostAndPort: testResMgr.HostAndPort, |
| 223 | Args: testResMgr.Args, |
| 224 | KVStore: testResMgr.KVStore, |
| 225 | DeviceType: testResMgr.DeviceType, |
| 226 | Host: testResMgr.Host, |
| 227 | Port: testResMgr.Port, |
| 228 | DevInfo: testResMgr.DevInfo, |
| 229 | ResourceMgrs: testResMgr.ResourceMgrs, |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | func TestNewResourceMgr(t *testing.T) { |
| 234 | type args struct { |
| 235 | deviceID string |
| 236 | KVStoreHostPort string |
| 237 | kvStoreType string |
| 238 | deviceType string |
| 239 | devInfo *openolt.DeviceInfo |
| 240 | } |
| 241 | tests := []struct { |
| 242 | name string |
| 243 | args args |
| 244 | want *OpenOltResourceMgr |
| 245 | }{ |
| 246 | {"NewResourceMgr-1", args{"olt1", "1:2", "consul", |
| 247 | "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}}, |
| 248 | {"NewResourceMgr-2", args{"olt2", "3:4", "etcd", |
| 249 | "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}}, |
| 250 | } |
| 251 | for _, tt := range tests { |
| 252 | t.Run(tt.name, func(t *testing.T) { |
| 253 | if got := NewResourceMgr(tt.args.deviceID, tt.args.KVStoreHostPort, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 254 | t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want) |
| 255 | } |
| 256 | }) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func TestOpenOltResourceMgr_Delete(t *testing.T) { |
| 261 | tests := []struct { |
| 262 | name string |
| 263 | fields *fields |
| 264 | wantErr error |
| 265 | }{ |
| 266 | {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")}, |
| 267 | } |
| 268 | for _, tt := range tests { |
| 269 | t.Run(tt.name, func(t *testing.T) { |
| 270 | RsrcMgr := testResMgrObject(tt.fields) |
| 271 | if err := RsrcMgr.Delete(); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 272 | t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) |
| 273 | } |
| 274 | }) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) { |
| 279 | type args struct { |
| 280 | IntfID uint32 |
| 281 | onuID int32 |
| 282 | uniID int32 |
| 283 | FlowID uint32 |
| 284 | } |
| 285 | tests := []struct { |
| 286 | name string |
| 287 | fields *fields |
| 288 | args args |
| 289 | }{ |
| 290 | {"FreeFlowID-1", getResMgr(), args{2, 2, 2, 2}}, |
| 291 | } |
| 292 | for _, tt := range tests { |
| 293 | t.Run(tt.name, func(t *testing.T) { |
| 294 | RsrcMgr := testResMgrObject(tt.fields) |
| 295 | RsrcMgr.FreeFlowID(tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID) |
| 296 | }) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) { |
| 301 | |
| 302 | type args struct { |
| 303 | IntfID uint32 |
| 304 | onuID uint32 |
| 305 | uniID uint32 |
| 306 | FlowID []uint32 |
| 307 | } |
| 308 | tests := []struct { |
| 309 | name string |
| 310 | fields *fields |
| 311 | args args |
| 312 | }{ |
| 313 | {"FreeFlowIDs-1", getResMgr(), args{2, 2, 2, []uint32{1, 2}}}, |
| 314 | } |
| 315 | for _, tt := range tests { |
| 316 | t.Run(tt.name, func(t *testing.T) { |
| 317 | RsrcMgr := testResMgrObject(tt.fields) |
| 318 | RsrcMgr.FreeFlowIDs(tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID) |
| 319 | }) |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) { |
| 324 | type args struct { |
| 325 | intfID uint32 |
| 326 | onuID uint32 |
| 327 | uniID uint32 |
| 328 | } |
| 329 | tests := []struct { |
| 330 | name string |
| 331 | fields *fields |
| 332 | args args |
| 333 | }{ |
| 334 | {"FreePONResourcesForONU-1", getResMgr(), args{2, 0, 2}}, |
| 335 | } |
| 336 | for _, tt := range tests { |
| 337 | t.Run(tt.name, func(t *testing.T) { |
| 338 | RsrcMgr := testResMgrObject(tt.fields) |
| 339 | RsrcMgr.FreePONResourcesForONU(tt.args.intfID, tt.args.onuID, tt.args.uniID) |
| 340 | }) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | func TestOpenOltResourceMgr_FreeonuID(t *testing.T) { |
| 345 | type args struct { |
| 346 | intfID uint32 |
| 347 | onuID []uint32 |
| 348 | } |
| 349 | tests := []struct { |
| 350 | name string |
| 351 | fields *fields |
| 352 | args args |
| 353 | }{ |
| 354 | {"FreeOnuID-1", getResMgr(), args{2, []uint32{1, 2}}}, |
| 355 | } |
| 356 | for _, tt := range tests { |
| 357 | t.Run(tt.name, func(t *testing.T) { |
| 358 | RsrcMgr := testResMgrObject(tt.fields) |
| 359 | RsrcMgr.FreeonuID(tt.args.intfID, tt.args.onuID) |
| 360 | }) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | func TestOpenOltResourceMgr_GetAllocID(t *testing.T) { |
| 365 | |
| 366 | type args struct { |
| 367 | intfID uint32 |
| 368 | onuID uint32 |
| 369 | uniID uint32 |
| 370 | } |
| 371 | tests := []struct { |
| 372 | name string |
| 373 | fields *fields |
| 374 | args args |
| 375 | want uint32 |
| 376 | }{ |
| 377 | {"GetAllocID-1", getResMgr(), args{2, 2, 2}, 0}, |
| 378 | } |
| 379 | for _, tt := range tests { |
| 380 | t.Run(tt.name, func(t *testing.T) { |
| 381 | RsrcMgr := testResMgrObject(tt.fields) |
| 382 | if got := RsrcMgr.GetAllocID(tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 383 | t.Errorf("GetAllocID() = %v, want %v", got, tt.want) |
| 384 | } |
| 385 | }) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) { |
| 390 | type args struct { |
| 391 | intfID uint32 |
| 392 | onuID uint32 |
| 393 | uniID uint32 |
| 394 | } |
| 395 | tests := []struct { |
| 396 | name string |
| 397 | fields *fields |
| 398 | args args |
| 399 | want uint32 |
| 400 | }{ |
| 401 | {"GetCurrentAllocIDForOnu-1", getResMgr(), args{2, 2, 2}, 0}, |
| 402 | } |
| 403 | for _, tt := range tests { |
| 404 | t.Run(tt.name, func(t *testing.T) { |
| 405 | RsrcMgr := testResMgrObject(tt.fields) |
| 406 | if got := RsrcMgr.GetCurrentAllocIDForOnu(tt.args.intfID, tt.args.onuID, tt.args.uniID); got != tt.want { |
| 407 | t.Errorf("GetCurrentAllocIDForOnu() = %v, want %v", got, tt.want) |
| 408 | } |
| 409 | }) |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) { |
| 414 | |
| 415 | type args struct { |
| 416 | PONIntfID uint32 |
| 417 | ONUID uint32 |
| 418 | UNIID uint32 |
| 419 | } |
| 420 | tests := []struct { |
| 421 | name string |
| 422 | fields *fields |
| 423 | args args |
| 424 | want []uint32 |
| 425 | }{ |
| 426 | {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}}, |
| 427 | } |
| 428 | for _, tt := range tests { |
| 429 | t.Run(tt.name, func(t *testing.T) { |
| 430 | RsrcMgr := testResMgrObject(tt.fields) |
| 431 | if got := RsrcMgr.GetCurrentFlowIDsForOnu(tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 432 | t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want) |
| 433 | } |
| 434 | }) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) { |
| 439 | type args struct { |
| 440 | intfID uint32 |
| 441 | onuID uint32 |
| 442 | uniID uint32 |
| 443 | } |
| 444 | tests := []struct { |
| 445 | name string |
| 446 | fields *fields |
| 447 | args args |
| 448 | want []uint32 |
| 449 | }{ |
| 450 | {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}}, |
| 451 | } |
| 452 | for _, tt := range tests { |
| 453 | t.Run(tt.name, func(t *testing.T) { |
| 454 | RsrcMgr := testResMgrObject(tt.fields) |
| 455 | if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 456 | t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want) |
| 457 | } |
| 458 | }) |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | func TestOpenOltResourceMgr_GetFlowID(t *testing.T) { |
| 463 | |
| 464 | type args struct { |
| 465 | ponIntfID uint32 |
| 466 | ONUID uint32 |
| 467 | uniID uint32 |
| 468 | gemportID uint32 |
| 469 | flowStoreCookie uint64 |
| 470 | flowCategory string |
| 471 | vlanPcp []uint32 |
| 472 | } |
| 473 | tests := []struct { |
| 474 | name string |
| 475 | fields *fields |
| 476 | args args |
| 477 | want uint32 |
| 478 | wantErr error |
| 479 | }{ |
| 480 | {"GetFlowID-1", getResMgr(), args{2, 2, 2, 2, 2, |
| 481 | "HSIA", nil}, 0, errors.New("failed to get flows")}, |
| 482 | } |
| 483 | for _, tt := range tests { |
| 484 | t.Run(tt.name, func(t *testing.T) { |
| 485 | RsrcMgr := testResMgrObject(tt.fields) |
| 486 | got, err := RsrcMgr.GetFlowID(tt.args.ponIntfID, tt.args.ONUID, tt.args.uniID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...) |
| 487 | if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 488 | t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr) |
| 489 | return |
| 490 | } |
| 491 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 492 | t.Errorf("GetFlowID() got = %v, want %v", got, tt.want) |
| 493 | } |
| 494 | }) |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) { |
| 499 | type args struct { |
| 500 | ponPort uint32 |
| 501 | onuID uint32 |
| 502 | uniID uint32 |
| 503 | NumOfPorts uint32 |
| 504 | } |
| 505 | tests := []struct { |
| 506 | name string |
| 507 | fields *fields |
| 508 | args args |
| 509 | want []uint32 |
| 510 | wantErr error |
| 511 | }{ |
| 512 | {"GetGEMPortID-1", getResMgr(), args{2, 2, 2, 2}, []uint32{}, |
| 513 | errors.New("failed to get gem port")}, |
| 514 | } |
| 515 | for _, tt := range tests { |
| 516 | t.Run(tt.name, func(t *testing.T) { |
| 517 | RsrcMgr := testResMgrObject(tt.fields) |
| 518 | got, err := RsrcMgr.GetGEMPortID(tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts) |
| 519 | if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 520 | t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr) |
| 521 | return |
| 522 | } |
| 523 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 524 | t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want) |
| 525 | } |
| 526 | }) |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) { |
| 531 | type args struct { |
| 532 | Direction string |
| 533 | IntfID uint32 |
| 534 | OnuID uint32 |
| 535 | UniID uint32 |
| 536 | } |
| 537 | tests := []struct { |
| 538 | name string |
| 539 | fields *fields |
| 540 | args args |
| 541 | want *ofp.OfpMeterConfig |
| 542 | wantErr error |
| 543 | }{ |
| 544 | {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 1, 1}, |
| 545 | &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")}, |
| 546 | {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 2, 2, 2}, |
| 547 | &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")}, |
| 548 | } |
| 549 | for _, tt := range tests { |
| 550 | t.Run(tt.name, func(t *testing.T) { |
| 551 | RsrcMgr := testResMgrObject(tt.fields) |
| 552 | got, err := RsrcMgr.GetMeterIDForOnu(tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID) |
| 553 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil { |
| 554 | t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want) |
| 555 | } |
| 556 | }) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | func TestOpenOltResourceMgr_GetONUID(t *testing.T) { |
| 561 | type args struct { |
| 562 | ponIntfID uint32 |
| 563 | } |
| 564 | tests := []struct { |
| 565 | name string |
| 566 | fields *fields |
| 567 | args args |
| 568 | want uint32 |
| 569 | wantErr error |
| 570 | }{ |
| 571 | {"GetONUID-1", getResMgr(), args{2}, uint32(0), errors.New("json errors")}, |
| 572 | } |
| 573 | for _, tt := range tests { |
| 574 | t.Run(tt.name, func(t *testing.T) { |
| 575 | RsrcMgr := testResMgrObject(tt.fields) |
| 576 | got, err := RsrcMgr.GetONUID(tt.args.ponIntfID) |
| 577 | if got != tt.want && err != nil { |
| 578 | t.Errorf("GetONUID() got = %v, want %v", got, tt.want) |
| 579 | } |
| 580 | }) |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) { |
| 585 | |
| 586 | type args struct { |
| 587 | IntfID uint32 |
| 588 | OnuID uint32 |
| 589 | UniID uint32 |
| 590 | } |
| 591 | tests := []struct { |
| 592 | name string |
| 593 | fields *fields |
| 594 | args args |
| 595 | want uint32 |
| 596 | }{ |
| 597 | {"GetTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2}, |
| 598 | uint32(1)}, |
| 599 | } |
| 600 | for _, tt := range tests { |
| 601 | t.Run(tt.name, func(t *testing.T) { |
| 602 | RsrcMgr := testResMgrObject(tt.fields) |
| 603 | if got := RsrcMgr.GetTechProfileIDForOnu(tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 604 | t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want) |
| 605 | } |
| 606 | }) |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) { |
| 611 | type args struct { |
| 612 | ponIntfID uint32 |
| 613 | onuID uint32 |
| 614 | uniID uint32 |
| 615 | flowStoreCookie uint64 |
| 616 | } |
| 617 | tests := []struct { |
| 618 | name string |
| 619 | fields *fields |
| 620 | args args |
| 621 | want bool |
| 622 | }{ |
| 623 | {"IsFlowCookieOnKVStore-1", getResMgr(), args{2, 2, 2, 2}, false}, |
| 624 | } |
| 625 | for _, tt := range tests { |
| 626 | t.Run(tt.name, func(t *testing.T) { |
| 627 | RsrcMgr := testResMgrObject(tt.fields) |
| 628 | if got := RsrcMgr.IsFlowCookieOnKVStore(tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowStoreCookie); got != tt.want { |
| 629 | t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want) |
| 630 | } |
| 631 | }) |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) { |
| 636 | |
| 637 | type args struct { |
| 638 | Direction string |
| 639 | IntfID uint32 |
| 640 | OnuID uint32 |
| 641 | UniID uint32 |
| 642 | } |
| 643 | tests := []struct { |
| 644 | name string |
| 645 | fields *fields |
| 646 | args args |
| 647 | wantErr error |
| 648 | }{ |
| 649 | {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1}, |
| 650 | errors.New("failed to delete meter id %s from kvstore")}, |
| 651 | } |
| 652 | for _, tt := range tests { |
| 653 | t.Run(tt.name, func(t *testing.T) { |
| 654 | RsrcMgr := testResMgrObject(tt.fields) |
| 655 | if err := RsrcMgr.RemoveMeterIDForOnu(tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 656 | t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 657 | } |
| 658 | }) |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) { |
| 663 | type args struct { |
| 664 | IntfID uint32 |
| 665 | OnuID uint32 |
| 666 | UniID uint32 |
| 667 | } |
| 668 | tests := []struct { |
| 669 | name string |
| 670 | fields *fields |
| 671 | args args |
| 672 | wantErr error |
| 673 | }{ |
| 674 | {"RemoveTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2}, |
| 675 | errors.New("failed to delete techprofile id resource %s in KV store")}, |
| 676 | } |
| 677 | for _, tt := range tests { |
| 678 | t.Run(tt.name, func(t *testing.T) { |
| 679 | RsrcMgr := testResMgrObject(tt.fields) |
| 680 | if err := RsrcMgr.RemoveTechProfileIDForOnu(tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 681 | t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 682 | } |
| 683 | }) |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) { |
| 688 | type args struct { |
| 689 | ponPort uint32 |
| 690 | onuID uint32 |
| 691 | uniID uint32 |
| 692 | allocID []uint32 |
| 693 | } |
| 694 | tests := []struct { |
| 695 | name string |
| 696 | fields *fields |
| 697 | args args |
| 698 | wantErr error |
| 699 | }{ |
| 700 | {"UpdateAllocIdsForOnu-1", getResMgr(), args{2, 2, 2, []uint32{1, 2}}, |
| 701 | errors.New("")}, |
| 702 | } |
| 703 | for _, tt := range tests { |
| 704 | t.Run(tt.name, func(t *testing.T) { |
| 705 | RsrcMgr := testResMgrObject(tt.fields) |
| 706 | if err := RsrcMgr.UpdateAllocIdsForOnu(tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.allocID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 707 | t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 708 | } |
| 709 | }) |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) { |
| 714 | type args struct { |
| 715 | ponIntfID int32 |
| 716 | onuID int32 |
| 717 | uniID int32 |
| 718 | flowID uint32 |
| 719 | flowData *[]FlowInfo |
| 720 | } |
| 721 | tests := []struct { |
| 722 | name string |
| 723 | fields *fields |
| 724 | args args |
| 725 | wantErr error |
| 726 | }{ |
| 727 | {"UpdateFlowIDInfo-1", getResMgr(), args{2, 2, 2, 2, &[]FlowInfo{}}, errors.New("")}, |
| 728 | } |
| 729 | for _, tt := range tests { |
| 730 | t.Run(tt.name, func(t *testing.T) { |
| 731 | RsrcMgr := testResMgrObject(tt.fields) |
| 732 | if err := RsrcMgr.UpdateFlowIDInfo(tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowID, tt.args.flowData); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 733 | t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 734 | } |
| 735 | }) |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) { |
| 740 | |
| 741 | type args struct { |
| 742 | ponPort uint32 |
| 743 | onuID uint32 |
| 744 | uniID uint32 |
| 745 | GEMPortList []uint32 |
| 746 | } |
| 747 | tests := []struct { |
| 748 | name string |
| 749 | fields *fields |
| 750 | args args |
| 751 | wantErr error |
| 752 | }{ |
| 753 | {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{2, 2, 2, |
| 754 | []uint32{1, 2}}, errors.New("failed to update resource")}, |
| 755 | } |
| 756 | for _, tt := range tests { |
| 757 | t.Run(tt.name, func(t *testing.T) { |
| 758 | RsrcMgr := testResMgrObject(tt.fields) |
| 759 | if err := RsrcMgr.UpdateGEMPortIDsForOnu(tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.GEMPortList); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 760 | t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 761 | } |
| 762 | }) |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) { |
| 767 | type args struct { |
| 768 | gemPorts []uint32 |
| 769 | PonPort uint32 |
| 770 | onuID uint32 |
| 771 | uniID uint32 |
| 772 | } |
| 773 | tests := []struct { |
| 774 | name string |
| 775 | fields *fields |
| 776 | args args |
| 777 | wantErr error |
| 778 | }{ |
| 779 | {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2}, |
| 780 | 2, 2, 2}, errors.New("failed to update resource")}, |
| 781 | } |
| 782 | for _, tt := range tests { |
| 783 | t.Run(tt.name, func(t *testing.T) { |
| 784 | RsrcMgr := testResMgrObject(tt.fields) |
| 785 | if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(tt.args.gemPorts, tt.args.PonPort, tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 786 | t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr) |
| 787 | } |
| 788 | }) |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) { |
| 793 | type args struct { |
| 794 | Direction string |
| 795 | IntfID uint32 |
| 796 | OnuID uint32 |
| 797 | UniID uint32 |
| 798 | MeterConfig *ofp.OfpMeterConfig |
| 799 | } |
| 800 | tests := []struct { |
| 801 | name string |
| 802 | fields *fields |
| 803 | args args |
| 804 | wantErr error |
| 805 | }{ |
| 806 | {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 2, 2, |
| 807 | 2, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")}, |
| 808 | } |
| 809 | for _, tt := range tests { |
| 810 | t.Run(tt.name, func(t *testing.T) { |
| 811 | RsrcMgr := testResMgrObject(tt.fields) |
| 812 | if err := RsrcMgr.UpdateMeterIDForOnu(tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 813 | t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 814 | } |
| 815 | }) |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) { |
| 820 | type args struct { |
| 821 | IntfID uint32 |
| 822 | OnuID uint32 |
| 823 | UniID uint32 |
| 824 | TpID uint32 |
| 825 | } |
| 826 | tests := []struct { |
| 827 | name string |
| 828 | fields *fields |
| 829 | args args |
| 830 | wantErr error |
| 831 | }{ |
| 832 | {"UpdateTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2, |
| 833 | 2}, errors.New("failed to update resource")}, |
| 834 | } |
| 835 | for _, tt := range tests { |
| 836 | t.Run(tt.name, func(t *testing.T) { |
| 837 | RsrcMgr := testResMgrObject(tt.fields) |
| 838 | if err := RsrcMgr.UpdateTechProfileIDForOnu(tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.TpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 839 | t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 840 | } |
| 841 | }) |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | func TestSetKVClient(t *testing.T) { |
| 846 | type args struct { |
| 847 | backend string |
| 848 | Host string |
| 849 | Port int |
| 850 | DeviceID string |
| 851 | } |
| 852 | tests := []struct { |
| 853 | name string |
| 854 | args args |
| 855 | want *model.Backend |
| 856 | }{ |
| 857 | {"setKVClient-1", args{"consul", "1.1.1.1", 1, "olt1"}, &model.Backend{}}, |
| 858 | {"setKVClient-1", args{"etcd", "2.2.2.2", 2, "olt2"}, &model.Backend{}}, |
| 859 | } |
| 860 | for _, tt := range tests { |
| 861 | t.Run(tt.name, func(t *testing.T) { |
| 862 | if got := SetKVClient(tt.args.backend, tt.args.Host, tt.args.Port, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 863 | t.Errorf("SetKVClient() = %v, want %v", got, tt.want) |
| 864 | } |
| 865 | }) |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | func Test_getFlowIDFromFlowInfo(t *testing.T) { |
| 870 | type args struct { |
| 871 | FlowInfo *[]FlowInfo |
| 872 | flowID uint32 |
| 873 | gemportID uint32 |
| 874 | flowStoreCookie uint64 |
| 875 | flowCategory string |
| 876 | vlanPcp []uint32 |
| 877 | } |
| 878 | flowInfo := &[]FlowInfo{ |
| 879 | { |
| 880 | &openolt.Flow{ |
| 881 | FlowId: 1, |
| 882 | GemportId: 1, |
| 883 | Classifier: &openolt.Classifier{ |
| 884 | OPbits: 1, |
| 885 | }}, |
| 886 | 1, |
| 887 | "HSIA_FLOW", |
| 888 | }, |
| 889 | { |
| 890 | &openolt.Flow{ |
| 891 | GemportId: 1, |
| 892 | }, |
| 893 | 1, |
| 894 | "EAPOL", |
| 895 | }, |
| 896 | } |
| 897 | tests := []struct { |
| 898 | name string |
| 899 | args args |
| 900 | wantErr error |
| 901 | }{ |
| 902 | {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")}, |
| 903 | {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1, |
| 904 | "HSIA_FLOW", []uint32{1, 2}}, errors.New("invalid flow-info")}, |
| 905 | {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1, |
| 906 | "EAPOL", []uint32{1, 2}}, errors.New("invalid flow-info")}, |
| 907 | } |
| 908 | for _, tt := range tests { |
| 909 | t.Run(tt.name, func(t *testing.T) { |
| 910 | err := getFlowIDFromFlowInfo(tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...) |
| 911 | if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 912 | t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 913 | } |
| 914 | if err == nil { |
| 915 | t.Log("return'd nil") |
| 916 | } |
| 917 | }) |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | func Test_newKVClient(t *testing.T) { |
| 922 | type args struct { |
| 923 | storeType string |
| 924 | address string |
| 925 | timeout uint32 |
| 926 | } |
| 927 | var kvClient kvstore.Client |
| 928 | tests := []struct { |
| 929 | name string |
| 930 | args args |
| 931 | want kvstore.Client |
| 932 | wantErr error |
| 933 | }{ |
| 934 | {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")}, |
| 935 | } |
| 936 | for _, tt := range tests { |
| 937 | t.Run(tt.name, func(t *testing.T) { |
| 938 | got, err := newKVClient(tt.args.storeType, tt.args.address, tt.args.timeout) |
| 939 | if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 940 | t.Errorf("newKVClient() got = %v, want %v", got, tt.want) |
| 941 | } |
| 942 | if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 943 | t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 944 | return |
| 945 | } |
| 946 | |
| 947 | }) |
| 948 | } |
| 949 | } |