blob: 9d8d068765fe0c029497a7109bae8fe4918863e2 [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"
Girish Gowdra76a1b092021-07-28 10:07:04 -070030 "github.com/opencord/voltha-lib-go/v6/pkg/techprofile"
31 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030032 "reflect"
33 "strconv"
34 "strings"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030035 "testing"
36 "time"
37
Girish Gowdra4c3d4602021-07-22 16:33:37 -070038 "github.com/opencord/voltha-lib-go/v6/pkg/db"
39 "github.com/opencord/voltha-lib-go/v6/pkg/db/kvstore"
40 fu "github.com/opencord/voltha-lib-go/v6/pkg/flows"
41 "github.com/opencord/voltha-lib-go/v6/pkg/log"
42 ponrmgr "github.com/opencord/voltha-lib-go/v6/pkg/ponresourcemanager"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070043 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
44 "github.com/opencord/voltha-protos/v4/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020045)
46
47func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040048 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020049}
50
51const (
52 // MeterConfig meter to extract meter
53 MeterConfig = "meter_id"
54 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040055 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020056 // FlowIDInfo to extract flows
57 FlowIDInfo = "flow_id_info"
58 // FlowIds to extract flows
59 FlowIDs = "flow_ids"
60 // GemportIDs to gemport_ids
61 GemportIDs = "gemport_ids"
62 // AllocIDs to extract alloc_ids
63 AllocIDs = "alloc_ids"
64 // GemportIDPool to extract gemport
65 GemportIDPool = "gemport_id_pool"
66 // AllocIDPool to extract allocid
67 AllocIDPool = "alloc_id_pool"
68 // FlowIDpool to extract Flow ids
69 FlowIDpool = "flow_id_pool"
70)
71
cbabubef89432019-10-18 11:47:27 +020072// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020073type fields struct {
Girish Gowdra76a1b092021-07-28 10:07:04 -070074 DeviceID string
75 Address string
76 Args string
77 KVStore *db.Backend
78 DeviceType string
79 DevInfo *openolt.DeviceInfo
80 PonRsrMgr *ponrmgr.PONResourceManager
81 NumOfPonPorts uint32
82 TechProfileRef techprofile.TechProfileIf
cbabuabf02352019-10-15 13:14:56 +020083}
cbabubef89432019-10-18 11:47:27 +020084
85// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020086type MockResKVClient struct {
87}
88
cbabubef89432019-10-18 11:47:27 +020089// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020090func getResMgr() *fields {
91 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050092 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020093 Client: &MockResKVClient{},
94 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070095 resMgr.PonRsrMgr = &ponrmgr.PONResourceManager{}
cbabuabf02352019-10-15 13:14:56 +020096 ranges := make(map[string]interface{})
97 sharedIdxByType := make(map[string]string)
98 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
99 sharedIdxByType["ONU_ID"] = "ONU_ID"
100 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
101 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
102 ranges["ONU_ID"] = uint32(0)
103 ranges["GEMPORT_ID"] = uint32(0)
104 ranges["ALLOC_ID"] = uint32(0)
105 ranges["FLOW_ID"] = uint32(0)
106 ranges["onu_id_shared"] = uint32(0)
107 ranges["alloc_id_shared"] = uint32(0)
108 ranges["gemport_id_shared"] = uint32(0)
109 ranges["flow_id_shared"] = uint32(0)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700110 resMgr.NumOfPonPorts = 16
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700111 resMgr.PonRsrMgr.DeviceID = "onu-1"
112 resMgr.PonRsrMgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
113 resMgr.PonRsrMgr.KVStore = &db.Backend{
Matteo Scandolo84585372021-03-18 14:21:22 -0700114 Client: &MockResKVClient{},
115 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700116 resMgr.PonRsrMgr.Technology = "XGS-PON"
117 resMgr.PonRsrMgr.PonResourceRanges = ranges
118 resMgr.PonRsrMgr.SharedIdxByType = sharedIdxByType
Girish Gowdra76a1b092021-07-28 10:07:04 -0700119 resMgr.TechProfileRef = mocks.MockTechProfile{}
120
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700121 /*
122 tpMgr, err := tp.NewTechProfile(ctx, resMgr.PonRsrMgr, "etcd", "127.0.0.1", "/")
123 if err != nil {
124 logger.Fatal(ctx, err.Error())
125 }
126 */
Matteo Scandolo84585372021-03-18 14:21:22 -0700127
cbabuabf02352019-10-15 13:14:56 +0200128 return &resMgr
129}
cbabubef89432019-10-18 11:47:27 +0200130
131// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530132func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200133 return nil, errors.New("key didn't find")
134}
135
136// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530137func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000138 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200139 if key != "" {
140 if strings.Contains(key, MeterConfig) {
141 var bands []*ofp.OfpMeterBandHeader
142 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
143 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
144
145 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
146 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
147
Gamze Abakafee36392019-10-03 11:17:24 +0000148 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200149 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
150 if uint32(val) > 1 {
151 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
152 str, _ := json.Marshal(meterConfig)
153
154 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
155 }
156 return nil, errors.New("invalid meter")
157 }
158 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000159 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200160 data := make(map[string]interface{})
161 data["pool"] = "1024"
162 data["start_idx"] = 1
163 data["end_idx"] = 1024
164 str, _ := json.Marshal(data)
165 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
166 }
167 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000168 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200169 str, _ := json.Marshal([]uint32{1, 2})
170 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
171 }
172 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000173 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200174 str, _ := json.Marshal(1)
175 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
176 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000177 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000178 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000179 mcastQueues := make(map[uint32][]uint32)
180 mcastQueues[10] = []uint32{4000, 0}
181 str, _ := json.Marshal(mcastQueues)
182 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
183 }
184 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
185 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
186 str, _ := json.Marshal(groupInfo)
187 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
188 }
189
cbabuabf02352019-10-15 13:14:56 +0200190 maps := make(map[string]*kvstore.KVPair)
191 maps[key] = &kvstore.KVPair{Key: key}
192 return maps[key], nil
193 }
194 return nil, errors.New("key didn't find")
195}
196
197// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530198func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200199 if key != "" {
200 return nil
201 }
202 return errors.New("key didn't find")
203}
204
205// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530206func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200207 return nil
208}
209
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300210// DeleteWithPrefix mock function implementation for KVClient
211func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
212 return nil
213}
214
cbabuabf02352019-10-15 13:14:56 +0200215// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000216func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200217 return nil, errors.New("key didn't find")
218}
219
220// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530221func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200222 return nil
223}
224
225// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530226func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200227 return nil
228}
229
230// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530231func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200232 return nil
233}
234
235// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800236func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200237 return nil
238}
239
240// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000241func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200242 return nil
243}
244
245// ReleaseLock mock function implementation for KVClient
246func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
247 return nil
248}
249
250// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530251func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200252 return true
253}
254
255// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000256func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200257}
258
259// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000260func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200261}
262
cbabubef89432019-10-18 11:47:27 +0200263// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200264func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700265 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700266 DeviceID: testResMgr.DeviceID,
267 Args: testResMgr.Args,
268 KVStore: testResMgr.KVStore,
269 DeviceType: testResMgr.DeviceType,
270 Address: testResMgr.Address,
271 DevInfo: testResMgr.DevInfo,
272 PonRsrMgr: testResMgr.PonRsrMgr,
273 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200274 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700275 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700276
277 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200278}
279
280func TestNewResourceMgr(t *testing.T) {
281 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000282 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700283 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000284 KVStoreAddress string
285 kvStoreType string
286 deviceType string
287 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800288 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200289 }
290 tests := []struct {
291 name string
292 args args
293 want *OpenOltResourceMgr
294 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700295 {"NewResourceMgr-2", args{"olt1", 0, "1:2", "etcd",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800296 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200297 }
298 for _, tt := range tests {
299 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530300 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
301 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700302 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 +0200303 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
304 }
305 })
306 }
307}
308
309func TestOpenOltResourceMgr_Delete(t *testing.T) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700310 type args struct {
311 intfID uint32
312 }
cbabuabf02352019-10-15 13:14:56 +0200313 tests := []struct {
314 name string
315 fields *fields
316 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700317 args args
cbabuabf02352019-10-15 13:14:56 +0200318 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700319 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200320 }
321 for _, tt := range tests {
322 t.Run(tt.name, func(t *testing.T) {
323 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530324 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
325 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700326 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200327 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
328 }
329 })
330 }
331}
332
cbabuabf02352019-10-15 13:14:56 +0200333func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
334 type args struct {
335 intfID uint32
336 onuID uint32
337 uniID uint32
338 }
339 tests := []struct {
340 name string
341 fields *fields
342 args args
343 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700344 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200345 }
346 for _, tt := range tests {
347 t.Run(tt.name, func(t *testing.T) {
348 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530349 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
350 defer cancel()
351 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200352 })
353 }
354}
355
356func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
357 type args struct {
358 intfID uint32
359 onuID []uint32
360 }
361 tests := []struct {
362 name string
363 fields *fields
364 args args
365 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700366 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200367 }
368 for _, tt := range tests {
369 t.Run(tt.name, func(t *testing.T) {
370 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530371 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
372 defer cancel()
373 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200374 })
375 }
376}
377
cbabuabf02352019-10-15 13:14:56 +0200378func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
379 type args struct {
380 intfID uint32
381 onuID uint32
382 uniID uint32
383 }
384 tests := []struct {
385 name string
386 fields *fields
387 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000388 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200389 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700390 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200391 }
392 for _, tt := range tests {
393 t.Run(tt.name, func(t *testing.T) {
394 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530395 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
396 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700397 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
398 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000399 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700400 } else {
401 for i := range tt.want {
402 if got[i] != tt.want[i] {
403 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
404 break
405 }
406 }
cbabuabf02352019-10-15 13:14:56 +0200407 }
408 })
409 }
410}
411
412func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
413
414 type args struct {
415 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530416 ONUID int32
417 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200418 }
419 tests := []struct {
420 name string
421 fields *fields
422 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700423 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200424 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700425 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200426 }
427 for _, tt := range tests {
428 t.Run(tt.name, func(t *testing.T) {
429 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530430 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
431 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700432 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
433 if err != nil {
434 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
435 }
436 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200437 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
438 }
439 })
440 }
441}
442
443func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
444 type args struct {
445 intfID uint32
446 onuID uint32
447 uniID uint32
448 }
449 tests := []struct {
450 name string
451 fields *fields
452 args args
453 want []uint32
454 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700455 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200456 }
457 for _, tt := range tests {
458 t.Run(tt.name, func(t *testing.T) {
459 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530460 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
461 defer cancel()
462 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 +0200463 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
464 }
465 })
466 }
467}
468
Girish Gowdraa482f272021-03-24 23:04:19 -0700469func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200470 type args struct {
471 Direction string
472 IntfID uint32
473 OnuID uint32
474 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000475 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200476 }
477 tests := []struct {
478 name string
479 fields *fields
480 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700481 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200482 wantErr error
483 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700484 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
485 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
486 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
487 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200488 }
489 for _, tt := range tests {
490 t.Run(tt.name, func(t *testing.T) {
491 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530492 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
493 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700494 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 +0200495 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700496 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200497 }
498 })
499 }
500}
501
502func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
503 type args struct {
504 ponIntfID uint32
505 }
506 tests := []struct {
507 name string
508 fields *fields
509 args args
510 want uint32
511 wantErr error
512 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700513 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200514 }
515 for _, tt := range tests {
516 t.Run(tt.name, func(t *testing.T) {
517 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530518 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
519 defer cancel()
520 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200521 if got != tt.want && err != nil {
522 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
523 }
524 })
525 }
526}
527
528func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
529
530 type args struct {
531 IntfID uint32
532 OnuID uint32
533 UniID uint32
534 }
535 tests := []struct {
536 name string
537 fields *fields
538 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000539 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200540 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700541 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000542 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200543 }
544 for _, tt := range tests {
545 t.Run(tt.name, func(t *testing.T) {
546 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530547 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
548 defer cancel()
549 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 +0200550 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
551 }
552 })
553 }
554}
555
cbabuabf02352019-10-15 13:14:56 +0200556func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
557
558 type args struct {
559 Direction string
560 IntfID uint32
561 OnuID uint32
562 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000563 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200564 }
565 tests := []struct {
566 name string
567 fields *fields
568 args args
569 wantErr error
570 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000571 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200572 errors.New("failed to delete meter id %s from kvstore")},
573 }
574 for _, tt := range tests {
575 t.Run(tt.name, func(t *testing.T) {
576 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530577 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
578 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700579 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000580 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200581 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
582 }
583 })
584 }
585}
586
587func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
588 type args struct {
589 IntfID uint32
590 OnuID uint32
591 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000592 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200593 }
594 tests := []struct {
595 name string
596 fields *fields
597 args args
598 wantErr error
599 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700600 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200601 errors.New("failed to delete techprofile id resource %s in KV store")},
602 }
603 for _, tt := range tests {
604 t.Run(tt.name, func(t *testing.T) {
605 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530606 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
607 defer cancel()
608 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000609 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200610 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
611 }
612 })
613 }
614}
615
616func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
617 type args struct {
618 ponPort uint32
619 onuID uint32
620 uniID uint32
621 allocID []uint32
622 }
623 tests := []struct {
624 name string
625 fields *fields
626 args args
627 wantErr error
628 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700629 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200630 errors.New("")},
631 }
632 for _, tt := range tests {
633 t.Run(tt.name, func(t *testing.T) {
634 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530635 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
636 defer cancel()
637 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 +0200638 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
639 }
640 })
641 }
642}
643
cbabuabf02352019-10-15 13:14:56 +0200644func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
645
646 type args struct {
647 ponPort uint32
648 onuID uint32
649 uniID uint32
650 GEMPortList []uint32
651 }
652 tests := []struct {
653 name string
654 fields *fields
655 args args
656 wantErr error
657 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700658 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200659 []uint32{1, 2}}, errors.New("failed to update resource")},
660 }
661 for _, tt := range tests {
662 t.Run(tt.name, func(t *testing.T) {
663 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530664 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
665 defer cancel()
666 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 +0200667 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
668 }
669 })
670 }
671}
672
cbabuabf02352019-10-15 13:14:56 +0200673func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
674 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700675 Direction string
676 IntfID uint32
677 OnuID uint32
678 UniID uint32
679 tpID uint32
680 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200681 }
682 tests := []struct {
683 name string
684 fields *fields
685 args args
686 wantErr error
687 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700688 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700689 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200690 }
691 for _, tt := range tests {
692 t.Run(tt.name, func(t *testing.T) {
693 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530694 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
695 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700696 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
697 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200698 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
699 }
700 })
701 }
702}
703
704func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
705 type args struct {
706 IntfID uint32
707 OnuID uint32
708 UniID uint32
709 TpID uint32
710 }
711 tests := []struct {
712 name string
713 fields *fields
714 args args
715 wantErr error
716 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700717 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200718 2}, errors.New("failed to update resource")},
719 }
720 for _, tt := range tests {
721 t.Run(tt.name, func(t *testing.T) {
722 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530723 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
724 defer cancel()
725 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 +0200726 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
727 }
728 })
729 }
730}
731
732func TestSetKVClient(t *testing.T) {
733 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800734 backend string
735 address string
736 DeviceID string
737 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200738 }
739 tests := []struct {
740 name string
741 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500742 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200743 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300744 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200745 }
746 for _, tt := range tests {
747 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800748 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 +0200749 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
750 }
751 })
752 }
753}
754
cbabuabf02352019-10-15 13:14:56 +0200755func Test_newKVClient(t *testing.T) {
756 type args struct {
757 storeType string
758 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000759 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200760 }
761 var kvClient kvstore.Client
762 tests := []struct {
763 name string
764 args args
765 want kvstore.Client
766 wantErr error
767 }{
768 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
769 }
770 for _, tt := range tests {
771 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000772 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200773 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
774 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
775 }
776 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
777 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
778 return
779 }
780
781 })
782 }
783}
Esin Karamanccb714b2019-11-29 15:02:06 +0000784
785func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
786 type args struct {
787 intf uint32
788 gem uint32
789 servicePriority uint32
790 }
791 tests := []struct {
792 name string
793 args args
794 fields *fields
795 }{
796 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
797 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
798 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
799 }
800 for _, tt := range tests {
801 t.Run(tt.name, func(t *testing.T) {
802 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530803 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
804 defer cancel()
805 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000806 if err != nil {
807 t.Errorf("%s got err= %s wants nil", tt.name, err)
808 return
809 }
810 })
811 }
812}
813
814func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
815 groupDesc := ofp.OfpGroupDesc{
816 Type: ofp.OfpGroupType_OFPGT_ALL,
817 GroupId: groupID,
818 }
819 groupEntry := ofp.OfpGroupEntry{
820 Desc: &groupDesc,
821 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000822 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000823 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000824 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000825 bucket := ofp.OfpBucket{
826 Actions: acts,
827 }
828 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000829 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000830 return &groupEntry
831}
832
833func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
834 type args struct {
835 group *ofp.OfpGroupEntry
836 cached bool
837 }
838 //create group 1
839 group1 := newGroup(1, []uint32{1})
840 //create group 2
841 group2 := newGroup(2, []uint32{2})
842 //define test set
843 tests := []struct {
844 name string
845 args args
846 fields *fields
847 }{
848 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
849 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
850 }
851 //execute tests
852 for _, tt := range tests {
853 t.Run(tt.name, func(t *testing.T) {
854 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530855 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
856 defer cancel()
857 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000858 if err != nil {
859 t.Errorf("%s got err= %s wants nil", tt.name, err)
860 return
861 }
862 })
863 }
864}
865
866func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
867 type args struct {
868 groupID uint32
869 cached bool
870 }
871 //define test set
872 tests := []struct {
873 name string
874 args args
875 fields *fields
876 }{
877 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
878 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
879 }
880 //execute tests
881 for _, tt := range tests {
882 t.Run(tt.name, func(t *testing.T) {
883 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530884 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
885 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000886 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
887 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000888 t.Errorf("%s got false but wants true", tt.name)
889 return
890 }
891 })
892 }
893}
894
895func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
896 type args struct {
897 groupID uint32
898 cached bool
899 }
900 //define test set
901 tests := []struct {
902 name string
903 args args
904 fields *fields
905 }{
906 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
907 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
908 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
909 }
910 //execute tests
911 for _, tt := range tests {
912 t.Run(tt.name, func(t *testing.T) {
913 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530914 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
915 defer cancel()
916 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000917 if err != nil {
918 t.Errorf("%s got error but wants nil error", tt.name)
919 return
920 } else if exists && (groupInfo.GroupID == 0) {
921 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
922 return
923 } else if tt.args.groupID == 3 && exists {
924 t.Errorf("%s got true but wants false", tt.name)
925 return
926 }
927 })
928 }
929}