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