blob: 2e408aebfb541c470cccff8d78f6803ed565a961 [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 }
535 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000536 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000537 t.Errorf("failed to add gem to onu")
538 }
539 }
540 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
yasin saplibddc2d72022-02-08 13:10:17 +0000541 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.onuID, gemPortDeleted); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000542 t.Errorf("failed to remove gem from onu")
543 }
544 }
545 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000546 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000547 if err != nil || gP == nil {
548 t.Errorf("failed to get onuGemInfo")
549 }
550 var gemPorts []uint32
551
552 lenofGemPorts = len(gP.GemPorts)
553 gemPorts = gP.GemPorts
554
555 if lenofGemPorts != tt.args.finalLength {
556 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
557 }
558
559 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
560 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
561 }
562 })
563 }
564}
565
566func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000567 type args struct {
568 PONIntfID uint32
569 OnuCount uint32
570 }
571 tests := []struct {
572 name string
573 fields *fields
574 args args
575 want error
576 }{
577 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
578 }
579 for _, tt := range tests {
580 t.Run(tt.name, func(t *testing.T) {
581 RsrcMgr := testResMgrObject(tt.fields)
582 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
583 defer cancel()
584 for j := 1; j <= int(tt.args.OnuCount); j++ {
Akash Kankanala041a2122024-10-16 15:49:22 +0530585 go func(i uint32) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000586 // TODO: actually verify success
yasin saplibddc2d72022-02-08 13:10:17 +0000587 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, fmt.Sprintf("onu-%d", i))
Akash Kankanala041a2122024-10-16 15:49:22 +0530588 }(tt.args.PONIntfID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000589 }
590 })
591 }
592}
593
594func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000595 type args struct {
596 intfID uint32
597 onuID uint32
598 gemPortIDs []uint32
599 gemPortIDsRemaining []uint32
600 serialNum string
601 finalLength int
602 }
603 tests := []struct {
604 name string
605 fields *fields
606 args args
607 }{
608 // Add single gem port
609 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
610 // Delete all gemports
611 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
612 // Do not add any gemport
613 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
614 }
615 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
616 defer cancel()
617 for _, tt := range tests {
618 t.Run(tt.name, func(t *testing.T) {
619 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000620 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000621 t.Errorf("failed to remove onu")
622 }
yasin saplibddc2d72022-02-08 13:10:17 +0000623 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000624 t.Errorf("failed to add onu")
625 }
626 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000627 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000628 t.Errorf("failed to add gem to onu")
629 }
630 }
631
632 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000633 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000634
635 var gemPorts []uint32
636 if err == nil && gP != nil {
637 lenofGemPorts = len(gP.GemPorts)
638 gemPorts = gP.GemPorts
639 }
640 if lenofGemPorts != tt.args.finalLength {
641 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
642 }
643
644 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
645 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
646 }
647 })
648 }
649}
650
cbabuabf02352019-10-15 13:14:56 +0200651func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
652 type args struct {
653 intfID uint32
654 onuID uint32
655 uniID uint32
656 }
657 tests := []struct {
658 name string
659 fields *fields
660 args args
661 want []uint32
662 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700663 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200664 }
665 for _, tt := range tests {
666 t.Run(tt.name, func(t *testing.T) {
667 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530668 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
669 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000670 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200671 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
672 }
673 })
674 }
675}
676
Girish Gowdraa482f272021-03-24 23:04:19 -0700677func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200678 type args struct {
679 Direction string
680 IntfID uint32
681 OnuID uint32
682 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000683 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200684 }
685 tests := []struct {
686 name string
687 fields *fields
688 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700689 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200690 wantErr error
691 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700692 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
693 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
694 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
695 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200696 }
697 for _, tt := range tests {
698 t.Run(tt.name, func(t *testing.T) {
699 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530700 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
701 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000702 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200703 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700704 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200705 }
706 })
707 }
708}
709
710func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
711 type args struct {
712 ponIntfID uint32
713 }
714 tests := []struct {
715 name string
716 fields *fields
717 args args
718 want uint32
719 wantErr error
720 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700721 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200722 }
723 for _, tt := range tests {
724 t.Run(tt.name, func(t *testing.T) {
725 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530726 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
727 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000728 got, err := RsrcMgr.GetONUID(ctx)
cbabuabf02352019-10-15 13:14:56 +0200729 if got != tt.want && err != nil {
730 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
731 }
732 })
733 }
734}
735
736func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200737 type args struct {
738 IntfID uint32
739 OnuID uint32
740 UniID uint32
741 }
742 tests := []struct {
743 name string
744 fields *fields
745 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000746 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200747 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700748 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000749 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200750 }
751 for _, tt := range tests {
752 t.Run(tt.name, func(t *testing.T) {
753 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530754 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
755 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000756 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200757 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
758 }
759 })
760 }
761}
762
cbabuabf02352019-10-15 13:14:56 +0200763func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200764 type args struct {
765 Direction string
766 IntfID uint32
767 OnuID uint32
768 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000769 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200770 }
771 tests := []struct {
772 name string
773 fields *fields
774 args args
775 wantErr error
776 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000777 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200778 errors.New("failed to delete meter id %s from kvstore")},
779 }
780 for _, tt := range tests {
781 t.Run(tt.name, func(t *testing.T) {
782 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530783 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
784 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000785 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000786 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200787 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
788 }
789 })
790 }
791}
792
793func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
794 type args struct {
795 IntfID uint32
796 OnuID uint32
797 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000798 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200799 }
800 tests := []struct {
801 name string
802 fields *fields
803 args args
804 wantErr error
805 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700806 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200807 errors.New("failed to delete techprofile id resource %s in KV store")},
808 }
809 for _, tt := range tests {
810 t.Run(tt.name, func(t *testing.T) {
811 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530812 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
813 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000814 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000815 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200816 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
817 }
818 })
819 }
820}
821
822func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
823 type args struct {
824 ponPort uint32
825 onuID uint32
826 uniID uint32
827 allocID []uint32
828 }
829 tests := []struct {
830 name string
831 fields *fields
832 args args
833 wantErr error
834 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700835 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200836 errors.New("")},
837 }
838 for _, tt := range tests {
839 t.Run(tt.name, func(t *testing.T) {
840 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530841 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
842 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000843 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 +0200844 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
845 }
846 })
847 }
848}
849
cbabuabf02352019-10-15 13:14:56 +0200850func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200851 type args struct {
852 ponPort uint32
853 onuID uint32
854 uniID uint32
855 GEMPortList []uint32
856 }
857 tests := []struct {
858 name string
859 fields *fields
860 args args
861 wantErr error
862 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700863 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200864 []uint32{1, 2}}, errors.New("failed to update resource")},
865 }
866 for _, tt := range tests {
867 t.Run(tt.name, func(t *testing.T) {
868 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530869 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
870 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000871 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 +0200872 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
873 }
874 })
875 }
876}
877
cbabuabf02352019-10-15 13:14:56 +0200878func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
879 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700880 Direction string
881 IntfID uint32
882 OnuID uint32
883 UniID uint32
884 tpID uint32
885 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200886 }
887 tests := []struct {
888 name string
889 fields *fields
890 args args
891 wantErr error
892 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700893 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700894 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200895 }
896 for _, tt := range tests {
897 t.Run(tt.name, func(t *testing.T) {
898 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530899 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
900 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000901 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Girish Gowdraa482f272021-03-24 23:04:19 -0700902 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200903 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
904 }
905 })
906 }
907}
908
909func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
910 type args struct {
911 IntfID uint32
912 OnuID uint32
913 UniID uint32
914 TpID uint32
915 }
916 tests := []struct {
917 name string
918 fields *fields
919 args args
920 wantErr error
921 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700922 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200923 2}, errors.New("failed to update resource")},
924 }
925 for _, tt := range tests {
926 t.Run(tt.name, func(t *testing.T) {
927 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530928 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
929 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000930 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 +0200931 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
932 }
933 })
934 }
935}
936
937func TestSetKVClient(t *testing.T) {
938 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800939 backend string
940 address string
941 DeviceID string
942 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200943 }
944 tests := []struct {
945 name string
946 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500947 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200948 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300949 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200950 }
951 for _, tt := range tests {
952 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800953 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 +0200954 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
955 }
956 })
957 }
958}
959
cbabuabf02352019-10-15 13:14:56 +0200960func Test_newKVClient(t *testing.T) {
961 type args struct {
962 storeType string
963 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000964 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200965 }
966 var kvClient kvstore.Client
967 tests := []struct {
968 name string
969 args args
970 want kvstore.Client
971 wantErr error
972 }{
973 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
974 }
975 for _, tt := range tests {
976 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000977 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200978 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
979 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
980 }
981 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
982 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
983 return
984 }
cbabuabf02352019-10-15 13:14:56 +0200985 })
986 }
987}
Esin Karamanccb714b2019-11-29 15:02:06 +0000988
989func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
990 type args struct {
991 intf uint32
992 gem uint32
993 servicePriority uint32
994 }
995 tests := []struct {
996 name string
997 args args
998 fields *fields
999 }{
1000 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1001 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1002 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1003 }
1004 for _, tt := range tests {
1005 t.Run(tt.name, func(t *testing.T) {
1006 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301007 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1008 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +00001009 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001010 if err != nil {
1011 t.Errorf("%s got err= %s wants nil", tt.name, err)
1012 return
1013 }
1014 })
1015 }
1016}
1017
Girish Gowdraf3728b12022-02-02 21:46:51 -08001018func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
1019 tests := []struct {
1020 name string
1021 fields *fields
1022 }{
1023 {"DeleteMcastQueueForIntf-1", getResMgr()},
1024 }
1025 for _, tt := range tests {
1026 t.Run(tt.name, func(t *testing.T) {
1027 RsrcMgr := testResMgrObject(tt.fields)
1028 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1029 defer cancel()
Gustavo Silva41af9122022-10-11 11:05:13 -03001030 _ = RsrcMgr.DeleteMcastQueueForIntf(ctx)
Girish Gowdraf3728b12022-02-02 21:46:51 -08001031 })
1032 }
1033}
1034
Esin Karamanccb714b2019-11-29 15:02:06 +00001035func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1036 groupDesc := ofp.OfpGroupDesc{
1037 Type: ofp.OfpGroupType_OFPGT_ALL,
1038 GroupId: groupID,
1039 }
1040 groupEntry := ofp.OfpGroupEntry{
1041 Desc: &groupDesc,
1042 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001043 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001044 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001045 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001046 bucket := ofp.OfpBucket{
1047 Actions: acts,
1048 }
1049 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001050 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001051 return &groupEntry
1052}
1053
1054func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1055 type args struct {
1056 group *ofp.OfpGroupEntry
1057 cached bool
1058 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301059 // create group 1
Esin Karamanccb714b2019-11-29 15:02:06 +00001060 group1 := newGroup(1, []uint32{1})
Akash Kankanala041a2122024-10-16 15:49:22 +05301061 // create group 2
Esin Karamanccb714b2019-11-29 15:02:06 +00001062 group2 := newGroup(2, []uint32{2})
Akash Kankanala041a2122024-10-16 15:49:22 +05301063 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001064 tests := []struct {
1065 name string
1066 args args
1067 fields *fields
1068 }{
1069 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1070 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1071 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301072 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001073 for _, tt := range tests {
1074 t.Run(tt.name, func(t *testing.T) {
1075 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301076 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1077 defer cancel()
1078 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001079 if err != nil {
1080 t.Errorf("%s got err= %s wants nil", tt.name, err)
1081 return
1082 }
1083 })
1084 }
1085}
1086
1087func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1088 type args struct {
1089 groupID uint32
1090 cached bool
1091 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301092 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001093 tests := []struct {
1094 name string
1095 args args
1096 fields *fields
1097 }{
1098 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1099 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1100 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301101 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001102 for _, tt := range tests {
1103 t.Run(tt.name, func(t *testing.T) {
1104 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301105 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1106 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001107 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1108 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001109 t.Errorf("%s got false but wants true", tt.name)
1110 return
1111 }
1112 })
1113 }
1114}
1115
1116func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1117 type args struct {
1118 groupID uint32
1119 cached bool
1120 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301121 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001122 tests := []struct {
1123 name string
1124 args args
1125 fields *fields
1126 }{
1127 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1128 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1129 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1130 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301131 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001132 for _, tt := range tests {
1133 t.Run(tt.name, func(t *testing.T) {
1134 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301135 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1136 defer cancel()
1137 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001138 if err != nil {
1139 t.Errorf("%s got error but wants nil error", tt.name)
1140 return
1141 } else if exists && (groupInfo.GroupID == 0) {
1142 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1143 return
1144 } else if tt.args.groupID == 3 && exists {
1145 t.Errorf("%s got true but wants false", tt.name)
1146 return
1147 }
1148 })
1149 }
1150}