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" |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 30 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 31 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 32 | fu "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 33 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 34 | ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager" |
| 35 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 36 | "github.com/opencord/voltha-protos/v3/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 | |
| 318 | func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) { |
| 319 | type args struct { |
| 320 | IntfID uint32 |
| 321 | onuID int32 |
| 322 | uniID int32 |
| 323 | FlowID uint32 |
| 324 | } |
| 325 | tests := []struct { |
| 326 | name string |
| 327 | fields *fields |
| 328 | args args |
| 329 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 330 | {"FreeFlowID-1", getResMgr(), args{1, 2, 2, 2}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 331 | } |
| 332 | for _, tt := range tests { |
| 333 | t.Run(tt.name, func(t *testing.T) { |
| 334 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 335 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 336 | defer cancel() |
| 337 | RsrcMgr.FreeFlowID(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 338 | }) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) { |
| 343 | |
| 344 | type args struct { |
| 345 | IntfID uint32 |
| 346 | onuID uint32 |
| 347 | uniID uint32 |
| 348 | FlowID []uint32 |
| 349 | } |
| 350 | tests := []struct { |
| 351 | name string |
| 352 | fields *fields |
| 353 | args args |
| 354 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 355 | {"FreeFlowIDs-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 356 | } |
| 357 | for _, tt := range tests { |
| 358 | t.Run(tt.name, func(t *testing.T) { |
| 359 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 360 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 361 | defer cancel() |
| 362 | RsrcMgr.FreeFlowIDs(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 363 | }) |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) { |
| 368 | type args struct { |
| 369 | intfID uint32 |
| 370 | onuID uint32 |
| 371 | uniID uint32 |
| 372 | } |
| 373 | tests := []struct { |
| 374 | name string |
| 375 | fields *fields |
| 376 | args args |
| 377 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 378 | {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}}, |
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 | RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 386 | }) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | func TestOpenOltResourceMgr_FreeonuID(t *testing.T) { |
| 391 | type args struct { |
| 392 | intfID uint32 |
| 393 | onuID []uint32 |
| 394 | } |
| 395 | tests := []struct { |
| 396 | name string |
| 397 | fields *fields |
| 398 | args args |
| 399 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 400 | {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 401 | } |
| 402 | for _, tt := range tests { |
| 403 | t.Run(tt.name, func(t *testing.T) { |
| 404 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 405 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 406 | defer cancel() |
| 407 | RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 408 | }) |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | func TestOpenOltResourceMgr_GetAllocID(t *testing.T) { |
| 413 | |
| 414 | type args struct { |
| 415 | intfID uint32 |
| 416 | onuID uint32 |
| 417 | uniID uint32 |
| 418 | } |
| 419 | tests := []struct { |
| 420 | name string |
| 421 | fields *fields |
| 422 | args args |
| 423 | want uint32 |
| 424 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 425 | {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 426 | } |
| 427 | for _, tt := range tests { |
| 428 | t.Run(tt.name, func(t *testing.T) { |
| 429 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 430 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 431 | defer cancel() |
| 432 | 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] | 433 | t.Errorf("GetAllocID() = %v, want %v", got, tt.want) |
| 434 | } |
| 435 | }) |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) { |
| 440 | type args struct { |
| 441 | intfID uint32 |
| 442 | onuID uint32 |
| 443 | uniID uint32 |
| 444 | } |
| 445 | tests := []struct { |
| 446 | name string |
| 447 | fields *fields |
| 448 | args args |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 449 | want []uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 450 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 451 | {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 452 | } |
| 453 | for _, tt := range tests { |
| 454 | t.Run(tt.name, func(t *testing.T) { |
| 455 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 456 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 457 | defer cancel() |
| 458 | 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] | 459 | t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 460 | } |
| 461 | }) |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) { |
| 466 | |
| 467 | type args struct { |
| 468 | PONIntfID uint32 |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 469 | ONUID int32 |
| 470 | UNIID int32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 471 | } |
| 472 | tests := []struct { |
| 473 | name string |
| 474 | fields *fields |
| 475 | args args |
| 476 | want []uint32 |
| 477 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 478 | {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 479 | } |
| 480 | for _, tt := range tests { |
| 481 | t.Run(tt.name, func(t *testing.T) { |
| 482 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 483 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 484 | defer cancel() |
| 485 | if got := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 486 | t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want) |
| 487 | } |
| 488 | }) |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) { |
| 493 | type args struct { |
| 494 | intfID uint32 |
| 495 | onuID uint32 |
| 496 | uniID uint32 |
| 497 | } |
| 498 | tests := []struct { |
| 499 | name string |
| 500 | fields *fields |
| 501 | args args |
| 502 | want []uint32 |
| 503 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 504 | {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 505 | } |
| 506 | for _, tt := range tests { |
| 507 | t.Run(tt.name, func(t *testing.T) { |
| 508 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 509 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 510 | defer cancel() |
| 511 | 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] | 512 | t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want) |
| 513 | } |
| 514 | }) |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | func TestOpenOltResourceMgr_GetFlowID(t *testing.T) { |
| 519 | |
| 520 | type args struct { |
| 521 | ponIntfID uint32 |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 522 | ONUID int32 |
| 523 | uniID int32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 524 | gemportID uint32 |
| 525 | flowStoreCookie uint64 |
| 526 | flowCategory string |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 527 | vlanVid uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 528 | vlanPcp []uint32 |
| 529 | } |
| 530 | tests := []struct { |
| 531 | name string |
| 532 | fields *fields |
| 533 | args args |
| 534 | want uint32 |
| 535 | wantErr error |
| 536 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 537 | {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2, |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 538 | "HSIA", 33, nil}, 0, errors.New("failed to get flows")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 539 | } |
| 540 | for _, tt := range tests { |
| 541 | t.Run(tt.name, func(t *testing.T) { |
| 542 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 543 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 544 | defer cancel() |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 545 | got, err := RsrcMgr.GetFlowID(ctx, tt.args.ponIntfID, tt.args.ONUID, tt.args.uniID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 546 | if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 547 | t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr) |
| 548 | return |
| 549 | } |
| 550 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 551 | t.Errorf("GetFlowID() got = %v, want %v", got, tt.want) |
| 552 | } |
| 553 | }) |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) { |
| 558 | type args struct { |
| 559 | ponPort uint32 |
| 560 | onuID uint32 |
| 561 | uniID uint32 |
| 562 | NumOfPorts uint32 |
| 563 | } |
| 564 | tests := []struct { |
| 565 | name string |
| 566 | fields *fields |
| 567 | args args |
| 568 | want []uint32 |
| 569 | wantErr error |
| 570 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 571 | {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 572 | errors.New("failed to get gem port")}, |
| 573 | } |
| 574 | for _, tt := range tests { |
| 575 | t.Run(tt.name, func(t *testing.T) { |
| 576 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 577 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 578 | defer cancel() |
| 579 | 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] | 580 | if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 581 | t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr) |
| 582 | return |
| 583 | } |
| 584 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 585 | t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want) |
| 586 | } |
| 587 | }) |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) { |
| 592 | type args struct { |
| 593 | Direction string |
| 594 | IntfID uint32 |
| 595 | OnuID uint32 |
| 596 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 597 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 598 | } |
| 599 | tests := []struct { |
| 600 | name string |
| 601 | fields *fields |
| 602 | args args |
| 603 | want *ofp.OfpMeterConfig |
| 604 | wantErr error |
| 605 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 606 | {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 607 | &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] | 608 | {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 609 | &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")}, |
| 610 | } |
| 611 | for _, tt := range tests { |
| 612 | t.Run(tt.name, func(t *testing.T) { |
| 613 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 614 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 615 | defer cancel() |
| 616 | 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] | 617 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil { |
| 618 | t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want) |
| 619 | } |
| 620 | }) |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | func TestOpenOltResourceMgr_GetONUID(t *testing.T) { |
| 625 | type args struct { |
| 626 | ponIntfID uint32 |
| 627 | } |
| 628 | tests := []struct { |
| 629 | name string |
| 630 | fields *fields |
| 631 | args args |
| 632 | want uint32 |
| 633 | wantErr error |
| 634 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 635 | {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 636 | } |
| 637 | for _, tt := range tests { |
| 638 | t.Run(tt.name, func(t *testing.T) { |
| 639 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 640 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 641 | defer cancel() |
| 642 | got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 643 | if got != tt.want && err != nil { |
| 644 | t.Errorf("GetONUID() got = %v, want %v", got, tt.want) |
| 645 | } |
| 646 | }) |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) { |
| 651 | |
| 652 | type args struct { |
| 653 | IntfID uint32 |
| 654 | OnuID uint32 |
| 655 | UniID uint32 |
| 656 | } |
| 657 | tests := []struct { |
| 658 | name string |
| 659 | fields *fields |
| 660 | args args |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 661 | want []uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 662 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 663 | {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2}, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 664 | []uint32{1}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 665 | } |
| 666 | for _, tt := range tests { |
| 667 | t.Run(tt.name, func(t *testing.T) { |
| 668 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 669 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 670 | defer cancel() |
| 671 | 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] | 672 | t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want) |
| 673 | } |
| 674 | }) |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) { |
| 679 | type args struct { |
| 680 | ponIntfID uint32 |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 681 | onuID int32 |
| 682 | uniID int32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 683 | flowStoreCookie uint64 |
| 684 | } |
| 685 | tests := []struct { |
| 686 | name string |
| 687 | fields *fields |
| 688 | args args |
| 689 | want bool |
| 690 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 691 | {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 692 | } |
| 693 | for _, tt := range tests { |
| 694 | t.Run(tt.name, func(t *testing.T) { |
| 695 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 696 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 697 | defer cancel() |
| 698 | if got := RsrcMgr.IsFlowCookieOnKVStore(ctx, tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowStoreCookie); got != tt.want { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 699 | t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want) |
| 700 | } |
| 701 | }) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) { |
| 706 | |
| 707 | type args struct { |
| 708 | Direction string |
| 709 | IntfID uint32 |
| 710 | OnuID uint32 |
| 711 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 712 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 713 | } |
| 714 | tests := []struct { |
| 715 | name string |
| 716 | fields *fields |
| 717 | args args |
| 718 | wantErr error |
| 719 | }{ |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 720 | {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 721 | errors.New("failed to delete meter id %s from kvstore")}, |
| 722 | } |
| 723 | for _, tt := range tests { |
| 724 | t.Run(tt.name, func(t *testing.T) { |
| 725 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 726 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 727 | defer cancel() |
| 728 | 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] | 729 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 730 | t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 731 | } |
| 732 | }) |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) { |
| 737 | type args struct { |
| 738 | IntfID uint32 |
| 739 | OnuID uint32 |
| 740 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 741 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 742 | } |
| 743 | tests := []struct { |
| 744 | name string |
| 745 | fields *fields |
| 746 | args args |
| 747 | wantErr error |
| 748 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 749 | {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 750 | errors.New("failed to delete techprofile id resource %s in KV store")}, |
| 751 | } |
| 752 | for _, tt := range tests { |
| 753 | t.Run(tt.name, func(t *testing.T) { |
| 754 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 755 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 756 | defer cancel() |
| 757 | 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] | 758 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 759 | t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 760 | } |
| 761 | }) |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) { |
| 766 | type args struct { |
| 767 | ponPort uint32 |
| 768 | onuID uint32 |
| 769 | uniID uint32 |
| 770 | allocID []uint32 |
| 771 | } |
| 772 | tests := []struct { |
| 773 | name string |
| 774 | fields *fields |
| 775 | args args |
| 776 | wantErr error |
| 777 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 778 | {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 779 | errors.New("")}, |
| 780 | } |
| 781 | for _, tt := range tests { |
| 782 | t.Run(tt.name, func(t *testing.T) { |
| 783 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 784 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 785 | defer cancel() |
| 786 | 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] | 787 | t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 788 | } |
| 789 | }) |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) { |
| 794 | type args struct { |
| 795 | ponIntfID int32 |
| 796 | onuID int32 |
| 797 | uniID int32 |
| 798 | flowID uint32 |
| 799 | flowData *[]FlowInfo |
| 800 | } |
| 801 | tests := []struct { |
| 802 | name string |
| 803 | fields *fields |
| 804 | args args |
| 805 | wantErr error |
| 806 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 807 | {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 808 | } |
| 809 | for _, tt := range tests { |
| 810 | t.Run(tt.name, func(t *testing.T) { |
| 811 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 812 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 813 | defer cancel() |
| 814 | if err := RsrcMgr.UpdateFlowIDInfo(ctx, 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] | 815 | t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 816 | } |
| 817 | }) |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) { |
| 822 | |
| 823 | type args struct { |
| 824 | ponPort uint32 |
| 825 | onuID uint32 |
| 826 | uniID uint32 |
| 827 | GEMPortList []uint32 |
| 828 | } |
| 829 | tests := []struct { |
| 830 | name string |
| 831 | fields *fields |
| 832 | args args |
| 833 | wantErr error |
| 834 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 835 | {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 836 | []uint32{1, 2}}, errors.New("failed to update resource")}, |
| 837 | } |
| 838 | for _, tt := range tests { |
| 839 | t.Run(tt.name, func(t *testing.T) { |
| 840 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 841 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 842 | defer cancel() |
| 843 | 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] | 844 | t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 845 | } |
| 846 | }) |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) { |
| 851 | type args struct { |
| 852 | gemPorts []uint32 |
| 853 | PonPort uint32 |
| 854 | onuID uint32 |
| 855 | uniID uint32 |
| 856 | } |
| 857 | tests := []struct { |
| 858 | name string |
| 859 | fields *fields |
| 860 | args args |
| 861 | wantErr error |
| 862 | }{ |
| 863 | {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2}, |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 864 | 1, 2, 2}, errors.New("failed to update resource")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 865 | } |
| 866 | for _, tt := range tests { |
| 867 | t.Run(tt.name, func(t *testing.T) { |
| 868 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 869 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 870 | defer cancel() |
| 871 | if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 872 | 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] | 873 | t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr) |
| 874 | } |
| 875 | }) |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) { |
| 880 | type args struct { |
| 881 | Direction string |
| 882 | IntfID uint32 |
| 883 | OnuID uint32 |
| 884 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 885 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 886 | MeterConfig *ofp.OfpMeterConfig |
| 887 | } |
| 888 | tests := []struct { |
| 889 | name string |
| 890 | fields *fields |
| 891 | args args |
| 892 | wantErr error |
| 893 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 894 | {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 895 | 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] | 896 | } |
| 897 | for _, tt := range tests { |
| 898 | t.Run(tt.name, func(t *testing.T) { |
| 899 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 900 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 901 | defer cancel() |
| 902 | 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] | 903 | 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] | 904 | t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 905 | } |
| 906 | }) |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) { |
| 911 | type args struct { |
| 912 | IntfID uint32 |
| 913 | OnuID uint32 |
| 914 | UniID uint32 |
| 915 | TpID uint32 |
| 916 | } |
| 917 | tests := []struct { |
| 918 | name string |
| 919 | fields *fields |
| 920 | args args |
| 921 | wantErr error |
| 922 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 923 | {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 924 | 2}, errors.New("failed to update resource")}, |
| 925 | } |
| 926 | for _, tt := range tests { |
| 927 | t.Run(tt.name, func(t *testing.T) { |
| 928 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 929 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 930 | defer cancel() |
| 931 | 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] | 932 | t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 933 | } |
| 934 | }) |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | func TestSetKVClient(t *testing.T) { |
| 939 | type args struct { |
| 940 | backend string |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 941 | address string |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 942 | DeviceID string |
| 943 | } |
| 944 | tests := []struct { |
| 945 | name string |
| 946 | args args |
sbarbari | a8910ba | 2019-11-05 10:12:23 -0500 | [diff] [blame] | 947 | want *db.Backend |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 948 | }{ |
Neha Sharma | 3f221ae | 2020-04-29 19:02:12 +0000 | [diff] [blame] | 949 | {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1"}, &db.Backend{}}, |
| 950 | {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2"}, &db.Backend{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 951 | } |
| 952 | for _, tt := range tests { |
| 953 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 954 | 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] | 955 | t.Errorf("SetKVClient() = %v, want %v", got, tt.want) |
| 956 | } |
| 957 | }) |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | func Test_getFlowIDFromFlowInfo(t *testing.T) { |
| 962 | type args struct { |
| 963 | FlowInfo *[]FlowInfo |
| 964 | flowID uint32 |
| 965 | gemportID uint32 |
| 966 | flowStoreCookie uint64 |
| 967 | flowCategory string |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 968 | vlanVid uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 969 | vlanPcp []uint32 |
| 970 | } |
| 971 | flowInfo := &[]FlowInfo{ |
| 972 | { |
| 973 | &openolt.Flow{ |
| 974 | FlowId: 1, |
| 975 | GemportId: 1, |
| 976 | Classifier: &openolt.Classifier{ |
| 977 | OPbits: 1, |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 978 | OVid: 33, |
| 979 | }, |
| 980 | Action: &openolt.Action{ |
| 981 | Cmd: &openolt.ActionCmd{ |
| 982 | AddOuterTag: true, |
| 983 | }, |
| 984 | OVid: 7, |
| 985 | }, |
| 986 | }, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 987 | 1, |
| 988 | "HSIA_FLOW", |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 989 | 2000, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 990 | }, |
| 991 | { |
| 992 | &openolt.Flow{ |
| 993 | GemportId: 1, |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 994 | Classifier: &openolt.Classifier{ |
| 995 | OVid: 0, |
| 996 | }, |
| 997 | Action: &openolt.Action{ |
| 998 | Cmd: &openolt.ActionCmd{ |
| 999 | TrapToHost: true, |
| 1000 | }, |
| 1001 | }, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1002 | }, |
| 1003 | 1, |
| 1004 | "EAPOL", |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 1005 | 3000, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1006 | }, |
| 1007 | } |
| 1008 | tests := []struct { |
| 1009 | name string |
| 1010 | args args |
| 1011 | wantErr error |
| 1012 | }{ |
| 1013 | {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")}, |
| 1014 | {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1, |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 1015 | "HSIA_FLOW", 33, []uint32{1, 2}}, errors.New("invalid flow-info")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1016 | {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1, |
Gamze Abaka | 724d085 | 2020-03-18 12:10:24 +0000 | [diff] [blame] | 1017 | "EAPOL", 33, []uint32{1, 2}}, errors.New("invalid flow-info")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1018 | } |
| 1019 | for _, tt := range tests { |
| 1020 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1021 | err := getFlowIDFromFlowInfo(context.Background(), tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1022 | if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 1023 | t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 1024 | } |
| 1025 | if err == nil { |
| 1026 | t.Log("return'd nil") |
| 1027 | } |
| 1028 | }) |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | func Test_newKVClient(t *testing.T) { |
| 1033 | type args struct { |
| 1034 | storeType string |
| 1035 | address string |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 1036 | timeout time.Duration |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 1037 | } |
| 1038 | var kvClient kvstore.Client |
| 1039 | tests := []struct { |
| 1040 | name string |
| 1041 | args args |
| 1042 | want kvstore.Client |
| 1043 | wantErr error |
| 1044 | }{ |
| 1045 | {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")}, |
| 1046 | } |
| 1047 | for _, tt := range tests { |
| 1048 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1049 | 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] | 1050 | if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 1051 | t.Errorf("newKVClient() got = %v, want %v", got, tt.want) |
| 1052 | } |
| 1053 | if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 1054 | t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 1055 | return |
| 1056 | } |
| 1057 | |
| 1058 | }) |
| 1059 | } |
| 1060 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1061 | |
| 1062 | func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) { |
| 1063 | type args struct { |
| 1064 | intf uint32 |
| 1065 | gem uint32 |
| 1066 | servicePriority uint32 |
| 1067 | } |
| 1068 | tests := []struct { |
| 1069 | name string |
| 1070 | args args |
| 1071 | fields *fields |
| 1072 | }{ |
| 1073 | {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()}, |
| 1074 | {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()}, |
| 1075 | {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()}, |
| 1076 | } |
| 1077 | for _, tt := range tests { |
| 1078 | t.Run(tt.name, func(t *testing.T) { |
| 1079 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1080 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1081 | defer cancel() |
| 1082 | 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] | 1083 | if err != nil { |
| 1084 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 1085 | return |
| 1086 | } |
| 1087 | }) |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry { |
| 1092 | groupDesc := ofp.OfpGroupDesc{ |
| 1093 | Type: ofp.OfpGroupType_OFPGT_ALL, |
| 1094 | GroupId: groupID, |
| 1095 | } |
| 1096 | groupEntry := ofp.OfpGroupEntry{ |
| 1097 | Desc: &groupDesc, |
| 1098 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1099 | for i := 0; i < len(outPorts); i++ { |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 1100 | var acts []*ofp.OfpAction |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1101 | acts = append(acts, fu.Output(outPorts[i])) |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 1102 | bucket := ofp.OfpBucket{ |
| 1103 | Actions: acts, |
| 1104 | } |
| 1105 | groupDesc.Buckets = append(groupDesc.Buckets, &bucket) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1106 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1107 | return &groupEntry |
| 1108 | } |
| 1109 | |
| 1110 | func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) { |
| 1111 | type args struct { |
| 1112 | group *ofp.OfpGroupEntry |
| 1113 | cached bool |
| 1114 | } |
| 1115 | //create group 1 |
| 1116 | group1 := newGroup(1, []uint32{1}) |
| 1117 | //create group 2 |
| 1118 | group2 := newGroup(2, []uint32{2}) |
| 1119 | //define test set |
| 1120 | tests := []struct { |
| 1121 | name string |
| 1122 | args args |
| 1123 | fields *fields |
| 1124 | }{ |
| 1125 | {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()}, |
| 1126 | {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()}, |
| 1127 | } |
| 1128 | //execute tests |
| 1129 | for _, tt := range tests { |
| 1130 | t.Run(tt.name, func(t *testing.T) { |
| 1131 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1132 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1133 | defer cancel() |
| 1134 | err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1135 | if err != nil { |
| 1136 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 1137 | return |
| 1138 | } |
| 1139 | }) |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) { |
| 1144 | type args struct { |
| 1145 | groupID uint32 |
| 1146 | cached bool |
| 1147 | } |
| 1148 | //define test set |
| 1149 | tests := []struct { |
| 1150 | name string |
| 1151 | args args |
| 1152 | fields *fields |
| 1153 | }{ |
| 1154 | {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 1155 | {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 1156 | } |
| 1157 | //execute tests |
| 1158 | for _, tt := range tests { |
| 1159 | t.Run(tt.name, func(t *testing.T) { |
| 1160 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1161 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1162 | defer cancel() |
Esin Karaman | d519bbf | 2020-07-01 11:16:03 +0000 | [diff] [blame] | 1163 | err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
| 1164 | if err != nil { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1165 | t.Errorf("%s got false but wants true", tt.name) |
| 1166 | return |
| 1167 | } |
| 1168 | }) |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) { |
| 1173 | type args struct { |
| 1174 | groupID uint32 |
| 1175 | cached bool |
| 1176 | } |
| 1177 | //define test set |
| 1178 | tests := []struct { |
| 1179 | name string |
| 1180 | args args |
| 1181 | fields *fields |
| 1182 | }{ |
| 1183 | {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 1184 | {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 1185 | {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()}, |
| 1186 | } |
| 1187 | //execute tests |
| 1188 | for _, tt := range tests { |
| 1189 | t.Run(tt.name, func(t *testing.T) { |
| 1190 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1191 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1192 | defer cancel() |
| 1193 | exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1194 | if err != nil { |
| 1195 | t.Errorf("%s got error but wants nil error", tt.name) |
| 1196 | return |
| 1197 | } else if exists && (groupInfo.GroupID == 0) { |
| 1198 | t.Errorf("%s got true and nil group info but expected not nil group info", tt.name) |
| 1199 | return |
| 1200 | } else if tt.args.groupID == 3 && exists { |
| 1201 | t.Errorf("%s got true but wants false", tt.name) |
| 1202 | return |
| 1203 | } |
| 1204 | }) |
| 1205 | } |
| 1206 | } |