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 | |
| 444 | func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) { |
| 445 | type args struct { |
| 446 | intfID uint32 |
| 447 | onuID uint32 |
| 448 | uniID uint32 |
| 449 | } |
| 450 | tests := []struct { |
| 451 | name string |
| 452 | fields *fields |
| 453 | args args |
| 454 | want []uint32 |
| 455 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 456 | {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 457 | } |
| 458 | for _, tt := range tests { |
| 459 | t.Run(tt.name, func(t *testing.T) { |
| 460 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 461 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 462 | defer cancel() |
| 463 | 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] | 464 | t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want) |
| 465 | } |
| 466 | }) |
| 467 | } |
| 468 | } |
| 469 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 470 | func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 471 | type args struct { |
| 472 | Direction string |
| 473 | IntfID uint32 |
| 474 | OnuID uint32 |
| 475 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 476 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 477 | } |
| 478 | tests := []struct { |
| 479 | name string |
| 480 | fields *fields |
| 481 | args args |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 482 | want *MeterInfo |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 483 | wantErr error |
| 484 | }{ |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 485 | {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64}, |
| 486 | &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")}, |
| 487 | {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65}, |
| 488 | &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 489 | } |
| 490 | for _, tt := range tests { |
| 491 | t.Run(tt.name, func(t *testing.T) { |
| 492 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 493 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 494 | defer cancel() |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 495 | 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] | 496 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil { |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 497 | t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 498 | } |
| 499 | }) |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | func TestOpenOltResourceMgr_GetONUID(t *testing.T) { |
| 504 | type args struct { |
| 505 | ponIntfID uint32 |
| 506 | } |
| 507 | tests := []struct { |
| 508 | name string |
| 509 | fields *fields |
| 510 | args args |
| 511 | want uint32 |
| 512 | wantErr error |
| 513 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 514 | {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 515 | } |
| 516 | for _, tt := range tests { |
| 517 | t.Run(tt.name, func(t *testing.T) { |
| 518 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 519 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 520 | defer cancel() |
| 521 | got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 522 | if got != tt.want && err != nil { |
| 523 | t.Errorf("GetONUID() got = %v, want %v", got, tt.want) |
| 524 | } |
| 525 | }) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) { |
| 530 | |
| 531 | type args struct { |
| 532 | IntfID uint32 |
| 533 | OnuID uint32 |
| 534 | UniID uint32 |
| 535 | } |
| 536 | tests := []struct { |
| 537 | name string |
| 538 | fields *fields |
| 539 | args args |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 540 | want []uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 541 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 542 | {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2}, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 543 | []uint32{1}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 544 | } |
| 545 | for _, tt := range tests { |
| 546 | t.Run(tt.name, func(t *testing.T) { |
| 547 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 548 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 549 | defer cancel() |
| 550 | 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] | 551 | t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want) |
| 552 | } |
| 553 | }) |
| 554 | } |
| 555 | } |
| 556 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 557 | func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) { |
| 558 | |
| 559 | type args struct { |
| 560 | Direction string |
| 561 | IntfID uint32 |
| 562 | OnuID uint32 |
| 563 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 564 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 565 | } |
| 566 | tests := []struct { |
| 567 | name string |
| 568 | fields *fields |
| 569 | args args |
| 570 | wantErr error |
| 571 | }{ |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 572 | {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 573 | errors.New("failed to delete meter id %s from kvstore")}, |
| 574 | } |
| 575 | for _, tt := range tests { |
| 576 | t.Run(tt.name, func(t *testing.T) { |
| 577 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 578 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 579 | defer cancel() |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 580 | 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] | 581 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 582 | t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 583 | } |
| 584 | }) |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) { |
| 589 | type args struct { |
| 590 | IntfID uint32 |
| 591 | OnuID uint32 |
| 592 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 593 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 594 | } |
| 595 | tests := []struct { |
| 596 | name string |
| 597 | fields *fields |
| 598 | args args |
| 599 | wantErr error |
| 600 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 601 | {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 602 | errors.New("failed to delete techprofile id resource %s in KV store")}, |
| 603 | } |
| 604 | for _, tt := range tests { |
| 605 | t.Run(tt.name, func(t *testing.T) { |
| 606 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 607 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 608 | defer cancel() |
| 609 | 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] | 610 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 611 | t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 612 | } |
| 613 | }) |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) { |
| 618 | type args struct { |
| 619 | ponPort uint32 |
| 620 | onuID uint32 |
| 621 | uniID uint32 |
| 622 | allocID []uint32 |
| 623 | } |
| 624 | tests := []struct { |
| 625 | name string |
| 626 | fields *fields |
| 627 | args args |
| 628 | wantErr error |
| 629 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 630 | {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 631 | errors.New("")}, |
| 632 | } |
| 633 | for _, tt := range tests { |
| 634 | t.Run(tt.name, func(t *testing.T) { |
| 635 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 636 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 637 | defer cancel() |
| 638 | 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] | 639 | t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 640 | } |
| 641 | }) |
| 642 | } |
| 643 | } |
| 644 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 645 | func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) { |
| 646 | |
| 647 | type args struct { |
| 648 | ponPort uint32 |
| 649 | onuID uint32 |
| 650 | uniID uint32 |
| 651 | GEMPortList []uint32 |
| 652 | } |
| 653 | tests := []struct { |
| 654 | name string |
| 655 | fields *fields |
| 656 | args args |
| 657 | wantErr error |
| 658 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 659 | {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 660 | []uint32{1, 2}}, errors.New("failed to update resource")}, |
| 661 | } |
| 662 | for _, tt := range tests { |
| 663 | t.Run(tt.name, func(t *testing.T) { |
| 664 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 665 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 666 | defer cancel() |
| 667 | 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] | 668 | t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 669 | } |
| 670 | }) |
| 671 | } |
| 672 | } |
| 673 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 674 | func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) { |
| 675 | type args struct { |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 676 | Direction string |
| 677 | IntfID uint32 |
| 678 | OnuID uint32 |
| 679 | UniID uint32 |
| 680 | tpID uint32 |
| 681 | MeterInfo *MeterInfo |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 682 | } |
| 683 | tests := []struct { |
| 684 | name string |
| 685 | fields *fields |
| 686 | args args |
| 687 | wantErr error |
| 688 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 689 | {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2, |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 690 | 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] | 691 | } |
| 692 | for _, tt := range tests { |
| 693 | t.Run(tt.name, func(t *testing.T) { |
| 694 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 695 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 696 | defer cancel() |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 697 | if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, |
| 698 | 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] | 699 | t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 700 | } |
| 701 | }) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) { |
| 706 | type args struct { |
| 707 | IntfID uint32 |
| 708 | OnuID uint32 |
| 709 | UniID uint32 |
| 710 | TpID uint32 |
| 711 | } |
| 712 | tests := []struct { |
| 713 | name string |
| 714 | fields *fields |
| 715 | args args |
| 716 | wantErr error |
| 717 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 718 | {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 719 | 2}, errors.New("failed to update resource")}, |
| 720 | } |
| 721 | for _, tt := range tests { |
| 722 | t.Run(tt.name, func(t *testing.T) { |
| 723 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 724 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 725 | defer cancel() |
| 726 | 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] | 727 | t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 728 | } |
| 729 | }) |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | func TestSetKVClient(t *testing.T) { |
| 734 | type args struct { |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 735 | backend string |
| 736 | address string |
| 737 | DeviceID string |
| 738 | kvStorePrefix string |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 739 | } |
| 740 | tests := []struct { |
| 741 | name string |
| 742 | args args |
sbarbari | a8910ba | 2019-11-05 10:12:23 -0500 | [diff] [blame] | 743 | want *db.Backend |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 744 | }{ |
serkant.uluderya | 7b8211e | 2021-02-24 16:39:18 +0300 | [diff] [blame] | 745 | {"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] | 746 | } |
| 747 | for _, tt := range tests { |
| 748 | t.Run(tt.name, func(t *testing.T) { |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 749 | 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] | 750 | t.Errorf("SetKVClient() = %v, want %v", got, tt.want) |
| 751 | } |
| 752 | }) |
| 753 | } |
| 754 | } |
| 755 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 756 | func Test_newKVClient(t *testing.T) { |
| 757 | type args struct { |
| 758 | storeType string |
| 759 | address string |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 760 | timeout time.Duration |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 761 | } |
| 762 | var kvClient kvstore.Client |
| 763 | tests := []struct { |
| 764 | name string |
| 765 | args args |
| 766 | want kvstore.Client |
| 767 | wantErr error |
| 768 | }{ |
| 769 | {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")}, |
| 770 | } |
| 771 | for _, tt := range tests { |
| 772 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 773 | 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] | 774 | if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 775 | t.Errorf("newKVClient() got = %v, want %v", got, tt.want) |
| 776 | } |
| 777 | if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 778 | t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 779 | return |
| 780 | } |
| 781 | |
| 782 | }) |
| 783 | } |
| 784 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 785 | |
| 786 | func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) { |
| 787 | type args struct { |
| 788 | intf uint32 |
| 789 | gem uint32 |
| 790 | servicePriority uint32 |
| 791 | } |
| 792 | tests := []struct { |
| 793 | name string |
| 794 | args args |
| 795 | fields *fields |
| 796 | }{ |
| 797 | {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()}, |
| 798 | {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()}, |
| 799 | {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()}, |
| 800 | } |
| 801 | for _, tt := range tests { |
| 802 | t.Run(tt.name, func(t *testing.T) { |
| 803 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 804 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 805 | defer cancel() |
| 806 | 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] | 807 | if err != nil { |
| 808 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 809 | return |
| 810 | } |
| 811 | }) |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry { |
| 816 | groupDesc := ofp.OfpGroupDesc{ |
| 817 | Type: ofp.OfpGroupType_OFPGT_ALL, |
| 818 | GroupId: groupID, |
| 819 | } |
| 820 | groupEntry := ofp.OfpGroupEntry{ |
| 821 | Desc: &groupDesc, |
| 822 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 823 | for i := 0; i < len(outPorts); i++ { |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 824 | var acts []*ofp.OfpAction |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 825 | acts = append(acts, fu.Output(outPorts[i])) |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 826 | bucket := ofp.OfpBucket{ |
| 827 | Actions: acts, |
| 828 | } |
| 829 | groupDesc.Buckets = append(groupDesc.Buckets, &bucket) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 830 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 831 | return &groupEntry |
| 832 | } |
| 833 | |
| 834 | func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) { |
| 835 | type args struct { |
| 836 | group *ofp.OfpGroupEntry |
| 837 | cached bool |
| 838 | } |
| 839 | //create group 1 |
| 840 | group1 := newGroup(1, []uint32{1}) |
| 841 | //create group 2 |
| 842 | group2 := newGroup(2, []uint32{2}) |
| 843 | //define test set |
| 844 | tests := []struct { |
| 845 | name string |
| 846 | args args |
| 847 | fields *fields |
| 848 | }{ |
| 849 | {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()}, |
| 850 | {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()}, |
| 851 | } |
| 852 | //execute tests |
| 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.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached) |
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 TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) { |
| 868 | type args struct { |
| 869 | groupID uint32 |
| 870 | cached bool |
| 871 | } |
| 872 | //define test set |
| 873 | tests := []struct { |
| 874 | name string |
| 875 | args args |
| 876 | fields *fields |
| 877 | }{ |
| 878 | {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 879 | {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 880 | } |
| 881 | //execute tests |
| 882 | for _, tt := range tests { |
| 883 | t.Run(tt.name, func(t *testing.T) { |
| 884 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 885 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 886 | defer cancel() |
Esin Karaman | d519bbf | 2020-07-01 11:16:03 +0000 | [diff] [blame] | 887 | err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
| 888 | if err != nil { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 889 | t.Errorf("%s got false but wants true", tt.name) |
| 890 | return |
| 891 | } |
| 892 | }) |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) { |
| 897 | type args struct { |
| 898 | groupID uint32 |
| 899 | cached bool |
| 900 | } |
| 901 | //define test set |
| 902 | tests := []struct { |
| 903 | name string |
| 904 | args args |
| 905 | fields *fields |
| 906 | }{ |
| 907 | {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 908 | {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 909 | {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()}, |
| 910 | } |
| 911 | //execute tests |
| 912 | for _, tt := range tests { |
| 913 | t.Run(tt.name, func(t *testing.T) { |
| 914 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 915 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 916 | defer cancel() |
| 917 | exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 918 | if err != nil { |
| 919 | t.Errorf("%s got error but wants nil error", tt.name) |
| 920 | return |
| 921 | } else if exists && (groupInfo.GroupID == 0) { |
| 922 | t.Errorf("%s got true and nil group info but expected not nil group info", tt.name) |
| 923 | return |
| 924 | } else if tt.args.groupID == 3 && exists { |
| 925 | t.Errorf("%s got true but wants false", tt.name) |
| 926 | return |
| 927 | } |
| 928 | }) |
| 929 | } |
| 930 | } |