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