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