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