blob: c11d2f93d4d632c39241455802c51df30a316b97 [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
270// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000271func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200272}
273
274// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000275func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200276}
277
cbabubef89432019-10-18 11:47:27 +0200278// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200279func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700280 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700281 DeviceID: testResMgr.DeviceID,
282 Args: testResMgr.Args,
283 KVStore: testResMgr.KVStore,
284 DeviceType: testResMgr.DeviceType,
285 Address: testResMgr.Address,
286 DevInfo: testResMgr.DevInfo,
287 PonRsrMgr: testResMgr.PonRsrMgr,
288 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200289 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700290 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700291
292 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200293}
294
295func TestNewResourceMgr(t *testing.T) {
296 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000297 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700298 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000299 KVStoreAddress string
300 kvStoreType string
301 deviceType string
302 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800303 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200304 }
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +0530305 /* As of not the current NewResourceMgr test is not doing anything as there was no resourceranges passed.
306 passing the resource ranges would mean passing a mock and changes in all around including device handler and other places.
307 For now , removed the older version of proto which used ONUIDSTart and ONUIDENd which is not valid.
308 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.
309 intfids := []uint32{0, 1, 2, 3, 4, 5}
310 devOnuRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 60}
311 devGemRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 10000}
312 devAllocRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 256}
313 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}
314 pool := []*openolt.DeviceInfo_DeviceResourceRanges_Pool{devOnuRsrcPools, devGemRsrcPools, devAllocRsrcPools, devFlowRsrcPools}
315 devRsrc := &openolt.DeviceInfo_DeviceResourceRanges{IntfIds: intfids, Technology: "GPON", Pools: pool}
316 devRsrcPool := []*openolt.DeviceInfo_DeviceResourceRanges{devRsrc}
317 */
cbabuabf02352019-10-15 13:14:56 +0200318 tests := []struct {
319 name string
320 args args
321 want *OpenOltResourceMgr
322 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700323 {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd",
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +0530324 "olt", &openolt.DeviceInfo{}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200325 }
326 for _, tt := range tests {
327 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530328 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
329 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700330 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 +0200331 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
332 }
333 })
334 }
335}
336
337func TestOpenOltResourceMgr_Delete(t *testing.T) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700338 type args struct {
339 intfID uint32
340 }
cbabuabf02352019-10-15 13:14:56 +0200341 tests := []struct {
342 name string
343 fields *fields
344 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700345 args args
cbabuabf02352019-10-15 13:14:56 +0200346 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700347 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200348 }
349 for _, tt := range tests {
350 t.Run(tt.name, func(t *testing.T) {
351 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530352 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
353 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700354 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200355 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
356 }
357 })
358 }
359}
360
cbabuabf02352019-10-15 13:14:56 +0200361func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
362 type args struct {
363 intfID uint32
364 onuID uint32
365 uniID uint32
366 }
367 tests := []struct {
368 name string
369 fields *fields
370 args args
371 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700372 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200373 }
374 for _, tt := range tests {
375 t.Run(tt.name, func(t *testing.T) {
376 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530377 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
378 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000379 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200380 })
381 }
382}
383
384func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
385 type args struct {
386 intfID uint32
387 onuID []uint32
388 }
389 tests := []struct {
390 name string
391 fields *fields
392 args args
393 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700394 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200395 }
396 for _, tt := range tests {
397 t.Run(tt.name, func(t *testing.T) {
398 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530399 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
400 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000401 RsrcMgr.FreeonuID(ctx, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200402 })
403 }
404}
405
cbabuabf02352019-10-15 13:14:56 +0200406func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
407 type args struct {
408 intfID uint32
409 onuID uint32
410 uniID uint32
411 }
412 tests := []struct {
413 name string
414 fields *fields
415 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000416 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200417 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700418 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200419 }
420 for _, tt := range tests {
421 t.Run(tt.name, func(t *testing.T) {
422 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530423 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
424 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000425 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.onuID, tt.args.uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700426 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000427 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700428 } else {
429 for i := range tt.want {
430 if got[i] != tt.want[i] {
431 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
432 break
433 }
434 }
cbabuabf02352019-10-15 13:14:56 +0200435 }
436 })
437 }
438}
439
Girish Gowdra950326e2021-11-05 12:43:24 -0700440func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) {
Girish Gowdra950326e2021-11-05 12:43:24 -0700441 type args struct {
442 PONIntfID uint32
443 }
444 tests := []struct {
445 name string
446 fields *fields
447 args args
448 want error
449 }{
450 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
451 }
452 for _, tt := range tests {
453 t.Run(tt.name, func(t *testing.T) {
454 RsrcMgr := testResMgrObject(tt.fields)
455 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
456 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000457 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700458 if err != nil {
459 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
460 }
461 })
462 }
463}
464
465func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
Girish Gowdra950326e2021-11-05 12:43:24 -0700466 type args struct {
467 PONIntfID uint32
468 }
469 tests := []struct {
470 name string
471 fields *fields
472 args args
473 want error
474 }{
475 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
476 }
477 for _, tt := range tests {
478 t.Run(tt.name, func(t *testing.T) {
479 RsrcMgr := testResMgrObject(tt.fields)
480 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
481 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000482 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700483 if err != nil {
484 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
485 }
486 })
487 }
488}
489
yasin sapli9e4c5092022-02-01 13:52:33 +0000490func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000491 type args struct {
492 intfID uint32
493 onuID uint32
494 gemPortIDs []uint32
495 gemPortIDsToBeDeleted []uint32
496 gemPortIDsRemaining []uint32
497 serialNum string
498 finalLength int
499 }
500 tests := []struct {
501 name string
502 fields *fields
503 args args
504 }{
505 // Add/Delete single gem port
506 {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
507 // Delete all gemports
508 {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
509 // Try to delete when there is no gem port
510 {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}},
511 // Try to delete non-existent gem port
512 {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
513 // Try to delete two of the gem ports
514 {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
515 }
516 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
517 defer cancel()
518 for _, tt := range tests {
519 t.Run(tt.name, func(t *testing.T) {
520 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000521 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000522 t.Errorf("failed to remove onu")
523 }
yasin saplibddc2d72022-02-08 13:10:17 +0000524 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000525 t.Errorf("failed to add onu")
526 }
527 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000528 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000529 t.Errorf("failed to add gem to onu")
530 }
531 }
532 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
yasin saplibddc2d72022-02-08 13:10:17 +0000533 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.onuID, gemPortDeleted); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000534 t.Errorf("failed to remove gem from onu")
535 }
536 }
537 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000538 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000539 if err != nil || gP == nil {
540 t.Errorf("failed to get onuGemInfo")
541 }
542 var gemPorts []uint32
543
544 lenofGemPorts = len(gP.GemPorts)
545 gemPorts = gP.GemPorts
546
547 if lenofGemPorts != tt.args.finalLength {
548 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
549 }
550
551 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
552 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
553 }
554 })
555 }
556}
557
558func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000559 type args struct {
560 PONIntfID uint32
561 OnuCount uint32
562 }
563 tests := []struct {
564 name string
565 fields *fields
566 args args
567 want error
568 }{
569 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
570 }
571 for _, tt := range tests {
572 t.Run(tt.name, func(t *testing.T) {
573 RsrcMgr := testResMgrObject(tt.fields)
574 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
575 defer cancel()
576 for j := 1; j <= int(tt.args.OnuCount); j++ {
Akash Kankanala041a2122024-10-16 15:49:22 +0530577 go func(i uint32) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000578 // TODO: actually verify success
yasin saplibddc2d72022-02-08 13:10:17 +0000579 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, fmt.Sprintf("onu-%d", i))
Akash Kankanala041a2122024-10-16 15:49:22 +0530580 }(tt.args.PONIntfID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000581 }
582 })
583 }
584}
585
586func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
yasin sapli9e4c5092022-02-01 13:52:33 +0000587 type args struct {
588 intfID uint32
589 onuID uint32
590 gemPortIDs []uint32
591 gemPortIDsRemaining []uint32
592 serialNum string
593 finalLength int
594 }
595 tests := []struct {
596 name string
597 fields *fields
598 args args
599 }{
600 // Add single gem port
601 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
602 // Delete all gemports
603 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
604 // Do not add any gemport
605 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
606 }
607 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
608 defer cancel()
609 for _, tt := range tests {
610 t.Run(tt.name, func(t *testing.T) {
611 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000612 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000613 t.Errorf("failed to remove onu")
614 }
yasin saplibddc2d72022-02-08 13:10:17 +0000615 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000616 t.Errorf("failed to add onu")
617 }
618 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000619 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000620 t.Errorf("failed to add gem to onu")
621 }
622 }
623
624 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000625 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000626
627 var gemPorts []uint32
628 if err == nil && gP != nil {
629 lenofGemPorts = len(gP.GemPorts)
630 gemPorts = gP.GemPorts
631 }
632 if lenofGemPorts != tt.args.finalLength {
633 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
634 }
635
636 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
637 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
638 }
639 })
640 }
641}
642
cbabuabf02352019-10-15 13:14:56 +0200643func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
644 type args struct {
645 intfID uint32
646 onuID uint32
647 uniID uint32
648 }
649 tests := []struct {
650 name string
651 fields *fields
652 args args
653 want []uint32
654 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700655 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200656 }
657 for _, tt := range tests {
658 t.Run(tt.name, func(t *testing.T) {
659 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530660 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
661 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000662 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200663 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
664 }
665 })
666 }
667}
668
Girish Gowdraa482f272021-03-24 23:04:19 -0700669func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200670 type args struct {
671 Direction string
672 IntfID uint32
673 OnuID uint32
674 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000675 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200676 }
677 tests := []struct {
678 name string
679 fields *fields
680 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700681 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200682 wantErr error
683 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700684 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
685 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
686 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
687 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200688 }
689 for _, tt := range tests {
690 t.Run(tt.name, func(t *testing.T) {
691 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530692 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
693 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000694 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200695 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700696 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200697 }
698 })
699 }
700}
701
702func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
703 type args struct {
704 ponIntfID uint32
705 }
706 tests := []struct {
707 name string
708 fields *fields
709 args args
710 want uint32
711 wantErr error
712 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700713 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200714 }
715 for _, tt := range tests {
716 t.Run(tt.name, func(t *testing.T) {
717 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530718 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
719 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000720 got, err := RsrcMgr.GetONUID(ctx)
cbabuabf02352019-10-15 13:14:56 +0200721 if got != tt.want && err != nil {
722 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
723 }
724 })
725 }
726}
727
728func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200729 type args struct {
730 IntfID uint32
731 OnuID uint32
732 UniID uint32
733 }
734 tests := []struct {
735 name string
736 fields *fields
737 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000738 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200739 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700740 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000741 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200742 }
743 for _, tt := range tests {
744 t.Run(tt.name, func(t *testing.T) {
745 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530746 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
747 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000748 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200749 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
750 }
751 })
752 }
753}
754
cbabuabf02352019-10-15 13:14:56 +0200755func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200756 type args struct {
757 Direction string
758 IntfID uint32
759 OnuID uint32
760 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000761 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200762 }
763 tests := []struct {
764 name string
765 fields *fields
766 args args
767 wantErr error
768 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000769 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200770 errors.New("failed to delete meter id %s from kvstore")},
771 }
772 for _, tt := range tests {
773 t.Run(tt.name, func(t *testing.T) {
774 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530775 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
776 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000777 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000778 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200779 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
780 }
781 })
782 }
783}
784
785func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
786 type args struct {
787 IntfID uint32
788 OnuID uint32
789 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000790 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200791 }
792 tests := []struct {
793 name string
794 fields *fields
795 args args
796 wantErr error
797 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700798 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200799 errors.New("failed to delete techprofile id resource %s in KV store")},
800 }
801 for _, tt := range tests {
802 t.Run(tt.name, func(t *testing.T) {
803 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530804 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
805 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000806 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000807 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200808 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
809 }
810 })
811 }
812}
813
814func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
815 type args struct {
816 ponPort uint32
817 onuID uint32
818 uniID uint32
819 allocID []uint32
820 }
821 tests := []struct {
822 name string
823 fields *fields
824 args args
825 wantErr error
826 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700827 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200828 errors.New("")},
829 }
830 for _, tt := range tests {
831 t.Run(tt.name, func(t *testing.T) {
832 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530833 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
834 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000835 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 +0200836 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
837 }
838 })
839 }
840}
841
cbabuabf02352019-10-15 13:14:56 +0200842func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200843 type args struct {
844 ponPort uint32
845 onuID uint32
846 uniID uint32
847 GEMPortList []uint32
848 }
849 tests := []struct {
850 name string
851 fields *fields
852 args args
853 wantErr error
854 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700855 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200856 []uint32{1, 2}}, errors.New("failed to update resource")},
857 }
858 for _, tt := range tests {
859 t.Run(tt.name, func(t *testing.T) {
860 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530861 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
862 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000863 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 +0200864 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
865 }
866 })
867 }
868}
869
cbabuabf02352019-10-15 13:14:56 +0200870func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
871 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700872 Direction string
873 IntfID uint32
874 OnuID uint32
875 UniID uint32
876 tpID uint32
877 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200878 }
879 tests := []struct {
880 name string
881 fields *fields
882 args args
883 wantErr error
884 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700885 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700886 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200887 }
888 for _, tt := range tests {
889 t.Run(tt.name, func(t *testing.T) {
890 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530891 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
892 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000893 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Girish Gowdraa482f272021-03-24 23:04:19 -0700894 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200895 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
896 }
897 })
898 }
899}
900
901func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
902 type args struct {
903 IntfID uint32
904 OnuID uint32
905 UniID uint32
906 TpID uint32
907 }
908 tests := []struct {
909 name string
910 fields *fields
911 args args
912 wantErr error
913 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700914 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200915 2}, errors.New("failed to update resource")},
916 }
917 for _, tt := range tests {
918 t.Run(tt.name, func(t *testing.T) {
919 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530920 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
921 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000922 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 +0200923 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
924 }
925 })
926 }
927}
928
929func TestSetKVClient(t *testing.T) {
930 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800931 backend string
932 address string
933 DeviceID string
934 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200935 }
936 tests := []struct {
937 name string
938 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500939 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200940 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300941 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200942 }
943 for _, tt := range tests {
944 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800945 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 +0200946 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
947 }
948 })
949 }
950}
951
cbabuabf02352019-10-15 13:14:56 +0200952func Test_newKVClient(t *testing.T) {
953 type args struct {
954 storeType string
955 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000956 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200957 }
958 var kvClient kvstore.Client
959 tests := []struct {
960 name string
961 args args
962 want kvstore.Client
963 wantErr error
964 }{
965 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
966 }
967 for _, tt := range tests {
968 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000969 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200970 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
971 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
972 }
973 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
974 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
975 return
976 }
cbabuabf02352019-10-15 13:14:56 +0200977 })
978 }
979}
Esin Karamanccb714b2019-11-29 15:02:06 +0000980
981func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
982 type args struct {
983 intf uint32
984 gem uint32
985 servicePriority uint32
986 }
987 tests := []struct {
988 name string
989 args args
990 fields *fields
991 }{
992 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
993 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
994 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
995 }
996 for _, tt := range tests {
997 t.Run(tt.name, func(t *testing.T) {
998 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530999 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1000 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +00001001 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001002 if err != nil {
1003 t.Errorf("%s got err= %s wants nil", tt.name, err)
1004 return
1005 }
1006 })
1007 }
1008}
1009
Girish Gowdraf3728b12022-02-02 21:46:51 -08001010func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
1011 tests := []struct {
1012 name string
1013 fields *fields
1014 }{
1015 {"DeleteMcastQueueForIntf-1", getResMgr()},
1016 }
1017 for _, tt := range tests {
1018 t.Run(tt.name, func(t *testing.T) {
1019 RsrcMgr := testResMgrObject(tt.fields)
1020 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1021 defer cancel()
Gustavo Silva41af9122022-10-11 11:05:13 -03001022 _ = RsrcMgr.DeleteMcastQueueForIntf(ctx)
Girish Gowdraf3728b12022-02-02 21:46:51 -08001023 })
1024 }
1025}
1026
Esin Karamanccb714b2019-11-29 15:02:06 +00001027func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1028 groupDesc := ofp.OfpGroupDesc{
1029 Type: ofp.OfpGroupType_OFPGT_ALL,
1030 GroupId: groupID,
1031 }
1032 groupEntry := ofp.OfpGroupEntry{
1033 Desc: &groupDesc,
1034 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001035 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001036 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001037 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001038 bucket := ofp.OfpBucket{
1039 Actions: acts,
1040 }
1041 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001042 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001043 return &groupEntry
1044}
1045
1046func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1047 type args struct {
1048 group *ofp.OfpGroupEntry
1049 cached bool
1050 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301051 // create group 1
Esin Karamanccb714b2019-11-29 15:02:06 +00001052 group1 := newGroup(1, []uint32{1})
Akash Kankanala041a2122024-10-16 15:49:22 +05301053 // create group 2
Esin Karamanccb714b2019-11-29 15:02:06 +00001054 group2 := newGroup(2, []uint32{2})
Akash Kankanala041a2122024-10-16 15:49:22 +05301055 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001056 tests := []struct {
1057 name string
1058 args args
1059 fields *fields
1060 }{
1061 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1062 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1063 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301064 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001065 for _, tt := range tests {
1066 t.Run(tt.name, func(t *testing.T) {
1067 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301068 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1069 defer cancel()
1070 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001071 if err != nil {
1072 t.Errorf("%s got err= %s wants nil", tt.name, err)
1073 return
1074 }
1075 })
1076 }
1077}
1078
1079func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1080 type args struct {
1081 groupID uint32
1082 cached bool
1083 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301084 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001085 tests := []struct {
1086 name string
1087 args args
1088 fields *fields
1089 }{
1090 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1091 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1092 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301093 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001094 for _, tt := range tests {
1095 t.Run(tt.name, func(t *testing.T) {
1096 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301097 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1098 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001099 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1100 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001101 t.Errorf("%s got false but wants true", tt.name)
1102 return
1103 }
1104 })
1105 }
1106}
1107
1108func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1109 type args struct {
1110 groupID uint32
1111 cached bool
1112 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301113 // define test set
Esin Karamanccb714b2019-11-29 15:02:06 +00001114 tests := []struct {
1115 name string
1116 args args
1117 fields *fields
1118 }{
1119 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1120 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1121 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1122 }
Akash Kankanala041a2122024-10-16 15:49:22 +05301123 // execute tests
Esin Karamanccb714b2019-11-29 15:02:06 +00001124 for _, tt := range tests {
1125 t.Run(tt.name, func(t *testing.T) {
1126 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301127 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1128 defer cancel()
1129 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001130 if err != nil {
1131 t.Errorf("%s got error but wants nil error", tt.name)
1132 return
1133 } else if exists && (groupInfo.GroupID == 0) {
1134 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1135 return
1136 } else if tt.args.groupID == 3 && exists {
1137 t.Errorf("%s got true but wants false", tt.name)
1138 return
1139 }
1140 })
1141 }
1142}