blob: 50fcf4a91f338242c42da41ad62ac9e7b83bd800 [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 Gowdrafb3d6102020-10-16 16:32:36 -0700107 resMgr.NumOfPonPorts = 16
cbabuabf02352019-10-15 13:14:56 +0200108 ponMgr := &ponrmgr.PONResourceManager{
109 DeviceID: "onu-1",
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700110 IntfIDs: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
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 }
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700117 var ponIntf uint32
118 for ponIntf = 0; ponIntf < resMgr.NumOfPonPorts; ponIntf++ {
119 resMgr.ResourceMgrs[ponIntf] = ponMgr
120 }
cbabuabf02352019-10-15 13:14:56 +0200121 return &resMgr
122}
cbabubef89432019-10-18 11:47:27 +0200123
124// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530125func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200126 return nil, errors.New("key didn't find")
127}
128
129// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530130func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000131 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200132 if key != "" {
133 if strings.Contains(key, MeterConfig) {
134 var bands []*ofp.OfpMeterBandHeader
135 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
136 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
137
138 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
139 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
140
Gamze Abakafee36392019-10-03 11:17:24 +0000141 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200142 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
143 if uint32(val) > 1 {
144 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
145 str, _ := json.Marshal(meterConfig)
146
147 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
148 }
149 return nil, errors.New("invalid meter")
150 }
151 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000152 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200153 data := make(map[string]interface{})
154 data["pool"] = "1024"
155 data["start_idx"] = 1
156 data["end_idx"] = 1024
157 str, _ := json.Marshal(data)
158 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
159 }
160 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000161 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200162 str, _ := json.Marshal([]uint32{1, 2})
163 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
164 }
165 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000166 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200167 str, _ := json.Marshal(1)
168 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
169 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000170 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000171 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000172 mcastQueues := make(map[uint32][]uint32)
173 mcastQueues[10] = []uint32{4000, 0}
174 str, _ := json.Marshal(mcastQueues)
175 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
176 }
177 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
178 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
179 str, _ := json.Marshal(groupInfo)
180 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
181 }
182
cbabuabf02352019-10-15 13:14:56 +0200183 maps := make(map[string]*kvstore.KVPair)
184 maps[key] = &kvstore.KVPair{Key: key}
185 return maps[key], nil
186 }
187 return nil, errors.New("key didn't find")
188}
189
190// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530191func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200192 if key != "" {
193 return nil
194 }
195 return errors.New("key didn't find")
196}
197
198// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530199func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200200 return nil
201}
202
203// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000204func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200205 return nil, errors.New("key didn't find")
206}
207
208// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530209func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200210 return nil
211}
212
213// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530214func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200215 return nil
216}
217
218// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530219func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200220 return nil
221}
222
223// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800224func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200225 return nil
226}
227
228// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000229func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200230 return nil
231}
232
233// ReleaseLock mock function implementation for KVClient
234func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
235 return nil
236}
237
238// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530239func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200240 return true
241}
242
243// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000244func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200245}
246
247// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000248func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200249}
250
cbabubef89432019-10-18 11:47:27 +0200251// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200252func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700253 var rsrMgr = OpenOltResourceMgr{
cbabuabf02352019-10-15 13:14:56 +0200254 DeviceID: testResMgr.DeviceID,
cbabuabf02352019-10-15 13:14:56 +0200255 Args: testResMgr.Args,
256 KVStore: testResMgr.KVStore,
257 DeviceType: testResMgr.DeviceType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000258 Address: testResMgr.Address,
cbabuabf02352019-10-15 13:14:56 +0200259 DevInfo: testResMgr.DevInfo,
260 ResourceMgrs: testResMgr.ResourceMgrs,
261 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700262
263 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
264 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
265 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
266
267 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200268}
269
270func TestNewResourceMgr(t *testing.T) {
271 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000272 deviceID string
273 KVStoreAddress string
274 kvStoreType string
275 deviceType string
276 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800277 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200278 }
279 tests := []struct {
280 name string
281 args args
282 want *OpenOltResourceMgr
283 }{
284 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800285 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200286 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800287 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200288 }
289 for _, tt := range tests {
290 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530291 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
292 defer cancel()
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800293 if got := NewResourceMgr(ctx, 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 +0200294 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
295 }
296 })
297 }
298}
299
300func TestOpenOltResourceMgr_Delete(t *testing.T) {
301 tests := []struct {
302 name string
303 fields *fields
304 wantErr error
305 }{
306 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
307 }
308 for _, tt := range tests {
309 t.Run(tt.name, func(t *testing.T) {
310 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530311 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
312 defer cancel()
313 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200314 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
315 }
316 })
317 }
318}
319
cbabuabf02352019-10-15 13:14:56 +0200320func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
321 type args struct {
322 intfID uint32
323 onuID uint32
324 uniID uint32
325 }
326 tests := []struct {
327 name string
328 fields *fields
329 args args
330 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700331 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200332 }
333 for _, tt := range tests {
334 t.Run(tt.name, func(t *testing.T) {
335 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530336 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
337 defer cancel()
338 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200339 })
340 }
341}
342
343func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
344 type args struct {
345 intfID uint32
346 onuID []uint32
347 }
348 tests := []struct {
349 name string
350 fields *fields
351 args args
352 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700353 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200354 }
355 for _, tt := range tests {
356 t.Run(tt.name, func(t *testing.T) {
357 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530358 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
359 defer cancel()
360 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200361 })
362 }
363}
364
365func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
366
367 type args struct {
368 intfID uint32
369 onuID uint32
370 uniID uint32
371 }
372 tests := []struct {
373 name string
374 fields *fields
375 args args
376 want uint32
377 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700378 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200379 }
380 for _, tt := range tests {
381 t.Run(tt.name, func(t *testing.T) {
382 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530383 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
384 defer cancel()
385 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 +0200386 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
387 }
388 })
389 }
390}
391
392func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
393 type args struct {
394 intfID uint32
395 onuID uint32
396 uniID uint32
397 }
398 tests := []struct {
399 name string
400 fields *fields
401 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000402 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200403 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700404 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200405 }
406 for _, tt := range tests {
407 t.Run(tt.name, func(t *testing.T) {
408 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530409 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
410 defer cancel()
411 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 +0000412 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200413 }
414 })
415 }
416}
417
418func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
419
420 type args struct {
421 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530422 ONUID int32
423 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200424 }
425 tests := []struct {
426 name string
427 fields *fields
428 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700429 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200430 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700431 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200432 }
433 for _, tt := range tests {
434 t.Run(tt.name, func(t *testing.T) {
435 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530436 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
437 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700438 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
439 if err != nil {
440 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
441 }
442 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200443 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
444 }
445 })
446 }
447}
448
449func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
450 type args struct {
451 intfID uint32
452 onuID uint32
453 uniID uint32
454 }
455 tests := []struct {
456 name string
457 fields *fields
458 args args
459 want []uint32
460 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700461 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200462 }
463 for _, tt := range tests {
464 t.Run(tt.name, func(t *testing.T) {
465 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530466 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
467 defer cancel()
468 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 +0200469 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
470 }
471 })
472 }
473}
474
cbabuabf02352019-10-15 13:14:56 +0200475func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
476 type args struct {
477 ponPort uint32
478 onuID uint32
479 uniID uint32
480 NumOfPorts uint32
481 }
482 tests := []struct {
483 name string
484 fields *fields
485 args args
486 want []uint32
487 wantErr error
488 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700489 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200490 errors.New("failed to get gem port")},
491 }
492 for _, tt := range tests {
493 t.Run(tt.name, func(t *testing.T) {
494 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530495 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
496 defer cancel()
497 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200498 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
499 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
500 return
501 }
502 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
503 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
504 }
505 })
506 }
507}
508
509func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
510 type args struct {
511 Direction string
512 IntfID uint32
513 OnuID uint32
514 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000515 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200516 }
517 tests := []struct {
518 name string
519 fields *fields
520 args args
521 want *ofp.OfpMeterConfig
522 wantErr error
523 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700524 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200525 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700526 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200527 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
528 }
529 for _, tt := range tests {
530 t.Run(tt.name, func(t *testing.T) {
531 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530532 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
533 defer cancel()
534 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 +0200535 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
536 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
537 }
538 })
539 }
540}
541
542func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
543 type args struct {
544 ponIntfID uint32
545 }
546 tests := []struct {
547 name string
548 fields *fields
549 args args
550 want uint32
551 wantErr error
552 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700553 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200554 }
555 for _, tt := range tests {
556 t.Run(tt.name, func(t *testing.T) {
557 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530558 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
559 defer cancel()
560 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200561 if got != tt.want && err != nil {
562 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
563 }
564 })
565 }
566}
567
568func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
569
570 type args struct {
571 IntfID uint32
572 OnuID uint32
573 UniID uint32
574 }
575 tests := []struct {
576 name string
577 fields *fields
578 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000579 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200580 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700581 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000582 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200583 }
584 for _, tt := range tests {
585 t.Run(tt.name, func(t *testing.T) {
586 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530587 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
588 defer cancel()
589 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 +0200590 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
591 }
592 })
593 }
594}
595
cbabuabf02352019-10-15 13:14:56 +0200596func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
597
598 type args struct {
599 Direction string
600 IntfID uint32
601 OnuID uint32
602 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000603 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200604 }
605 tests := []struct {
606 name string
607 fields *fields
608 args args
609 wantErr error
610 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000611 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200612 errors.New("failed to delete meter id %s from kvstore")},
613 }
614 for _, tt := range tests {
615 t.Run(tt.name, func(t *testing.T) {
616 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530617 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
618 defer cancel()
619 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000620 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200621 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
622 }
623 })
624 }
625}
626
627func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
628 type args struct {
629 IntfID uint32
630 OnuID uint32
631 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000632 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200633 }
634 tests := []struct {
635 name string
636 fields *fields
637 args args
638 wantErr error
639 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700640 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200641 errors.New("failed to delete techprofile id resource %s in KV store")},
642 }
643 for _, tt := range tests {
644 t.Run(tt.name, func(t *testing.T) {
645 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530646 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
647 defer cancel()
648 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000649 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200650 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
651 }
652 })
653 }
654}
655
656func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
657 type args struct {
658 ponPort uint32
659 onuID uint32
660 uniID uint32
661 allocID []uint32
662 }
663 tests := []struct {
664 name string
665 fields *fields
666 args args
667 wantErr error
668 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700669 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200670 errors.New("")},
671 }
672 for _, tt := range tests {
673 t.Run(tt.name, func(t *testing.T) {
674 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530675 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
676 defer cancel()
677 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 +0200678 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
679 }
680 })
681 }
682}
683
684func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
685 type args struct {
686 ponIntfID int32
687 onuID int32
688 uniID int32
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700689 flowID uint64
690 flowData FlowInfo
cbabuabf02352019-10-15 13:14:56 +0200691 }
692 tests := []struct {
693 name string
694 fields *fields
695 args args
696 wantErr error
697 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700698 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200699 }
700 for _, tt := range tests {
701 t.Run(tt.name, func(t *testing.T) {
702 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530703 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
704 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700705 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 +0200706 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
707 }
708 })
709 }
710}
711
712func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
713
714 type args struct {
715 ponPort uint32
716 onuID uint32
717 uniID uint32
718 GEMPortList []uint32
719 }
720 tests := []struct {
721 name string
722 fields *fields
723 args args
724 wantErr error
725 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700726 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200727 []uint32{1, 2}}, errors.New("failed to update resource")},
728 }
729 for _, tt := range tests {
730 t.Run(tt.name, func(t *testing.T) {
731 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530732 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
733 defer cancel()
734 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 +0200735 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
736 }
737 })
738 }
739}
740
741func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
742 type args struct {
743 gemPorts []uint32
744 PonPort uint32
745 onuID uint32
746 uniID uint32
747 }
748 tests := []struct {
749 name string
750 fields *fields
751 args args
752 wantErr error
753 }{
754 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700755 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200756 }
757 for _, tt := range tests {
758 t.Run(tt.name, func(t *testing.T) {
759 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530760 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
761 defer cancel()
762 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000763 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200764 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
765 }
766 })
767 }
768}
769
770func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
771 type args struct {
772 Direction string
773 IntfID uint32
774 OnuID uint32
775 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000776 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200777 MeterConfig *ofp.OfpMeterConfig
778 }
779 tests := []struct {
780 name string
781 fields *fields
782 args args
783 wantErr error
784 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700785 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000786 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200787 }
788 for _, tt := range tests {
789 t.Run(tt.name, func(t *testing.T) {
790 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530791 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
792 defer cancel()
793 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000794 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200795 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
796 }
797 })
798 }
799}
800
801func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
802 type args struct {
803 IntfID uint32
804 OnuID uint32
805 UniID uint32
806 TpID uint32
807 }
808 tests := []struct {
809 name string
810 fields *fields
811 args args
812 wantErr error
813 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700814 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200815 2}, errors.New("failed to update resource")},
816 }
817 for _, tt := range tests {
818 t.Run(tt.name, func(t *testing.T) {
819 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530820 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
821 defer cancel()
822 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 +0200823 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
824 }
825 })
826 }
827}
828
829func TestSetKVClient(t *testing.T) {
830 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800831 backend string
832 address string
833 DeviceID string
834 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200835 }
836 tests := []struct {
837 name string
838 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500839 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200840 }{
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800841 {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
842 {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200843 }
844 for _, tt := range tests {
845 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800846 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 +0200847 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
848 }
849 })
850 }
851}
852
cbabuabf02352019-10-15 13:14:56 +0200853func Test_newKVClient(t *testing.T) {
854 type args struct {
855 storeType string
856 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000857 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200858 }
859 var kvClient kvstore.Client
860 tests := []struct {
861 name string
862 args args
863 want kvstore.Client
864 wantErr error
865 }{
866 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
867 }
868 for _, tt := range tests {
869 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000870 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200871 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
872 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
873 }
874 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
875 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
876 return
877 }
878
879 })
880 }
881}
Esin Karamanccb714b2019-11-29 15:02:06 +0000882
883func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
884 type args struct {
885 intf uint32
886 gem uint32
887 servicePriority uint32
888 }
889 tests := []struct {
890 name string
891 args args
892 fields *fields
893 }{
894 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
895 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
896 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
897 }
898 for _, tt := range tests {
899 t.Run(tt.name, func(t *testing.T) {
900 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530901 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
902 defer cancel()
903 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000904 if err != nil {
905 t.Errorf("%s got err= %s wants nil", tt.name, err)
906 return
907 }
908 })
909 }
910}
911
912func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
913 groupDesc := ofp.OfpGroupDesc{
914 Type: ofp.OfpGroupType_OFPGT_ALL,
915 GroupId: groupID,
916 }
917 groupEntry := ofp.OfpGroupEntry{
918 Desc: &groupDesc,
919 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000920 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000921 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000922 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000923 bucket := ofp.OfpBucket{
924 Actions: acts,
925 }
926 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000927 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000928 return &groupEntry
929}
930
931func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
932 type args struct {
933 group *ofp.OfpGroupEntry
934 cached bool
935 }
936 //create group 1
937 group1 := newGroup(1, []uint32{1})
938 //create group 2
939 group2 := newGroup(2, []uint32{2})
940 //define test set
941 tests := []struct {
942 name string
943 args args
944 fields *fields
945 }{
946 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
947 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
948 }
949 //execute tests
950 for _, tt := range tests {
951 t.Run(tt.name, func(t *testing.T) {
952 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530953 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
954 defer cancel()
955 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000956 if err != nil {
957 t.Errorf("%s got err= %s wants nil", tt.name, err)
958 return
959 }
960 })
961 }
962}
963
964func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
965 type args struct {
966 groupID uint32
967 cached bool
968 }
969 //define test set
970 tests := []struct {
971 name string
972 args args
973 fields *fields
974 }{
975 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
976 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
977 }
978 //execute tests
979 for _, tt := range tests {
980 t.Run(tt.name, func(t *testing.T) {
981 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530982 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
983 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000984 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
985 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000986 t.Errorf("%s got false but wants true", tt.name)
987 return
988 }
989 })
990 }
991}
992
993func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
994 type args struct {
995 groupID uint32
996 cached bool
997 }
998 //define test set
999 tests := []struct {
1000 name string
1001 args args
1002 fields *fields
1003 }{
1004 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1005 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1006 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1007 }
1008 //execute tests
1009 for _, tt := range tests {
1010 t.Run(tt.name, func(t *testing.T) {
1011 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301012 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1013 defer cancel()
1014 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001015 if err != nil {
1016 t.Errorf("%s got error but wants nil error", tt.name)
1017 return
1018 } else if exists && (groupInfo.GroupID == 0) {
1019 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1020 return
1021 } else if tt.args.groupID == 3 && exists {
1022 t.Errorf("%s got true but wants false", tt.name)
1023 return
1024 }
1025 })
1026 }
1027}