blob: 562bc08508eaf903f0a90bbbffef9de0aaa0e62e [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"
Esin Karamanccb714b2019-11-29 15:02:06 +000030 "github.com/opencord/voltha-lib-go/v3/pkg/db"
31 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
32 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v3/pkg/log"
34 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
35 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
36 "github.com/opencord/voltha-protos/v3/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020037 "reflect"
38 "strconv"
39 "strings"
Girish Gowdra38d533d2020-03-30 20:38:51 -070040 "sync"
cbabuabf02352019-10-15 13:14:56 +020041 "testing"
npujarec5762e2020-01-01 14:08:48 +053042 "time"
cbabuabf02352019-10-15 13:14:56 +020043)
44
45func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040046 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020047}
48
49const (
50 // MeterConfig meter to extract meter
51 MeterConfig = "meter_id"
52 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040053 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020054 // FlowIDInfo to extract flows
55 FlowIDInfo = "flow_id_info"
56 // FlowIds to extract flows
57 FlowIDs = "flow_ids"
58 // GemportIDs to gemport_ids
59 GemportIDs = "gemport_ids"
60 // AllocIDs to extract alloc_ids
61 AllocIDs = "alloc_ids"
62 // GemportIDPool to extract gemport
63 GemportIDPool = "gemport_id_pool"
64 // AllocIDPool to extract allocid
65 AllocIDPool = "alloc_id_pool"
66 // FlowIDpool to extract Flow ids
67 FlowIDpool = "flow_id_pool"
68)
69
cbabubef89432019-10-18 11:47:27 +020070// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020071type fields struct {
Girish Gowdra38d533d2020-03-30 20:38:51 -070072 DeviceID string
Neha Sharma3f221ae2020-04-29 19:02:12 +000073 Address string
Girish Gowdra38d533d2020-03-30 20:38:51 -070074 Args string
75 KVStore *db.Backend
76 DeviceType string
Girish Gowdra38d533d2020-03-30 20:38:51 -070077 DevInfo *openolt.DeviceInfo
78 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
79 NumOfPonPorts uint32
cbabuabf02352019-10-15 13:14:56 +020080}
cbabubef89432019-10-18 11:47:27 +020081
82// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020083type MockResKVClient struct {
84}
85
cbabubef89432019-10-18 11:47:27 +020086// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020087func getResMgr() *fields {
88 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050089 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020090 Client: &MockResKVClient{},
91 }
92 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
93 ranges := make(map[string]interface{})
94 sharedIdxByType := make(map[string]string)
95 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
96 sharedIdxByType["ONU_ID"] = "ONU_ID"
97 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
98 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
99 ranges["ONU_ID"] = uint32(0)
100 ranges["GEMPORT_ID"] = uint32(0)
101 ranges["ALLOC_ID"] = uint32(0)
102 ranges["FLOW_ID"] = uint32(0)
103 ranges["onu_id_shared"] = uint32(0)
104 ranges["alloc_id_shared"] = uint32(0)
105 ranges["gemport_id_shared"] = uint32(0)
106 ranges["flow_id_shared"] = uint32(0)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700107 resMgr.NumOfPonPorts = 2
cbabuabf02352019-10-15 13:14:56 +0200108 ponMgr := &ponrmgr.PONResourceManager{
109 DeviceID: "onu-1",
110 IntfIDs: []uint32{1, 2},
sbarbaria8910ba2019-11-05 10:12:23 -0500111 KVStore: &db.Backend{
cbabuabf02352019-10-15 13:14:56 +0200112 Client: &MockResKVClient{},
113 },
114 PonResourceRanges: ranges,
115 SharedIdxByType: sharedIdxByType,
116 }
117 resMgr.ResourceMgrs[1] = ponMgr
118 resMgr.ResourceMgrs[2] = ponMgr
Girish Gowdra38d533d2020-03-30 20:38:51 -0700119
cbabuabf02352019-10-15 13:14:56 +0200120 return &resMgr
121}
cbabubef89432019-10-18 11:47:27 +0200122
123// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530124func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200125 return nil, errors.New("key didn't find")
126}
127
128// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530129func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000130 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200131 if key != "" {
132 if strings.Contains(key, MeterConfig) {
133 var bands []*ofp.OfpMeterBandHeader
134 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
135 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
136
137 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
138 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
139
Gamze Abakafee36392019-10-03 11:17:24 +0000140 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200141 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
142 if uint32(val) > 1 {
143 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
144 str, _ := json.Marshal(meterConfig)
145
146 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
147 }
148 return nil, errors.New("invalid meter")
149 }
150 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000151 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200152 data := make(map[string]interface{})
153 data["pool"] = "1024"
154 data["start_idx"] = 1
155 data["end_idx"] = 1024
156 str, _ := json.Marshal(data)
157 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
158 }
159 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000160 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200161 str, _ := json.Marshal([]uint32{1, 2})
162 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
163 }
164 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000165 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200166 str, _ := json.Marshal(1)
167 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
168 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000169 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000170 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000171 mcastQueues := make(map[uint32][]uint32)
172 mcastQueues[10] = []uint32{4000, 0}
173 str, _ := json.Marshal(mcastQueues)
174 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
175 }
176 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
177 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
178 str, _ := json.Marshal(groupInfo)
179 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
180 }
181
cbabuabf02352019-10-15 13:14:56 +0200182 maps := make(map[string]*kvstore.KVPair)
183 maps[key] = &kvstore.KVPair{Key: key}
184 return maps[key], nil
185 }
186 return nil, errors.New("key didn't find")
187}
188
189// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530190func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200191 if key != "" {
192 return nil
193 }
194 return errors.New("key didn't find")
195}
196
197// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530198func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200199 return nil
200}
201
202// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000203func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200204 return nil, errors.New("key didn't find")
205}
206
207// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530208func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200209 return nil
210}
211
212// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530213func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200214 return nil
215}
216
217// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530218func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200219 return nil
220}
221
222// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800223func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200224 return nil
225}
226
227// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000228func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200229 return nil
230}
231
232// ReleaseLock mock function implementation for KVClient
233func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
234 return nil
235}
236
237// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530238func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200239 return true
240}
241
242// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000243func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200244}
245
246// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000247func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200248}
249
cbabubef89432019-10-18 11:47:27 +0200250// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200251func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700252 var rsrMgr = OpenOltResourceMgr{
cbabuabf02352019-10-15 13:14:56 +0200253 DeviceID: testResMgr.DeviceID,
cbabuabf02352019-10-15 13:14:56 +0200254 Args: testResMgr.Args,
255 KVStore: testResMgr.KVStore,
256 DeviceType: testResMgr.DeviceType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000257 Address: testResMgr.Address,
cbabuabf02352019-10-15 13:14:56 +0200258 DevInfo: testResMgr.DevInfo,
259 ResourceMgrs: testResMgr.ResourceMgrs,
260 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700261
262 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
263 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
264 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
265
266 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200267}
268
269func TestNewResourceMgr(t *testing.T) {
270 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000271 deviceID string
272 KVStoreAddress string
273 kvStoreType string
274 deviceType string
275 devInfo *openolt.DeviceInfo
cbabuabf02352019-10-15 13:14:56 +0200276 }
277 tests := []struct {
278 name string
279 args args
280 want *OpenOltResourceMgr
281 }{
282 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
283 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
284 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
285 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
286 }
287 for _, tt := range tests {
288 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530289 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
290 defer cancel()
Neha Sharma3f221ae2020-04-29 19:02:12 +0000291 if got := NewResourceMgr(ctx, tt.args.deviceID, tt.args.KVStoreAddress, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200292 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
293 }
294 })
295 }
296}
297
298func TestOpenOltResourceMgr_Delete(t *testing.T) {
299 tests := []struct {
300 name string
301 fields *fields
302 wantErr error
303 }{
304 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
305 }
306 for _, tt := range tests {
307 t.Run(tt.name, func(t *testing.T) {
308 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530309 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
310 defer cancel()
311 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200312 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
313 }
314 })
315 }
316}
317
318func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) {
319 type args struct {
320 IntfID uint32
321 onuID int32
322 uniID int32
323 FlowID uint32
324 }
325 tests := []struct {
326 name string
327 fields *fields
328 args args
329 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700330 {"FreeFlowID-1", getResMgr(), args{1, 2, 2, 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.FreeFlowID(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
cbabuabf02352019-10-15 13:14:56 +0200338 })
339 }
340}
341
342func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) {
343
344 type args struct {
345 IntfID uint32
346 onuID uint32
347 uniID uint32
348 FlowID []uint32
349 }
350 tests := []struct {
351 name string
352 fields *fields
353 args args
354 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700355 {"FreeFlowIDs-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200356 }
357 for _, tt := range tests {
358 t.Run(tt.name, func(t *testing.T) {
359 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530360 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
361 defer cancel()
362 RsrcMgr.FreeFlowIDs(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
cbabuabf02352019-10-15 13:14:56 +0200363 })
364 }
365}
366
367func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
368 type args struct {
369 intfID uint32
370 onuID uint32
371 uniID uint32
372 }
373 tests := []struct {
374 name string
375 fields *fields
376 args args
377 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700378 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
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 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200386 })
387 }
388}
389
390func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
391 type args struct {
392 intfID uint32
393 onuID []uint32
394 }
395 tests := []struct {
396 name string
397 fields *fields
398 args args
399 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700400 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200401 }
402 for _, tt := range tests {
403 t.Run(tt.name, func(t *testing.T) {
404 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530405 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
406 defer cancel()
407 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200408 })
409 }
410}
411
412func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
413
414 type args struct {
415 intfID uint32
416 onuID uint32
417 uniID uint32
418 }
419 tests := []struct {
420 name string
421 fields *fields
422 args args
423 want uint32
424 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700425 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200426 }
427 for _, tt := range tests {
428 t.Run(tt.name, func(t *testing.T) {
429 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530430 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
431 defer cancel()
432 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 +0200433 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
434 }
435 })
436 }
437}
438
439func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
440 type args struct {
441 intfID uint32
442 onuID uint32
443 uniID uint32
444 }
445 tests := []struct {
446 name string
447 fields *fields
448 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000449 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200450 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700451 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200452 }
453 for _, tt := range tests {
454 t.Run(tt.name, func(t *testing.T) {
455 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530456 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
457 defer cancel()
458 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 +0000459 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200460 }
461 })
462 }
463}
464
465func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
466
467 type args struct {
468 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530469 ONUID int32
470 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200471 }
472 tests := []struct {
473 name string
474 fields *fields
475 args args
476 want []uint32
477 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700478 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200479 }
480 for _, tt := range tests {
481 t.Run(tt.name, func(t *testing.T) {
482 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530483 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
484 defer cancel()
485 if got := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200486 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
487 }
488 })
489 }
490}
491
492func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
493 type args struct {
494 intfID uint32
495 onuID uint32
496 uniID uint32
497 }
498 tests := []struct {
499 name string
500 fields *fields
501 args args
502 want []uint32
503 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700504 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200505 }
506 for _, tt := range tests {
507 t.Run(tt.name, func(t *testing.T) {
508 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530509 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
510 defer cancel()
511 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 +0200512 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
513 }
514 })
515 }
516}
517
518func TestOpenOltResourceMgr_GetFlowID(t *testing.T) {
519
520 type args struct {
521 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530522 ONUID int32
523 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200524 gemportID uint32
525 flowStoreCookie uint64
526 flowCategory string
Gamze Abaka724d0852020-03-18 12:10:24 +0000527 vlanVid uint32
cbabuabf02352019-10-15 13:14:56 +0200528 vlanPcp []uint32
529 }
530 tests := []struct {
531 name string
532 fields *fields
533 args args
534 want uint32
535 wantErr error
536 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700537 {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2,
Gamze Abaka724d0852020-03-18 12:10:24 +0000538 "HSIA", 33, nil}, 0, errors.New("failed to get flows")},
cbabuabf02352019-10-15 13:14:56 +0200539 }
540 for _, tt := range tests {
541 t.Run(tt.name, func(t *testing.T) {
542 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530543 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
544 defer cancel()
Gamze Abaka724d0852020-03-18 12:10:24 +0000545 got, err := RsrcMgr.GetFlowID(ctx, tt.args.ponIntfID, tt.args.ONUID, tt.args.uniID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...)
cbabuabf02352019-10-15 13:14:56 +0200546 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
547 t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
548 return
549 }
550 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
551 t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
552 }
553 })
554 }
555}
556
557func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
558 type args struct {
559 ponPort uint32
560 onuID uint32
561 uniID uint32
562 NumOfPorts uint32
563 }
564 tests := []struct {
565 name string
566 fields *fields
567 args args
568 want []uint32
569 wantErr error
570 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700571 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200572 errors.New("failed to get gem port")},
573 }
574 for _, tt := range tests {
575 t.Run(tt.name, func(t *testing.T) {
576 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530577 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
578 defer cancel()
579 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200580 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
581 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
582 return
583 }
584 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
585 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
586 }
587 })
588 }
589}
590
591func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
592 type args struct {
593 Direction string
594 IntfID uint32
595 OnuID uint32
596 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000597 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200598 }
599 tests := []struct {
600 name string
601 fields *fields
602 args args
603 want *ofp.OfpMeterConfig
604 wantErr error
605 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700606 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200607 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700608 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200609 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
610 }
611 for _, tt := range tests {
612 t.Run(tt.name, func(t *testing.T) {
613 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530614 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
615 defer cancel()
616 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 +0200617 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
618 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
619 }
620 })
621 }
622}
623
624func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
625 type args struct {
626 ponIntfID uint32
627 }
628 tests := []struct {
629 name string
630 fields *fields
631 args args
632 want uint32
633 wantErr error
634 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700635 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200636 }
637 for _, tt := range tests {
638 t.Run(tt.name, func(t *testing.T) {
639 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530640 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
641 defer cancel()
642 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200643 if got != tt.want && err != nil {
644 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
645 }
646 })
647 }
648}
649
650func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
651
652 type args struct {
653 IntfID uint32
654 OnuID uint32
655 UniID uint32
656 }
657 tests := []struct {
658 name string
659 fields *fields
660 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000661 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200662 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700663 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000664 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200665 }
666 for _, tt := range tests {
667 t.Run(tt.name, func(t *testing.T) {
668 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530669 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
670 defer cancel()
671 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 +0200672 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
673 }
674 })
675 }
676}
677
678func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
679 type args struct {
680 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530681 onuID int32
682 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200683 flowStoreCookie uint64
684 }
685 tests := []struct {
686 name string
687 fields *fields
688 args args
689 want bool
690 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700691 {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false},
cbabuabf02352019-10-15 13:14:56 +0200692 }
693 for _, tt := range tests {
694 t.Run(tt.name, func(t *testing.T) {
695 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530696 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
697 defer cancel()
698 if got := RsrcMgr.IsFlowCookieOnKVStore(ctx, tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowStoreCookie); got != tt.want {
cbabuabf02352019-10-15 13:14:56 +0200699 t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
700 }
701 })
702 }
703}
704
705func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
706
707 type args struct {
708 Direction string
709 IntfID uint32
710 OnuID uint32
711 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000712 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200713 }
714 tests := []struct {
715 name string
716 fields *fields
717 args args
718 wantErr error
719 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000720 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200721 errors.New("failed to delete meter id %s from kvstore")},
722 }
723 for _, tt := range tests {
724 t.Run(tt.name, func(t *testing.T) {
725 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530726 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
727 defer cancel()
728 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000729 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200730 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
731 }
732 })
733 }
734}
735
736func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
737 type args struct {
738 IntfID uint32
739 OnuID uint32
740 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000741 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200742 }
743 tests := []struct {
744 name string
745 fields *fields
746 args args
747 wantErr error
748 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700749 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200750 errors.New("failed to delete techprofile id resource %s in KV store")},
751 }
752 for _, tt := range tests {
753 t.Run(tt.name, func(t *testing.T) {
754 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530755 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
756 defer cancel()
757 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000758 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200759 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
760 }
761 })
762 }
763}
764
765func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
766 type args struct {
767 ponPort uint32
768 onuID uint32
769 uniID uint32
770 allocID []uint32
771 }
772 tests := []struct {
773 name string
774 fields *fields
775 args args
776 wantErr error
777 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700778 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200779 errors.New("")},
780 }
781 for _, tt := range tests {
782 t.Run(tt.name, func(t *testing.T) {
783 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530784 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
785 defer cancel()
786 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 +0200787 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
788 }
789 })
790 }
791}
792
793func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
794 type args struct {
795 ponIntfID int32
796 onuID int32
797 uniID int32
798 flowID uint32
799 flowData *[]FlowInfo
800 }
801 tests := []struct {
802 name string
803 fields *fields
804 args args
805 wantErr error
806 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700807 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200808 }
809 for _, tt := range tests {
810 t.Run(tt.name, func(t *testing.T) {
811 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530812 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
813 defer cancel()
814 if err := RsrcMgr.UpdateFlowIDInfo(ctx, 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 +0200815 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
816 }
817 })
818 }
819}
820
821func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
822
823 type args struct {
824 ponPort uint32
825 onuID uint32
826 uniID uint32
827 GEMPortList []uint32
828 }
829 tests := []struct {
830 name string
831 fields *fields
832 args args
833 wantErr error
834 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700835 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200836 []uint32{1, 2}}, errors.New("failed to update resource")},
837 }
838 for _, tt := range tests {
839 t.Run(tt.name, func(t *testing.T) {
840 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530841 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
842 defer cancel()
843 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 +0200844 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
845 }
846 })
847 }
848}
849
850func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
851 type args struct {
852 gemPorts []uint32
853 PonPort uint32
854 onuID uint32
855 uniID uint32
856 }
857 tests := []struct {
858 name string
859 fields *fields
860 args args
861 wantErr error
862 }{
863 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700864 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200865 }
866 for _, tt := range tests {
867 t.Run(tt.name, func(t *testing.T) {
868 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530869 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
870 defer cancel()
871 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000872 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200873 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
874 }
875 })
876 }
877}
878
879func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
880 type args struct {
881 Direction string
882 IntfID uint32
883 OnuID uint32
884 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000885 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200886 MeterConfig *ofp.OfpMeterConfig
887 }
888 tests := []struct {
889 name string
890 fields *fields
891 args args
892 wantErr error
893 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700894 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000895 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200896 }
897 for _, tt := range tests {
898 t.Run(tt.name, func(t *testing.T) {
899 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530900 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
901 defer cancel()
902 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000903 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200904 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
905 }
906 })
907 }
908}
909
910func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
911 type args struct {
912 IntfID uint32
913 OnuID uint32
914 UniID uint32
915 TpID uint32
916 }
917 tests := []struct {
918 name string
919 fields *fields
920 args args
921 wantErr error
922 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700923 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200924 2}, errors.New("failed to update resource")},
925 }
926 for _, tt := range tests {
927 t.Run(tt.name, func(t *testing.T) {
928 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530929 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
930 defer cancel()
931 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 +0200932 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
933 }
934 })
935 }
936}
937
938func TestSetKVClient(t *testing.T) {
939 type args struct {
940 backend string
Neha Sharma3f221ae2020-04-29 19:02:12 +0000941 address string
cbabuabf02352019-10-15 13:14:56 +0200942 DeviceID string
943 }
944 tests := []struct {
945 name string
946 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500947 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200948 }{
Neha Sharma3f221ae2020-04-29 19:02:12 +0000949 {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1"}, &db.Backend{}},
950 {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200951 }
952 for _, tt := range tests {
953 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000954 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 +0200955 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
956 }
957 })
958 }
959}
960
961func Test_getFlowIDFromFlowInfo(t *testing.T) {
962 type args struct {
963 FlowInfo *[]FlowInfo
964 flowID uint32
965 gemportID uint32
966 flowStoreCookie uint64
967 flowCategory string
Gamze Abaka724d0852020-03-18 12:10:24 +0000968 vlanVid uint32
cbabuabf02352019-10-15 13:14:56 +0200969 vlanPcp []uint32
970 }
971 flowInfo := &[]FlowInfo{
972 {
973 &openolt.Flow{
974 FlowId: 1,
975 GemportId: 1,
976 Classifier: &openolt.Classifier{
977 OPbits: 1,
Gamze Abaka724d0852020-03-18 12:10:24 +0000978 OVid: 33,
979 },
980 Action: &openolt.Action{
981 Cmd: &openolt.ActionCmd{
982 AddOuterTag: true,
983 },
984 OVid: 7,
985 },
986 },
cbabuabf02352019-10-15 13:14:56 +0200987 1,
988 "HSIA_FLOW",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530989 2000,
cbabuabf02352019-10-15 13:14:56 +0200990 },
991 {
992 &openolt.Flow{
993 GemportId: 1,
Gamze Abaka724d0852020-03-18 12:10:24 +0000994 Classifier: &openolt.Classifier{
995 OVid: 0,
996 },
997 Action: &openolt.Action{
998 Cmd: &openolt.ActionCmd{
999 TrapToHost: true,
1000 },
1001 },
cbabuabf02352019-10-15 13:14:56 +02001002 },
1003 1,
1004 "EAPOL",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301005 3000,
cbabuabf02352019-10-15 13:14:56 +02001006 },
1007 }
1008 tests := []struct {
1009 name string
1010 args args
1011 wantErr error
1012 }{
1013 {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
1014 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
Gamze Abaka724d0852020-03-18 12:10:24 +00001015 "HSIA_FLOW", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
cbabuabf02352019-10-15 13:14:56 +02001016 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
Gamze Abaka724d0852020-03-18 12:10:24 +00001017 "EAPOL", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
cbabuabf02352019-10-15 13:14:56 +02001018 }
1019 for _, tt := range tests {
1020 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001021 err := getFlowIDFromFlowInfo(context.Background(), tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...)
cbabuabf02352019-10-15 13:14:56 +02001022 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
1023 t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
1024 }
1025 if err == nil {
1026 t.Log("return'd nil")
1027 }
1028 })
1029 }
1030}
1031
1032func Test_newKVClient(t *testing.T) {
1033 type args struct {
1034 storeType string
1035 address string
Neha Sharmacc656962020-04-14 14:26:11 +00001036 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +02001037 }
1038 var kvClient kvstore.Client
1039 tests := []struct {
1040 name string
1041 args args
1042 want kvstore.Client
1043 wantErr error
1044 }{
1045 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
1046 }
1047 for _, tt := range tests {
1048 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +00001049 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +02001050 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
1051 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
1052 }
1053 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
1054 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
1055 return
1056 }
1057
1058 })
1059 }
1060}
Esin Karamanccb714b2019-11-29 15:02:06 +00001061
1062func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
1063 type args struct {
1064 intf uint32
1065 gem uint32
1066 servicePriority uint32
1067 }
1068 tests := []struct {
1069 name string
1070 args args
1071 fields *fields
1072 }{
1073 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1074 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1075 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1076 }
1077 for _, tt := range tests {
1078 t.Run(tt.name, func(t *testing.T) {
1079 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301080 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1081 defer cancel()
1082 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001083 if err != nil {
1084 t.Errorf("%s got err= %s wants nil", tt.name, err)
1085 return
1086 }
1087 })
1088 }
1089}
1090
1091func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1092 groupDesc := ofp.OfpGroupDesc{
1093 Type: ofp.OfpGroupType_OFPGT_ALL,
1094 GroupId: groupID,
1095 }
1096 groupEntry := ofp.OfpGroupEntry{
1097 Desc: &groupDesc,
1098 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001099 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001100 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001101 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001102 bucket := ofp.OfpBucket{
1103 Actions: acts,
1104 }
1105 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001106 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001107 return &groupEntry
1108}
1109
1110func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1111 type args struct {
1112 group *ofp.OfpGroupEntry
1113 cached bool
1114 }
1115 //create group 1
1116 group1 := newGroup(1, []uint32{1})
1117 //create group 2
1118 group2 := newGroup(2, []uint32{2})
1119 //define test set
1120 tests := []struct {
1121 name string
1122 args args
1123 fields *fields
1124 }{
1125 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1126 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1127 }
1128 //execute tests
1129 for _, tt := range tests {
1130 t.Run(tt.name, func(t *testing.T) {
1131 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301132 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1133 defer cancel()
1134 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001135 if err != nil {
1136 t.Errorf("%s got err= %s wants nil", tt.name, err)
1137 return
1138 }
1139 })
1140 }
1141}
1142
1143func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1144 type args struct {
1145 groupID uint32
1146 cached bool
1147 }
1148 //define test set
1149 tests := []struct {
1150 name string
1151 args args
1152 fields *fields
1153 }{
1154 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1155 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1156 }
1157 //execute tests
1158 for _, tt := range tests {
1159 t.Run(tt.name, func(t *testing.T) {
1160 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301161 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1162 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001163 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1164 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001165 t.Errorf("%s got false but wants true", tt.name)
1166 return
1167 }
1168 })
1169 }
1170}
1171
1172func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1173 type args struct {
1174 groupID uint32
1175 cached bool
1176 }
1177 //define test set
1178 tests := []struct {
1179 name string
1180 args args
1181 fields *fields
1182 }{
1183 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1184 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1185 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1186 }
1187 //execute tests
1188 for _, tt := range tests {
1189 t.Run(tt.name, func(t *testing.T) {
1190 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301191 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1192 defer cancel()
1193 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001194 if err != nil {
1195 t.Errorf("%s got error but wants nil error", tt.name)
1196 return
1197 } else if exists && (groupInfo.GroupID == 0) {
1198 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1199 return
1200 } else if tt.args.groupID == 3 && exists {
1201 t.Errorf("%s got true but wants false", tt.name)
1202 return
1203 }
1204 })
1205 }
1206}