blob: 465daac056795ff8993a6d6d30ed5d9d32801637 [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
200// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530201func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200202 if key != "" {
203 return nil
204 }
205 return errors.New("key didn't find")
206}
207
208// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530209func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200210 return nil
211}
212
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300213// DeleteWithPrefix mock function implementation for KVClient
214func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
215 return nil
216}
217
cbabuabf02352019-10-15 13:14:56 +0200218// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000219func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200220 return nil, errors.New("key didn't find")
221}
222
223// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530224func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200225 return nil
226}
227
228// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530229func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200230 return nil
231}
232
233// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530234func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200235 return nil
236}
237
238// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800239func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200240 return nil
241}
242
243// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000244func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200245 return nil
246}
247
248// ReleaseLock mock function implementation for KVClient
249func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
250 return nil
251}
252
253// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530254func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200255 return true
256}
257
258// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000259func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200260}
261
262// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000263func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200264}
265
cbabubef89432019-10-18 11:47:27 +0200266// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200267func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700268 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700269 DeviceID: testResMgr.DeviceID,
270 Args: testResMgr.Args,
271 KVStore: testResMgr.KVStore,
272 DeviceType: testResMgr.DeviceType,
273 Address: testResMgr.Address,
274 DevInfo: testResMgr.DevInfo,
275 PonRsrMgr: testResMgr.PonRsrMgr,
276 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200277 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700278 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700279
280 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200281}
282
283func TestNewResourceMgr(t *testing.T) {
284 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000285 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700286 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000287 KVStoreAddress string
288 kvStoreType string
289 deviceType string
290 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800291 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200292 }
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +0530293 /* As of not the current NewResourceMgr test is not doing anything as there was no resourceranges passed.
294 passing the resource ranges would mean passing a mock and changes in all around including device handler and other places.
295 For now , removed the older version of proto which used ONUIDSTart and ONUIDENd which is not valid.
296 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.
297 intfids := []uint32{0, 1, 2, 3, 4, 5}
298 devOnuRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 60}
299 devGemRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 10000}
300 devAllocRsrcPools := &openolt.DeviceInfo_DeviceResourceRanges_Pool{Type: openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID, Sharing: openolt.DeviceInfo_DeviceResourceRanges_Pool_DEDICATED_PER_INTF, Start: 1, End: 256}
301 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}
302 pool := []*openolt.DeviceInfo_DeviceResourceRanges_Pool{devOnuRsrcPools, devGemRsrcPools, devAllocRsrcPools, devFlowRsrcPools}
303 devRsrc := &openolt.DeviceInfo_DeviceResourceRanges{IntfIds: intfids, Technology: "GPON", Pools: pool}
304 devRsrcPool := []*openolt.DeviceInfo_DeviceResourceRanges{devRsrc}
305 */
cbabuabf02352019-10-15 13:14:56 +0200306 tests := []struct {
307 name string
308 args args
309 want *OpenOltResourceMgr
310 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700311 {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd",
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +0530312 "olt", &openolt.DeviceInfo{}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200313 }
314 for _, tt := range tests {
315 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530316 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
317 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700318 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 +0200319 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
320 }
321 })
322 }
323}
324
325func TestOpenOltResourceMgr_Delete(t *testing.T) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700326 type args struct {
327 intfID uint32
328 }
cbabuabf02352019-10-15 13:14:56 +0200329 tests := []struct {
330 name string
331 fields *fields
332 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700333 args args
cbabuabf02352019-10-15 13:14:56 +0200334 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700335 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200336 }
337 for _, tt := range tests {
338 t.Run(tt.name, func(t *testing.T) {
339 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530340 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
341 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700342 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200343 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
344 }
345 })
346 }
347}
348
cbabuabf02352019-10-15 13:14:56 +0200349func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
350 type args struct {
351 intfID uint32
352 onuID uint32
353 uniID uint32
354 }
355 tests := []struct {
356 name string
357 fields *fields
358 args args
359 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700360 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200361 }
362 for _, tt := range tests {
363 t.Run(tt.name, func(t *testing.T) {
364 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530365 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
366 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000367 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200368 })
369 }
370}
371
372func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
373 type args struct {
374 intfID uint32
375 onuID []uint32
376 }
377 tests := []struct {
378 name string
379 fields *fields
380 args args
381 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700382 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200383 }
384 for _, tt := range tests {
385 t.Run(tt.name, func(t *testing.T) {
386 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530387 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
388 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000389 RsrcMgr.FreeonuID(ctx, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200390 })
391 }
392}
393
cbabuabf02352019-10-15 13:14:56 +0200394func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
395 type args struct {
396 intfID uint32
397 onuID uint32
398 uniID uint32
399 }
400 tests := []struct {
401 name string
402 fields *fields
403 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000404 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200405 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700406 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200407 }
408 for _, tt := range tests {
409 t.Run(tt.name, func(t *testing.T) {
410 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530411 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
412 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000413 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.onuID, tt.args.uniID)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700414 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000415 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700416 } else {
417 for i := range tt.want {
418 if got[i] != tt.want[i] {
419 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
420 break
421 }
422 }
cbabuabf02352019-10-15 13:14:56 +0200423 }
424 })
425 }
426}
427
Girish Gowdra950326e2021-11-05 12:43:24 -0700428func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) {
429
430 type args struct {
431 PONIntfID uint32
432 }
433 tests := []struct {
434 name string
435 fields *fields
436 args args
437 want error
438 }{
439 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
440 }
441 for _, tt := range tests {
442 t.Run(tt.name, func(t *testing.T) {
443 RsrcMgr := testResMgrObject(tt.fields)
444 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
445 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000446 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700447 if err != nil {
448 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
449 }
450 })
451 }
452}
453
454func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
455
456 type args struct {
457 PONIntfID uint32
458 }
459 tests := []struct {
460 name string
461 fields *fields
462 args args
463 want error
464 }{
465 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
466 }
467 for _, tt := range tests {
468 t.Run(tt.name, func(t *testing.T) {
469 RsrcMgr := testResMgrObject(tt.fields)
470 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
471 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000472 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx)
Girish Gowdra950326e2021-11-05 12:43:24 -0700473 if err != nil {
474 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
475 }
476 })
477 }
478}
479
yasin sapli9e4c5092022-02-01 13:52:33 +0000480func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) {
481
482 type args struct {
483 intfID uint32
484 onuID uint32
485 gemPortIDs []uint32
486 gemPortIDsToBeDeleted []uint32
487 gemPortIDsRemaining []uint32
488 serialNum string
489 finalLength int
490 }
491 tests := []struct {
492 name string
493 fields *fields
494 args args
495 }{
496 // Add/Delete single gem port
497 {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
498 // Delete all gemports
499 {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
500 // Try to delete when there is no gem port
501 {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}},
502 // Try to delete non-existent gem port
503 {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
504 // Try to delete two of the gem ports
505 {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
506 }
507 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
508 defer cancel()
509 for _, tt := range tests {
510 t.Run(tt.name, func(t *testing.T) {
511 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000512 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000513 t.Errorf("failed to remove onu")
514 }
yasin saplibddc2d72022-02-08 13:10:17 +0000515 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000516 t.Errorf("failed to add onu")
517 }
518 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000519 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000520 t.Errorf("failed to add gem to onu")
521 }
522 }
523 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
yasin saplibddc2d72022-02-08 13:10:17 +0000524 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.onuID, gemPortDeleted); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000525 t.Errorf("failed to remove gem from onu")
526 }
527 }
528 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000529 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000530 if err != nil || gP == nil {
531 t.Errorf("failed to get onuGemInfo")
532 }
533 var gemPorts []uint32
534
535 lenofGemPorts = len(gP.GemPorts)
536 gemPorts = gP.GemPorts
537
538 if lenofGemPorts != tt.args.finalLength {
539 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
540 }
541
542 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
543 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
544 }
545 })
546 }
547}
548
549func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
550
551 type args struct {
552 PONIntfID uint32
553 OnuCount uint32
554 }
555 tests := []struct {
556 name string
557 fields *fields
558 args args
559 want error
560 }{
561 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
562 }
563 for _, tt := range tests {
564 t.Run(tt.name, func(t *testing.T) {
565 RsrcMgr := testResMgrObject(tt.fields)
566 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
567 defer cancel()
568 for j := 1; j <= int(tt.args.OnuCount); j++ {
569 go func(i uint32, j uint32) {
570 // TODO: actually verify success
yasin saplibddc2d72022-02-08 13:10:17 +0000571 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, fmt.Sprintf("onu-%d", i))
yasin sapli9e4c5092022-02-01 13:52:33 +0000572 }(tt.args.PONIntfID, uint32(j))
573 }
574 })
575 }
576}
577
578func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
579
580 type args struct {
581 intfID uint32
582 onuID uint32
583 gemPortIDs []uint32
584 gemPortIDsRemaining []uint32
585 serialNum string
586 finalLength int
587 }
588 tests := []struct {
589 name string
590 fields *fields
591 args args
592 }{
593 // Add single gem port
594 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
595 // Delete all gemports
596 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
597 // Do not add any gemport
598 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
599 }
600 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
601 defer cancel()
602 for _, tt := range tests {
603 t.Run(tt.name, func(t *testing.T) {
604 RsrcMgr := testResMgrObject(tt.fields)
yasin saplibddc2d72022-02-08 13:10:17 +0000605 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.onuID); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000606 t.Errorf("failed to remove onu")
607 }
yasin saplibddc2d72022-02-08 13:10:17 +0000608 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.onuID, tt.args.serialNum); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000609 t.Errorf("failed to add onu")
610 }
611 for _, gemPort := range tt.args.gemPortIDs {
yasin saplibddc2d72022-02-08 13:10:17 +0000612 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.onuID, gemPort); err != nil {
yasin sapli9e4c5092022-02-01 13:52:33 +0000613 t.Errorf("failed to add gem to onu")
614 }
615 }
616
617 lenofGemPorts := 0
yasin saplibddc2d72022-02-08 13:10:17 +0000618 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.onuID)
yasin sapli9e4c5092022-02-01 13:52:33 +0000619
620 var gemPorts []uint32
621 if err == nil && gP != nil {
622 lenofGemPorts = len(gP.GemPorts)
623 gemPorts = gP.GemPorts
624 }
625 if lenofGemPorts != tt.args.finalLength {
626 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
627 }
628
629 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
630 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
631 }
632 })
633 }
634}
635
cbabuabf02352019-10-15 13:14:56 +0200636func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
637 type args struct {
638 intfID uint32
639 onuID uint32
640 uniID uint32
641 }
642 tests := []struct {
643 name string
644 fields *fields
645 args args
646 want []uint32
647 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700648 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200649 }
650 for _, tt := range tests {
651 t.Run(tt.name, func(t *testing.T) {
652 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530653 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
654 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000655 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200656 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
657 }
658 })
659 }
660}
661
Girish Gowdraa482f272021-03-24 23:04:19 -0700662func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200663 type args struct {
664 Direction string
665 IntfID uint32
666 OnuID uint32
667 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000668 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200669 }
670 tests := []struct {
671 name string
672 fields *fields
673 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700674 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200675 wantErr error
676 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700677 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
678 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
679 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
680 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200681 }
682 for _, tt := range tests {
683 t.Run(tt.name, func(t *testing.T) {
684 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530685 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
686 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000687 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200688 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700689 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200690 }
691 })
692 }
693}
694
695func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
696 type args struct {
697 ponIntfID uint32
698 }
699 tests := []struct {
700 name string
701 fields *fields
702 args args
703 want uint32
704 wantErr error
705 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700706 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200707 }
708 for _, tt := range tests {
709 t.Run(tt.name, func(t *testing.T) {
710 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530711 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
712 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000713 got, err := RsrcMgr.GetONUID(ctx)
cbabuabf02352019-10-15 13:14:56 +0200714 if got != tt.want && err != nil {
715 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
716 }
717 })
718 }
719}
720
721func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
722
723 type args struct {
724 IntfID uint32
725 OnuID uint32
726 UniID uint32
727 }
728 tests := []struct {
729 name string
730 fields *fields
731 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000732 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200733 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700734 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000735 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200736 }
737 for _, tt := range tests {
738 t.Run(tt.name, func(t *testing.T) {
739 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530740 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
741 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000742 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200743 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
744 }
745 })
746 }
747}
748
cbabuabf02352019-10-15 13:14:56 +0200749func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
750
751 type args struct {
752 Direction string
753 IntfID uint32
754 OnuID uint32
755 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000756 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200757 }
758 tests := []struct {
759 name string
760 fields *fields
761 args args
762 wantErr error
763 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000764 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200765 errors.New("failed to delete meter id %s from kvstore")},
766 }
767 for _, tt := range tests {
768 t.Run(tt.name, func(t *testing.T) {
769 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530770 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
771 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000772 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000773 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200774 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
775 }
776 })
777 }
778}
779
780func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
781 type args struct {
782 IntfID uint32
783 OnuID uint32
784 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000785 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200786 }
787 tests := []struct {
788 name string
789 fields *fields
790 args args
791 wantErr error
792 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700793 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200794 errors.New("failed to delete techprofile id resource %s in KV store")},
795 }
796 for _, tt := range tests {
797 t.Run(tt.name, func(t *testing.T) {
798 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530799 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
800 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000801 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000802 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200803 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
804 }
805 })
806 }
807}
808
809func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
810 type args struct {
811 ponPort uint32
812 onuID uint32
813 uniID uint32
814 allocID []uint32
815 }
816 tests := []struct {
817 name string
818 fields *fields
819 args args
820 wantErr error
821 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700822 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200823 errors.New("")},
824 }
825 for _, tt := range tests {
826 t.Run(tt.name, func(t *testing.T) {
827 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530828 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
829 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000830 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 +0200831 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
832 }
833 })
834 }
835}
836
cbabuabf02352019-10-15 13:14:56 +0200837func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
838
839 type args struct {
840 ponPort uint32
841 onuID uint32
842 uniID uint32
843 GEMPortList []uint32
844 }
845 tests := []struct {
846 name string
847 fields *fields
848 args args
849 wantErr error
850 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700851 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200852 []uint32{1, 2}}, errors.New("failed to update resource")},
853 }
854 for _, tt := range tests {
855 t.Run(tt.name, func(t *testing.T) {
856 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530857 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
858 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000859 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 +0200860 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
861 }
862 })
863 }
864}
865
cbabuabf02352019-10-15 13:14:56 +0200866func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
867 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700868 Direction string
869 IntfID uint32
870 OnuID uint32
871 UniID uint32
872 tpID uint32
873 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200874 }
875 tests := []struct {
876 name string
877 fields *fields
878 args args
879 wantErr error
880 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700881 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700882 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200883 }
884 for _, tt := range tests {
885 t.Run(tt.name, func(t *testing.T) {
886 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530887 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
888 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000889 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.OnuID, tt.args.UniID,
Girish Gowdraa482f272021-03-24 23:04:19 -0700890 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200891 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
892 }
893 })
894 }
895}
896
897func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
898 type args struct {
899 IntfID uint32
900 OnuID uint32
901 UniID uint32
902 TpID uint32
903 }
904 tests := []struct {
905 name string
906 fields *fields
907 args args
908 wantErr error
909 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700910 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200911 2}, errors.New("failed to update resource")},
912 }
913 for _, tt := range tests {
914 t.Run(tt.name, func(t *testing.T) {
915 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530916 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
917 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000918 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 +0200919 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
920 }
921 })
922 }
923}
924
925func TestSetKVClient(t *testing.T) {
926 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800927 backend string
928 address string
929 DeviceID string
930 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200931 }
932 tests := []struct {
933 name string
934 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500935 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200936 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300937 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200938 }
939 for _, tt := range tests {
940 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800941 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 +0200942 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
943 }
944 })
945 }
946}
947
cbabuabf02352019-10-15 13:14:56 +0200948func Test_newKVClient(t *testing.T) {
949 type args struct {
950 storeType string
951 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000952 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200953 }
954 var kvClient kvstore.Client
955 tests := []struct {
956 name string
957 args args
958 want kvstore.Client
959 wantErr error
960 }{
961 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
962 }
963 for _, tt := range tests {
964 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000965 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200966 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
967 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
968 }
969 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
970 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
971 return
972 }
973
974 })
975 }
976}
Esin Karamanccb714b2019-11-29 15:02:06 +0000977
978func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
979 type args struct {
980 intf uint32
981 gem uint32
982 servicePriority uint32
983 }
984 tests := []struct {
985 name string
986 args args
987 fields *fields
988 }{
989 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
990 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
991 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
992 }
993 for _, tt := range tests {
994 t.Run(tt.name, func(t *testing.T) {
995 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530996 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
997 defer cancel()
yasin saplibddc2d72022-02-08 13:10:17 +0000998 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000999 if err != nil {
1000 t.Errorf("%s got err= %s wants nil", tt.name, err)
1001 return
1002 }
1003 })
1004 }
1005}
1006
Girish Gowdraf3728b12022-02-02 21:46:51 -08001007func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
1008 tests := []struct {
1009 name string
1010 fields *fields
1011 }{
1012 {"DeleteMcastQueueForIntf-1", getResMgr()},
1013 }
1014 for _, tt := range tests {
1015 t.Run(tt.name, func(t *testing.T) {
1016 RsrcMgr := testResMgrObject(tt.fields)
1017 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1018 defer cancel()
Gustavo Silva41af9122022-10-11 11:05:13 -03001019 _ = RsrcMgr.DeleteMcastQueueForIntf(ctx)
Girish Gowdraf3728b12022-02-02 21:46:51 -08001020 })
1021 }
1022}
1023
Esin Karamanccb714b2019-11-29 15:02:06 +00001024func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1025 groupDesc := ofp.OfpGroupDesc{
1026 Type: ofp.OfpGroupType_OFPGT_ALL,
1027 GroupId: groupID,
1028 }
1029 groupEntry := ofp.OfpGroupEntry{
1030 Desc: &groupDesc,
1031 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001032 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001033 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001034 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001035 bucket := ofp.OfpBucket{
1036 Actions: acts,
1037 }
1038 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001039 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001040 return &groupEntry
1041}
1042
1043func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1044 type args struct {
1045 group *ofp.OfpGroupEntry
1046 cached bool
1047 }
1048 //create group 1
1049 group1 := newGroup(1, []uint32{1})
1050 //create group 2
1051 group2 := newGroup(2, []uint32{2})
1052 //define test set
1053 tests := []struct {
1054 name string
1055 args args
1056 fields *fields
1057 }{
1058 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1059 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1060 }
1061 //execute tests
1062 for _, tt := range tests {
1063 t.Run(tt.name, func(t *testing.T) {
1064 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301065 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1066 defer cancel()
1067 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001068 if err != nil {
1069 t.Errorf("%s got err= %s wants nil", tt.name, err)
1070 return
1071 }
1072 })
1073 }
1074}
1075
1076func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1077 type args struct {
1078 groupID uint32
1079 cached bool
1080 }
1081 //define test set
1082 tests := []struct {
1083 name string
1084 args args
1085 fields *fields
1086 }{
1087 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1088 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1089 }
1090 //execute tests
1091 for _, tt := range tests {
1092 t.Run(tt.name, func(t *testing.T) {
1093 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301094 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1095 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001096 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1097 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001098 t.Errorf("%s got false but wants true", tt.name)
1099 return
1100 }
1101 })
1102 }
1103}
1104
1105func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1106 type args struct {
1107 groupID uint32
1108 cached bool
1109 }
1110 //define test set
1111 tests := []struct {
1112 name string
1113 args args
1114 fields *fields
1115 }{
1116 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1117 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1118 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1119 }
1120 //execute tests
1121 for _, tt := range tests {
1122 t.Run(tt.name, func(t *testing.T) {
1123 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301124 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1125 defer cancel()
1126 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001127 if err != nil {
1128 t.Errorf("%s got error but wants nil error", tt.name)
1129 return
1130 } else if exists && (groupInfo.GroupID == 0) {
1131 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1132 return
1133 } else if tt.args.groupID == 3 && exists {
1134 t.Errorf("%s got true but wants false", tt.name)
1135 return
1136 }
1137 })
1138 }
1139}