blob: 4786940e515fbe4a4b528577da5d3b0ef9df0bfd [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 Gowdra3f182d82020-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() {
46 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
47}
48
49const (
50 // MeterConfig meter to extract meter
51 MeterConfig = "meter_id"
52 // TpIDSuffixPath to extract Techprofile
53 TpIDSuffixPath = "tp_id"
54 // 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 Gowdra3f182d82020-03-30 20:38:51 -070072 DeviceID string
73 HostAndPort string
74 Args string
75 KVStore *db.Backend
76 DeviceType string
77 Host string
78 Port int
79 DevInfo *openolt.DeviceInfo
80 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
81 NumOfPonPorts uint32
cbabuabf02352019-10-15 13:14:56 +020082}
cbabubef89432019-10-18 11:47:27 +020083
84// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020085type MockResKVClient struct {
86}
87
cbabubef89432019-10-18 11:47:27 +020088// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020089func getResMgr() *fields {
90 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050091 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020092 Client: &MockResKVClient{},
93 }
94 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
95 ranges := make(map[string]interface{})
96 sharedIdxByType := make(map[string]string)
97 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
98 sharedIdxByType["ONU_ID"] = "ONU_ID"
99 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
100 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
101 ranges["ONU_ID"] = uint32(0)
102 ranges["GEMPORT_ID"] = uint32(0)
103 ranges["ALLOC_ID"] = uint32(0)
104 ranges["FLOW_ID"] = uint32(0)
105 ranges["onu_id_shared"] = uint32(0)
106 ranges["alloc_id_shared"] = uint32(0)
107 ranges["gemport_id_shared"] = uint32(0)
108 ranges["flow_id_shared"] = uint32(0)
Girish Gowdra3f182d82020-03-30 20:38:51 -0700109 resMgr.NumOfPonPorts = 2
cbabuabf02352019-10-15 13:14:56 +0200110 ponMgr := &ponrmgr.PONResourceManager{
111 DeviceID: "onu-1",
112 IntfIDs: []uint32{1, 2},
sbarbaria8910ba2019-11-05 10:12:23 -0500113 KVStore: &db.Backend{
cbabuabf02352019-10-15 13:14:56 +0200114 Client: &MockResKVClient{},
115 },
116 PonResourceRanges: ranges,
117 SharedIdxByType: sharedIdxByType,
118 }
119 resMgr.ResourceMgrs[1] = ponMgr
120 resMgr.ResourceMgrs[2] = ponMgr
Girish Gowdra3f182d82020-03-30 20:38:51 -0700121
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) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000132 logger.Debugw("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) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000153 logger.Debug("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) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000162 logger.Debug("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) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000167 logger.Debug("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) {
Girish Kumar2ad402b2020-03-20 19:45:12 +0000172 logger.Debug("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
204// Reserve mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530205func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl int64) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200206 return nil, errors.New("key didn't find")
207}
208
209// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530210func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200211 return nil
212}
213
214// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530215func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200216 return nil
217}
218
219// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530220func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200221 return nil
222}
223
224// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800225func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200226 return nil
227}
228
229// AcquireLock mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530230func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout int) error {
cbabuabf02352019-10-15 13:14:56 +0200231 return nil
232}
233
234// ReleaseLock mock function implementation for KVClient
235func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
236 return nil
237}
238
239// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530240func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200241 return true
242}
243
244// CloseWatch mock function implementation for KVClient
245func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
246}
247
248// Close mock function implementation for KVClient
249func (kvclient *MockResKVClient) Close() {
250}
251
cbabubef89432019-10-18 11:47:27 +0200252// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200253func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra3f182d82020-03-30 20:38:51 -0700254 var rsrMgr = OpenOltResourceMgr{
cbabuabf02352019-10-15 13:14:56 +0200255 DeviceID: testResMgr.DeviceID,
256 HostAndPort: testResMgr.HostAndPort,
257 Args: testResMgr.Args,
258 KVStore: testResMgr.KVStore,
259 DeviceType: testResMgr.DeviceType,
260 Host: testResMgr.Host,
261 Port: testResMgr.Port,
262 DevInfo: testResMgr.DevInfo,
263 ResourceMgrs: testResMgr.ResourceMgrs,
264 }
Girish Gowdra3f182d82020-03-30 20:38:51 -0700265
266 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
267 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
268 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
269
270 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200271}
272
273func TestNewResourceMgr(t *testing.T) {
274 type args struct {
275 deviceID string
276 KVStoreHostPort string
277 kvStoreType string
278 deviceType string
279 devInfo *openolt.DeviceInfo
280 }
281 tests := []struct {
282 name string
283 args args
284 want *OpenOltResourceMgr
285 }{
286 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
287 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
288 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
289 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
290 }
291 for _, tt := range tests {
292 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530293 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
294 defer cancel()
295 if got := NewResourceMgr(ctx, tt.args.deviceID, tt.args.KVStoreHostPort, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200296 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
297 }
298 })
299 }
300}
301
302func TestOpenOltResourceMgr_Delete(t *testing.T) {
303 tests := []struct {
304 name string
305 fields *fields
306 wantErr error
307 }{
308 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
309 }
310 for _, tt := range tests {
311 t.Run(tt.name, func(t *testing.T) {
312 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530313 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
314 defer cancel()
315 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200316 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
317 }
318 })
319 }
320}
321
322func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) {
323 type args struct {
324 IntfID uint32
325 onuID int32
326 uniID int32
327 FlowID uint32
328 }
329 tests := []struct {
330 name string
331 fields *fields
332 args args
333 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700334 {"FreeFlowID-1", getResMgr(), args{1, 2, 2, 2}},
cbabuabf02352019-10-15 13:14:56 +0200335 }
336 for _, tt := range tests {
337 t.Run(tt.name, func(t *testing.T) {
338 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530339 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
340 defer cancel()
341 RsrcMgr.FreeFlowID(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
cbabuabf02352019-10-15 13:14:56 +0200342 })
343 }
344}
345
346func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) {
347
348 type args struct {
349 IntfID uint32
350 onuID uint32
351 uniID uint32
352 FlowID []uint32
353 }
354 tests := []struct {
355 name string
356 fields *fields
357 args args
358 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700359 {"FreeFlowIDs-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200360 }
361 for _, tt := range tests {
362 t.Run(tt.name, func(t *testing.T) {
363 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530364 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
365 defer cancel()
366 RsrcMgr.FreeFlowIDs(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
cbabuabf02352019-10-15 13:14:56 +0200367 })
368 }
369}
370
371func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
372 type args struct {
373 intfID uint32
374 onuID uint32
375 uniID uint32
376 }
377 tests := []struct {
378 name string
379 fields *fields
380 args args
381 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700382 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
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 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200390 })
391 }
392}
393
394func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
395 type args struct {
396 intfID uint32
397 onuID []uint32
398 }
399 tests := []struct {
400 name string
401 fields *fields
402 args args
403 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700404 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200405 }
406 for _, tt := range tests {
407 t.Run(tt.name, func(t *testing.T) {
408 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530409 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
410 defer cancel()
411 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200412 })
413 }
414}
415
416func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
417
418 type args struct {
419 intfID uint32
420 onuID uint32
421 uniID uint32
422 }
423 tests := []struct {
424 name string
425 fields *fields
426 args args
427 want uint32
428 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700429 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200430 }
431 for _, tt := range tests {
432 t.Run(tt.name, func(t *testing.T) {
433 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530434 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
435 defer cancel()
436 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 +0200437 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
438 }
439 })
440 }
441}
442
443func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
444 type args struct {
445 intfID uint32
446 onuID uint32
447 uniID uint32
448 }
449 tests := []struct {
450 name string
451 fields *fields
452 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000453 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200454 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700455 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200456 }
457 for _, tt := range tests {
458 t.Run(tt.name, func(t *testing.T) {
459 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530460 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
461 defer cancel()
462 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 +0000463 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200464 }
465 })
466 }
467}
468
469func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
470
471 type args struct {
472 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530473 ONUID int32
474 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200475 }
476 tests := []struct {
477 name string
478 fields *fields
479 args args
480 want []uint32
481 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700482 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200483 }
484 for _, tt := range tests {
485 t.Run(tt.name, func(t *testing.T) {
486 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530487 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
488 defer cancel()
489 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 +0200490 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
491 }
492 })
493 }
494}
495
496func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
497 type args struct {
498 intfID uint32
499 onuID uint32
500 uniID uint32
501 }
502 tests := []struct {
503 name string
504 fields *fields
505 args args
506 want []uint32
507 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700508 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200509 }
510 for _, tt := range tests {
511 t.Run(tt.name, func(t *testing.T) {
512 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530513 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
514 defer cancel()
515 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 +0200516 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
517 }
518 })
519 }
520}
521
522func TestOpenOltResourceMgr_GetFlowID(t *testing.T) {
523
524 type args struct {
525 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530526 ONUID int32
527 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200528 gemportID uint32
529 flowStoreCookie uint64
530 flowCategory string
531 vlanPcp []uint32
532 }
533 tests := []struct {
534 name string
535 fields *fields
536 args args
537 want uint32
538 wantErr error
539 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700540 {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200541 "HSIA", nil}, 0, errors.New("failed to get flows")},
542 }
543 for _, tt := range tests {
544 t.Run(tt.name, func(t *testing.T) {
545 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530546 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
547 defer cancel()
548 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.vlanPcp...)
cbabuabf02352019-10-15 13:14:56 +0200549 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
550 t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
551 return
552 }
553 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
554 t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
555 }
556 })
557 }
558}
559
560func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
561 type args struct {
562 ponPort uint32
563 onuID uint32
564 uniID uint32
565 NumOfPorts uint32
566 }
567 tests := []struct {
568 name string
569 fields *fields
570 args args
571 want []uint32
572 wantErr error
573 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700574 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200575 errors.New("failed to get gem port")},
576 }
577 for _, tt := range tests {
578 t.Run(tt.name, func(t *testing.T) {
579 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530580 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
581 defer cancel()
582 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200583 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
584 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
585 return
586 }
587 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
588 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
589 }
590 })
591 }
592}
593
594func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
595 type args struct {
596 Direction string
597 IntfID uint32
598 OnuID uint32
599 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000600 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200601 }
602 tests := []struct {
603 name string
604 fields *fields
605 args args
606 want *ofp.OfpMeterConfig
607 wantErr error
608 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700609 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200610 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra3f182d82020-03-30 20:38:51 -0700611 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200612 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
613 }
614 for _, tt := range tests {
615 t.Run(tt.name, func(t *testing.T) {
616 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530617 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
618 defer cancel()
619 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 +0200620 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
621 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
622 }
623 })
624 }
625}
626
627func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
628 type args struct {
629 ponIntfID uint32
630 }
631 tests := []struct {
632 name string
633 fields *fields
634 args args
635 want uint32
636 wantErr error
637 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700638 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200639 }
640 for _, tt := range tests {
641 t.Run(tt.name, func(t *testing.T) {
642 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530643 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
644 defer cancel()
645 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200646 if got != tt.want && err != nil {
647 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
648 }
649 })
650 }
651}
652
653func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
654
655 type args struct {
656 IntfID uint32
657 OnuID uint32
658 UniID uint32
659 }
660 tests := []struct {
661 name string
662 fields *fields
663 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000664 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200665 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700666 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000667 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200668 }
669 for _, tt := range tests {
670 t.Run(tt.name, func(t *testing.T) {
671 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530672 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
673 defer cancel()
674 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 +0200675 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
676 }
677 })
678 }
679}
680
681func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
682 type args struct {
683 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530684 onuID int32
685 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200686 flowStoreCookie uint64
687 }
688 tests := []struct {
689 name string
690 fields *fields
691 args args
692 want bool
693 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700694 {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false},
cbabuabf02352019-10-15 13:14:56 +0200695 }
696 for _, tt := range tests {
697 t.Run(tt.name, func(t *testing.T) {
698 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530699 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
700 defer cancel()
701 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 +0200702 t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
703 }
704 })
705 }
706}
707
708func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
709
710 type args struct {
711 Direction string
712 IntfID uint32
713 OnuID uint32
714 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000715 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200716 }
717 tests := []struct {
718 name string
719 fields *fields
720 args args
721 wantErr error
722 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000723 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200724 errors.New("failed to delete meter id %s from kvstore")},
725 }
726 for _, tt := range tests {
727 t.Run(tt.name, func(t *testing.T) {
728 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530729 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
730 defer cancel()
731 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000732 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200733 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
734 }
735 })
736 }
737}
738
739func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
740 type args struct {
741 IntfID uint32
742 OnuID uint32
743 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000744 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200745 }
746 tests := []struct {
747 name string
748 fields *fields
749 args args
750 wantErr error
751 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700752 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200753 errors.New("failed to delete techprofile id resource %s in KV store")},
754 }
755 for _, tt := range tests {
756 t.Run(tt.name, func(t *testing.T) {
757 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530758 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
759 defer cancel()
760 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000761 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200762 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
763 }
764 })
765 }
766}
767
768func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
769 type args struct {
770 ponPort uint32
771 onuID uint32
772 uniID uint32
773 allocID []uint32
774 }
775 tests := []struct {
776 name string
777 fields *fields
778 args args
779 wantErr error
780 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700781 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200782 errors.New("")},
783 }
784 for _, tt := range tests {
785 t.Run(tt.name, func(t *testing.T) {
786 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530787 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
788 defer cancel()
789 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 +0200790 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
791 }
792 })
793 }
794}
795
796func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
797 type args struct {
798 ponIntfID int32
799 onuID int32
800 uniID int32
801 flowID uint32
802 flowData *[]FlowInfo
803 }
804 tests := []struct {
805 name string
806 fields *fields
807 args args
808 wantErr error
809 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700810 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200811 }
812 for _, tt := range tests {
813 t.Run(tt.name, func(t *testing.T) {
814 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530815 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
816 defer cancel()
817 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 +0200818 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
819 }
820 })
821 }
822}
823
824func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
825
826 type args struct {
827 ponPort uint32
828 onuID uint32
829 uniID uint32
830 GEMPortList []uint32
831 }
832 tests := []struct {
833 name string
834 fields *fields
835 args args
836 wantErr error
837 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700838 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200839 []uint32{1, 2}}, errors.New("failed to update resource")},
840 }
841 for _, tt := range tests {
842 t.Run(tt.name, func(t *testing.T) {
843 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530844 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
845 defer cancel()
846 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 +0200847 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
848 }
849 })
850 }
851}
852
853func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
854 type args struct {
855 gemPorts []uint32
856 PonPort uint32
857 onuID uint32
858 uniID uint32
859 }
860 tests := []struct {
861 name string
862 fields *fields
863 args args
864 wantErr error
865 }{
866 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra3f182d82020-03-30 20:38:51 -0700867 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200868 }
869 for _, tt := range tests {
870 t.Run(tt.name, func(t *testing.T) {
871 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530872 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
873 defer cancel()
874 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000875 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200876 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
877 }
878 })
879 }
880}
881
882func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
883 type args struct {
884 Direction string
885 IntfID uint32
886 OnuID uint32
887 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000888 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200889 MeterConfig *ofp.OfpMeterConfig
890 }
891 tests := []struct {
892 name string
893 fields *fields
894 args args
895 wantErr error
896 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700897 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000898 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200899 }
900 for _, tt := range tests {
901 t.Run(tt.name, func(t *testing.T) {
902 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530903 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
904 defer cancel()
905 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000906 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200907 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
908 }
909 })
910 }
911}
912
913func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
914 type args struct {
915 IntfID uint32
916 OnuID uint32
917 UniID uint32
918 TpID uint32
919 }
920 tests := []struct {
921 name string
922 fields *fields
923 args args
924 wantErr error
925 }{
Girish Gowdra3f182d82020-03-30 20:38:51 -0700926 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200927 2}, errors.New("failed to update resource")},
928 }
929 for _, tt := range tests {
930 t.Run(tt.name, func(t *testing.T) {
931 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530932 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
933 defer cancel()
934 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 +0200935 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
936 }
937 })
938 }
939}
940
941func TestSetKVClient(t *testing.T) {
942 type args struct {
943 backend string
944 Host string
945 Port int
946 DeviceID string
947 }
948 tests := []struct {
949 name string
950 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500951 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200952 }{
sbarbaria8910ba2019-11-05 10:12:23 -0500953 {"setKVClient-1", args{"consul", "1.1.1.1", 1, "olt1"}, &db.Backend{}},
954 {"setKVClient-1", args{"etcd", "2.2.2.2", 2, "olt2"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200955 }
956 for _, tt := range tests {
957 t.Run(tt.name, func(t *testing.T) {
958 if got := SetKVClient(tt.args.backend, tt.args.Host, tt.args.Port, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
959 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
960 }
961 })
962 }
963}
964
965func Test_getFlowIDFromFlowInfo(t *testing.T) {
966 type args struct {
967 FlowInfo *[]FlowInfo
968 flowID uint32
969 gemportID uint32
970 flowStoreCookie uint64
971 flowCategory string
972 vlanPcp []uint32
973 }
974 flowInfo := &[]FlowInfo{
975 {
976 &openolt.Flow{
977 FlowId: 1,
978 GemportId: 1,
979 Classifier: &openolt.Classifier{
980 OPbits: 1,
981 }},
982 1,
983 "HSIA_FLOW",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530984 2000,
cbabuabf02352019-10-15 13:14:56 +0200985 },
986 {
987 &openolt.Flow{
988 GemportId: 1,
989 },
990 1,
991 "EAPOL",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530992 3000,
cbabuabf02352019-10-15 13:14:56 +0200993 },
994 }
995 tests := []struct {
996 name string
997 args args
998 wantErr error
999 }{
1000 {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
1001 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
1002 "HSIA_FLOW", []uint32{1, 2}}, errors.New("invalid flow-info")},
1003 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
1004 "EAPOL", []uint32{1, 2}}, errors.New("invalid flow-info")},
1005 }
1006 for _, tt := range tests {
1007 t.Run(tt.name, func(t *testing.T) {
1008 err := getFlowIDFromFlowInfo(tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...)
1009 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
1010 t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
1011 }
1012 if err == nil {
1013 t.Log("return'd nil")
1014 }
1015 })
1016 }
1017}
1018
1019func Test_newKVClient(t *testing.T) {
1020 type args struct {
1021 storeType string
1022 address string
1023 timeout uint32
1024 }
1025 var kvClient kvstore.Client
1026 tests := []struct {
1027 name string
1028 args args
1029 want kvstore.Client
1030 wantErr error
1031 }{
1032 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
1033 }
1034 for _, tt := range tests {
1035 t.Run(tt.name, func(t *testing.T) {
1036 got, err := newKVClient(tt.args.storeType, tt.args.address, tt.args.timeout)
1037 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
1038 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
1039 }
1040 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
1041 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
1042 return
1043 }
1044
1045 })
1046 }
1047}
Esin Karamanccb714b2019-11-29 15:02:06 +00001048
1049func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
1050 type args struct {
1051 intf uint32
1052 gem uint32
1053 servicePriority uint32
1054 }
1055 tests := []struct {
1056 name string
1057 args args
1058 fields *fields
1059 }{
1060 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1061 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1062 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1063 }
1064 for _, tt := range tests {
1065 t.Run(tt.name, func(t *testing.T) {
1066 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301067 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1068 defer cancel()
1069 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001070 if err != nil {
1071 t.Errorf("%s got err= %s wants nil", tt.name, err)
1072 return
1073 }
1074 })
1075 }
1076}
1077
1078func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1079 groupDesc := ofp.OfpGroupDesc{
1080 Type: ofp.OfpGroupType_OFPGT_ALL,
1081 GroupId: groupID,
1082 }
1083 groupEntry := ofp.OfpGroupEntry{
1084 Desc: &groupDesc,
1085 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001086 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001087 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001088 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001089 bucket := ofp.OfpBucket{
1090 Actions: acts,
1091 }
1092 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001093 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001094 return &groupEntry
1095}
1096
1097func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1098 type args struct {
1099 group *ofp.OfpGroupEntry
1100 cached bool
1101 }
1102 //create group 1
1103 group1 := newGroup(1, []uint32{1})
1104 //create group 2
1105 group2 := newGroup(2, []uint32{2})
1106 //define test set
1107 tests := []struct {
1108 name string
1109 args args
1110 fields *fields
1111 }{
1112 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1113 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1114 }
1115 //execute tests
1116 for _, tt := range tests {
1117 t.Run(tt.name, func(t *testing.T) {
1118 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301119 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1120 defer cancel()
1121 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001122 if err != nil {
1123 t.Errorf("%s got err= %s wants nil", tt.name, err)
1124 return
1125 }
1126 })
1127 }
1128}
1129
1130func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1131 type args struct {
1132 groupID uint32
1133 cached bool
1134 }
1135 //define test set
1136 tests := []struct {
1137 name string
1138 args args
1139 fields *fields
1140 }{
1141 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1142 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1143 }
1144 //execute tests
1145 for _, tt := range tests {
1146 t.Run(tt.name, func(t *testing.T) {
1147 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301148 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1149 defer cancel()
1150 success := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001151 if !success {
1152 t.Errorf("%s got false but wants true", tt.name)
1153 return
1154 }
1155 })
1156 }
1157}
1158
1159func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1160 type args struct {
1161 groupID uint32
1162 cached bool
1163 }
1164 //define test set
1165 tests := []struct {
1166 name string
1167 args args
1168 fields *fields
1169 }{
1170 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1171 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1172 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1173 }
1174 //execute tests
1175 for _, tt := range tests {
1176 t.Run(tt.name, func(t *testing.T) {
1177 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301178 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1179 defer cancel()
1180 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001181 if err != nil {
1182 t.Errorf("%s got error but wants nil error", tt.name)
1183 return
1184 } else if exists && (groupInfo.GroupID == 0) {
1185 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1186 return
1187 } else if tt.args.groupID == 3 && exists {
1188 t.Errorf("%s got true but wants false", tt.name)
1189 return
1190 }
1191 })
1192 }
1193}