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