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