blob: 5901c7c6cbebed9ab09c494e7856de1cada74e8a [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
cbabuabf02352019-10-15 13:14:56 +0200277 }
278 tests := []struct {
279 name string
280 args args
281 want *OpenOltResourceMgr
282 }{
283 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
284 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
285 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
286 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
287 }
288 for _, tt := range tests {
289 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530290 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
291 defer cancel()
Neha Sharma3f221ae2020-04-29 19:02:12 +0000292 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 +0200293 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
294 }
295 })
296 }
297}
298
299func TestOpenOltResourceMgr_Delete(t *testing.T) {
300 tests := []struct {
301 name string
302 fields *fields
303 wantErr error
304 }{
305 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
306 }
307 for _, tt := range tests {
308 t.Run(tt.name, func(t *testing.T) {
309 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530310 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
311 defer cancel()
312 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200313 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
314 }
315 })
316 }
317}
318
cbabuabf02352019-10-15 13:14:56 +0200319func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
320 type args struct {
321 intfID uint32
322 onuID uint32
323 uniID uint32
324 }
325 tests := []struct {
326 name string
327 fields *fields
328 args args
329 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700330 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200331 }
332 for _, tt := range tests {
333 t.Run(tt.name, func(t *testing.T) {
334 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530335 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
336 defer cancel()
337 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200338 })
339 }
340}
341
342func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
343 type args struct {
344 intfID uint32
345 onuID []uint32
346 }
347 tests := []struct {
348 name string
349 fields *fields
350 args args
351 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700352 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200353 }
354 for _, tt := range tests {
355 t.Run(tt.name, func(t *testing.T) {
356 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530357 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
358 defer cancel()
359 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200360 })
361 }
362}
363
364func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
365
366 type args struct {
367 intfID uint32
368 onuID uint32
369 uniID uint32
370 }
371 tests := []struct {
372 name string
373 fields *fields
374 args args
375 want uint32
376 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700377 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200378 }
379 for _, tt := range tests {
380 t.Run(tt.name, func(t *testing.T) {
381 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530382 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
383 defer cancel()
384 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 +0200385 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
386 }
387 })
388 }
389}
390
391func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
392 type args struct {
393 intfID uint32
394 onuID uint32
395 uniID uint32
396 }
397 tests := []struct {
398 name string
399 fields *fields
400 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000401 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200402 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700403 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200404 }
405 for _, tt := range tests {
406 t.Run(tt.name, func(t *testing.T) {
407 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530408 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
409 defer cancel()
410 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 +0000411 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200412 }
413 })
414 }
415}
416
417func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
418
419 type args struct {
420 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530421 ONUID int32
422 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200423 }
424 tests := []struct {
425 name string
426 fields *fields
427 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700428 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200429 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700430 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200431 }
432 for _, tt := range tests {
433 t.Run(tt.name, func(t *testing.T) {
434 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530435 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
436 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700437 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
438 if err != nil {
439 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
440 }
441 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200442 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
443 }
444 })
445 }
446}
447
448func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
449 type args struct {
450 intfID uint32
451 onuID uint32
452 uniID uint32
453 }
454 tests := []struct {
455 name string
456 fields *fields
457 args args
458 want []uint32
459 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700460 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200461 }
462 for _, tt := range tests {
463 t.Run(tt.name, func(t *testing.T) {
464 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530465 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
466 defer cancel()
467 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 +0200468 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
469 }
470 })
471 }
472}
473
cbabuabf02352019-10-15 13:14:56 +0200474func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
475 type args struct {
476 ponPort uint32
477 onuID uint32
478 uniID uint32
479 NumOfPorts uint32
480 }
481 tests := []struct {
482 name string
483 fields *fields
484 args args
485 want []uint32
486 wantErr error
487 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700488 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200489 errors.New("failed to get gem port")},
490 }
491 for _, tt := range tests {
492 t.Run(tt.name, func(t *testing.T) {
493 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530494 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
495 defer cancel()
496 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200497 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
498 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
499 return
500 }
501 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
502 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
503 }
504 })
505 }
506}
507
508func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
509 type args struct {
510 Direction string
511 IntfID uint32
512 OnuID uint32
513 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000514 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200515 }
516 tests := []struct {
517 name string
518 fields *fields
519 args args
520 want *ofp.OfpMeterConfig
521 wantErr error
522 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700523 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200524 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700525 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200526 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
527 }
528 for _, tt := range tests {
529 t.Run(tt.name, func(t *testing.T) {
530 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530531 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
532 defer cancel()
533 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 +0200534 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
535 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
536 }
537 })
538 }
539}
540
541func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
542 type args struct {
543 ponIntfID uint32
544 }
545 tests := []struct {
546 name string
547 fields *fields
548 args args
549 want uint32
550 wantErr error
551 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700552 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200553 }
554 for _, tt := range tests {
555 t.Run(tt.name, func(t *testing.T) {
556 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530557 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
558 defer cancel()
559 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200560 if got != tt.want && err != nil {
561 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
562 }
563 })
564 }
565}
566
567func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
568
569 type args struct {
570 IntfID uint32
571 OnuID uint32
572 UniID uint32
573 }
574 tests := []struct {
575 name string
576 fields *fields
577 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000578 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200579 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700580 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000581 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200582 }
583 for _, tt := range tests {
584 t.Run(tt.name, func(t *testing.T) {
585 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530586 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
587 defer cancel()
588 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 +0200589 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
590 }
591 })
592 }
593}
594
cbabuabf02352019-10-15 13:14:56 +0200595func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
596
597 type args struct {
598 Direction string
599 IntfID uint32
600 OnuID uint32
601 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000602 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200603 }
604 tests := []struct {
605 name string
606 fields *fields
607 args args
608 wantErr error
609 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000610 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200611 errors.New("failed to delete meter id %s from kvstore")},
612 }
613 for _, tt := range tests {
614 t.Run(tt.name, func(t *testing.T) {
615 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530616 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
617 defer cancel()
618 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000619 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200620 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
621 }
622 })
623 }
624}
625
626func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
627 type args struct {
628 IntfID uint32
629 OnuID uint32
630 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000631 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200632 }
633 tests := []struct {
634 name string
635 fields *fields
636 args args
637 wantErr error
638 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700639 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200640 errors.New("failed to delete techprofile id resource %s in KV store")},
641 }
642 for _, tt := range tests {
643 t.Run(tt.name, func(t *testing.T) {
644 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530645 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
646 defer cancel()
647 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000648 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200649 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
650 }
651 })
652 }
653}
654
655func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
656 type args struct {
657 ponPort uint32
658 onuID uint32
659 uniID uint32
660 allocID []uint32
661 }
662 tests := []struct {
663 name string
664 fields *fields
665 args args
666 wantErr error
667 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700668 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200669 errors.New("")},
670 }
671 for _, tt := range tests {
672 t.Run(tt.name, func(t *testing.T) {
673 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530674 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
675 defer cancel()
676 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 +0200677 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
678 }
679 })
680 }
681}
682
683func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
684 type args struct {
685 ponIntfID int32
686 onuID int32
687 uniID int32
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700688 flowID uint64
689 flowData FlowInfo
cbabuabf02352019-10-15 13:14:56 +0200690 }
691 tests := []struct {
692 name string
693 fields *fields
694 args args
695 wantErr error
696 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700697 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200698 }
699 for _, tt := range tests {
700 t.Run(tt.name, func(t *testing.T) {
701 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530702 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
703 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700704 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 +0200705 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
706 }
707 })
708 }
709}
710
711func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
712
713 type args struct {
714 ponPort uint32
715 onuID uint32
716 uniID uint32
717 GEMPortList []uint32
718 }
719 tests := []struct {
720 name string
721 fields *fields
722 args args
723 wantErr error
724 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700725 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200726 []uint32{1, 2}}, errors.New("failed to update resource")},
727 }
728 for _, tt := range tests {
729 t.Run(tt.name, func(t *testing.T) {
730 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530731 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
732 defer cancel()
733 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 +0200734 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
735 }
736 })
737 }
738}
739
740func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
741 type args struct {
742 gemPorts []uint32
743 PonPort uint32
744 onuID uint32
745 uniID uint32
746 }
747 tests := []struct {
748 name string
749 fields *fields
750 args args
751 wantErr error
752 }{
753 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700754 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200755 }
756 for _, tt := range tests {
757 t.Run(tt.name, func(t *testing.T) {
758 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530759 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
760 defer cancel()
761 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000762 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200763 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
764 }
765 })
766 }
767}
768
769func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
770 type args struct {
771 Direction string
772 IntfID uint32
773 OnuID uint32
774 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000775 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200776 MeterConfig *ofp.OfpMeterConfig
777 }
778 tests := []struct {
779 name string
780 fields *fields
781 args args
782 wantErr error
783 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700784 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000785 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200786 }
787 for _, tt := range tests {
788 t.Run(tt.name, func(t *testing.T) {
789 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530790 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
791 defer cancel()
792 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000793 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200794 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
795 }
796 })
797 }
798}
799
800func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
801 type args struct {
802 IntfID uint32
803 OnuID uint32
804 UniID uint32
805 TpID uint32
806 }
807 tests := []struct {
808 name string
809 fields *fields
810 args args
811 wantErr error
812 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700813 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200814 2}, errors.New("failed to update resource")},
815 }
816 for _, tt := range tests {
817 t.Run(tt.name, func(t *testing.T) {
818 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530819 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
820 defer cancel()
821 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 +0200822 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
823 }
824 })
825 }
826}
827
828func TestSetKVClient(t *testing.T) {
829 type args struct {
830 backend string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000831 address string
cbabuabf02352019-10-15 13:14:56 +0200832 DeviceID string
833 }
834 tests := []struct {
835 name string
836 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500837 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200838 }{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000839 {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1"}, &db.Backend{}},
840 {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200841 }
842 for _, tt := range tests {
843 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000844 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 +0200845 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
846 }
847 })
848 }
849}
850
cbabuabf02352019-10-15 13:14:56 +0200851func Test_newKVClient(t *testing.T) {
852 type args struct {
853 storeType string
854 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000855 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200856 }
857 var kvClient kvstore.Client
858 tests := []struct {
859 name string
860 args args
861 want kvstore.Client
862 wantErr error
863 }{
864 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
865 }
866 for _, tt := range tests {
867 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000868 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200869 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
870 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
871 }
872 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
873 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
874 return
875 }
876
877 })
878 }
879}
Esin Karamanccb714b2019-11-29 15:02:06 +0000880
881func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
882 type args struct {
883 intf uint32
884 gem uint32
885 servicePriority uint32
886 }
887 tests := []struct {
888 name string
889 args args
890 fields *fields
891 }{
892 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
893 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
894 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
895 }
896 for _, tt := range tests {
897 t.Run(tt.name, func(t *testing.T) {
898 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530899 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
900 defer cancel()
901 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000902 if err != nil {
903 t.Errorf("%s got err= %s wants nil", tt.name, err)
904 return
905 }
906 })
907 }
908}
909
910func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
911 groupDesc := ofp.OfpGroupDesc{
912 Type: ofp.OfpGroupType_OFPGT_ALL,
913 GroupId: groupID,
914 }
915 groupEntry := ofp.OfpGroupEntry{
916 Desc: &groupDesc,
917 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000918 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000919 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000920 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000921 bucket := ofp.OfpBucket{
922 Actions: acts,
923 }
924 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000925 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000926 return &groupEntry
927}
928
929func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
930 type args struct {
931 group *ofp.OfpGroupEntry
932 cached bool
933 }
934 //create group 1
935 group1 := newGroup(1, []uint32{1})
936 //create group 2
937 group2 := newGroup(2, []uint32{2})
938 //define test set
939 tests := []struct {
940 name string
941 args args
942 fields *fields
943 }{
944 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
945 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
946 }
947 //execute tests
948 for _, tt := range tests {
949 t.Run(tt.name, func(t *testing.T) {
950 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530951 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
952 defer cancel()
953 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000954 if err != nil {
955 t.Errorf("%s got err= %s wants nil", tt.name, err)
956 return
957 }
958 })
959 }
960}
961
962func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
963 type args struct {
964 groupID uint32
965 cached bool
966 }
967 //define test set
968 tests := []struct {
969 name string
970 args args
971 fields *fields
972 }{
973 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
974 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
975 }
976 //execute tests
977 for _, tt := range tests {
978 t.Run(tt.name, func(t *testing.T) {
979 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530980 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
981 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000982 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
983 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000984 t.Errorf("%s got false but wants true", tt.name)
985 return
986 }
987 })
988 }
989}
990
991func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
992 type args struct {
993 groupID uint32
994 cached bool
995 }
996 //define test set
997 tests := []struct {
998 name string
999 args args
1000 fields *fields
1001 }{
1002 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1003 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1004 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1005 }
1006 //execute tests
1007 for _, tt := range tests {
1008 t.Run(tt.name, func(t *testing.T) {
1009 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301010 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1011 defer cancel()
1012 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001013 if err != nil {
1014 t.Errorf("%s got error but wants nil error", tt.name)
1015 return
1016 } else if exists && (groupInfo.GroupID == 0) {
1017 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1018 return
1019 } else if tt.args.groupID == 3 && exists {
1020 t.Errorf("%s got true but wants false", tt.name)
1021 return
1022 }
1023 })
1024 }
1025}