blob: 18074cc24c7898c42bd5208bf5376d2de45c32e1 [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() {
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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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 Gowdra38d533d2020-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
Gamze Abaka724d0852020-03-18 12:10:24 +0000531 vlanVid uint32
cbabuabf02352019-10-15 13:14:56 +0200532 vlanPcp []uint32
533 }
534 tests := []struct {
535 name string
536 fields *fields
537 args args
538 want uint32
539 wantErr error
540 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700541 {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2,
Gamze Abaka724d0852020-03-18 12:10:24 +0000542 "HSIA", 33, nil}, 0, errors.New("failed to get flows")},
cbabuabf02352019-10-15 13:14:56 +0200543 }
544 for _, tt := range tests {
545 t.Run(tt.name, func(t *testing.T) {
546 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530547 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
548 defer cancel()
Gamze Abaka724d0852020-03-18 12:10:24 +0000549 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 +0200550 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
551 t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
552 return
553 }
554 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
555 t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
556 }
557 })
558 }
559}
560
561func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
562 type args struct {
563 ponPort uint32
564 onuID uint32
565 uniID uint32
566 NumOfPorts uint32
567 }
568 tests := []struct {
569 name string
570 fields *fields
571 args args
572 want []uint32
573 wantErr error
574 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700575 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200576 errors.New("failed to get gem port")},
577 }
578 for _, tt := range tests {
579 t.Run(tt.name, func(t *testing.T) {
580 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530581 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
582 defer cancel()
583 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200584 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
585 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
586 return
587 }
588 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
589 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
590 }
591 })
592 }
593}
594
595func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
596 type args struct {
597 Direction string
598 IntfID uint32
599 OnuID uint32
600 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000601 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200602 }
603 tests := []struct {
604 name string
605 fields *fields
606 args args
607 want *ofp.OfpMeterConfig
608 wantErr error
609 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700610 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200611 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700612 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200613 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
614 }
615 for _, tt := range tests {
616 t.Run(tt.name, func(t *testing.T) {
617 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530618 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
619 defer cancel()
620 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 +0200621 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
622 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
623 }
624 })
625 }
626}
627
628func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
629 type args struct {
630 ponIntfID uint32
631 }
632 tests := []struct {
633 name string
634 fields *fields
635 args args
636 want uint32
637 wantErr error
638 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700639 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200640 }
641 for _, tt := range tests {
642 t.Run(tt.name, func(t *testing.T) {
643 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530644 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
645 defer cancel()
646 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200647 if got != tt.want && err != nil {
648 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
649 }
650 })
651 }
652}
653
654func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
655
656 type args struct {
657 IntfID uint32
658 OnuID uint32
659 UniID uint32
660 }
661 tests := []struct {
662 name string
663 fields *fields
664 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000665 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200666 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700667 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000668 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200669 }
670 for _, tt := range tests {
671 t.Run(tt.name, func(t *testing.T) {
672 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530673 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
674 defer cancel()
675 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 +0200676 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
677 }
678 })
679 }
680}
681
682func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
683 type args struct {
684 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530685 onuID int32
686 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200687 flowStoreCookie uint64
688 }
689 tests := []struct {
690 name string
691 fields *fields
692 args args
693 want bool
694 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700695 {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false},
cbabuabf02352019-10-15 13:14:56 +0200696 }
697 for _, tt := range tests {
698 t.Run(tt.name, func(t *testing.T) {
699 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530700 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
701 defer cancel()
702 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 +0200703 t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
704 }
705 })
706 }
707}
708
709func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
710
711 type args struct {
712 Direction string
713 IntfID uint32
714 OnuID uint32
715 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000716 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200717 }
718 tests := []struct {
719 name string
720 fields *fields
721 args args
722 wantErr error
723 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000724 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200725 errors.New("failed to delete meter id %s from kvstore")},
726 }
727 for _, tt := range tests {
728 t.Run(tt.name, func(t *testing.T) {
729 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530730 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
731 defer cancel()
732 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000733 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200734 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
735 }
736 })
737 }
738}
739
740func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
741 type args struct {
742 IntfID uint32
743 OnuID uint32
744 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000745 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200746 }
747 tests := []struct {
748 name string
749 fields *fields
750 args args
751 wantErr error
752 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700753 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200754 errors.New("failed to delete techprofile id resource %s in KV store")},
755 }
756 for _, tt := range tests {
757 t.Run(tt.name, func(t *testing.T) {
758 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530759 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
760 defer cancel()
761 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000762 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200763 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
764 }
765 })
766 }
767}
768
769func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
770 type args struct {
771 ponPort uint32
772 onuID uint32
773 uniID uint32
774 allocID []uint32
775 }
776 tests := []struct {
777 name string
778 fields *fields
779 args args
780 wantErr error
781 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700782 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200783 errors.New("")},
784 }
785 for _, tt := range tests {
786 t.Run(tt.name, func(t *testing.T) {
787 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530788 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
789 defer cancel()
790 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 +0200791 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
792 }
793 })
794 }
795}
796
797func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
798 type args struct {
799 ponIntfID int32
800 onuID int32
801 uniID int32
802 flowID uint32
803 flowData *[]FlowInfo
804 }
805 tests := []struct {
806 name string
807 fields *fields
808 args args
809 wantErr error
810 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700811 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200812 }
813 for _, tt := range tests {
814 t.Run(tt.name, func(t *testing.T) {
815 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530816 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
817 defer cancel()
818 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 +0200819 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
820 }
821 })
822 }
823}
824
825func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
826
827 type args struct {
828 ponPort uint32
829 onuID uint32
830 uniID uint32
831 GEMPortList []uint32
832 }
833 tests := []struct {
834 name string
835 fields *fields
836 args args
837 wantErr error
838 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700839 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200840 []uint32{1, 2}}, errors.New("failed to update resource")},
841 }
842 for _, tt := range tests {
843 t.Run(tt.name, func(t *testing.T) {
844 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530845 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
846 defer cancel()
847 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 +0200848 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
849 }
850 })
851 }
852}
853
854func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
855 type args struct {
856 gemPorts []uint32
857 PonPort uint32
858 onuID uint32
859 uniID uint32
860 }
861 tests := []struct {
862 name string
863 fields *fields
864 args args
865 wantErr error
866 }{
867 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700868 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200869 }
870 for _, tt := range tests {
871 t.Run(tt.name, func(t *testing.T) {
872 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530873 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
874 defer cancel()
875 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000876 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200877 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
878 }
879 })
880 }
881}
882
883func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
884 type args struct {
885 Direction string
886 IntfID uint32
887 OnuID uint32
888 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000889 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200890 MeterConfig *ofp.OfpMeterConfig
891 }
892 tests := []struct {
893 name string
894 fields *fields
895 args args
896 wantErr error
897 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700898 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000899 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200900 }
901 for _, tt := range tests {
902 t.Run(tt.name, func(t *testing.T) {
903 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530904 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
905 defer cancel()
906 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000907 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200908 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
909 }
910 })
911 }
912}
913
914func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
915 type args struct {
916 IntfID uint32
917 OnuID uint32
918 UniID uint32
919 TpID uint32
920 }
921 tests := []struct {
922 name string
923 fields *fields
924 args args
925 wantErr error
926 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700927 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200928 2}, errors.New("failed to update resource")},
929 }
930 for _, tt := range tests {
931 t.Run(tt.name, func(t *testing.T) {
932 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530933 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
934 defer cancel()
935 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 +0200936 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
937 }
938 })
939 }
940}
941
942func TestSetKVClient(t *testing.T) {
943 type args struct {
944 backend string
945 Host string
946 Port int
947 DeviceID string
948 }
949 tests := []struct {
950 name string
951 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500952 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200953 }{
sbarbaria8910ba2019-11-05 10:12:23 -0500954 {"setKVClient-1", args{"consul", "1.1.1.1", 1, "olt1"}, &db.Backend{}},
955 {"setKVClient-1", args{"etcd", "2.2.2.2", 2, "olt2"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200956 }
957 for _, tt := range tests {
958 t.Run(tt.name, func(t *testing.T) {
959 if got := SetKVClient(tt.args.backend, tt.args.Host, tt.args.Port, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
960 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
961 }
962 })
963 }
964}
965
966func Test_getFlowIDFromFlowInfo(t *testing.T) {
967 type args struct {
968 FlowInfo *[]FlowInfo
969 flowID uint32
970 gemportID uint32
971 flowStoreCookie uint64
972 flowCategory string
Gamze Abaka724d0852020-03-18 12:10:24 +0000973 vlanVid uint32
cbabuabf02352019-10-15 13:14:56 +0200974 vlanPcp []uint32
975 }
976 flowInfo := &[]FlowInfo{
977 {
978 &openolt.Flow{
979 FlowId: 1,
980 GemportId: 1,
981 Classifier: &openolt.Classifier{
982 OPbits: 1,
Gamze Abaka724d0852020-03-18 12:10:24 +0000983 OVid: 33,
984 },
985 Action: &openolt.Action{
986 Cmd: &openolt.ActionCmd{
987 AddOuterTag: true,
988 },
989 OVid: 7,
990 },
991 },
cbabuabf02352019-10-15 13:14:56 +0200992 1,
993 "HSIA_FLOW",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530994 2000,
cbabuabf02352019-10-15 13:14:56 +0200995 },
996 {
997 &openolt.Flow{
998 GemportId: 1,
Gamze Abaka724d0852020-03-18 12:10:24 +0000999 Classifier: &openolt.Classifier{
1000 OVid: 0,
1001 },
1002 Action: &openolt.Action{
1003 Cmd: &openolt.ActionCmd{
1004 TrapToHost: true,
1005 },
1006 },
cbabuabf02352019-10-15 13:14:56 +02001007 },
1008 1,
1009 "EAPOL",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +05301010 3000,
cbabuabf02352019-10-15 13:14:56 +02001011 },
1012 }
1013 tests := []struct {
1014 name string
1015 args args
1016 wantErr error
1017 }{
1018 {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
1019 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
Gamze Abaka724d0852020-03-18 12:10:24 +00001020 "HSIA_FLOW", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
cbabuabf02352019-10-15 13:14:56 +02001021 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
Gamze Abaka724d0852020-03-18 12:10:24 +00001022 "EAPOL", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
cbabuabf02352019-10-15 13:14:56 +02001023 }
1024 for _, tt := range tests {
1025 t.Run(tt.name, func(t *testing.T) {
Gamze Abaka724d0852020-03-18 12:10:24 +00001026 err := getFlowIDFromFlowInfo(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 +02001027 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
1028 t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
1029 }
1030 if err == nil {
1031 t.Log("return'd nil")
1032 }
1033 })
1034 }
1035}
1036
1037func Test_newKVClient(t *testing.T) {
1038 type args struct {
1039 storeType string
1040 address string
1041 timeout uint32
1042 }
1043 var kvClient kvstore.Client
1044 tests := []struct {
1045 name string
1046 args args
1047 want kvstore.Client
1048 wantErr error
1049 }{
1050 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
1051 }
1052 for _, tt := range tests {
1053 t.Run(tt.name, func(t *testing.T) {
1054 got, err := newKVClient(tt.args.storeType, tt.args.address, tt.args.timeout)
1055 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
1056 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
1057 }
1058 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
1059 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
1060 return
1061 }
1062
1063 })
1064 }
1065}
Esin Karamanccb714b2019-11-29 15:02:06 +00001066
1067func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
1068 type args struct {
1069 intf uint32
1070 gem uint32
1071 servicePriority uint32
1072 }
1073 tests := []struct {
1074 name string
1075 args args
1076 fields *fields
1077 }{
1078 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1079 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1080 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1081 }
1082 for _, tt := range tests {
1083 t.Run(tt.name, func(t *testing.T) {
1084 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301085 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1086 defer cancel()
1087 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001088 if err != nil {
1089 t.Errorf("%s got err= %s wants nil", tt.name, err)
1090 return
1091 }
1092 })
1093 }
1094}
1095
1096func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1097 groupDesc := ofp.OfpGroupDesc{
1098 Type: ofp.OfpGroupType_OFPGT_ALL,
1099 GroupId: groupID,
1100 }
1101 groupEntry := ofp.OfpGroupEntry{
1102 Desc: &groupDesc,
1103 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001104 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001105 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001106 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001107 bucket := ofp.OfpBucket{
1108 Actions: acts,
1109 }
1110 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001111 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001112 return &groupEntry
1113}
1114
1115func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1116 type args struct {
1117 group *ofp.OfpGroupEntry
1118 cached bool
1119 }
1120 //create group 1
1121 group1 := newGroup(1, []uint32{1})
1122 //create group 2
1123 group2 := newGroup(2, []uint32{2})
1124 //define test set
1125 tests := []struct {
1126 name string
1127 args args
1128 fields *fields
1129 }{
1130 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1131 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1132 }
1133 //execute tests
1134 for _, tt := range tests {
1135 t.Run(tt.name, func(t *testing.T) {
1136 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301137 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1138 defer cancel()
1139 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001140 if err != nil {
1141 t.Errorf("%s got err= %s wants nil", tt.name, err)
1142 return
1143 }
1144 })
1145 }
1146}
1147
1148func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1149 type args struct {
1150 groupID uint32
1151 cached bool
1152 }
1153 //define test set
1154 tests := []struct {
1155 name string
1156 args args
1157 fields *fields
1158 }{
1159 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1160 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1161 }
1162 //execute tests
1163 for _, tt := range tests {
1164 t.Run(tt.name, func(t *testing.T) {
1165 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301166 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1167 defer cancel()
1168 success := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001169 if !success {
1170 t.Errorf("%s got false but wants true", tt.name)
1171 return
1172 }
1173 })
1174 }
1175}
1176
1177func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1178 type args struct {
1179 groupID uint32
1180 cached bool
1181 }
1182 //define test set
1183 tests := []struct {
1184 name string
1185 args args
1186 fields *fields
1187 }{
1188 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1189 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1190 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1191 }
1192 //execute tests
1193 for _, tt := range tests {
1194 t.Run(tt.name, func(t *testing.T) {
1195 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301196 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1197 defer cancel()
1198 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001199 if err != nil {
1200 t.Errorf("%s got error but wants nil error", tt.name)
1201 return
1202 } else if exists && (groupInfo.GroupID == 0) {
1203 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1204 return
1205 } else if tt.args.groupID == 3 && exists {
1206 t.Errorf("%s got true but wants false", tt.name)
1207 return
1208 }
1209 })
1210 }
1211}