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" |
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 | } |
| 293 | tests := []struct { |
| 294 | name string |
| 295 | args args |
| 296 | want *OpenOltResourceMgr |
| 297 | }{ |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 298 | {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd", |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 299 | "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 300 | } |
| 301 | for _, tt := range tests { |
| 302 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 303 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 304 | defer cancel() |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 305 | 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] | 306 | t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want) |
| 307 | } |
| 308 | }) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | func TestOpenOltResourceMgr_Delete(t *testing.T) { |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 313 | type args struct { |
| 314 | intfID uint32 |
| 315 | } |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 316 | tests := []struct { |
| 317 | name string |
| 318 | fields *fields |
| 319 | wantErr error |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 320 | args args |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 321 | }{ |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 322 | {"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] | 323 | } |
| 324 | for _, tt := range tests { |
| 325 | t.Run(tt.name, func(t *testing.T) { |
| 326 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 327 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 328 | defer cancel() |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 329 | 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] | 330 | t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) |
| 331 | } |
| 332 | }) |
| 333 | } |
| 334 | } |
| 335 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 336 | func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) { |
| 337 | type args struct { |
| 338 | intfID uint32 |
| 339 | onuID uint32 |
| 340 | uniID uint32 |
| 341 | } |
| 342 | tests := []struct { |
| 343 | name string |
| 344 | fields *fields |
| 345 | args args |
| 346 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 347 | {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 348 | } |
| 349 | for _, tt := range tests { |
| 350 | t.Run(tt.name, func(t *testing.T) { |
| 351 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 352 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 353 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 354 | RsrcMgr.FreePONResourcesForONU(ctx, tt.args.onuID, tt.args.uniID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 355 | }) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | func TestOpenOltResourceMgr_FreeonuID(t *testing.T) { |
| 360 | type args struct { |
| 361 | intfID uint32 |
| 362 | onuID []uint32 |
| 363 | } |
| 364 | tests := []struct { |
| 365 | name string |
| 366 | fields *fields |
| 367 | args args |
| 368 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 369 | {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 370 | } |
| 371 | for _, tt := range tests { |
| 372 | t.Run(tt.name, func(t *testing.T) { |
| 373 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 374 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 375 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 376 | RsrcMgr.FreeonuID(ctx, tt.args.onuID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 377 | }) |
| 378 | } |
| 379 | } |
| 380 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 381 | func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) { |
| 382 | type args struct { |
| 383 | intfID uint32 |
| 384 | onuID uint32 |
| 385 | uniID uint32 |
| 386 | } |
| 387 | tests := []struct { |
| 388 | name string |
| 389 | fields *fields |
| 390 | args args |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 391 | want []uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 392 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 393 | {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 394 | } |
| 395 | for _, tt := range tests { |
| 396 | t.Run(tt.name, func(t *testing.T) { |
| 397 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 398 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 399 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 400 | got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.onuID, tt.args.uniID) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 401 | if len(got) != len(tt.want) { |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 402 | t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want) |
Girish Gowdra | 8a0bdcd | 2021-05-13 12:31:04 -0700 | [diff] [blame] | 403 | } else { |
| 404 | for i := range tt.want { |
| 405 | if got[i] != tt.want[i] { |
| 406 | t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want) |
| 407 | break |
| 408 | } |
| 409 | } |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 410 | } |
| 411 | }) |
| 412 | } |
| 413 | } |
| 414 | |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 415 | func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) { |
| 416 | |
| 417 | type args struct { |
| 418 | PONIntfID uint32 |
| 419 | } |
| 420 | tests := []struct { |
| 421 | name string |
| 422 | fields *fields |
| 423 | args args |
| 424 | want error |
| 425 | }{ |
| 426 | {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil}, |
| 427 | } |
| 428 | for _, tt := range tests { |
| 429 | t.Run(tt.name, func(t *testing.T) { |
| 430 | RsrcMgr := testResMgrObject(tt.fields) |
| 431 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 432 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 433 | err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx) |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 434 | if err != nil { |
| 435 | t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error") |
| 436 | } |
| 437 | }) |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) { |
| 442 | |
| 443 | type args struct { |
| 444 | PONIntfID uint32 |
| 445 | } |
| 446 | tests := []struct { |
| 447 | name string |
| 448 | fields *fields |
| 449 | args args |
| 450 | want error |
| 451 | }{ |
| 452 | {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil}, |
| 453 | } |
| 454 | for _, tt := range tests { |
| 455 | t.Run(tt.name, func(t *testing.T) { |
| 456 | RsrcMgr := testResMgrObject(tt.fields) |
| 457 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 458 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 459 | err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx) |
Girish Gowdra | 950326e | 2021-11-05 12:43:24 -0700 | [diff] [blame] | 460 | if err != nil { |
| 461 | t.Errorf("DeleteAllOnuGemInfoForIntf() returned error") |
| 462 | } |
| 463 | }) |
| 464 | } |
| 465 | } |
| 466 | |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 467 | func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) { |
| 468 | |
| 469 | type args struct { |
| 470 | intfID uint32 |
| 471 | onuID uint32 |
| 472 | gemPortIDs []uint32 |
| 473 | gemPortIDsToBeDeleted []uint32 |
| 474 | gemPortIDsRemaining []uint32 |
| 475 | serialNum string |
| 476 | finalLength int |
| 477 | } |
| 478 | tests := []struct { |
| 479 | name string |
| 480 | fields *fields |
| 481 | args args |
| 482 | }{ |
| 483 | // Add/Delete single gem port |
| 484 | {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}}, |
| 485 | // Delete all gemports |
| 486 | {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}}, |
| 487 | // Try to delete when there is no gem port |
| 488 | {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}}, |
| 489 | // Try to delete non-existent gem port |
| 490 | {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}}, |
| 491 | // Try to delete two of the gem ports |
| 492 | {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}}, |
| 493 | } |
| 494 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 495 | defer cancel() |
| 496 | for _, tt := range tests { |
| 497 | t.Run(tt.name, func(t *testing.T) { |
| 498 | RsrcMgr := testResMgrObject(tt.fields) |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 499 | if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 500 | t.Errorf("failed to remove onu") |
| 501 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 502 | 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] | 503 | t.Errorf("failed to add onu") |
| 504 | } |
| 505 | for _, gemPort := range tt.args.gemPortIDs { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 506 | if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 507 | t.Errorf("failed to add gem to onu") |
| 508 | } |
| 509 | } |
| 510 | for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 511 | if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.onuID, gemPortDeleted); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 512 | t.Errorf("failed to remove gem from onu") |
| 513 | } |
| 514 | } |
| 515 | lenofGemPorts := 0 |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 516 | gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 517 | if err != nil || gP == nil { |
| 518 | t.Errorf("failed to get onuGemInfo") |
| 519 | } |
| 520 | var gemPorts []uint32 |
| 521 | |
| 522 | lenofGemPorts = len(gP.GemPorts) |
| 523 | gemPorts = gP.GemPorts |
| 524 | |
| 525 | if lenofGemPorts != tt.args.finalLength { |
| 526 | t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength) |
| 527 | } |
| 528 | |
| 529 | if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) { |
| 530 | t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining) |
| 531 | } |
| 532 | }) |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) { |
| 537 | |
| 538 | type args struct { |
| 539 | PONIntfID uint32 |
| 540 | OnuCount uint32 |
| 541 | } |
| 542 | tests := []struct { |
| 543 | name string |
| 544 | fields *fields |
| 545 | args args |
| 546 | want error |
| 547 | }{ |
| 548 | {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil}, |
| 549 | } |
| 550 | for _, tt := range tests { |
| 551 | t.Run(tt.name, func(t *testing.T) { |
| 552 | RsrcMgr := testResMgrObject(tt.fields) |
| 553 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 554 | defer cancel() |
| 555 | for j := 1; j <= int(tt.args.OnuCount); j++ { |
| 556 | go func(i uint32, j uint32) { |
| 557 | // TODO: actually verify success |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 558 | _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, fmt.Sprintf("onu-%d", i)) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 559 | }(tt.args.PONIntfID, uint32(j)) |
| 560 | } |
| 561 | }) |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) { |
| 566 | |
| 567 | type args struct { |
| 568 | intfID uint32 |
| 569 | onuID uint32 |
| 570 | gemPortIDs []uint32 |
| 571 | gemPortIDsRemaining []uint32 |
| 572 | serialNum string |
| 573 | finalLength int |
| 574 | } |
| 575 | tests := []struct { |
| 576 | name string |
| 577 | fields *fields |
| 578 | args args |
| 579 | }{ |
| 580 | // Add single gem port |
| 581 | {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}}, |
| 582 | // Delete all gemports |
| 583 | {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}}, |
| 584 | // Do not add any gemport |
| 585 | {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}}, |
| 586 | } |
| 587 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 588 | defer cancel() |
| 589 | for _, tt := range tests { |
| 590 | t.Run(tt.name, func(t *testing.T) { |
| 591 | RsrcMgr := testResMgrObject(tt.fields) |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 592 | if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 593 | t.Errorf("failed to remove onu") |
| 594 | } |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 595 | 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] | 596 | t.Errorf("failed to add onu") |
| 597 | } |
| 598 | for _, gemPort := range tt.args.gemPortIDs { |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 599 | if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil { |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 600 | t.Errorf("failed to add gem to onu") |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | lenofGemPorts := 0 |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 605 | gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID) |
yasin sapli | 9e4c509 | 2022-02-01 13:52:33 +0000 | [diff] [blame] | 606 | |
| 607 | var gemPorts []uint32 |
| 608 | if err == nil && gP != nil { |
| 609 | lenofGemPorts = len(gP.GemPorts) |
| 610 | gemPorts = gP.GemPorts |
| 611 | } |
| 612 | if lenofGemPorts != tt.args.finalLength { |
| 613 | t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength) |
| 614 | } |
| 615 | |
| 616 | if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) { |
| 617 | t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining) |
| 618 | } |
| 619 | }) |
| 620 | } |
| 621 | } |
| 622 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 623 | func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) { |
| 624 | type args struct { |
| 625 | intfID uint32 |
| 626 | onuID uint32 |
| 627 | uniID uint32 |
| 628 | } |
| 629 | tests := []struct { |
| 630 | name string |
| 631 | fields *fields |
| 632 | args args |
| 633 | want []uint32 |
| 634 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 635 | {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 636 | } |
| 637 | for _, tt := range tests { |
| 638 | t.Run(tt.name, func(t *testing.T) { |
| 639 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 640 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 641 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 642 | 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] | 643 | t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want) |
| 644 | } |
| 645 | }) |
| 646 | } |
| 647 | } |
| 648 | |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 649 | func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 650 | type args struct { |
| 651 | Direction string |
| 652 | IntfID uint32 |
| 653 | OnuID uint32 |
| 654 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 655 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 656 | } |
| 657 | tests := []struct { |
| 658 | name string |
| 659 | fields *fields |
| 660 | args args |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 661 | want *MeterInfo |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 662 | wantErr error |
| 663 | }{ |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 664 | {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64}, |
| 665 | &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")}, |
| 666 | {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65}, |
| 667 | &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 668 | } |
| 669 | for _, tt := range tests { |
| 670 | t.Run(tt.name, func(t *testing.T) { |
| 671 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 672 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 673 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 674 | 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] | 675 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil { |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 676 | t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 677 | } |
| 678 | }) |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | func TestOpenOltResourceMgr_GetONUID(t *testing.T) { |
| 683 | type args struct { |
| 684 | ponIntfID uint32 |
| 685 | } |
| 686 | tests := []struct { |
| 687 | name string |
| 688 | fields *fields |
| 689 | args args |
| 690 | want uint32 |
| 691 | wantErr error |
| 692 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 693 | {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 694 | } |
| 695 | for _, tt := range tests { |
| 696 | t.Run(tt.name, func(t *testing.T) { |
| 697 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 698 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 699 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 700 | got, err := RsrcMgr.GetONUID(ctx) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 701 | if got != tt.want && err != nil { |
| 702 | t.Errorf("GetONUID() got = %v, want %v", got, tt.want) |
| 703 | } |
| 704 | }) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) { |
| 709 | |
| 710 | type args struct { |
| 711 | IntfID uint32 |
| 712 | OnuID uint32 |
| 713 | UniID uint32 |
| 714 | } |
| 715 | tests := []struct { |
| 716 | name string |
| 717 | fields *fields |
| 718 | args args |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 719 | want []uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 720 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 721 | {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2}, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 722 | []uint32{1}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 723 | } |
| 724 | for _, tt := range tests { |
| 725 | t.Run(tt.name, func(t *testing.T) { |
| 726 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 727 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 728 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 729 | 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] | 730 | t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want) |
| 731 | } |
| 732 | }) |
| 733 | } |
| 734 | } |
| 735 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 736 | func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) { |
| 737 | |
| 738 | type args struct { |
| 739 | Direction string |
| 740 | IntfID uint32 |
| 741 | OnuID uint32 |
| 742 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 743 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 744 | } |
| 745 | tests := []struct { |
| 746 | name string |
| 747 | fields *fields |
| 748 | args args |
| 749 | wantErr error |
| 750 | }{ |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 751 | {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 752 | errors.New("failed to delete meter id %s from kvstore")}, |
| 753 | } |
| 754 | for _, tt := range tests { |
| 755 | t.Run(tt.name, func(t *testing.T) { |
| 756 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 757 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 758 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 759 | 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] | 760 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 761 | t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 762 | } |
| 763 | }) |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) { |
| 768 | type args struct { |
| 769 | IntfID uint32 |
| 770 | OnuID uint32 |
| 771 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 772 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 773 | } |
| 774 | tests := []struct { |
| 775 | name string |
| 776 | fields *fields |
| 777 | args args |
| 778 | wantErr error |
| 779 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 780 | {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 781 | errors.New("failed to delete techprofile id resource %s in KV store")}, |
| 782 | } |
| 783 | for _, tt := range tests { |
| 784 | t.Run(tt.name, func(t *testing.T) { |
| 785 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 786 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 787 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 788 | if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 789 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 790 | t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 791 | } |
| 792 | }) |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) { |
| 797 | type args struct { |
| 798 | ponPort uint32 |
| 799 | onuID uint32 |
| 800 | uniID uint32 |
| 801 | allocID []uint32 |
| 802 | } |
| 803 | tests := []struct { |
| 804 | name string |
| 805 | fields *fields |
| 806 | args args |
| 807 | wantErr error |
| 808 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 809 | {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 810 | errors.New("")}, |
| 811 | } |
| 812 | for _, tt := range tests { |
| 813 | t.Run(tt.name, func(t *testing.T) { |
| 814 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 815 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 816 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 817 | 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] | 818 | t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 819 | } |
| 820 | }) |
| 821 | } |
| 822 | } |
| 823 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 824 | func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) { |
| 825 | |
| 826 | type args struct { |
| 827 | ponPort uint32 |
| 828 | onuID uint32 |
| 829 | uniID uint32 |
| 830 | GEMPortList []uint32 |
| 831 | } |
| 832 | tests := []struct { |
| 833 | name string |
| 834 | fields *fields |
| 835 | args args |
| 836 | wantErr error |
| 837 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 838 | {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 839 | []uint32{1, 2}}, errors.New("failed to update resource")}, |
| 840 | } |
| 841 | for _, tt := range tests { |
| 842 | t.Run(tt.name, func(t *testing.T) { |
| 843 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 844 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 845 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 846 | 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] | 847 | t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 848 | } |
| 849 | }) |
| 850 | } |
| 851 | } |
| 852 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 853 | func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) { |
| 854 | type args struct { |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 855 | Direction string |
| 856 | IntfID uint32 |
| 857 | OnuID uint32 |
| 858 | UniID uint32 |
| 859 | tpID uint32 |
| 860 | MeterInfo *MeterInfo |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 861 | } |
| 862 | tests := []struct { |
| 863 | name string |
| 864 | fields *fields |
| 865 | args args |
| 866 | wantErr error |
| 867 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 868 | {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2, |
Girish Gowdra | a482f27 | 2021-03-24 23:04:19 -0700 | [diff] [blame] | 869 | 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] | 870 | } |
| 871 | for _, tt := range tests { |
| 872 | t.Run(tt.name, func(t *testing.T) { |
| 873 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 874 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 875 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 876 | 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] | 877 | 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] | 878 | t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 879 | } |
| 880 | }) |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) { |
| 885 | type args struct { |
| 886 | IntfID uint32 |
| 887 | OnuID uint32 |
| 888 | UniID uint32 |
| 889 | TpID uint32 |
| 890 | } |
| 891 | tests := []struct { |
| 892 | name string |
| 893 | fields *fields |
| 894 | args args |
| 895 | wantErr error |
| 896 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 897 | {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 898 | 2}, errors.New("failed to update resource")}, |
| 899 | } |
| 900 | for _, tt := range tests { |
| 901 | t.Run(tt.name, func(t *testing.T) { |
| 902 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 903 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 904 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 905 | 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] | 906 | t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 907 | } |
| 908 | }) |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | func TestSetKVClient(t *testing.T) { |
| 913 | type args struct { |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 914 | backend string |
| 915 | address string |
| 916 | DeviceID string |
| 917 | kvStorePrefix string |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 918 | } |
| 919 | tests := []struct { |
| 920 | name string |
| 921 | args args |
sbarbari | a8910ba | 2019-11-05 10:12:23 -0500 | [diff] [blame] | 922 | want *db.Backend |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 923 | }{ |
serkant.uluderya | 7b8211e | 2021-02-24 16:39:18 +0300 | [diff] [blame] | 924 | {"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] | 925 | } |
| 926 | for _, tt := range tests { |
| 927 | t.Run(tt.name, func(t *testing.T) { |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 928 | 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] | 929 | t.Errorf("SetKVClient() = %v, want %v", got, tt.want) |
| 930 | } |
| 931 | }) |
| 932 | } |
| 933 | } |
| 934 | |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 935 | func Test_newKVClient(t *testing.T) { |
| 936 | type args struct { |
| 937 | storeType string |
| 938 | address string |
Neha Sharma | cc65696 | 2020-04-14 14:26:11 +0000 | [diff] [blame] | 939 | timeout time.Duration |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 940 | } |
| 941 | var kvClient kvstore.Client |
| 942 | tests := []struct { |
| 943 | name string |
| 944 | args args |
| 945 | want kvstore.Client |
| 946 | wantErr error |
| 947 | }{ |
| 948 | {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")}, |
| 949 | } |
| 950 | for _, tt := range tests { |
| 951 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 952 | 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] | 953 | if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 954 | t.Errorf("newKVClient() got = %v, want %v", got, tt.want) |
| 955 | } |
| 956 | if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 957 | t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 958 | return |
| 959 | } |
| 960 | |
| 961 | }) |
| 962 | } |
| 963 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 964 | |
| 965 | func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) { |
| 966 | type args struct { |
| 967 | intf uint32 |
| 968 | gem uint32 |
| 969 | servicePriority uint32 |
| 970 | } |
| 971 | tests := []struct { |
| 972 | name string |
| 973 | args args |
| 974 | fields *fields |
| 975 | }{ |
| 976 | {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()}, |
| 977 | {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()}, |
| 978 | {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()}, |
| 979 | } |
| 980 | for _, tt := range tests { |
| 981 | t.Run(tt.name, func(t *testing.T) { |
| 982 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 983 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 984 | defer cancel() |
yasin sapli | bddc2d7 | 2022-02-08 13:10:17 +0000 | [diff] [blame] | 985 | err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.gem, tt.args.servicePriority) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 986 | if err != nil { |
| 987 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 988 | return |
| 989 | } |
| 990 | }) |
| 991 | } |
| 992 | } |
| 993 | |
Girish Gowdra | f3728b1 | 2022-02-02 21:46:51 -0800 | [diff] [blame] | 994 | func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) { |
| 995 | tests := []struct { |
| 996 | name string |
| 997 | fields *fields |
| 998 | }{ |
| 999 | {"DeleteMcastQueueForIntf-1", getResMgr()}, |
| 1000 | } |
| 1001 | for _, tt := range tests { |
| 1002 | t.Run(tt.name, func(t *testing.T) { |
| 1003 | RsrcMgr := testResMgrObject(tt.fields) |
| 1004 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1005 | defer cancel() |
| 1006 | RsrcMgr.DeleteMcastQueueForIntf(ctx) |
| 1007 | }) |
| 1008 | } |
| 1009 | } |
| 1010 | |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1011 | func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry { |
| 1012 | groupDesc := ofp.OfpGroupDesc{ |
| 1013 | Type: ofp.OfpGroupType_OFPGT_ALL, |
| 1014 | GroupId: groupID, |
| 1015 | } |
| 1016 | groupEntry := ofp.OfpGroupEntry{ |
| 1017 | Desc: &groupDesc, |
| 1018 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1019 | for i := 0; i < len(outPorts); i++ { |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 1020 | var acts []*ofp.OfpAction |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1021 | acts = append(acts, fu.Output(outPorts[i])) |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 1022 | bucket := ofp.OfpBucket{ |
| 1023 | Actions: acts, |
| 1024 | } |
| 1025 | groupDesc.Buckets = append(groupDesc.Buckets, &bucket) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1026 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1027 | return &groupEntry |
| 1028 | } |
| 1029 | |
| 1030 | func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) { |
| 1031 | type args struct { |
| 1032 | group *ofp.OfpGroupEntry |
| 1033 | cached bool |
| 1034 | } |
| 1035 | //create group 1 |
| 1036 | group1 := newGroup(1, []uint32{1}) |
| 1037 | //create group 2 |
| 1038 | group2 := newGroup(2, []uint32{2}) |
| 1039 | //define test set |
| 1040 | tests := []struct { |
| 1041 | name string |
| 1042 | args args |
| 1043 | fields *fields |
| 1044 | }{ |
| 1045 | {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()}, |
| 1046 | {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()}, |
| 1047 | } |
| 1048 | //execute tests |
| 1049 | for _, tt := range tests { |
| 1050 | t.Run(tt.name, func(t *testing.T) { |
| 1051 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1052 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1053 | defer cancel() |
| 1054 | err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1055 | if err != nil { |
| 1056 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 1057 | return |
| 1058 | } |
| 1059 | }) |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) { |
| 1064 | type args struct { |
| 1065 | groupID uint32 |
| 1066 | cached bool |
| 1067 | } |
| 1068 | //define test set |
| 1069 | tests := []struct { |
| 1070 | name string |
| 1071 | args args |
| 1072 | fields *fields |
| 1073 | }{ |
| 1074 | {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 1075 | {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 1076 | } |
| 1077 | //execute tests |
| 1078 | for _, tt := range tests { |
| 1079 | t.Run(tt.name, func(t *testing.T) { |
| 1080 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1081 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1082 | defer cancel() |
Esin Karaman | d519bbf | 2020-07-01 11:16:03 +0000 | [diff] [blame] | 1083 | err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
| 1084 | if err != nil { |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1085 | t.Errorf("%s got false but wants true", tt.name) |
| 1086 | return |
| 1087 | } |
| 1088 | }) |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) { |
| 1093 | type args struct { |
| 1094 | groupID uint32 |
| 1095 | cached bool |
| 1096 | } |
| 1097 | //define test set |
| 1098 | tests := []struct { |
| 1099 | name string |
| 1100 | args args |
| 1101 | fields *fields |
| 1102 | }{ |
| 1103 | {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 1104 | {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 1105 | {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()}, |
| 1106 | } |
| 1107 | //execute tests |
| 1108 | for _, tt := range tests { |
| 1109 | t.Run(tt.name, func(t *testing.T) { |
| 1110 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1111 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1112 | defer cancel() |
| 1113 | exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1114 | if err != nil { |
| 1115 | t.Errorf("%s got error but wants nil error", tt.name) |
| 1116 | return |
| 1117 | } else if exists && (groupInfo.GroupID == 0) { |
| 1118 | t.Errorf("%s got true and nil group info but expected not nil group info", tt.name) |
| 1119 | return |
| 1120 | } else if tt.args.groupID == 3 && exists { |
| 1121 | t.Errorf("%s got true but wants false", tt.name) |
| 1122 | return |
| 1123 | } |
| 1124 | }) |
| 1125 | } |
| 1126 | } |