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