blob: bef6709a6664520da4c4321facc7858bfdf64c05 [file] [log] [blame]
cbabuabf02352019-10-15 13:14:56 +02001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
cbabubef89432019-10-18 11:47:27 +020017/*
18This file contains unit test cases for functions in the file resourcemanager.go.
19This file also implements the Client interface to mock the kv-client, fields struct to mock OpenOltResourceMgr
20and few utility functions.
21*/
22
23//Package adaptercore provides the utility for olt devices, flows and statistics
cbabuabf02352019-10-15 13:14:56 +020024package resourcemanager
25
26import (
npujarec5762e2020-01-01 14:08:48 +053027 "context"
cbabuabf02352019-10-15 13:14:56 +020028 "encoding/json"
29 "errors"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030030 "reflect"
31 "strconv"
32 "strings"
33 "sync"
34 "testing"
35 "time"
36
Girish Gowdraa09aeab2020-09-14 16:30:52 -070037 "github.com/opencord/voltha-lib-go/v4/pkg/db"
38 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
39 fu "github.com/opencord/voltha-lib-go/v4/pkg/flows"
40 "github.com/opencord/voltha-lib-go/v4/pkg/log"
41 ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager"
42 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
43 "github.com/opencord/voltha-protos/v4/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020044)
45
46func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040047 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020048}
49
50const (
51 // MeterConfig meter to extract meter
52 MeterConfig = "meter_id"
53 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040054 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020055 // FlowIDInfo to extract flows
56 FlowIDInfo = "flow_id_info"
57 // FlowIds to extract flows
58 FlowIDs = "flow_ids"
59 // GemportIDs to gemport_ids
60 GemportIDs = "gemport_ids"
61 // AllocIDs to extract alloc_ids
62 AllocIDs = "alloc_ids"
63 // GemportIDPool to extract gemport
64 GemportIDPool = "gemport_id_pool"
65 // AllocIDPool to extract allocid
66 AllocIDPool = "alloc_id_pool"
67 // FlowIDpool to extract Flow ids
68 FlowIDpool = "flow_id_pool"
69)
70
cbabubef89432019-10-18 11:47:27 +020071// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020072type fields struct {
Girish Gowdra38d533d2020-03-30 20:38:51 -070073 DeviceID string
Neha Sharma3f221ae2020-04-29 19:02:12 +000074 Address string
Girish Gowdra38d533d2020-03-30 20:38:51 -070075 Args string
76 KVStore *db.Backend
77 DeviceType string
Girish Gowdra38d533d2020-03-30 20:38:51 -070078 DevInfo *openolt.DeviceInfo
79 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
80 NumOfPonPorts uint32
cbabuabf02352019-10-15 13:14:56 +020081}
cbabubef89432019-10-18 11:47:27 +020082
83// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020084type MockResKVClient struct {
85}
86
cbabubef89432019-10-18 11:47:27 +020087// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020088func getResMgr() *fields {
89 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050090 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020091 Client: &MockResKVClient{},
92 }
93 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
94 ranges := make(map[string]interface{})
95 sharedIdxByType := make(map[string]string)
96 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
97 sharedIdxByType["ONU_ID"] = "ONU_ID"
98 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
99 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
100 ranges["ONU_ID"] = uint32(0)
101 ranges["GEMPORT_ID"] = uint32(0)
102 ranges["ALLOC_ID"] = uint32(0)
103 ranges["FLOW_ID"] = uint32(0)
104 ranges["onu_id_shared"] = uint32(0)
105 ranges["alloc_id_shared"] = uint32(0)
106 ranges["gemport_id_shared"] = uint32(0)
107 ranges["flow_id_shared"] = uint32(0)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700108 resMgr.NumOfPonPorts = 16
cbabuabf02352019-10-15 13:14:56 +0200109 ponMgr := &ponrmgr.PONResourceManager{
110 DeviceID: "onu-1",
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700111 IntfIDs: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
sbarbaria8910ba2019-11-05 10:12:23 -0500112 KVStore: &db.Backend{
cbabuabf02352019-10-15 13:14:56 +0200113 Client: &MockResKVClient{},
114 },
115 PonResourceRanges: ranges,
116 SharedIdxByType: sharedIdxByType,
117 }
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700118 var ponIntf uint32
119 for ponIntf = 0; ponIntf < resMgr.NumOfPonPorts; ponIntf++ {
120 resMgr.ResourceMgrs[ponIntf] = ponMgr
121 }
cbabuabf02352019-10-15 13:14:56 +0200122 return &resMgr
123}
cbabubef89432019-10-18 11:47:27 +0200124
125// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530126func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200127 return nil, errors.New("key didn't find")
128}
129
130// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530131func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000132 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200133 if key != "" {
134 if strings.Contains(key, MeterConfig) {
135 var bands []*ofp.OfpMeterBandHeader
136 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
137 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
138
139 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
140 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
141
Gamze Abakafee36392019-10-03 11:17:24 +0000142 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200143 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
144 if uint32(val) > 1 {
145 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
146 str, _ := json.Marshal(meterConfig)
147
148 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
149 }
150 return nil, errors.New("invalid meter")
151 }
152 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000153 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200154 data := make(map[string]interface{})
155 data["pool"] = "1024"
156 data["start_idx"] = 1
157 data["end_idx"] = 1024
158 str, _ := json.Marshal(data)
159 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
160 }
161 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000162 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200163 str, _ := json.Marshal([]uint32{1, 2})
164 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
165 }
166 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000167 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200168 str, _ := json.Marshal(1)
169 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
170 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000171 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000172 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000173 mcastQueues := make(map[uint32][]uint32)
174 mcastQueues[10] = []uint32{4000, 0}
175 str, _ := json.Marshal(mcastQueues)
176 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
177 }
178 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
179 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
180 str, _ := json.Marshal(groupInfo)
181 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
182 }
183
cbabuabf02352019-10-15 13:14:56 +0200184 maps := make(map[string]*kvstore.KVPair)
185 maps[key] = &kvstore.KVPair{Key: key}
186 return maps[key], nil
187 }
188 return nil, errors.New("key didn't find")
189}
190
191// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530192func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200193 if key != "" {
194 return nil
195 }
196 return errors.New("key didn't find")
197}
198
199// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530200func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200201 return nil
202}
203
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300204// DeleteWithPrefix mock function implementation for KVClient
205func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
206 return nil
207}
208
cbabuabf02352019-10-15 13:14:56 +0200209// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000210func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200211 return nil, errors.New("key didn't find")
212}
213
214// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530215func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200216 return nil
217}
218
219// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530220func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200221 return nil
222}
223
224// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530225func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200226 return nil
227}
228
229// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800230func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200231 return nil
232}
233
234// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000235func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200236 return nil
237}
238
239// ReleaseLock mock function implementation for KVClient
240func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
241 return nil
242}
243
244// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530245func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200246 return true
247}
248
249// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000250func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200251}
252
253// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000254func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200255}
256
cbabubef89432019-10-18 11:47:27 +0200257// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200258func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700259 var rsrMgr = OpenOltResourceMgr{
cbabuabf02352019-10-15 13:14:56 +0200260 DeviceID: testResMgr.DeviceID,
cbabuabf02352019-10-15 13:14:56 +0200261 Args: testResMgr.Args,
262 KVStore: testResMgr.KVStore,
263 DeviceType: testResMgr.DeviceType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000264 Address: testResMgr.Address,
cbabuabf02352019-10-15 13:14:56 +0200265 DevInfo: testResMgr.DevInfo,
266 ResourceMgrs: testResMgr.ResourceMgrs,
267 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700268
269 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
270 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
271 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
272
273 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200274}
275
276func TestNewResourceMgr(t *testing.T) {
277 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000278 deviceID string
279 KVStoreAddress string
280 kvStoreType string
281 deviceType string
282 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800283 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200284 }
285 tests := []struct {
286 name string
287 args args
288 want *OpenOltResourceMgr
289 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300290 {"NewResourceMgr-2", args{"olt1", "1:2", "etcd",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800291 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200292 }
293 for _, tt := range tests {
294 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530295 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
296 defer cancel()
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800297 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 +0200298 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
299 }
300 })
301 }
302}
303
304func TestOpenOltResourceMgr_Delete(t *testing.T) {
305 tests := []struct {
306 name string
307 fields *fields
308 wantErr error
309 }{
310 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
311 }
312 for _, tt := range tests {
313 t.Run(tt.name, func(t *testing.T) {
314 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530315 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
316 defer cancel()
317 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200318 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
319 }
320 })
321 }
322}
323
cbabuabf02352019-10-15 13:14:56 +0200324func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
325 type args struct {
326 intfID uint32
327 onuID uint32
328 uniID uint32
329 }
330 tests := []struct {
331 name string
332 fields *fields
333 args args
334 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700335 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200336 }
337 for _, tt := range tests {
338 t.Run(tt.name, func(t *testing.T) {
339 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530340 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
341 defer cancel()
342 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200343 })
344 }
345}
346
347func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
348 type args struct {
349 intfID uint32
350 onuID []uint32
351 }
352 tests := []struct {
353 name string
354 fields *fields
355 args args
356 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700357 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200358 }
359 for _, tt := range tests {
360 t.Run(tt.name, func(t *testing.T) {
361 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530362 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
363 defer cancel()
364 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200365 })
366 }
367}
368
369func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
370
371 type args struct {
372 intfID uint32
373 onuID uint32
374 uniID uint32
375 }
376 tests := []struct {
377 name string
378 fields *fields
379 args args
380 want uint32
381 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700382 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200383 }
384 for _, tt := range tests {
385 t.Run(tt.name, func(t *testing.T) {
386 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530387 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
388 defer cancel()
389 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 +0200390 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
391 }
392 })
393 }
394}
395
396func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
397 type args struct {
398 intfID uint32
399 onuID uint32
400 uniID uint32
401 }
402 tests := []struct {
403 name string
404 fields *fields
405 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000406 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200407 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700408 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200409 }
410 for _, tt := range tests {
411 t.Run(tt.name, func(t *testing.T) {
412 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530413 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
414 defer cancel()
415 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 +0000416 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200417 }
418 })
419 }
420}
421
422func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
423
424 type args struct {
425 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530426 ONUID int32
427 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200428 }
429 tests := []struct {
430 name string
431 fields *fields
432 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700433 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200434 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700435 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200436 }
437 for _, tt := range tests {
438 t.Run(tt.name, func(t *testing.T) {
439 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530440 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
441 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700442 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
443 if err != nil {
444 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
445 }
446 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200447 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
448 }
449 })
450 }
451}
452
453func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
454 type args struct {
455 intfID uint32
456 onuID uint32
457 uniID uint32
458 }
459 tests := []struct {
460 name string
461 fields *fields
462 args args
463 want []uint32
464 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700465 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200466 }
467 for _, tt := range tests {
468 t.Run(tt.name, func(t *testing.T) {
469 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530470 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
471 defer cancel()
472 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 +0200473 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
474 }
475 })
476 }
477}
478
cbabuabf02352019-10-15 13:14:56 +0200479func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
480 type args struct {
481 ponPort uint32
482 onuID uint32
483 uniID uint32
484 NumOfPorts uint32
485 }
486 tests := []struct {
487 name string
488 fields *fields
489 args args
490 want []uint32
491 wantErr error
492 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700493 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200494 errors.New("failed to get gem port")},
495 }
496 for _, tt := range tests {
497 t.Run(tt.name, func(t *testing.T) {
498 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530499 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
500 defer cancel()
501 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200502 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
503 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
504 return
505 }
506 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
507 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
508 }
509 })
510 }
511}
512
513func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
514 type args struct {
515 Direction string
516 IntfID uint32
517 OnuID uint32
518 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000519 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200520 }
521 tests := []struct {
522 name string
523 fields *fields
524 args args
525 want *ofp.OfpMeterConfig
526 wantErr error
527 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700528 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200529 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700530 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200531 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
532 }
533 for _, tt := range tests {
534 t.Run(tt.name, func(t *testing.T) {
535 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530536 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
537 defer cancel()
538 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 +0200539 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
540 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
541 }
542 })
543 }
544}
545
546func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
547 type args struct {
548 ponIntfID uint32
549 }
550 tests := []struct {
551 name string
552 fields *fields
553 args args
554 want uint32
555 wantErr error
556 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700557 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200558 }
559 for _, tt := range tests {
560 t.Run(tt.name, func(t *testing.T) {
561 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530562 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
563 defer cancel()
564 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200565 if got != tt.want && err != nil {
566 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
567 }
568 })
569 }
570}
571
572func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
573
574 type args struct {
575 IntfID uint32
576 OnuID uint32
577 UniID uint32
578 }
579 tests := []struct {
580 name string
581 fields *fields
582 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000583 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200584 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700585 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000586 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200587 }
588 for _, tt := range tests {
589 t.Run(tt.name, func(t *testing.T) {
590 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530591 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
592 defer cancel()
593 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 +0200594 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
595 }
596 })
597 }
598}
599
cbabuabf02352019-10-15 13:14:56 +0200600func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
601
602 type args struct {
603 Direction string
604 IntfID uint32
605 OnuID uint32
606 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000607 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200608 }
609 tests := []struct {
610 name string
611 fields *fields
612 args args
613 wantErr error
614 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000615 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200616 errors.New("failed to delete meter id %s from kvstore")},
617 }
618 for _, tt := range tests {
619 t.Run(tt.name, func(t *testing.T) {
620 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530621 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
622 defer cancel()
623 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000624 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200625 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
626 }
627 })
628 }
629}
630
631func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
632 type args struct {
633 IntfID uint32
634 OnuID uint32
635 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000636 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200637 }
638 tests := []struct {
639 name string
640 fields *fields
641 args args
642 wantErr error
643 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700644 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200645 errors.New("failed to delete techprofile id resource %s in KV store")},
646 }
647 for _, tt := range tests {
648 t.Run(tt.name, func(t *testing.T) {
649 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530650 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
651 defer cancel()
652 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000653 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200654 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
655 }
656 })
657 }
658}
659
660func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
661 type args struct {
662 ponPort uint32
663 onuID uint32
664 uniID uint32
665 allocID []uint32
666 }
667 tests := []struct {
668 name string
669 fields *fields
670 args args
671 wantErr error
672 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700673 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200674 errors.New("")},
675 }
676 for _, tt := range tests {
677 t.Run(tt.name, func(t *testing.T) {
678 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530679 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
680 defer cancel()
681 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 +0200682 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
683 }
684 })
685 }
686}
687
688func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
689 type args struct {
690 ponIntfID int32
691 onuID int32
692 uniID int32
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700693 flowID uint64
694 flowData FlowInfo
cbabuabf02352019-10-15 13:14:56 +0200695 }
696 tests := []struct {
697 name string
698 fields *fields
699 args args
700 wantErr error
701 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700702 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200703 }
704 for _, tt := range tests {
705 t.Run(tt.name, func(t *testing.T) {
706 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530707 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
708 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700709 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 +0200710 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
711 }
712 })
713 }
714}
715
716func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
717
718 type args struct {
719 ponPort uint32
720 onuID uint32
721 uniID uint32
722 GEMPortList []uint32
723 }
724 tests := []struct {
725 name string
726 fields *fields
727 args args
728 wantErr error
729 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700730 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200731 []uint32{1, 2}}, errors.New("failed to update resource")},
732 }
733 for _, tt := range tests {
734 t.Run(tt.name, func(t *testing.T) {
735 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530736 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
737 defer cancel()
738 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 +0200739 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
740 }
741 })
742 }
743}
744
745func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
746 type args struct {
747 gemPorts []uint32
748 PonPort uint32
749 onuID uint32
750 uniID uint32
751 }
752 tests := []struct {
753 name string
754 fields *fields
755 args args
756 wantErr error
757 }{
758 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700759 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200760 }
761 for _, tt := range tests {
762 t.Run(tt.name, func(t *testing.T) {
763 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530764 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
765 defer cancel()
766 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000767 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200768 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
769 }
770 })
771 }
772}
773
774func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
775 type args struct {
776 Direction string
777 IntfID uint32
778 OnuID uint32
779 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000780 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200781 MeterConfig *ofp.OfpMeterConfig
782 }
783 tests := []struct {
784 name string
785 fields *fields
786 args args
787 wantErr error
788 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700789 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000790 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200791 }
792 for _, tt := range tests {
793 t.Run(tt.name, func(t *testing.T) {
794 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530795 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
796 defer cancel()
797 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000798 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200799 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
800 }
801 })
802 }
803}
804
805func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
806 type args struct {
807 IntfID uint32
808 OnuID uint32
809 UniID uint32
810 TpID uint32
811 }
812 tests := []struct {
813 name string
814 fields *fields
815 args args
816 wantErr error
817 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700818 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200819 2}, errors.New("failed to update resource")},
820 }
821 for _, tt := range tests {
822 t.Run(tt.name, func(t *testing.T) {
823 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530824 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
825 defer cancel()
826 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 +0200827 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
828 }
829 })
830 }
831}
832
833func TestSetKVClient(t *testing.T) {
834 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800835 backend string
836 address string
837 DeviceID string
838 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200839 }
840 tests := []struct {
841 name string
842 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500843 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200844 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300845 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200846 }
847 for _, tt := range tests {
848 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800849 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 +0200850 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
851 }
852 })
853 }
854}
855
cbabuabf02352019-10-15 13:14:56 +0200856func Test_newKVClient(t *testing.T) {
857 type args struct {
858 storeType string
859 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000860 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200861 }
862 var kvClient kvstore.Client
863 tests := []struct {
864 name string
865 args args
866 want kvstore.Client
867 wantErr error
868 }{
869 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
870 }
871 for _, tt := range tests {
872 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000873 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200874 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
875 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
876 }
877 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
878 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
879 return
880 }
881
882 })
883 }
884}
Esin Karamanccb714b2019-11-29 15:02:06 +0000885
886func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
887 type args struct {
888 intf uint32
889 gem uint32
890 servicePriority uint32
891 }
892 tests := []struct {
893 name string
894 args args
895 fields *fields
896 }{
897 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
898 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
899 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
900 }
901 for _, tt := range tests {
902 t.Run(tt.name, func(t *testing.T) {
903 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530904 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
905 defer cancel()
906 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000907 if err != nil {
908 t.Errorf("%s got err= %s wants nil", tt.name, err)
909 return
910 }
911 })
912 }
913}
914
915func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
916 groupDesc := ofp.OfpGroupDesc{
917 Type: ofp.OfpGroupType_OFPGT_ALL,
918 GroupId: groupID,
919 }
920 groupEntry := ofp.OfpGroupEntry{
921 Desc: &groupDesc,
922 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000923 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000924 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000925 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000926 bucket := ofp.OfpBucket{
927 Actions: acts,
928 }
929 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000930 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000931 return &groupEntry
932}
933
934func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
935 type args struct {
936 group *ofp.OfpGroupEntry
937 cached bool
938 }
939 //create group 1
940 group1 := newGroup(1, []uint32{1})
941 //create group 2
942 group2 := newGroup(2, []uint32{2})
943 //define test set
944 tests := []struct {
945 name string
946 args args
947 fields *fields
948 }{
949 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
950 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
951 }
952 //execute tests
953 for _, tt := range tests {
954 t.Run(tt.name, func(t *testing.T) {
955 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530956 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
957 defer cancel()
958 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000959 if err != nil {
960 t.Errorf("%s got err= %s wants nil", tt.name, err)
961 return
962 }
963 })
964 }
965}
966
967func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
968 type args struct {
969 groupID uint32
970 cached bool
971 }
972 //define test set
973 tests := []struct {
974 name string
975 args args
976 fields *fields
977 }{
978 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
979 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
980 }
981 //execute tests
982 for _, tt := range tests {
983 t.Run(tt.name, func(t *testing.T) {
984 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530985 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
986 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000987 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
988 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000989 t.Errorf("%s got false but wants true", tt.name)
990 return
991 }
992 })
993 }
994}
995
996func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
997 type args struct {
998 groupID uint32
999 cached bool
1000 }
1001 //define test set
1002 tests := []struct {
1003 name string
1004 args args
1005 fields *fields
1006 }{
1007 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1008 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1009 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1010 }
1011 //execute tests
1012 for _, tt := range tests {
1013 t.Run(tt.name, func(t *testing.T) {
1014 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301015 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1016 defer cancel()
1017 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001018 if err != nil {
1019 t.Errorf("%s got error but wants nil error", tt.name)
1020 return
1021 } else if exists && (groupInfo.GroupID == 0) {
1022 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1023 return
1024 } else if tt.args.groupID == 3 && exists {
1025 t.Errorf("%s got true but wants false", tt.name)
1026 return
1027 }
1028 })
1029 }
1030}