blob: c4a6751a36e7cd71bf7973fb2b40edb0785d378b [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"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030030 "reflect"
31 "strconv"
32 "strings"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030033 "testing"
34 "time"
35
khenaidoo106c61a2021-08-11 18:05:46 -040036 "github.com/opencord/voltha-lib-go/v7/pkg/techprofile"
37 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
38
39 "github.com/opencord/voltha-lib-go/v7/pkg/db"
40 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
41 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
42 "github.com/opencord/voltha-lib-go/v7/pkg/log"
43 ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager"
44 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
45 "github.com/opencord/voltha-protos/v5/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020046)
47
48func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040049 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020050}
51
52const (
53 // MeterConfig meter to extract meter
54 MeterConfig = "meter_id"
55 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040056 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020057 // FlowIDInfo to extract flows
58 FlowIDInfo = "flow_id_info"
59 // FlowIds to extract flows
60 FlowIDs = "flow_ids"
61 // GemportIDs to gemport_ids
62 GemportIDs = "gemport_ids"
63 // AllocIDs to extract alloc_ids
64 AllocIDs = "alloc_ids"
65 // GemportIDPool to extract gemport
66 GemportIDPool = "gemport_id_pool"
67 // AllocIDPool to extract allocid
68 AllocIDPool = "alloc_id_pool"
69 // FlowIDpool to extract Flow ids
70 FlowIDpool = "flow_id_pool"
71)
72
cbabubef89432019-10-18 11:47:27 +020073// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020074type fields struct {
Girish Gowdra76a1b092021-07-28 10:07:04 -070075 DeviceID string
76 Address string
77 Args string
78 KVStore *db.Backend
79 DeviceType string
80 DevInfo *openolt.DeviceInfo
81 PonRsrMgr *ponrmgr.PONResourceManager
82 NumOfPonPorts uint32
83 TechProfileRef techprofile.TechProfileIf
cbabuabf02352019-10-15 13:14:56 +020084}
cbabubef89432019-10-18 11:47:27 +020085
86// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020087type MockResKVClient struct {
88}
89
cbabubef89432019-10-18 11:47:27 +020090// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020091func getResMgr() *fields {
92 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050093 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020094 Client: &MockResKVClient{},
95 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070096 resMgr.PonRsrMgr = &ponrmgr.PONResourceManager{}
cbabuabf02352019-10-15 13:14:56 +020097 ranges := make(map[string]interface{})
98 sharedIdxByType := make(map[string]string)
99 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
100 sharedIdxByType["ONU_ID"] = "ONU_ID"
101 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
102 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
103 ranges["ONU_ID"] = uint32(0)
104 ranges["GEMPORT_ID"] = uint32(0)
105 ranges["ALLOC_ID"] = uint32(0)
106 ranges["FLOW_ID"] = uint32(0)
107 ranges["onu_id_shared"] = uint32(0)
108 ranges["alloc_id_shared"] = uint32(0)
109 ranges["gemport_id_shared"] = uint32(0)
110 ranges["flow_id_shared"] = uint32(0)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700111 resMgr.NumOfPonPorts = 16
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700112 resMgr.PonRsrMgr.DeviceID = "onu-1"
113 resMgr.PonRsrMgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
114 resMgr.PonRsrMgr.KVStore = &db.Backend{
Matteo Scandolo84585372021-03-18 14:21:22 -0700115 Client: &MockResKVClient{},
116 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700117 resMgr.PonRsrMgr.Technology = "XGS-PON"
118 resMgr.PonRsrMgr.PonResourceRanges = ranges
119 resMgr.PonRsrMgr.SharedIdxByType = sharedIdxByType
Girish Gowdra76a1b092021-07-28 10:07:04 -0700120 resMgr.TechProfileRef = mocks.MockTechProfile{}
121
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700122 /*
123 tpMgr, err := tp.NewTechProfile(ctx, resMgr.PonRsrMgr, "etcd", "127.0.0.1", "/")
124 if err != nil {
125 logger.Fatal(ctx, err.Error())
126 }
127 */
Matteo Scandolo84585372021-03-18 14:21:22 -0700128
cbabuabf02352019-10-15 13:14:56 +0200129 return &resMgr
130}
cbabubef89432019-10-18 11:47:27 +0200131
132// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530133func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200134 return nil, errors.New("key didn't find")
135}
136
137// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530138func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000139 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200140 if key != "" {
141 if strings.Contains(key, MeterConfig) {
142 var bands []*ofp.OfpMeterBandHeader
143 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
144 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
145
146 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
147 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
148
Gamze Abakafee36392019-10-03 11:17:24 +0000149 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200150 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
151 if uint32(val) > 1 {
152 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
153 str, _ := json.Marshal(meterConfig)
154
155 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
156 }
157 return nil, errors.New("invalid meter")
158 }
159 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000160 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200161 data := make(map[string]interface{})
162 data["pool"] = "1024"
163 data["start_idx"] = 1
164 data["end_idx"] = 1024
165 str, _ := json.Marshal(data)
166 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
167 }
168 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000169 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200170 str, _ := json.Marshal([]uint32{1, 2})
171 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
172 }
173 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000174 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200175 str, _ := json.Marshal(1)
176 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
177 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000178 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000179 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000180 mcastQueues := make(map[uint32][]uint32)
181 mcastQueues[10] = []uint32{4000, 0}
182 str, _ := json.Marshal(mcastQueues)
183 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
184 }
185 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
186 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
187 str, _ := json.Marshal(groupInfo)
188 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
189 }
190
cbabuabf02352019-10-15 13:14:56 +0200191 maps := make(map[string]*kvstore.KVPair)
192 maps[key] = &kvstore.KVPair{Key: key}
193 return maps[key], nil
194 }
195 return nil, errors.New("key didn't find")
196}
197
198// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530199func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200200 if key != "" {
201 return nil
202 }
203 return errors.New("key didn't find")
204}
205
206// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530207func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200208 return nil
209}
210
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300211// DeleteWithPrefix mock function implementation for KVClient
212func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
213 return nil
214}
215
cbabuabf02352019-10-15 13:14:56 +0200216// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000217func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200218 return nil, errors.New("key didn't find")
219}
220
221// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530222func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200223 return nil
224}
225
226// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530227func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200228 return nil
229}
230
231// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530232func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200233 return nil
234}
235
236// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800237func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200238 return nil
239}
240
241// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000242func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200243 return nil
244}
245
246// ReleaseLock mock function implementation for KVClient
247func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
248 return nil
249}
250
251// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530252func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200253 return true
254}
255
256// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000257func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200258}
259
260// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000261func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200262}
263
cbabubef89432019-10-18 11:47:27 +0200264// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200265func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700266 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700267 DeviceID: testResMgr.DeviceID,
268 Args: testResMgr.Args,
269 KVStore: testResMgr.KVStore,
270 DeviceType: testResMgr.DeviceType,
271 Address: testResMgr.Address,
272 DevInfo: testResMgr.DevInfo,
273 PonRsrMgr: testResMgr.PonRsrMgr,
274 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200275 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700276 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700277
278 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200279}
280
281func TestNewResourceMgr(t *testing.T) {
282 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000283 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700284 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000285 KVStoreAddress string
286 kvStoreType string
287 deviceType string
288 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800289 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200290 }
291 tests := []struct {
292 name string
293 args args
294 want *OpenOltResourceMgr
295 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700296 {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800297 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200298 }
299 for _, tt := range tests {
300 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530301 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
302 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700303 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 +0200304 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
305 }
306 })
307 }
308}
309
310func TestOpenOltResourceMgr_Delete(t *testing.T) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700311 type args struct {
312 intfID uint32
313 }
cbabuabf02352019-10-15 13:14:56 +0200314 tests := []struct {
315 name string
316 fields *fields
317 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700318 args args
cbabuabf02352019-10-15 13:14:56 +0200319 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700320 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200321 }
322 for _, tt := range tests {
323 t.Run(tt.name, func(t *testing.T) {
324 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530325 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
326 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700327 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200328 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
329 }
330 })
331 }
332}
333
cbabuabf02352019-10-15 13:14:56 +0200334func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
335 type args struct {
336 intfID uint32
337 onuID uint32
338 uniID uint32
339 }
340 tests := []struct {
341 name string
342 fields *fields
343 args args
344 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700345 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200346 }
347 for _, tt := range tests {
348 t.Run(tt.name, func(t *testing.T) {
349 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530350 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
351 defer cancel()
352 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200353 })
354 }
355}
356
357func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
358 type args struct {
359 intfID uint32
360 onuID []uint32
361 }
362 tests := []struct {
363 name string
364 fields *fields
365 args args
366 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700367 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200368 }
369 for _, tt := range tests {
370 t.Run(tt.name, func(t *testing.T) {
371 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530372 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
373 defer cancel()
374 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200375 })
376 }
377}
378
cbabuabf02352019-10-15 13:14:56 +0200379func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
380 type args struct {
381 intfID uint32
382 onuID uint32
383 uniID uint32
384 }
385 tests := []struct {
386 name string
387 fields *fields
388 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000389 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200390 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700391 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200392 }
393 for _, tt := range tests {
394 t.Run(tt.name, func(t *testing.T) {
395 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530396 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
397 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700398 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
399 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000400 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700401 } else {
402 for i := range tt.want {
403 if got[i] != tt.want[i] {
404 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
405 break
406 }
407 }
cbabuabf02352019-10-15 13:14:56 +0200408 }
409 })
410 }
411}
412
413func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
414
415 type args struct {
416 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530417 ONUID int32
418 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200419 }
420 tests := []struct {
421 name string
422 fields *fields
423 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700424 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200425 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700426 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200427 }
428 for _, tt := range tests {
429 t.Run(tt.name, func(t *testing.T) {
430 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530431 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
432 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700433 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
434 if err != nil {
435 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
436 }
437 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200438 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
439 }
440 })
441 }
442}
443
Girish Gowdra950326e2021-11-05 12:43:24 -0700444func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) {
445
446 type args struct {
447 PONIntfID uint32
448 }
449 tests := []struct {
450 name string
451 fields *fields
452 args args
453 want error
454 }{
455 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
456 }
457 for _, tt := range tests {
458 t.Run(tt.name, func(t *testing.T) {
459 RsrcMgr := testResMgrObject(tt.fields)
460 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
461 defer cancel()
462 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx, tt.args.PONIntfID)
463 if err != nil {
464 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
465 }
466 })
467 }
468}
469
470func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
471
472 type args struct {
473 PONIntfID uint32
474 }
475 tests := []struct {
476 name string
477 fields *fields
478 args args
479 want error
480 }{
481 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
482 }
483 for _, tt := range tests {
484 t.Run(tt.name, func(t *testing.T) {
485 RsrcMgr := testResMgrObject(tt.fields)
486 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
487 defer cancel()
488 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx, tt.args.PONIntfID)
489 if err != nil {
490 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
491 }
492 })
493 }
494}
495
cbabuabf02352019-10-15 13:14:56 +0200496func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
497 type args struct {
498 intfID uint32
499 onuID uint32
500 uniID uint32
501 }
502 tests := []struct {
503 name string
504 fields *fields
505 args args
506 want []uint32
507 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700508 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200509 }
510 for _, tt := range tests {
511 t.Run(tt.name, func(t *testing.T) {
512 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530513 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
514 defer cancel()
515 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 +0200516 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
517 }
518 })
519 }
520}
521
Girish Gowdraa482f272021-03-24 23:04:19 -0700522func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200523 type args struct {
524 Direction string
525 IntfID uint32
526 OnuID uint32
527 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000528 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200529 }
530 tests := []struct {
531 name string
532 fields *fields
533 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700534 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200535 wantErr error
536 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700537 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
538 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
539 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
540 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200541 }
542 for _, tt := range tests {
543 t.Run(tt.name, func(t *testing.T) {
544 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530545 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
546 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700547 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 +0200548 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700549 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200550 }
551 })
552 }
553}
554
555func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
556 type args struct {
557 ponIntfID uint32
558 }
559 tests := []struct {
560 name string
561 fields *fields
562 args args
563 want uint32
564 wantErr error
565 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700566 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200567 }
568 for _, tt := range tests {
569 t.Run(tt.name, func(t *testing.T) {
570 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530571 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
572 defer cancel()
573 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200574 if got != tt.want && err != nil {
575 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
576 }
577 })
578 }
579}
580
581func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
582
583 type args struct {
584 IntfID uint32
585 OnuID uint32
586 UniID uint32
587 }
588 tests := []struct {
589 name string
590 fields *fields
591 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000592 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200593 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700594 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000595 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200596 }
597 for _, tt := range tests {
598 t.Run(tt.name, func(t *testing.T) {
599 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530600 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
601 defer cancel()
602 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 +0200603 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
604 }
605 })
606 }
607}
608
cbabuabf02352019-10-15 13:14:56 +0200609func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
610
611 type args struct {
612 Direction string
613 IntfID uint32
614 OnuID uint32
615 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000616 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200617 }
618 tests := []struct {
619 name string
620 fields *fields
621 args args
622 wantErr error
623 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000624 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200625 errors.New("failed to delete meter id %s from kvstore")},
626 }
627 for _, tt := range tests {
628 t.Run(tt.name, func(t *testing.T) {
629 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530630 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
631 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700632 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000633 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200634 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
635 }
636 })
637 }
638}
639
640func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
641 type args struct {
642 IntfID uint32
643 OnuID uint32
644 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000645 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200646 }
647 tests := []struct {
648 name string
649 fields *fields
650 args args
651 wantErr error
652 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700653 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200654 errors.New("failed to delete techprofile id resource %s in KV store")},
655 }
656 for _, tt := range tests {
657 t.Run(tt.name, func(t *testing.T) {
658 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530659 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
660 defer cancel()
661 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000662 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200663 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
664 }
665 })
666 }
667}
668
669func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
670 type args struct {
671 ponPort uint32
672 onuID uint32
673 uniID uint32
674 allocID []uint32
675 }
676 tests := []struct {
677 name string
678 fields *fields
679 args args
680 wantErr error
681 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700682 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200683 errors.New("")},
684 }
685 for _, tt := range tests {
686 t.Run(tt.name, func(t *testing.T) {
687 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530688 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
689 defer cancel()
690 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 +0200691 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
692 }
693 })
694 }
695}
696
cbabuabf02352019-10-15 13:14:56 +0200697func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
698
699 type args struct {
700 ponPort uint32
701 onuID uint32
702 uniID uint32
703 GEMPortList []uint32
704 }
705 tests := []struct {
706 name string
707 fields *fields
708 args args
709 wantErr error
710 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700711 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200712 []uint32{1, 2}}, errors.New("failed to update resource")},
713 }
714 for _, tt := range tests {
715 t.Run(tt.name, func(t *testing.T) {
716 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530717 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
718 defer cancel()
719 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 +0200720 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
721 }
722 })
723 }
724}
725
cbabuabf02352019-10-15 13:14:56 +0200726func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
727 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700728 Direction string
729 IntfID uint32
730 OnuID uint32
731 UniID uint32
732 tpID uint32
733 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200734 }
735 tests := []struct {
736 name string
737 fields *fields
738 args args
739 wantErr error
740 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700741 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700742 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200743 }
744 for _, tt := range tests {
745 t.Run(tt.name, func(t *testing.T) {
746 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530747 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
748 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700749 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
750 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200751 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
752 }
753 })
754 }
755}
756
757func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
758 type args struct {
759 IntfID uint32
760 OnuID uint32
761 UniID uint32
762 TpID uint32
763 }
764 tests := []struct {
765 name string
766 fields *fields
767 args args
768 wantErr error
769 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700770 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200771 2}, errors.New("failed to update resource")},
772 }
773 for _, tt := range tests {
774 t.Run(tt.name, func(t *testing.T) {
775 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530776 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
777 defer cancel()
778 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 +0200779 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
780 }
781 })
782 }
783}
784
785func TestSetKVClient(t *testing.T) {
786 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800787 backend string
788 address string
789 DeviceID string
790 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200791 }
792 tests := []struct {
793 name string
794 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500795 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200796 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300797 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200798 }
799 for _, tt := range tests {
800 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800801 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 +0200802 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
803 }
804 })
805 }
806}
807
cbabuabf02352019-10-15 13:14:56 +0200808func Test_newKVClient(t *testing.T) {
809 type args struct {
810 storeType string
811 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000812 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200813 }
814 var kvClient kvstore.Client
815 tests := []struct {
816 name string
817 args args
818 want kvstore.Client
819 wantErr error
820 }{
821 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
822 }
823 for _, tt := range tests {
824 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000825 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200826 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
827 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
828 }
829 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
830 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
831 return
832 }
833
834 })
835 }
836}
Esin Karamanccb714b2019-11-29 15:02:06 +0000837
838func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
839 type args struct {
840 intf uint32
841 gem uint32
842 servicePriority uint32
843 }
844 tests := []struct {
845 name string
846 args args
847 fields *fields
848 }{
849 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
850 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
851 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
852 }
853 for _, tt := range tests {
854 t.Run(tt.name, func(t *testing.T) {
855 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530856 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
857 defer cancel()
858 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000859 if err != nil {
860 t.Errorf("%s got err= %s wants nil", tt.name, err)
861 return
862 }
863 })
864 }
865}
866
867func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
868 groupDesc := ofp.OfpGroupDesc{
869 Type: ofp.OfpGroupType_OFPGT_ALL,
870 GroupId: groupID,
871 }
872 groupEntry := ofp.OfpGroupEntry{
873 Desc: &groupDesc,
874 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000875 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000876 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000877 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000878 bucket := ofp.OfpBucket{
879 Actions: acts,
880 }
881 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000882 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000883 return &groupEntry
884}
885
886func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
887 type args struct {
888 group *ofp.OfpGroupEntry
889 cached bool
890 }
891 //create group 1
892 group1 := newGroup(1, []uint32{1})
893 //create group 2
894 group2 := newGroup(2, []uint32{2})
895 //define test set
896 tests := []struct {
897 name string
898 args args
899 fields *fields
900 }{
901 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
902 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
903 }
904 //execute tests
905 for _, tt := range tests {
906 t.Run(tt.name, func(t *testing.T) {
907 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530908 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
909 defer cancel()
910 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000911 if err != nil {
912 t.Errorf("%s got err= %s wants nil", tt.name, err)
913 return
914 }
915 })
916 }
917}
918
919func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
920 type args struct {
921 groupID uint32
922 cached bool
923 }
924 //define test set
925 tests := []struct {
926 name string
927 args args
928 fields *fields
929 }{
930 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
931 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
932 }
933 //execute tests
934 for _, tt := range tests {
935 t.Run(tt.name, func(t *testing.T) {
936 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530937 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
938 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000939 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
940 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000941 t.Errorf("%s got false but wants true", tt.name)
942 return
943 }
944 })
945 }
946}
947
948func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
949 type args struct {
950 groupID uint32
951 cached bool
952 }
953 //define test set
954 tests := []struct {
955 name string
956 args args
957 fields *fields
958 }{
959 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
960 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
961 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
962 }
963 //execute tests
964 for _, tt := range tests {
965 t.Run(tt.name, func(t *testing.T) {
966 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530967 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
968 defer cancel()
969 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000970 if err != nil {
971 t.Errorf("%s got error but wants nil error", tt.name)
972 return
973 } else if exists && (groupInfo.GroupID == 0) {
974 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
975 return
976 } else if tt.args.groupID == 3 && exists {
977 t.Errorf("%s got true but wants false", tt.name)
978 return
979 }
980 })
981 }
982}