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