blob: fd69e940355ac1ce66004f52b652b3b5b88ff1ab [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 Gowdraa09aeab2020-09-14 16:30:52 -070030 "github.com/opencord/voltha-lib-go/v4/pkg/db"
31 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
32 fu "github.com/opencord/voltha-lib-go/v4/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v4/pkg/log"
34 ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager"
35 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
36 "github.com/opencord/voltha-protos/v4/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020037 "reflect"
38 "strconv"
39 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070040 "sync"
cbabuabf02352019-10-15 13:14:56 +020041 "testing"
npujarec5762e2020-01-01 14:08:48 +053042 "time"
cbabuabf02352019-10-15 13:14:56 +020043)
44
45func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040046 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020047}
48
49const (
50 // MeterConfig meter to extract meter
51 MeterConfig = "meter_id"
52 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040053 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020054 // FlowIDInfo to extract flows
55 FlowIDInfo = "flow_id_info"
56 // FlowIds to extract flows
57 FlowIDs = "flow_ids"
58 // GemportIDs to gemport_ids
59 GemportIDs = "gemport_ids"
60 // AllocIDs to extract alloc_ids
61 AllocIDs = "alloc_ids"
62 // GemportIDPool to extract gemport
63 GemportIDPool = "gemport_id_pool"
64 // AllocIDPool to extract allocid
65 AllocIDPool = "alloc_id_pool"
66 // FlowIDpool to extract Flow ids
67 FlowIDpool = "flow_id_pool"
68)
69
cbabubef89432019-10-18 11:47:27 +020070// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020071type fields struct {
Girish Gowdra38d533d2020-03-30 20:38:51 -070072 DeviceID string
Neha Sharma3f221ae2020-04-29 19:02:12 +000073 Address string
Girish Gowdra38d533d2020-03-30 20:38:51 -070074 Args string
75 KVStore *db.Backend
76 DeviceType string
Girish Gowdra38d533d2020-03-30 20:38:51 -070077 DevInfo *openolt.DeviceInfo
78 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
79 NumOfPonPorts uint32
cbabuabf02352019-10-15 13:14:56 +020080}
cbabubef89432019-10-18 11:47:27 +020081
82// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020083type MockResKVClient struct {
84}
85
cbabubef89432019-10-18 11:47:27 +020086// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020087func getResMgr() *fields {
88 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050089 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020090 Client: &MockResKVClient{},
91 }
92 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
93 ranges := make(map[string]interface{})
94 sharedIdxByType := make(map[string]string)
95 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
96 sharedIdxByType["ONU_ID"] = "ONU_ID"
97 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
98 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
99 ranges["ONU_ID"] = uint32(0)
100 ranges["GEMPORT_ID"] = uint32(0)
101 ranges["ALLOC_ID"] = uint32(0)
102 ranges["FLOW_ID"] = uint32(0)
103 ranges["onu_id_shared"] = uint32(0)
104 ranges["alloc_id_shared"] = uint32(0)
105 ranges["gemport_id_shared"] = uint32(0)
106 ranges["flow_id_shared"] = uint32(0)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700107 resMgr.NumOfPonPorts = 2
cbabuabf02352019-10-15 13:14:56 +0200108 ponMgr := &ponrmgr.PONResourceManager{
109 DeviceID: "onu-1",
110 IntfIDs: []uint32{1, 2},
sbarbaria8910ba2019-11-05 10:12:23 -0500111 KVStore: &db.Backend{
cbabuabf02352019-10-15 13:14:56 +0200112 Client: &MockResKVClient{},
113 },
114 PonResourceRanges: ranges,
115 SharedIdxByType: sharedIdxByType,
116 }
117 resMgr.ResourceMgrs[1] = ponMgr
118 resMgr.ResourceMgrs[2] = ponMgr
Girish Gowdra38d533d2020-03-30 20:38:51 -0700119
cbabuabf02352019-10-15 13:14:56 +0200120 return &resMgr
121}
cbabubef89432019-10-18 11:47:27 +0200122
123// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530124func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200125 return nil, errors.New("key didn't find")
126}
127
128// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530129func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000130 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200131 if key != "" {
132 if strings.Contains(key, MeterConfig) {
133 var bands []*ofp.OfpMeterBandHeader
134 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
135 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
136
137 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
138 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
139
Gamze Abakafee36392019-10-03 11:17:24 +0000140 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200141 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
142 if uint32(val) > 1 {
143 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
144 str, _ := json.Marshal(meterConfig)
145
146 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
147 }
148 return nil, errors.New("invalid meter")
149 }
150 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000151 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200152 data := make(map[string]interface{})
153 data["pool"] = "1024"
154 data["start_idx"] = 1
155 data["end_idx"] = 1024
156 str, _ := json.Marshal(data)
157 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
158 }
159 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000160 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200161 str, _ := json.Marshal([]uint32{1, 2})
162 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
163 }
164 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000165 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200166 str, _ := json.Marshal(1)
167 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
168 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000169 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000170 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000171 mcastQueues := make(map[uint32][]uint32)
172 mcastQueues[10] = []uint32{4000, 0}
173 str, _ := json.Marshal(mcastQueues)
174 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
175 }
176 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
177 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
178 str, _ := json.Marshal(groupInfo)
179 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
180 }
181
cbabuabf02352019-10-15 13:14:56 +0200182 maps := make(map[string]*kvstore.KVPair)
183 maps[key] = &kvstore.KVPair{Key: key}
184 return maps[key], nil
185 }
186 return nil, errors.New("key didn't find")
187}
188
189// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530190func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200191 if key != "" {
192 return nil
193 }
194 return errors.New("key didn't find")
195}
196
197// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530198func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200199 return nil
200}
201
202// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000203func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200204 return nil, errors.New("key didn't find")
205}
206
207// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530208func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200209 return nil
210}
211
212// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530213func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200214 return nil
215}
216
217// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530218func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200219 return nil
220}
221
222// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800223func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200224 return nil
225}
226
227// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000228func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200229 return nil
230}
231
232// ReleaseLock mock function implementation for KVClient
233func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
234 return nil
235}
236
237// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530238func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200239 return true
240}
241
242// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000243func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200244}
245
246// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000247func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200248}
249
cbabubef89432019-10-18 11:47:27 +0200250// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200251func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700252 var rsrMgr = OpenOltResourceMgr{
cbabuabf02352019-10-15 13:14:56 +0200253 DeviceID: testResMgr.DeviceID,
cbabuabf02352019-10-15 13:14:56 +0200254 Args: testResMgr.Args,
255 KVStore: testResMgr.KVStore,
256 DeviceType: testResMgr.DeviceType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000257 Address: testResMgr.Address,
cbabuabf02352019-10-15 13:14:56 +0200258 DevInfo: testResMgr.DevInfo,
259 ResourceMgrs: testResMgr.ResourceMgrs,
260 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700261
262 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
263 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
264 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
265
266 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200267}
268
269func TestNewResourceMgr(t *testing.T) {
270 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000271 deviceID string
272 KVStoreAddress string
273 kvStoreType string
274 deviceType string
275 devInfo *openolt.DeviceInfo
cbabuabf02352019-10-15 13:14:56 +0200276 }
277 tests := []struct {
278 name string
279 args args
280 want *OpenOltResourceMgr
281 }{
282 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
283 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
284 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
285 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
286 }
287 for _, tt := range tests {
288 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530289 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
290 defer cancel()
Neha Sharma3f221ae2020-04-29 19:02:12 +0000291 if got := NewResourceMgr(ctx, tt.args.deviceID, tt.args.KVStoreAddress, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200292 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
293 }
294 })
295 }
296}
297
298func TestOpenOltResourceMgr_Delete(t *testing.T) {
299 tests := []struct {
300 name string
301 fields *fields
302 wantErr error
303 }{
304 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
305 }
306 for _, tt := range tests {
307 t.Run(tt.name, func(t *testing.T) {
308 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530309 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
310 defer cancel()
311 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200312 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
313 }
314 })
315 }
316}
317
cbabuabf02352019-10-15 13:14:56 +0200318func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
319 type args struct {
320 intfID uint32
321 onuID uint32
322 uniID uint32
323 }
324 tests := []struct {
325 name string
326 fields *fields
327 args args
328 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700329 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200330 }
331 for _, tt := range tests {
332 t.Run(tt.name, func(t *testing.T) {
333 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530334 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
335 defer cancel()
336 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200337 })
338 }
339}
340
341func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
342 type args struct {
343 intfID uint32
344 onuID []uint32
345 }
346 tests := []struct {
347 name string
348 fields *fields
349 args args
350 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700351 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200352 }
353 for _, tt := range tests {
354 t.Run(tt.name, func(t *testing.T) {
355 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530356 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
357 defer cancel()
358 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200359 })
360 }
361}
362
363func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
364
365 type args struct {
366 intfID uint32
367 onuID uint32
368 uniID uint32
369 }
370 tests := []struct {
371 name string
372 fields *fields
373 args args
374 want uint32
375 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700376 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200377 }
378 for _, tt := range tests {
379 t.Run(tt.name, func(t *testing.T) {
380 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530381 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
382 defer cancel()
383 if got := RsrcMgr.GetAllocID(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200384 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
385 }
386 })
387 }
388}
389
390func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
391 type args struct {
392 intfID uint32
393 onuID uint32
394 uniID uint32
395 }
396 tests := []struct {
397 name string
398 fields *fields
399 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000400 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200401 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700402 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200403 }
404 for _, tt := range tests {
405 t.Run(tt.name, func(t *testing.T) {
406 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530407 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
408 defer cancel()
409 if got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); !reflect.DeepEqual(got, tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000410 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200411 }
412 })
413 }
414}
415
416func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
417
418 type args struct {
419 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530420 ONUID int32
421 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200422 }
423 tests := []struct {
424 name string
425 fields *fields
426 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700427 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200428 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700429 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200430 }
431 for _, tt := range tests {
432 t.Run(tt.name, func(t *testing.T) {
433 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530434 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
435 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700436 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
437 if err != nil {
438 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
439 }
440 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200441 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
442 }
443 })
444 }
445}
446
447func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
448 type args struct {
449 intfID uint32
450 onuID uint32
451 uniID uint32
452 }
453 tests := []struct {
454 name string
455 fields *fields
456 args args
457 want []uint32
458 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700459 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200460 }
461 for _, tt := range tests {
462 t.Run(tt.name, func(t *testing.T) {
463 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530464 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
465 defer cancel()
466 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 +0200467 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
468 }
469 })
470 }
471}
472
cbabuabf02352019-10-15 13:14:56 +0200473func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
474 type args struct {
475 ponPort uint32
476 onuID uint32
477 uniID uint32
478 NumOfPorts uint32
479 }
480 tests := []struct {
481 name string
482 fields *fields
483 args args
484 want []uint32
485 wantErr error
486 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700487 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200488 errors.New("failed to get gem port")},
489 }
490 for _, tt := range tests {
491 t.Run(tt.name, func(t *testing.T) {
492 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530493 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
494 defer cancel()
495 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200496 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
497 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
498 return
499 }
500 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
501 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
502 }
503 })
504 }
505}
506
507func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
508 type args struct {
509 Direction string
510 IntfID uint32
511 OnuID uint32
512 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000513 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200514 }
515 tests := []struct {
516 name string
517 fields *fields
518 args args
519 want *ofp.OfpMeterConfig
520 wantErr error
521 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700522 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200523 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700524 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200525 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
526 }
527 for _, tt := range tests {
528 t.Run(tt.name, func(t *testing.T) {
529 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530530 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
531 defer cancel()
532 got, err := RsrcMgr.GetMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200533 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
534 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
535 }
536 })
537 }
538}
539
540func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
541 type args struct {
542 ponIntfID uint32
543 }
544 tests := []struct {
545 name string
546 fields *fields
547 args args
548 want uint32
549 wantErr error
550 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700551 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200552 }
553 for _, tt := range tests {
554 t.Run(tt.name, func(t *testing.T) {
555 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530556 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
557 defer cancel()
558 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200559 if got != tt.want && err != nil {
560 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
561 }
562 })
563 }
564}
565
566func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
567
568 type args struct {
569 IntfID uint32
570 OnuID uint32
571 UniID uint32
572 }
573 tests := []struct {
574 name string
575 fields *fields
576 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000577 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200578 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700579 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000580 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200581 }
582 for _, tt := range tests {
583 t.Run(tt.name, func(t *testing.T) {
584 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530585 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
586 defer cancel()
587 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 +0200588 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
589 }
590 })
591 }
592}
593
cbabuabf02352019-10-15 13:14:56 +0200594func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
595
596 type args struct {
597 Direction string
598 IntfID uint32
599 OnuID uint32
600 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000601 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200602 }
603 tests := []struct {
604 name string
605 fields *fields
606 args args
607 wantErr error
608 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000609 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200610 errors.New("failed to delete meter id %s from kvstore")},
611 }
612 for _, tt := range tests {
613 t.Run(tt.name, func(t *testing.T) {
614 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530615 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
616 defer cancel()
617 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000618 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200619 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
620 }
621 })
622 }
623}
624
625func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
626 type args struct {
627 IntfID uint32
628 OnuID uint32
629 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000630 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200631 }
632 tests := []struct {
633 name string
634 fields *fields
635 args args
636 wantErr error
637 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700638 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200639 errors.New("failed to delete techprofile id resource %s in KV store")},
640 }
641 for _, tt := range tests {
642 t.Run(tt.name, func(t *testing.T) {
643 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530644 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
645 defer cancel()
646 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000647 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200648 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
649 }
650 })
651 }
652}
653
654func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
655 type args struct {
656 ponPort uint32
657 onuID uint32
658 uniID uint32
659 allocID []uint32
660 }
661 tests := []struct {
662 name string
663 fields *fields
664 args args
665 wantErr error
666 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700667 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200668 errors.New("")},
669 }
670 for _, tt := range tests {
671 t.Run(tt.name, func(t *testing.T) {
672 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530673 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
674 defer cancel()
675 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 +0200676 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
677 }
678 })
679 }
680}
681
682func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
683 type args struct {
684 ponIntfID int32
685 onuID int32
686 uniID int32
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700687 flowID uint64
688 flowData FlowInfo
cbabuabf02352019-10-15 13:14:56 +0200689 }
690 tests := []struct {
691 name string
692 fields *fields
693 args args
694 wantErr error
695 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700696 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200697 }
698 for _, tt := range tests {
699 t.Run(tt.name, func(t *testing.T) {
700 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530701 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
702 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700703 if err := RsrcMgr.UpdateFlowIDInfo(ctx, uint32(tt.args.ponIntfID), tt.args.onuID, tt.args.uniID, tt.args.flowID, tt.args.flowData); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200704 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
705 }
706 })
707 }
708}
709
710func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
711
712 type args struct {
713 ponPort uint32
714 onuID uint32
715 uniID uint32
716 GEMPortList []uint32
717 }
718 tests := []struct {
719 name string
720 fields *fields
721 args args
722 wantErr error
723 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700724 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200725 []uint32{1, 2}}, errors.New("failed to update resource")},
726 }
727 for _, tt := range tests {
728 t.Run(tt.name, func(t *testing.T) {
729 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530730 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
731 defer cancel()
732 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 +0200733 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
734 }
735 })
736 }
737}
738
739func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
740 type args struct {
741 gemPorts []uint32
742 PonPort uint32
743 onuID uint32
744 uniID uint32
745 }
746 tests := []struct {
747 name string
748 fields *fields
749 args args
750 wantErr error
751 }{
752 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700753 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200754 }
755 for _, tt := range tests {
756 t.Run(tt.name, func(t *testing.T) {
757 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530758 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
759 defer cancel()
760 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000761 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200762 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
763 }
764 })
765 }
766}
767
768func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
769 type args struct {
770 Direction string
771 IntfID uint32
772 OnuID uint32
773 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000774 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200775 MeterConfig *ofp.OfpMeterConfig
776 }
777 tests := []struct {
778 name string
779 fields *fields
780 args args
781 wantErr error
782 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700783 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000784 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200785 }
786 for _, tt := range tests {
787 t.Run(tt.name, func(t *testing.T) {
788 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530789 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
790 defer cancel()
791 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000792 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200793 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
794 }
795 })
796 }
797}
798
799func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
800 type args struct {
801 IntfID uint32
802 OnuID uint32
803 UniID uint32
804 TpID uint32
805 }
806 tests := []struct {
807 name string
808 fields *fields
809 args args
810 wantErr error
811 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700812 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200813 2}, errors.New("failed to update resource")},
814 }
815 for _, tt := range tests {
816 t.Run(tt.name, func(t *testing.T) {
817 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530818 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
819 defer cancel()
820 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 +0200821 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
822 }
823 })
824 }
825}
826
827func TestSetKVClient(t *testing.T) {
828 type args struct {
829 backend string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000830 address string
cbabuabf02352019-10-15 13:14:56 +0200831 DeviceID string
832 }
833 tests := []struct {
834 name string
835 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500836 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200837 }{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000838 {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1"}, &db.Backend{}},
839 {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200840 }
841 for _, tt := range tests {
842 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000843 if got := SetKVClient(context.Background(), tt.args.backend, tt.args.address, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200844 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
845 }
846 })
847 }
848}
849
cbabuabf02352019-10-15 13:14:56 +0200850func Test_newKVClient(t *testing.T) {
851 type args struct {
852 storeType string
853 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000854 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200855 }
856 var kvClient kvstore.Client
857 tests := []struct {
858 name string
859 args args
860 want kvstore.Client
861 wantErr error
862 }{
863 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
864 }
865 for _, tt := range tests {
866 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000867 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200868 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
869 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
870 }
871 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
872 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
873 return
874 }
875
876 })
877 }
878}
Esin Karamanccb714b2019-11-29 15:02:06 +0000879
880func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
881 type args struct {
882 intf uint32
883 gem uint32
884 servicePriority uint32
885 }
886 tests := []struct {
887 name string
888 args args
889 fields *fields
890 }{
891 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
892 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
893 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
894 }
895 for _, tt := range tests {
896 t.Run(tt.name, func(t *testing.T) {
897 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530898 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
899 defer cancel()
900 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000901 if err != nil {
902 t.Errorf("%s got err= %s wants nil", tt.name, err)
903 return
904 }
905 })
906 }
907}
908
909func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
910 groupDesc := ofp.OfpGroupDesc{
911 Type: ofp.OfpGroupType_OFPGT_ALL,
912 GroupId: groupID,
913 }
914 groupEntry := ofp.OfpGroupEntry{
915 Desc: &groupDesc,
916 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000917 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000918 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000919 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000920 bucket := ofp.OfpBucket{
921 Actions: acts,
922 }
923 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000924 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000925 return &groupEntry
926}
927
928func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
929 type args struct {
930 group *ofp.OfpGroupEntry
931 cached bool
932 }
933 //create group 1
934 group1 := newGroup(1, []uint32{1})
935 //create group 2
936 group2 := newGroup(2, []uint32{2})
937 //define test set
938 tests := []struct {
939 name string
940 args args
941 fields *fields
942 }{
943 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
944 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
945 }
946 //execute tests
947 for _, tt := range tests {
948 t.Run(tt.name, func(t *testing.T) {
949 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530950 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
951 defer cancel()
952 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000953 if err != nil {
954 t.Errorf("%s got err= %s wants nil", tt.name, err)
955 return
956 }
957 })
958 }
959}
960
961func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
962 type args struct {
963 groupID uint32
964 cached bool
965 }
966 //define test set
967 tests := []struct {
968 name string
969 args args
970 fields *fields
971 }{
972 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
973 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
974 }
975 //execute tests
976 for _, tt := range tests {
977 t.Run(tt.name, func(t *testing.T) {
978 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530979 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
980 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000981 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
982 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000983 t.Errorf("%s got false but wants true", tt.name)
984 return
985 }
986 })
987 }
988}
989
990func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
991 type args struct {
992 groupID uint32
993 cached bool
994 }
995 //define test set
996 tests := []struct {
997 name string
998 args args
999 fields *fields
1000 }{
1001 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1002 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1003 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1004 }
1005 //execute tests
1006 for _, tt := range tests {
1007 t.Run(tt.name, func(t *testing.T) {
1008 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301009 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1010 defer cancel()
1011 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001012 if err != nil {
1013 t.Errorf("%s got error but wants nil error", tt.name)
1014 return
1015 } else if exists && (groupInfo.GroupID == 0) {
1016 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1017 return
1018 } else if tt.args.groupID == 3 && exists {
1019 t.Errorf("%s got true but wants false", tt.name)
1020 return
1021 }
1022 })
1023 }
1024}