blob: 4763a313262f5f4d77083e3c1949d78abeb5eeae [file] [log] [blame]
cbabuabf02352019-10-15 13:14:56 +02001/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
cbabuabf02352019-10-15 13:14:56 +02003
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
cbabubef89432019-10-18 11:47:27 +020017/*
18This file contains unit test cases for functions in the file resourcemanager.go.
19This file also implements the Client interface to mock the kv-client, fields struct to mock OpenOltResourceMgr
20and few utility functions.
21*/
22
Joey Armstrong3f0e2422023-07-05 18:25:41 -040023// Package adaptercore provides the utility for olt devices, flows and statistics
cbabuabf02352019-10-15 13:14:56 +020024package resourcemanager
25
26import (
npujarec5762e2020-01-01 14:08:48 +053027 "context"
cbabuabf02352019-10-15 13:14:56 +020028 "encoding/json"
29 "errors"
yasin sapli9e4c5092022-02-01 13:52:33 +000030 "fmt"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030031 "reflect"
32 "strconv"
33 "strings"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030034 "testing"
35 "time"
36
khenaidoo106c61a2021-08-11 18:05:46 -040037 "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"
cbabuabf02352019-10-15 13:14:56 +020047)
48
49func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040050 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020051}
52
53const (
54 // MeterConfig meter to extract meter
55 MeterConfig = "meter_id"
56 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040057 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020058 // 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
cbabubef89432019-10-18 11:47:27 +020074// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020075type fields struct {
Girish Gowdra76a1b092021-07-28 10:07:04 -070076 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
cbabuabf02352019-10-15 13:14:56 +020085}
cbabubef89432019-10-18 11:47:27 +020086
87// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020088type MockResKVClient struct {
89}
90
cbabubef89432019-10-18 11:47:27 +020091// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020092func getResMgr() *fields {
93 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050094 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020095 Client: &MockResKVClient{},
96 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070097 resMgr.PonRsrMgr = &ponrmgr.PONResourceManager{}
cbabuabf02352019-10-15 13:14:56 +020098 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 Gowdrafb3d6102020-10-16 16:32:36 -0700112 resMgr.NumOfPonPorts = 16
yasin saplid0566272021-12-21 09:10:30 +0000113 resMgr.DevInfo = &openolt.DeviceInfo{PonPorts: 16}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700114 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 Scandolo84585372021-03-18 14:21:22 -0700117 Client: &MockResKVClient{},
118 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700119 resMgr.PonRsrMgr.Technology = "XGS-PON"
120 resMgr.PonRsrMgr.PonResourceRanges = ranges
121 resMgr.PonRsrMgr.SharedIdxByType = sharedIdxByType
Girish Gowdra76a1b092021-07-28 10:07:04 -0700122 resMgr.TechProfileRef = mocks.MockTechProfile{}
123
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700124 /*
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 Scandolo84585372021-03-18 14:21:22 -0700130
cbabuabf02352019-10-15 13:14:56 +0200131 return &resMgr
132}
cbabubef89432019-10-18 11:47:27 +0200133
134// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530135func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200136 return nil, errors.New("key didn't find")
137}
138
139// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530140func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000141 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200142 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 Abakafee36392019-10-03 11:17:24 +0000151 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200152 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 Sharma96b7bf22020-06-15 10:37:32 +0000162 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200163 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 Sharma96b7bf22020-06-15 10:37:32 +0000171 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200172 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 Sharma96b7bf22020-06-15 10:37:32 +0000176 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200177 str, _ := json.Marshal(1)
178 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
179 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000180 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000181 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000182 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
cbabuabf02352019-10-15 13:14:56 +0200193 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
pnalmas04ede3b2025-01-16 18:05:27 +0530200// GetWithPrefix mock function implementation for KVClient
201func (kvclient *MockResKVClient) GetWithPrefix(ctx context.Context, prefix string) (map[string]*kvstore.KVPair, error) {
202 // Implement your logic here to retrieve key-value pairs with the given prefix
203 return nil, errors.New("key didn't find")
204}
205
206// GetWithPrefixKeysOnly mock function implementation for KVClient
207func (kvclient *MockResKVClient) GetWithPrefixKeysOnly(ctx context.Context, prefix string) ([]string, error) {
208 // Implement your logic here to retrieve keys with the given prefix
209 return nil, errors.New("key didn't find")
210}
211
cbabuabf02352019-10-15 13:14:56 +0200212// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530213func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200214 if key != "" {
215 return nil
216 }
217 return errors.New("key didn't find")
218}
219
220// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530221func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200222 return nil
223}
224
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300225// DeleteWithPrefix mock function implementation for KVClient
226func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
227 return nil
228}
229
cbabuabf02352019-10-15 13:14:56 +0200230// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000231func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200232 return nil, errors.New("key didn't find")
233}
234
235// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530236func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200237 return nil
238}
239
240// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530241func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200242 return nil
243}
244
245// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530246func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200247 return nil
248}
249
250// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800251func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200252 return nil
253}
254
255// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000256func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200257 return nil
258}
259
260// ReleaseLock mock function implementation for KVClient
261func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
262 return nil
263}
264
265// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530266func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200267 return true
268}
269
Abhay Kumara61c5222025-11-10 07:32:50 +0000270// KeyExists mock function implementation for KVClient
271func (kvclient *MockResKVClient) KeyExists(ctx context.Context, key string) (bool, error) {
272 if key != "" {
273 return true, nil
274 }
275 return false, errors.New("key didn't find")
276}
277
cbabuabf02352019-10-15 13:14:56 +0200278// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000279func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200280}
281
282// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000283func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200284}
285
cbabubef89432019-10-18 11:47:27 +0200286// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200287func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700288 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700289 DeviceID: testResMgr.DeviceID,
290 Args: testResMgr.Args,
291 KVStore: testResMgr.KVStore,
292 DeviceType: testResMgr.DeviceType,
293 Address: testResMgr.Address,
294 DevInfo: testResMgr.DevInfo,
295 PonRsrMgr: testResMgr.PonRsrMgr,
296 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200297 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700298 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700299
300 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200301}
302
303func TestNewResourceMgr(t *testing.T) {
304 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000305 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700306 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000307 KVStoreAddress string
308 kvStoreType string
309 deviceType string
310 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800311 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200312 }
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +0530313 /* As of not the current NewResourceMgr test is not doing anything as there was no resourceranges passed.
314 passing the resource ranges would mean passing a mock and changes in all around including device handler and other places.
315 For now , removed the older version of proto which used ONUIDSTart and ONUIDENd which is not valid.
316 This test needs to be updated once the kv store mock is fixed all around. Use the below resource ranges in the Ranges of deviceinfo once the kv store is fixed.
317 intfids := []uint32{0, 1, 2, 3, 4, 5}
318 devOnuRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 60}
319 devGemRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 10000}
320 devAllocRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 256}
321 devFlowRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_FLOW_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_SHARED_BY_ALL_INTF_SAME_TECH, Start: 1, End: 20000}
322 pool := []*openolt.DeviceInfo_DeviceResourceRanges_Pool{devOnuRsrcPools, devGemRsrcPools, devAllocRsrcPools, devFlowRsrcPools}
323 devRsrc := &openolt.DeviceInfo_DeviceResourceRanges{IntfIds: intfids, Technology: "GPON", Pools: pool}
324 devRsrcPool := []*openolt.DeviceInfo_DeviceResourceRanges{devRsrc}
325 */
cbabuabf02352019-10-15 13:14:56 +0200326 tests := []struct {
327 name string
328 args args
329 want *OpenOltResourceMgr
330 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700331 {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd",
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +0530332 "olt", &openolt.DeviceInfo{}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200333 }
334 for _, tt := range tests {
335 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530336 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
337 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700338 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) {
cbabuabf02352019-10-15 13:14:56 +0200339 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
340 }
341 })
342 }
343}
344
345func TestOpenOltResourceMgr_Delete(t *testing.T) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700346 type args struct {
347 intfID uint32
348 }
cbabuabf02352019-10-15 13:14:56 +0200349 tests := []struct {
350 name string
351 fields *fields
352 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700353 args args
cbabuabf02352019-10-15 13:14:56 +0200354 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700355 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200356 }
357 for _, tt := range tests {
358 t.Run(tt.name, func(t *testing.T) {
359 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530360 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
361 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700362 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200363 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
364 }
365 })
366 }
367}
368
cbabuabf02352019-10-15 13:14:56 +0200369func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
370 type args struct {
371 intfID uint32
372 onuID uint32
373 uniID uint32
374 }
375 tests := []struct {
376 name string
377 fields *fields
378 args args
379 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700380 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200381 }
382 for _, tt := range tests {
383 t.Run(tt.name, func(t *testing.T) {
384 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530385 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
386 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000387 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200388 })
389 }
390}
391
392func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
393 type args struct {
394 intfID uint32
395 onuID []uint32
396 }
397 tests := []struct {
398 name string
399 fields *fields
400 args args
401 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700402 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200403 }
404 for _, tt := range tests {
405 t.Run(tt.name, func(t *testing.T) {
406 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530407 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
408 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000409 RsrcMgr.FreeonuID(ctx, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200410 })
411 }
412}
413
cbabuabf02352019-10-15 13:14:56 +0200414func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
415 type args struct {
416 intfID uint32
417 onuID uint32
418 uniID uint32
419 }
420 tests := []struct {
421 name string
422 fields *fields
423 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000424 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200425 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700426 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200427 }
428 for _, tt := range tests {
429 t.Run(tt.name, func(t *testing.T) {
430 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530431 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
432 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000433 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.onuID, tt.args.uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700434 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000435 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700436 } else {
437 for i := range tt.want {
438 if got[i] != tt.want[i] {
439 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
440 break
441 }
442 }
cbabuabf02352019-10-15 13:14:56 +0200443 }
444 })
445 }
446}
447
Girish Gowdra950326e2021-11-05 12:43:24 -0700448func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) {
Girish Gowdra950326e2021-11-05 12:43:24 -0700449 type args struct {
450 PONIntfID uint32
451 }
452 tests := []struct {
453 name string
454 fields *fields
455 args args
456 want error
457 }{
458 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
459 }
460 for _, tt := range tests {
461 t.Run(tt.name, func(t *testing.T) {
462 RsrcMgr := testResMgrObject(tt.fields)
463 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
464 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000465 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700466 if err != nil {
467 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
468 }
469 })
470 }
471}
472
473func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
Girish Gowdra950326e2021-11-05 12:43:24 -0700474 type args struct {
475 PONIntfID uint32
476 }
477 tests := []struct {
478 name string
479 fields *fields
480 args args
481 want error
482 }{
483 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
484 }
485 for _, tt := range tests {
486 t.Run(tt.name, func(t *testing.T) {
487 RsrcMgr := testResMgrObject(tt.fields)
488 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
489 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000490 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700491 if err != nil {
492 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
493 }
494 })
495 }
496}
497
yasin sapli9e4c5092022-02-01 13:52:33 +0000498func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000499 type args struct {
500 intfID uint32
501 onuID uint32
502 gemPortIDs []uint32
503 gemPortIDsToBeDeleted []uint32
504 gemPortIDsRemaining []uint32
505 serialNum string
506 finalLength int
507 }
508 tests := []struct {
509 name string
510 fields *fields
511 args args
512 }{
513 // Add/Delete single gem port
514 {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
515 // Delete all gemports
516 {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
517 // Try to delete when there is no gem port
518 {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}},
519 // Try to delete non-existent gem port
520 {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
521 // Try to delete two of the gem ports
522 {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
523 }
524 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
525 defer cancel()
526 for _, tt := range tests {
527 t.Run(tt.name, func(t *testing.T) {
528 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000529 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000530 t.Errorf("failed to remove onu")
531 }
yasin saplibddc2d72022-02-08 13:10:17 +0000532 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000533 t.Errorf("failed to add onu")
534 }
balaji.nagarajan29be8bc2026-01-26 16:31:41 +0530535 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, tt.args.gemPortIDs); err != nil {
536 t.Errorf("failed to add gem to onu")
yasin sapli9e4c5092022-02-01 13:52:33 +0000537 }
538 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
yasin saplibddc2d72022-02-08 13:10:17 +0000539 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.onuID, gemPortDeleted); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000540 t.Errorf("failed to remove gem from onu")
541 }
542 }
543 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000544 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000545 if err != nil || gP == nil {
546 t.Errorf("failed to get onuGemInfo")
547 }
548 var gemPorts []uint32
549
550 lenofGemPorts = len(gP.GemPorts)
551 gemPorts = gP.GemPorts
552
553 if lenofGemPorts != tt.args.finalLength {
554 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
555 }
556
557 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
558 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
559 }
560 })
561 }
562}
563
564func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000565 type args struct {
566 PONIntfID uint32
567 OnuCount uint32
568 }
569 tests := []struct {
570 name string
571 fields *fields
572 args args
573 want error
574 }{
575 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
576 }
577 for _, tt := range tests {
578 t.Run(tt.name, func(t *testing.T) {
579 RsrcMgr := testResMgrObject(tt.fields)
580 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
581 defer cancel()
582 for j := 1; j <= int(tt.args.OnuCount); j++ {
Akash Kankanala041a2122024-10-16 15:49:22 +0530583 go func(i uint32) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000584 // TODO: actually verify success
yasin saplibddc2d72022-02-08 13:10:17 +0000585 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, fmt.Sprintf("onu-%d", i))
Akash Kankanala041a2122024-10-16 15:49:22 +0530586 }(tt.args.PONIntfID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000587 }
588 })
589 }
590}
591
592func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000593 type args struct {
594 intfID uint32
595 onuID uint32
596 gemPortIDs []uint32
597 gemPortIDsRemaining []uint32
598 serialNum string
599 finalLength int
600 }
601 tests := []struct {
602 name string
603 fields *fields
604 args args
605 }{
606 // Add single gem port
607 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
608 // Delete all gemports
609 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
610 // Do not add any gemport
611 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
612 }
613 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
614 defer cancel()
615 for _, tt := range tests {
616 t.Run(tt.name, func(t *testing.T) {
617 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000618 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000619 t.Errorf("failed to remove onu")
620 }
yasin saplibddc2d72022-02-08 13:10:17 +0000621 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000622 t.Errorf("failed to add onu")
623 }
balaji.nagarajan29be8bc2026-01-26 16:31:41 +0530624 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, tt.args.gemPortIDs); err != nil {
625 t.Errorf("failed to add gem to onu")
yasin sapli9e4c5092022-02-01 13:52:33 +0000626 }
627
628 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000629 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000630
631 var gemPorts []uint32
632 if err == nil && gP != nil {
633 lenofGemPorts = len(gP.GemPorts)
634 gemPorts = gP.GemPorts
635 }
636 if lenofGemPorts != tt.args.finalLength {
637 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
638 }
639
640 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
641 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
642 }
643 })
644 }
645}
646
cbabuabf02352019-10-15 13:14:56 +0200647func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
648 type args struct {
649 intfID uint32
650 onuID uint32
651 uniID uint32
652 }
653 tests := []struct {
654 name string
655 fields *fields
656 args args
657 want []uint32
658 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700659 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200660 }
661 for _, tt := range tests {
662 t.Run(tt.name, func(t *testing.T) {
663 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530664 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
665 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000666 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200667 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
668 }
669 })
670 }
671}
672
Girish Gowdraa482f272021-03-24 23:04:19 -0700673func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200674 type args struct {
675 Direction string
676 IntfID uint32
677 OnuID uint32
678 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000679 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200680 }
681 tests := []struct {
682 name string
683 fields *fields
684 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700685 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200686 wantErr error
687 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700688 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
689 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
690 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
691 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200692 }
693 for _, tt := range tests {
694 t.Run(tt.name, func(t *testing.T) {
695 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530696 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
697 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000698 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200699 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700700 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200701 }
702 })
703 }
704}
705
706func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
707 type args struct {
708 ponIntfID uint32
709 }
710 tests := []struct {
711 name string
712 fields *fields
713 args args
714 want uint32
715 wantErr error
716 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700717 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200718 }
719 for _, tt := range tests {
720 t.Run(tt.name, func(t *testing.T) {
721 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530722 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
723 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000724 got, err := RsrcMgr.GetONUID(ctx)
cbabuabf02352019-10-15 13:14:56 +0200725 if got != tt.want && err != nil {
726 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
727 }
728 })
729 }
730}
731
732func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200733 type args struct {
734 IntfID uint32
735 OnuID uint32
736 UniID uint32
737 }
738 tests := []struct {
739 name string
740 fields *fields
741 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000742 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200743 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700744 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000745 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200746 }
747 for _, tt := range tests {
748 t.Run(tt.name, func(t *testing.T) {
749 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530750 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
751 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000752 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200753 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
754 }
755 })
756 }
757}
758
cbabuabf02352019-10-15 13:14:56 +0200759func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200760 type args struct {
761 Direction string
762 IntfID uint32
763 OnuID uint32
764 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000765 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200766 }
767 tests := []struct {
768 name string
769 fields *fields
770 args args
771 wantErr error
772 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000773 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200774 errors.New("failed to delete meter id %s from kvstore")},
775 }
776 for _, tt := range tests {
777 t.Run(tt.name, func(t *testing.T) {
778 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530779 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
780 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000781 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000782 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200783 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
784 }
785 })
786 }
787}
788
789func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
790 type args struct {
791 IntfID uint32
792 OnuID uint32
793 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000794 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200795 }
796 tests := []struct {
797 name string
798 fields *fields
799 args args
800 wantErr error
801 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700802 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200803 errors.New("failed to delete techprofile id resource %s in KV store")},
804 }
805 for _, tt := range tests {
806 t.Run(tt.name, func(t *testing.T) {
807 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530808 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
809 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000810 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000811 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200812 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
813 }
814 })
815 }
816}
817
818func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
819 type args struct {
820 ponPort uint32
821 onuID uint32
822 uniID uint32
823 allocID []uint32
824 }
825 tests := []struct {
826 name string
827 fields *fields
828 args args
829 wantErr error
830 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700831 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200832 errors.New("")},
833 }
834 for _, tt := range tests {
835 t.Run(tt.name, func(t *testing.T) {
836 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530837 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
838 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000839 if err := RsrcMgr.UpdateAllocIdsForOnu(ctx, tt.args.onuID, tt.args.uniID, tt.args.allocID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200840 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
841 }
842 })
843 }
844}
845
cbabuabf02352019-10-15 13:14:56 +0200846func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200847 type args struct {
848 ponPort uint32
849 onuID uint32
850 uniID uint32
851 GEMPortList []uint32
852 }
853 tests := []struct {
854 name string
855 fields *fields
856 args args
857 wantErr error
858 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700859 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200860 []uint32{1, 2}}, errors.New("failed to update resource")},
861 }
862 for _, tt := range tests {
863 t.Run(tt.name, func(t *testing.T) {
864 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530865 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
866 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000867 if err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, tt.args.onuID, tt.args.uniID, tt.args.GEMPortList); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200868 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
869 }
870 })
871 }
872}
873
cbabuabf02352019-10-15 13:14:56 +0200874func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
875 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700876 Direction string
877 IntfID uint32
878 OnuID uint32
879 UniID uint32
880 tpID uint32
881 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200882 }
883 tests := []struct {
884 name string
885 fields *fields
886 args args
887 wantErr error
888 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700889 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700890 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200891 }
892 for _, tt := range tests {
893 t.Run(tt.name, func(t *testing.T) {
894 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530895 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
896 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000897 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Girish Gowdraa482f272021-03-24 23:04:19 -0700898 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200899 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
900 }
901 })
902 }
903}
904
905func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
906 type args struct {
907 IntfID uint32
908 OnuID uint32
909 UniID uint32
910 TpID uint32
911 }
912 tests := []struct {
913 name string
914 fields *fields
915 args args
916 wantErr error
917 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700918 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200919 2}, errors.New("failed to update resource")},
920 }
921 for _, tt := range tests {
922 t.Run(tt.name, func(t *testing.T) {
923 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530924 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
925 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000926 if err := RsrcMgr.UpdateTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID, tt.args.TpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200927 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
928 }
929 })
930 }
931}
932
933func TestSetKVClient(t *testing.T) {
934 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800935 backend string
936 address string
937 DeviceID string
938 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200939 }
940 tests := []struct {
941 name string
942 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500943 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200944 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300945 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200946 }
947 for _, tt := range tests {
948 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800949 if got := SetKVClient(context.Background(), tt.args.backend, tt.args.address, tt.args.DeviceID, tt.args.kvStorePrefix); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200950 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
951 }
952 })
953 }
954}
955
cbabuabf02352019-10-15 13:14:56 +0200956func Test_newKVClient(t *testing.T) {
957 type args struct {
958 storeType string
959 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000960 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200961 }
962 var kvClient kvstore.Client
963 tests := []struct {
964 name string
965 args args
966 want kvstore.Client
967 wantErr error
968 }{
969 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
970 }
971 for _, tt := range tests {
972 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000973 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200974 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
975 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
976 }
977 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
978 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
979 return
980 }
cbabuabf02352019-10-15 13:14:56 +0200981 })
982 }
983}
Esin Karamanccb714b2019-11-29 15:02:06 +0000984
985func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
986 type args struct {
987 intf uint32
988 gem uint32
989 servicePriority uint32
990 }
991 tests := []struct {
992 name string
993 args args
994 fields *fields
995 }{
996 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
997 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
998 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
999 }
1000 for _, tt := range tests {
1001 t.Run(tt.name, func(t *testing.T) {
1002 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301003 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1004 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +00001005 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001006 if err != nil {
1007 t.Errorf("%s got err= %s wants nil", tt.name, err)
1008 return
1009 }
1010 })
1011 }
1012}
1013
Girish Gowdraf3728b12022-02-02 21:46:51 -08001014func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
1015 tests := []struct {
1016 name string
1017 fields *fields
1018 }{
1019 {"DeleteMcastQueueForIntf-1", getResMgr()},
1020 }
1021 for _, tt := range tests {
1022 t.Run(tt.name, func(t *testing.T) {
1023 RsrcMgr := testResMgrObject(tt.fields)
1024 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1025 defer cancel()
Gustavo Silva41af9122022-10-11 11:05:13 -03001026 _ = RsrcMgr.DeleteMcastQueueForIntf(ctx)
Girish Gowdraf3728b12022-02-02 21:46:51 -08001027 })
1028 }
1029}
1030
Esin Karamanccb714b2019-11-29 15:02:06 +00001031func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1032 groupDesc := ofp.OfpGroupDesc{
1033 Type: ofp.OfpGroupType_OFPGT_ALL,
1034 GroupId: groupID,
1035 }
1036 groupEntry := ofp.OfpGroupEntry{
1037 Desc: &groupDesc,
1038 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001039 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001040 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001041 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001042 bucket := ofp.OfpBucket{
1043 Actions: acts,
1044 }
1045 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001046 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001047 return &groupEntry
1048}
1049
1050func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1051 type args struct {
1052 group *ofp.OfpGroupEntry
1053 cached bool
1054 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301055 // create group 1
Esin Karamanccb714b2019-11-29 15:02:06 +00001056 group1 := newGroup(1, []uint32{1})
Akash Kankanala041a2122024-10-16 15:49:22 +05301057 // create group 2
Esin Karamanccb714b2019-11-29 15:02:06 +00001058 group2 := newGroup(2, []uint32{2})
Akash Kankanala041a2122024-10-16 15:49:22 +05301059 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001060 tests := []struct {
1061 name string
1062 args args
1063 fields *fields
1064 }{
1065 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1066 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1067 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301068 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001069 for _, tt := range tests {
1070 t.Run(tt.name, func(t *testing.T) {
1071 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301072 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1073 defer cancel()
1074 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001075 if err != nil {
1076 t.Errorf("%s got err= %s wants nil", tt.name, err)
1077 return
1078 }
1079 })
1080 }
1081}
1082
1083func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1084 type args struct {
1085 groupID uint32
1086 cached bool
1087 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301088 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001089 tests := []struct {
1090 name string
1091 args args
1092 fields *fields
1093 }{
1094 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1095 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1096 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301097 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001098 for _, tt := range tests {
1099 t.Run(tt.name, func(t *testing.T) {
1100 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301101 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1102 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001103 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1104 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001105 t.Errorf("%s got false but wants true", tt.name)
1106 return
1107 }
1108 })
1109 }
1110}
1111
1112func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1113 type args struct {
1114 groupID uint32
1115 cached bool
1116 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301117 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001118 tests := []struct {
1119 name string
1120 args args
1121 fields *fields
1122 }{
1123 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1124 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1125 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1126 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301127 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001128 for _, tt := range tests {
1129 t.Run(tt.name, func(t *testing.T) {
1130 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301131 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1132 defer cancel()
1133 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001134 if err != nil {
1135 t.Errorf("%s got error but wants nil error", tt.name)
1136 return
1137 } else if exists && (groupInfo.GroupID == 0) {
1138 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1139 return
1140 } else if tt.args.groupID == 3 && exists {
1141 t.Errorf("%s got true but wants false", tt.name)
1142 return
1143 }
1144 })
1145 }
1146}