blob: 9018d4dc0cd7d89d225208dcd8196bde64b690ef [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) {
441
442 type args struct {
443 PONIntfID uint32
444 }
445 tests := []struct {
446 name string
447 fields *fields
448 args args
449 want error
450 }{
451 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
452 }
453 for _, tt := range tests {
454 t.Run(tt.name, func(t *testing.T) {
455 RsrcMgr := testResMgrObject(tt.fields)
456 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
457 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000458 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700459 if err != nil {
460 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
461 }
462 })
463 }
464}
465
466func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
467
468 type args struct {
469 PONIntfID uint32
470 }
471 tests := []struct {
472 name string
473 fields *fields
474 args args
475 want error
476 }{
477 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
478 }
479 for _, tt := range tests {
480 t.Run(tt.name, func(t *testing.T) {
481 RsrcMgr := testResMgrObject(tt.fields)
482 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
483 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000484 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700485 if err != nil {
486 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
487 }
488 })
489 }
490}
491
yasin sapli9e4c5092022-02-01 13:52:33 +0000492func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) {
493
494 type args struct {
495 intfID uint32
496 onuID uint32
497 gemPortIDs []uint32
498 gemPortIDsToBeDeleted []uint32
499 gemPortIDsRemaining []uint32
500 serialNum string
501 finalLength int
502 }
503 tests := []struct {
504 name string
505 fields *fields
506 args args
507 }{
508 // Add/Delete single gem port
509 {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
510 // Delete all gemports
511 {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
512 // Try to delete when there is no gem port
513 {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}},
514 // Try to delete non-existent gem port
515 {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
516 // Try to delete two of the gem ports
517 {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
518 }
519 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
520 defer cancel()
521 for _, tt := range tests {
522 t.Run(tt.name, func(t *testing.T) {
523 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000524 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000525 t.Errorf("failed to remove onu")
526 }
yasin saplibddc2d72022-02-08 13:10:17 +0000527 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000528 t.Errorf("failed to add onu")
529 }
530 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000531 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000532 t.Errorf("failed to add gem to onu")
533 }
534 }
535 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
yasin saplibddc2d72022-02-08 13:10:17 +0000536 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.onuID, gemPortDeleted); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000537 t.Errorf("failed to remove gem from onu")
538 }
539 }
540 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000541 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000542 if err != nil || gP == nil {
543 t.Errorf("failed to get onuGemInfo")
544 }
545 var gemPorts []uint32
546
547 lenofGemPorts = len(gP.GemPorts)
548 gemPorts = gP.GemPorts
549
550 if lenofGemPorts != tt.args.finalLength {
551 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
552 }
553
554 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
555 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
556 }
557 })
558 }
559}
560
561func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
562
563 type args struct {
564 PONIntfID uint32
565 OnuCount uint32
566 }
567 tests := []struct {
568 name string
569 fields *fields
570 args args
571 want error
572 }{
573 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
574 }
575 for _, tt := range tests {
576 t.Run(tt.name, func(t *testing.T) {
577 RsrcMgr := testResMgrObject(tt.fields)
578 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
579 defer cancel()
580 for j := 1; j <= int(tt.args.OnuCount); j++ {
581 go func(i uint32, j uint32) {
582 // TODO: actually verify success
yasin saplibddc2d72022-02-08 13:10:17 +0000583 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, fmt.Sprintf("onu-%d", i))
yasin sapli9e4c5092022-02-01 13:52:33 +0000584 }(tt.args.PONIntfID, uint32(j))
585 }
586 })
587 }
588}
589
590func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
591
592 type args struct {
593 intfID uint32
594 onuID uint32
595 gemPortIDs []uint32
596 gemPortIDsRemaining []uint32
597 serialNum string
598 finalLength int
599 }
600 tests := []struct {
601 name string
602 fields *fields
603 args args
604 }{
605 // Add single gem port
606 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
607 // Delete all gemports
608 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
609 // Do not add any gemport
610 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
611 }
612 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
613 defer cancel()
614 for _, tt := range tests {
615 t.Run(tt.name, func(t *testing.T) {
616 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000617 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000618 t.Errorf("failed to remove onu")
619 }
yasin saplibddc2d72022-02-08 13:10:17 +0000620 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000621 t.Errorf("failed to add onu")
622 }
623 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000624 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000625 t.Errorf("failed to add gem to onu")
626 }
627 }
628
629 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000630 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000631
632 var gemPorts []uint32
633 if err == nil && gP != nil {
634 lenofGemPorts = len(gP.GemPorts)
635 gemPorts = gP.GemPorts
636 }
637 if lenofGemPorts != tt.args.finalLength {
638 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
639 }
640
641 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
642 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
643 }
644 })
645 }
646}
647
cbabuabf02352019-10-15 13:14:56 +0200648func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
649 type args struct {
650 intfID uint32
651 onuID uint32
652 uniID uint32
653 }
654 tests := []struct {
655 name string
656 fields *fields
657 args args
658 want []uint32
659 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700660 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200661 }
662 for _, tt := range tests {
663 t.Run(tt.name, func(t *testing.T) {
664 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530665 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
666 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000667 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200668 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
669 }
670 })
671 }
672}
673
Girish Gowdraa482f272021-03-24 23:04:19 -0700674func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200675 type args struct {
676 Direction string
677 IntfID uint32
678 OnuID uint32
679 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000680 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200681 }
682 tests := []struct {
683 name string
684 fields *fields
685 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700686 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200687 wantErr error
688 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700689 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
690 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
691 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
692 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200693 }
694 for _, tt := range tests {
695 t.Run(tt.name, func(t *testing.T) {
696 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530697 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
698 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000699 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200700 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700701 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200702 }
703 })
704 }
705}
706
707func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
708 type args struct {
709 ponIntfID uint32
710 }
711 tests := []struct {
712 name string
713 fields *fields
714 args args
715 want uint32
716 wantErr error
717 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700718 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200719 }
720 for _, tt := range tests {
721 t.Run(tt.name, func(t *testing.T) {
722 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530723 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
724 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000725 got, err := RsrcMgr.GetONUID(ctx)
cbabuabf02352019-10-15 13:14:56 +0200726 if got != tt.want && err != nil {
727 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
728 }
729 })
730 }
731}
732
733func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
734
735 type args struct {
736 IntfID uint32
737 OnuID uint32
738 UniID uint32
739 }
740 tests := []struct {
741 name string
742 fields *fields
743 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000744 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200745 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700746 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000747 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200748 }
749 for _, tt := range tests {
750 t.Run(tt.name, func(t *testing.T) {
751 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530752 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
753 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000754 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200755 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
756 }
757 })
758 }
759}
760
cbabuabf02352019-10-15 13:14:56 +0200761func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
762
763 type args struct {
764 Direction string
765 IntfID uint32
766 OnuID uint32
767 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000768 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200769 }
770 tests := []struct {
771 name string
772 fields *fields
773 args args
774 wantErr error
775 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000776 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200777 errors.New("failed to delete meter id %s from kvstore")},
778 }
779 for _, tt := range tests {
780 t.Run(tt.name, func(t *testing.T) {
781 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530782 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
783 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000784 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000785 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200786 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
787 }
788 })
789 }
790}
791
792func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
793 type args struct {
794 IntfID uint32
795 OnuID uint32
796 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000797 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200798 }
799 tests := []struct {
800 name string
801 fields *fields
802 args args
803 wantErr error
804 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700805 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200806 errors.New("failed to delete techprofile id resource %s in KV store")},
807 }
808 for _, tt := range tests {
809 t.Run(tt.name, func(t *testing.T) {
810 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530811 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
812 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000813 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000814 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200815 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
816 }
817 })
818 }
819}
820
821func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
822 type args struct {
823 ponPort uint32
824 onuID uint32
825 uniID uint32
826 allocID []uint32
827 }
828 tests := []struct {
829 name string
830 fields *fields
831 args args
832 wantErr error
833 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700834 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200835 errors.New("")},
836 }
837 for _, tt := range tests {
838 t.Run(tt.name, func(t *testing.T) {
839 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530840 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
841 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000842 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 +0200843 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
844 }
845 })
846 }
847}
848
cbabuabf02352019-10-15 13:14:56 +0200849func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
850
851 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 }
985
986 })
987 }
988}
Esin Karamanccb714b2019-11-29 15:02:06 +0000989
990func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
991 type args struct {
992 intf uint32
993 gem uint32
994 servicePriority uint32
995 }
996 tests := []struct {
997 name string
998 args args
999 fields *fields
1000 }{
1001 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1002 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1003 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1004 }
1005 for _, tt := range tests {
1006 t.Run(tt.name, func(t *testing.T) {
1007 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301008 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1009 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +00001010 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001011 if err != nil {
1012 t.Errorf("%s got err= %s wants nil", tt.name, err)
1013 return
1014 }
1015 })
1016 }
1017}
1018
Girish Gowdraf3728b12022-02-02 21:46:51 -08001019func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
1020 tests := []struct {
1021 name string
1022 fields *fields
1023 }{
1024 {"DeleteMcastQueueForIntf-1", getResMgr()},
1025 }
1026 for _, tt := range tests {
1027 t.Run(tt.name, func(t *testing.T) {
1028 RsrcMgr := testResMgrObject(tt.fields)
1029 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1030 defer cancel()
Gustavo Silva41af9122022-10-11 11:05:13 -03001031 _ = RsrcMgr.DeleteMcastQueueForIntf(ctx)
Girish Gowdraf3728b12022-02-02 21:46:51 -08001032 })
1033 }
1034}
1035
Esin Karamanccb714b2019-11-29 15:02:06 +00001036func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1037 groupDesc := ofp.OfpGroupDesc{
1038 Type: ofp.OfpGroupType_OFPGT_ALL,
1039 GroupId: groupID,
1040 }
1041 groupEntry := ofp.OfpGroupEntry{
1042 Desc: &groupDesc,
1043 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001044 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001045 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001046 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001047 bucket := ofp.OfpBucket{
1048 Actions: acts,
1049 }
1050 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001051 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001052 return &groupEntry
1053}
1054
1055func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1056 type args struct {
1057 group *ofp.OfpGroupEntry
1058 cached bool
1059 }
1060 //create group 1
1061 group1 := newGroup(1, []uint32{1})
1062 //create group 2
1063 group2 := newGroup(2, []uint32{2})
1064 //define test set
1065 tests := []struct {
1066 name string
1067 args args
1068 fields *fields
1069 }{
1070 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1071 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1072 }
1073 //execute tests
1074 for _, tt := range tests {
1075 t.Run(tt.name, func(t *testing.T) {
1076 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301077 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1078 defer cancel()
1079 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001080 if err != nil {
1081 t.Errorf("%s got err= %s wants nil", tt.name, err)
1082 return
1083 }
1084 })
1085 }
1086}
1087
1088func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1089 type args struct {
1090 groupID uint32
1091 cached bool
1092 }
1093 //define test set
1094 tests := []struct {
1095 name string
1096 args args
1097 fields *fields
1098 }{
1099 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1100 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1101 }
1102 //execute tests
1103 for _, tt := range tests {
1104 t.Run(tt.name, func(t *testing.T) {
1105 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301106 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1107 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001108 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1109 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001110 t.Errorf("%s got false but wants true", tt.name)
1111 return
1112 }
1113 })
1114 }
1115}
1116
1117func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1118 type args struct {
1119 groupID uint32
1120 cached bool
1121 }
1122 //define test set
1123 tests := []struct {
1124 name string
1125 args args
1126 fields *fields
1127 }{
1128 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1129 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1130 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1131 }
1132 //execute tests
1133 for _, tt := range tests {
1134 t.Run(tt.name, func(t *testing.T) {
1135 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301136 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1137 defer cancel()
1138 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001139 if err != nil {
1140 t.Errorf("%s got error but wants nil error", tt.name)
1141 return
1142 } else if exists && (groupInfo.GroupID == 0) {
1143 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1144 return
1145 } else if tt.args.groupID == 3 && exists {
1146 t.Errorf("%s got true but wants false", tt.name)
1147 return
1148 }
1149 })
1150 }
1151}