blob: 85a58115e23feb944b52645311d361e7c4714eb0 [file] [log] [blame]
cbabuabf02352019-10-15 13:14:56 +02001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
23//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
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700113 resMgr.PonRsrMgr.DeviceID = "onu-1"
114 resMgr.PonRsrMgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
115 resMgr.PonRsrMgr.KVStore = &db.Backend{
Matteo Scandolo84585372021-03-18 14:21:22 -0700116 Client: &MockResKVClient{},
117 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700118 resMgr.PonRsrMgr.Technology = "XGS-PON"
119 resMgr.PonRsrMgr.PonResourceRanges = ranges
120 resMgr.PonRsrMgr.SharedIdxByType = sharedIdxByType
Girish Gowdra76a1b092021-07-28 10:07:04 -0700121 resMgr.TechProfileRef = mocks.MockTechProfile{}
122
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700123 /*
124 tpMgr, err := tp.NewTechProfile(ctx, resMgr.PonRsrMgr, "etcd", "127.0.0.1", "/")
125 if err != nil {
126 logger.Fatal(ctx, err.Error())
127 }
128 */
Matteo Scandolo84585372021-03-18 14:21:22 -0700129
cbabuabf02352019-10-15 13:14:56 +0200130 return &resMgr
131}
cbabubef89432019-10-18 11:47:27 +0200132
133// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530134func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200135 return nil, errors.New("key didn't find")
136}
137
138// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530139func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000140 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200141 if key != "" {
142 if strings.Contains(key, MeterConfig) {
143 var bands []*ofp.OfpMeterBandHeader
144 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
145 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
146
147 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
148 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
149
Gamze Abakafee36392019-10-03 11:17:24 +0000150 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200151 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
152 if uint32(val) > 1 {
153 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
154 str, _ := json.Marshal(meterConfig)
155
156 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
157 }
158 return nil, errors.New("invalid meter")
159 }
160 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000161 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200162 data := make(map[string]interface{})
163 data["pool"] = "1024"
164 data["start_idx"] = 1
165 data["end_idx"] = 1024
166 str, _ := json.Marshal(data)
167 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
168 }
169 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000170 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200171 str, _ := json.Marshal([]uint32{1, 2})
172 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
173 }
174 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200176 str, _ := json.Marshal(1)
177 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
178 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000179 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000180 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000181 mcastQueues := make(map[uint32][]uint32)
182 mcastQueues[10] = []uint32{4000, 0}
183 str, _ := json.Marshal(mcastQueues)
184 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
185 }
186 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
187 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
188 str, _ := json.Marshal(groupInfo)
189 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
190 }
191
cbabuabf02352019-10-15 13:14:56 +0200192 maps := make(map[string]*kvstore.KVPair)
193 maps[key] = &kvstore.KVPair{Key: key}
194 return maps[key], nil
195 }
196 return nil, errors.New("key didn't find")
197}
198
199// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530200func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200201 if key != "" {
202 return nil
203 }
204 return errors.New("key didn't find")
205}
206
207// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530208func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200209 return nil
210}
211
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300212// DeleteWithPrefix mock function implementation for KVClient
213func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
214 return nil
215}
216
cbabuabf02352019-10-15 13:14:56 +0200217// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000218func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200219 return nil, errors.New("key didn't find")
220}
221
222// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530223func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200224 return nil
225}
226
227// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530228func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200229 return nil
230}
231
232// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530233func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200234 return nil
235}
236
237// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800238func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200239 return nil
240}
241
242// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000243func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200244 return nil
245}
246
247// ReleaseLock mock function implementation for KVClient
248func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
249 return nil
250}
251
252// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530253func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200254 return true
255}
256
257// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000258func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200259}
260
261// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000262func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200263}
264
cbabubef89432019-10-18 11:47:27 +0200265// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200266func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700267 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700268 DeviceID: testResMgr.DeviceID,
269 Args: testResMgr.Args,
270 KVStore: testResMgr.KVStore,
271 DeviceType: testResMgr.DeviceType,
272 Address: testResMgr.Address,
273 DevInfo: testResMgr.DevInfo,
274 PonRsrMgr: testResMgr.PonRsrMgr,
275 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200276 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700277 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700278
279 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200280}
281
282func TestNewResourceMgr(t *testing.T) {
283 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000284 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700285 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000286 KVStoreAddress string
287 kvStoreType string
288 deviceType string
289 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800290 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200291 }
292 tests := []struct {
293 name string
294 args args
295 want *OpenOltResourceMgr
296 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700297 {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800298 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200299 }
300 for _, tt := range tests {
301 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530302 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
303 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700304 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 +0200305 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
306 }
307 })
308 }
309}
310
311func TestOpenOltResourceMgr_Delete(t *testing.T) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700312 type args struct {
313 intfID uint32
314 }
cbabuabf02352019-10-15 13:14:56 +0200315 tests := []struct {
316 name string
317 fields *fields
318 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700319 args args
cbabuabf02352019-10-15 13:14:56 +0200320 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700321 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200322 }
323 for _, tt := range tests {
324 t.Run(tt.name, func(t *testing.T) {
325 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530326 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
327 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700328 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200329 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
330 }
331 })
332 }
333}
334
cbabuabf02352019-10-15 13:14:56 +0200335func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
336 type args struct {
337 intfID uint32
338 onuID uint32
339 uniID uint32
340 }
341 tests := []struct {
342 name string
343 fields *fields
344 args args
345 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700346 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200347 }
348 for _, tt := range tests {
349 t.Run(tt.name, func(t *testing.T) {
350 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530351 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
352 defer cancel()
353 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200354 })
355 }
356}
357
358func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
359 type args struct {
360 intfID uint32
361 onuID []uint32
362 }
363 tests := []struct {
364 name string
365 fields *fields
366 args args
367 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700368 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200369 }
370 for _, tt := range tests {
371 t.Run(tt.name, func(t *testing.T) {
372 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530373 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
374 defer cancel()
375 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200376 })
377 }
378}
379
cbabuabf02352019-10-15 13:14:56 +0200380func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
381 type args struct {
382 intfID uint32
383 onuID uint32
384 uniID uint32
385 }
386 tests := []struct {
387 name string
388 fields *fields
389 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000390 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200391 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700392 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200393 }
394 for _, tt := range tests {
395 t.Run(tt.name, func(t *testing.T) {
396 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530397 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
398 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700399 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
400 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000401 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700402 } else {
403 for i := range tt.want {
404 if got[i] != tt.want[i] {
405 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
406 break
407 }
408 }
cbabuabf02352019-10-15 13:14:56 +0200409 }
410 })
411 }
412}
413
414func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
415
416 type args struct {
417 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530418 ONUID int32
419 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200420 }
421 tests := []struct {
422 name string
423 fields *fields
424 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700425 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200426 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700427 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200428 }
429 for _, tt := range tests {
430 t.Run(tt.name, func(t *testing.T) {
431 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530432 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
433 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700434 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
435 if err != nil {
436 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
437 }
438 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200439 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
440 }
441 })
442 }
443}
444
Girish Gowdra950326e2021-11-05 12:43:24 -0700445func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) {
446
447 type args struct {
448 PONIntfID uint32
449 }
450 tests := []struct {
451 name string
452 fields *fields
453 args args
454 want error
455 }{
456 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
457 }
458 for _, tt := range tests {
459 t.Run(tt.name, func(t *testing.T) {
460 RsrcMgr := testResMgrObject(tt.fields)
461 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
462 defer cancel()
463 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx, tt.args.PONIntfID)
464 if err != nil {
465 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
466 }
467 })
468 }
469}
470
471func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
472
473 type args struct {
474 PONIntfID uint32
475 }
476 tests := []struct {
477 name string
478 fields *fields
479 args args
480 want error
481 }{
482 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
483 }
484 for _, tt := range tests {
485 t.Run(tt.name, func(t *testing.T) {
486 RsrcMgr := testResMgrObject(tt.fields)
487 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
488 defer cancel()
489 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx, tt.args.PONIntfID)
490 if err != nil {
491 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
492 }
493 })
494 }
495}
496
yasin sapli9e4c5092022-02-01 13:52:33 +0000497func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) {
498
499 type args struct {
500 intfID uint32
501 onuID uint32
502 gemPortIDs []uint32
503 gemPortIDsToBeDeleted []uint32
504 gemPortIDsRemaining []uint32
505 serialNum string
506 finalLength int
507 }
508 tests := []struct {
509 name string
510 fields *fields
511 args args
512 }{
513 // Add/Delete single gem port
514 {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
515 // Delete all gemports
516 {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
517 // Try to delete when there is no gem port
518 {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}},
519 // Try to delete non-existent gem port
520 {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
521 // Try to delete two of the gem ports
522 {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
523 }
524 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
525 defer cancel()
526 for _, tt := range tests {
527 t.Run(tt.name, func(t *testing.T) {
528 RsrcMgr := testResMgrObject(tt.fields)
529 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID); err != nil {
530 t.Errorf("failed to remove onu")
531 }
532 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum); err != nil {
533 t.Errorf("failed to add onu")
534 }
535 for _, gemPort := range tt.args.gemPortIDs {
536 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID, gemPort); err != nil {
537 t.Errorf("failed to add gem to onu")
538 }
539 }
540 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
541 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID, gemPortDeleted); err != nil {
542 t.Errorf("failed to remove gem from onu")
543 }
544 }
545 lenofGemPorts := 0
546 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID)
547 if err != nil || gP == nil {
548 t.Errorf("failed to get onuGemInfo")
549 }
550 var gemPorts []uint32
551
552 lenofGemPorts = len(gP.GemPorts)
553 gemPorts = gP.GemPorts
554
555 if lenofGemPorts != tt.args.finalLength {
556 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
557 }
558
559 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
560 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
561 }
562 })
563 }
564}
565
566func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
567
568 type args struct {
569 PONIntfID uint32
570 OnuCount uint32
571 }
572 tests := []struct {
573 name string
574 fields *fields
575 args args
576 want error
577 }{
578 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
579 }
580 for _, tt := range tests {
581 t.Run(tt.name, func(t *testing.T) {
582 RsrcMgr := testResMgrObject(tt.fields)
583 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
584 defer cancel()
585 for j := 1; j <= int(tt.args.OnuCount); j++ {
586 go func(i uint32, j uint32) {
587 // TODO: actually verify success
588 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, i, fmt.Sprintf("onu-%d", i))
589 }(tt.args.PONIntfID, uint32(j))
590 }
591 })
592 }
593}
594
595func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
596
597 type args struct {
598 intfID uint32
599 onuID uint32
600 gemPortIDs []uint32
601 gemPortIDsRemaining []uint32
602 serialNum string
603 finalLength int
604 }
605 tests := []struct {
606 name string
607 fields *fields
608 args args
609 }{
610 // Add single gem port
611 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
612 // Delete all gemports
613 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
614 // Do not add any gemport
615 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
616 }
617 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
618 defer cancel()
619 for _, tt := range tests {
620 t.Run(tt.name, func(t *testing.T) {
621 RsrcMgr := testResMgrObject(tt.fields)
622 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID); err != nil {
623 t.Errorf("failed to remove onu")
624 }
625 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum); err != nil {
626 t.Errorf("failed to add onu")
627 }
628 for _, gemPort := range tt.args.gemPortIDs {
629 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID, gemPort); err != nil {
630 t.Errorf("failed to add gem to onu")
631 }
632 }
633
634 lenofGemPorts := 0
635 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID)
636
637 var gemPorts []uint32
638 if err == nil && gP != nil {
639 lenofGemPorts = len(gP.GemPorts)
640 gemPorts = gP.GemPorts
641 }
642 if lenofGemPorts != tt.args.finalLength {
643 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
644 }
645
646 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
647 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
648 }
649 })
650 }
651}
652
cbabuabf02352019-10-15 13:14:56 +0200653func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
654 type args struct {
655 intfID uint32
656 onuID uint32
657 uniID uint32
658 }
659 tests := []struct {
660 name string
661 fields *fields
662 args args
663 want []uint32
664 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700665 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200666 }
667 for _, tt := range tests {
668 t.Run(tt.name, func(t *testing.T) {
669 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530670 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
671 defer cancel()
672 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200673 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
674 }
675 })
676 }
677}
678
Girish Gowdraa482f272021-03-24 23:04:19 -0700679func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200680 type args struct {
681 Direction string
682 IntfID uint32
683 OnuID uint32
684 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000685 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200686 }
687 tests := []struct {
688 name string
689 fields *fields
690 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700691 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200692 wantErr error
693 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700694 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
695 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
696 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
697 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200698 }
699 for _, tt := range tests {
700 t.Run(tt.name, func(t *testing.T) {
701 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530702 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
703 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700704 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200705 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700706 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200707 }
708 })
709 }
710}
711
712func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
713 type args struct {
714 ponIntfID uint32
715 }
716 tests := []struct {
717 name string
718 fields *fields
719 args args
720 want uint32
721 wantErr error
722 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700723 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200724 }
725 for _, tt := range tests {
726 t.Run(tt.name, func(t *testing.T) {
727 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530728 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
729 defer cancel()
730 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200731 if got != tt.want && err != nil {
732 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
733 }
734 })
735 }
736}
737
738func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
739
740 type args struct {
741 IntfID uint32
742 OnuID uint32
743 UniID uint32
744 }
745 tests := []struct {
746 name string
747 fields *fields
748 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000749 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200750 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700751 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000752 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200753 }
754 for _, tt := range tests {
755 t.Run(tt.name, func(t *testing.T) {
756 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530757 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
758 defer cancel()
759 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200760 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
761 }
762 })
763 }
764}
765
cbabuabf02352019-10-15 13:14:56 +0200766func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
767
768 type args struct {
769 Direction string
770 IntfID uint32
771 OnuID uint32
772 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000773 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200774 }
775 tests := []struct {
776 name string
777 fields *fields
778 args args
779 wantErr error
780 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000781 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200782 errors.New("failed to delete meter id %s from kvstore")},
783 }
784 for _, tt := range tests {
785 t.Run(tt.name, func(t *testing.T) {
786 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530787 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
788 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700789 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000790 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200791 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
792 }
793 })
794 }
795}
796
797func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
798 type args struct {
799 IntfID uint32
800 OnuID uint32
801 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000802 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200803 }
804 tests := []struct {
805 name string
806 fields *fields
807 args args
808 wantErr error
809 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700810 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200811 errors.New("failed to delete techprofile id resource %s in KV store")},
812 }
813 for _, tt := range tests {
814 t.Run(tt.name, func(t *testing.T) {
815 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530816 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
817 defer cancel()
818 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000819 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200820 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
821 }
822 })
823 }
824}
825
826func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
827 type args struct {
828 ponPort uint32
829 onuID uint32
830 uniID uint32
831 allocID []uint32
832 }
833 tests := []struct {
834 name string
835 fields *fields
836 args args
837 wantErr error
838 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700839 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200840 errors.New("")},
841 }
842 for _, tt := range tests {
843 t.Run(tt.name, func(t *testing.T) {
844 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530845 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
846 defer cancel()
847 if err := RsrcMgr.UpdateAllocIdsForOnu(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.allocID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200848 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
849 }
850 })
851 }
852}
853
cbabuabf02352019-10-15 13:14:56 +0200854func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
855
856 type args struct {
857 ponPort uint32
858 onuID uint32
859 uniID uint32
860 GEMPortList []uint32
861 }
862 tests := []struct {
863 name string
864 fields *fields
865 args args
866 wantErr error
867 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700868 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200869 []uint32{1, 2}}, errors.New("failed to update resource")},
870 }
871 for _, tt := range tests {
872 t.Run(tt.name, func(t *testing.T) {
873 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530874 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
875 defer cancel()
876 if err := RsrcMgr.UpdateGEMPortIDsForOnu(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.GEMPortList); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200877 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
878 }
879 })
880 }
881}
882
cbabuabf02352019-10-15 13:14:56 +0200883func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
884 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700885 Direction string
886 IntfID uint32
887 OnuID uint32
888 UniID uint32
889 tpID uint32
890 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200891 }
892 tests := []struct {
893 name string
894 fields *fields
895 args args
896 wantErr error
897 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700898 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700899 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200900 }
901 for _, tt := range tests {
902 t.Run(tt.name, func(t *testing.T) {
903 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530904 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
905 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700906 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
907 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200908 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
909 }
910 })
911 }
912}
913
914func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
915 type args struct {
916 IntfID uint32
917 OnuID uint32
918 UniID uint32
919 TpID uint32
920 }
921 tests := []struct {
922 name string
923 fields *fields
924 args args
925 wantErr error
926 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700927 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200928 2}, errors.New("failed to update resource")},
929 }
930 for _, tt := range tests {
931 t.Run(tt.name, func(t *testing.T) {
932 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530933 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
934 defer cancel()
935 if err := RsrcMgr.UpdateTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.TpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200936 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
937 }
938 })
939 }
940}
941
942func TestSetKVClient(t *testing.T) {
943 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800944 backend string
945 address string
946 DeviceID string
947 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200948 }
949 tests := []struct {
950 name string
951 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500952 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200953 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300954 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200955 }
956 for _, tt := range tests {
957 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800958 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 +0200959 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
960 }
961 })
962 }
963}
964
cbabuabf02352019-10-15 13:14:56 +0200965func Test_newKVClient(t *testing.T) {
966 type args struct {
967 storeType string
968 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000969 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200970 }
971 var kvClient kvstore.Client
972 tests := []struct {
973 name string
974 args args
975 want kvstore.Client
976 wantErr error
977 }{
978 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
979 }
980 for _, tt := range tests {
981 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000982 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200983 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
984 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
985 }
986 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
987 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
988 return
989 }
990
991 })
992 }
993}
Esin Karamanccb714b2019-11-29 15:02:06 +0000994
995func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
996 type args struct {
997 intf uint32
998 gem uint32
999 servicePriority uint32
1000 }
1001 tests := []struct {
1002 name string
1003 args args
1004 fields *fields
1005 }{
1006 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1007 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1008 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1009 }
1010 for _, tt := range tests {
1011 t.Run(tt.name, func(t *testing.T) {
1012 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301013 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1014 defer cancel()
1015 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001016 if err != nil {
1017 t.Errorf("%s got err= %s wants nil", tt.name, err)
1018 return
1019 }
1020 })
1021 }
1022}
1023
Girish Gowdraf3728b12022-02-02 21:46:51 -08001024func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
1025 tests := []struct {
1026 name string
1027 fields *fields
1028 }{
1029 {"DeleteMcastQueueForIntf-1", getResMgr()},
1030 }
1031 for _, tt := range tests {
1032 t.Run(tt.name, func(t *testing.T) {
1033 RsrcMgr := testResMgrObject(tt.fields)
1034 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1035 defer cancel()
1036 RsrcMgr.DeleteMcastQueueForIntf(ctx)
1037 })
1038 }
1039}
1040
Esin Karamanccb714b2019-11-29 15:02:06 +00001041func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1042 groupDesc := ofp.OfpGroupDesc{
1043 Type: ofp.OfpGroupType_OFPGT_ALL,
1044 GroupId: groupID,
1045 }
1046 groupEntry := ofp.OfpGroupEntry{
1047 Desc: &groupDesc,
1048 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001049 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001050 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001051 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001052 bucket := ofp.OfpBucket{
1053 Actions: acts,
1054 }
1055 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001056 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001057 return &groupEntry
1058}
1059
1060func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1061 type args struct {
1062 group *ofp.OfpGroupEntry
1063 cached bool
1064 }
1065 //create group 1
1066 group1 := newGroup(1, []uint32{1})
1067 //create group 2
1068 group2 := newGroup(2, []uint32{2})
1069 //define test set
1070 tests := []struct {
1071 name string
1072 args args
1073 fields *fields
1074 }{
1075 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1076 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1077 }
1078 //execute tests
1079 for _, tt := range tests {
1080 t.Run(tt.name, func(t *testing.T) {
1081 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301082 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1083 defer cancel()
1084 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001085 if err != nil {
1086 t.Errorf("%s got err= %s wants nil", tt.name, err)
1087 return
1088 }
1089 })
1090 }
1091}
1092
1093func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1094 type args struct {
1095 groupID uint32
1096 cached bool
1097 }
1098 //define test set
1099 tests := []struct {
1100 name string
1101 args args
1102 fields *fields
1103 }{
1104 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1105 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1106 }
1107 //execute tests
1108 for _, tt := range tests {
1109 t.Run(tt.name, func(t *testing.T) {
1110 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301111 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1112 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001113 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1114 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001115 t.Errorf("%s got false but wants true", tt.name)
1116 return
1117 }
1118 })
1119 }
1120}
1121
1122func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1123 type args struct {
1124 groupID uint32
1125 cached bool
1126 }
1127 //define test set
1128 tests := []struct {
1129 name string
1130 args args
1131 fields *fields
1132 }{
1133 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1134 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1135 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1136 }
1137 //execute tests
1138 for _, tt := range tests {
1139 t.Run(tt.name, func(t *testing.T) {
1140 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301141 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1142 defer cancel()
1143 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001144 if err != nil {
1145 t.Errorf("%s got error but wants nil error", tt.name)
1146 return
1147 } else if exists && (groupInfo.GroupID == 0) {
1148 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1149 return
1150 } else if tt.args.groupID == 3 && exists {
1151 t.Errorf("%s got true but wants false", tt.name)
1152 return
1153 }
1154 })
1155 }
1156}