blob: 5d96cc30ae9aad66bcee6cc37c33c3fdecf90e4b [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"
40 "testing"
npujarec5762e2020-01-01 14:08:48 +053041 "time"
cbabuabf02352019-10-15 13:14:56 +020042)
43
44func init() {
45 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
46}
47
48const (
49 // MeterConfig meter to extract meter
50 MeterConfig = "meter_id"
51 // TpIDSuffixPath to extract Techprofile
52 TpIDSuffixPath = "tp_id"
53 // FlowIDInfo to extract flows
54 FlowIDInfo = "flow_id_info"
55 // FlowIds to extract flows
56 FlowIDs = "flow_ids"
57 // GemportIDs to gemport_ids
58 GemportIDs = "gemport_ids"
59 // AllocIDs to extract alloc_ids
60 AllocIDs = "alloc_ids"
61 // GemportIDPool to extract gemport
62 GemportIDPool = "gemport_id_pool"
63 // AllocIDPool to extract allocid
64 AllocIDPool = "alloc_id_pool"
65 // FlowIDpool to extract Flow ids
66 FlowIDpool = "flow_id_pool"
67)
68
cbabubef89432019-10-18 11:47:27 +020069// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020070type fields struct {
71 DeviceID string
72 HostAndPort string
73 Args string
sbarbaria8910ba2019-11-05 10:12:23 -050074 KVStore *db.Backend
cbabuabf02352019-10-15 13:14:56 +020075 DeviceType string
76 Host string
77 Port int
78 DevInfo *openolt.DeviceInfo
79 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
80}
cbabubef89432019-10-18 11:47:27 +020081
82// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020083type MockResKVClient struct {
84}
85
cbabubef89432019-10-18 11:47:27 +020086// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020087func getResMgr() *fields {
88 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050089 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020090 Client: &MockResKVClient{},
91 }
92 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
93 ranges := make(map[string]interface{})
94 sharedIdxByType := make(map[string]string)
95 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
96 sharedIdxByType["ONU_ID"] = "ONU_ID"
97 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
98 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
99 ranges["ONU_ID"] = uint32(0)
100 ranges["GEMPORT_ID"] = uint32(0)
101 ranges["ALLOC_ID"] = uint32(0)
102 ranges["FLOW_ID"] = uint32(0)
103 ranges["onu_id_shared"] = uint32(0)
104 ranges["alloc_id_shared"] = uint32(0)
105 ranges["gemport_id_shared"] = uint32(0)
106 ranges["flow_id_shared"] = uint32(0)
107 ponMgr := &ponrmgr.PONResourceManager{
108 DeviceID: "onu-1",
109 IntfIDs: []uint32{1, 2},
sbarbaria8910ba2019-11-05 10:12:23 -0500110 KVStore: &db.Backend{
cbabuabf02352019-10-15 13:14:56 +0200111 Client: &MockResKVClient{},
112 },
113 PonResourceRanges: ranges,
114 SharedIdxByType: sharedIdxByType,
115 }
116 resMgr.ResourceMgrs[1] = ponMgr
117 resMgr.ResourceMgrs[2] = ponMgr
118 return &resMgr
119}
cbabubef89432019-10-18 11:47:27 +0200120
121// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530122func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200123 return nil, errors.New("key didn't find")
124}
125
126// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530127func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200128 log.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
129 if key != "" {
130 if strings.Contains(key, MeterConfig) {
131 var bands []*ofp.OfpMeterBandHeader
132 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
133 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
134
135 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
136 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
137
Gamze Abakafee36392019-10-03 11:17:24 +0000138 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200139 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
140 if uint32(val) > 1 {
141 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
142 str, _ := json.Marshal(meterConfig)
143
144 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
145 }
146 return nil, errors.New("invalid meter")
147 }
148 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
149 log.Debug("Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
150 data := make(map[string]interface{})
151 data["pool"] = "1024"
152 data["start_idx"] = 1
153 data["end_idx"] = 1024
154 str, _ := json.Marshal(data)
155 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
156 }
157 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
158 log.Debug("Error Error Error Key:", FlowIDs, FlowIDInfo)
159 str, _ := json.Marshal([]uint32{1, 2})
160 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
161 }
162 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
163 log.Debug("Error Error Error Key:", AllocIDs, GemportIDs)
164 str, _ := json.Marshal(1)
165 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
166 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000167 if strings.Contains(key, McastQueuesForIntf) {
168 log.Debug("Error Error Error Key:", McastQueuesForIntf)
169 mcastQueues := make(map[uint32][]uint32)
170 mcastQueues[10] = []uint32{4000, 0}
171 str, _ := json.Marshal(mcastQueues)
172 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
173 }
174 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
175 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
176 str, _ := json.Marshal(groupInfo)
177 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
178 }
179
cbabuabf02352019-10-15 13:14:56 +0200180 maps := make(map[string]*kvstore.KVPair)
181 maps[key] = &kvstore.KVPair{Key: key}
182 return maps[key], nil
183 }
184 return nil, errors.New("key didn't find")
185}
186
187// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530188func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200189 if key != "" {
190 return nil
191 }
192 return errors.New("key didn't find")
193}
194
195// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530196func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200197 return nil
198}
199
200// Reserve mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530201func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl int64) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200202 return nil, errors.New("key didn't find")
203}
204
205// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530206func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200207 return nil
208}
209
210// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530211func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200212 return nil
213}
214
215// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530216func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200217 return nil
218}
219
220// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800221func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200222 return nil
223}
224
225// AcquireLock mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530226func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout int) error {
cbabuabf02352019-10-15 13:14:56 +0200227 return nil
228}
229
230// ReleaseLock mock function implementation for KVClient
231func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
232 return nil
233}
234
235// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530236func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200237 return true
238}
239
240// CloseWatch mock function implementation for KVClient
241func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
242}
243
244// Close mock function implementation for KVClient
245func (kvclient *MockResKVClient) Close() {
246}
247
cbabubef89432019-10-18 11:47:27 +0200248// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200249func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
250 return &OpenOltResourceMgr{
251 DeviceID: testResMgr.DeviceID,
252 HostAndPort: testResMgr.HostAndPort,
253 Args: testResMgr.Args,
254 KVStore: testResMgr.KVStore,
255 DeviceType: testResMgr.DeviceType,
256 Host: testResMgr.Host,
257 Port: testResMgr.Port,
258 DevInfo: testResMgr.DevInfo,
259 ResourceMgrs: testResMgr.ResourceMgrs,
260 }
261}
262
263func TestNewResourceMgr(t *testing.T) {
264 type args struct {
265 deviceID string
266 KVStoreHostPort string
267 kvStoreType string
268 deviceType string
269 devInfo *openolt.DeviceInfo
270 }
271 tests := []struct {
272 name string
273 args args
274 want *OpenOltResourceMgr
275 }{
276 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
277 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
278 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
279 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
280 }
281 for _, tt := range tests {
282 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530283 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
284 defer cancel()
285 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 +0200286 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
287 }
288 })
289 }
290}
291
292func TestOpenOltResourceMgr_Delete(t *testing.T) {
293 tests := []struct {
294 name string
295 fields *fields
296 wantErr error
297 }{
298 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
299 }
300 for _, tt := range tests {
301 t.Run(tt.name, func(t *testing.T) {
302 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530303 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
304 defer cancel()
305 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200306 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
307 }
308 })
309 }
310}
311
312func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) {
313 type args struct {
314 IntfID uint32
315 onuID int32
316 uniID int32
317 FlowID uint32
318 }
319 tests := []struct {
320 name string
321 fields *fields
322 args args
323 }{
324 {"FreeFlowID-1", getResMgr(), args{2, 2, 2, 2}},
325 }
326 for _, tt := range tests {
327 t.Run(tt.name, func(t *testing.T) {
328 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530329 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
330 defer cancel()
331 RsrcMgr.FreeFlowID(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
cbabuabf02352019-10-15 13:14:56 +0200332 })
333 }
334}
335
336func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) {
337
338 type args struct {
339 IntfID uint32
340 onuID uint32
341 uniID uint32
342 FlowID []uint32
343 }
344 tests := []struct {
345 name string
346 fields *fields
347 args args
348 }{
349 {"FreeFlowIDs-1", getResMgr(), args{2, 2, 2, []uint32{1, 2}}},
350 }
351 for _, tt := range tests {
352 t.Run(tt.name, func(t *testing.T) {
353 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530354 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
355 defer cancel()
356 RsrcMgr.FreeFlowIDs(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
cbabuabf02352019-10-15 13:14:56 +0200357 })
358 }
359}
360
361func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
362 type args struct {
363 intfID uint32
364 onuID uint32
365 uniID uint32
366 }
367 tests := []struct {
368 name string
369 fields *fields
370 args args
371 }{
372 {"FreePONResourcesForONU-1", getResMgr(), args{2, 0, 2}},
373 }
374 for _, tt := range tests {
375 t.Run(tt.name, func(t *testing.T) {
376 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530377 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
378 defer cancel()
379 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200380 })
381 }
382}
383
384func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
385 type args struct {
386 intfID uint32
387 onuID []uint32
388 }
389 tests := []struct {
390 name string
391 fields *fields
392 args args
393 }{
394 {"FreeOnuID-1", getResMgr(), args{2, []uint32{1, 2}}},
395 }
396 for _, tt := range tests {
397 t.Run(tt.name, func(t *testing.T) {
398 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530399 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
400 defer cancel()
401 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200402 })
403 }
404}
405
406func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
407
408 type args struct {
409 intfID uint32
410 onuID uint32
411 uniID uint32
412 }
413 tests := []struct {
414 name string
415 fields *fields
416 args args
417 want uint32
418 }{
419 {"GetAllocID-1", getResMgr(), args{2, 2, 2}, 0},
420 }
421 for _, tt := range tests {
422 t.Run(tt.name, func(t *testing.T) {
423 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530424 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
425 defer cancel()
426 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 +0200427 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
428 }
429 })
430 }
431}
432
433func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
434 type args struct {
435 intfID uint32
436 onuID uint32
437 uniID uint32
438 }
439 tests := []struct {
440 name string
441 fields *fields
442 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000443 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200444 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000445 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200446 }
447 for _, tt := range tests {
448 t.Run(tt.name, func(t *testing.T) {
449 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530450 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
451 defer cancel()
452 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 +0000453 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200454 }
455 })
456 }
457}
458
459func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
460
461 type args struct {
462 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530463 ONUID int32
464 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200465 }
466 tests := []struct {
467 name string
468 fields *fields
469 args args
470 want []uint32
471 }{
472 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}},
473 }
474 for _, tt := range tests {
475 t.Run(tt.name, func(t *testing.T) {
476 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530477 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
478 defer cancel()
479 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 +0200480 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
481 }
482 })
483 }
484}
485
486func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
487 type args struct {
488 intfID uint32
489 onuID uint32
490 uniID uint32
491 }
492 tests := []struct {
493 name string
494 fields *fields
495 args args
496 want []uint32
497 }{
498 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}},
499 }
500 for _, tt := range tests {
501 t.Run(tt.name, func(t *testing.T) {
502 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530503 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
504 defer cancel()
505 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 +0200506 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
507 }
508 })
509 }
510}
511
512func TestOpenOltResourceMgr_GetFlowID(t *testing.T) {
513
514 type args struct {
515 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530516 ONUID int32
517 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200518 gemportID uint32
519 flowStoreCookie uint64
520 flowCategory string
521 vlanPcp []uint32
522 }
523 tests := []struct {
524 name string
525 fields *fields
526 args args
527 want uint32
528 wantErr error
529 }{
530 {"GetFlowID-1", getResMgr(), args{2, 2, 2, 2, 2,
531 "HSIA", nil}, 0, errors.New("failed to get flows")},
532 }
533 for _, tt := range tests {
534 t.Run(tt.name, func(t *testing.T) {
535 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530536 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
537 defer cancel()
538 got, err := RsrcMgr.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 +0200539 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
540 t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
541 return
542 }
543 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
544 t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
545 }
546 })
547 }
548}
549
550func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
551 type args struct {
552 ponPort uint32
553 onuID uint32
554 uniID uint32
555 NumOfPorts uint32
556 }
557 tests := []struct {
558 name string
559 fields *fields
560 args args
561 want []uint32
562 wantErr error
563 }{
564 {"GetGEMPortID-1", getResMgr(), args{2, 2, 2, 2}, []uint32{},
565 errors.New("failed to get gem port")},
566 }
567 for _, tt := range tests {
568 t.Run(tt.name, func(t *testing.T) {
569 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530570 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
571 defer cancel()
572 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200573 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
574 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
575 return
576 }
577 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
578 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
579 }
580 })
581 }
582}
583
584func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
585 type args struct {
586 Direction string
587 IntfID uint32
588 OnuID uint32
589 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000590 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200591 }
592 tests := []struct {
593 name string
594 fields *fields
595 args args
596 want *ofp.OfpMeterConfig
597 wantErr error
598 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000599 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200600 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Gamze Abakafee36392019-10-03 11:17:24 +0000601 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 2, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200602 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
603 }
604 for _, tt := range tests {
605 t.Run(tt.name, func(t *testing.T) {
606 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530607 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
608 defer cancel()
609 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 +0200610 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
611 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
612 }
613 })
614 }
615}
616
617func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
618 type args struct {
619 ponIntfID uint32
620 }
621 tests := []struct {
622 name string
623 fields *fields
624 args args
625 want uint32
626 wantErr error
627 }{
628 {"GetONUID-1", getResMgr(), args{2}, uint32(0), errors.New("json errors")},
629 }
630 for _, tt := range tests {
631 t.Run(tt.name, func(t *testing.T) {
632 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530633 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
634 defer cancel()
635 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200636 if got != tt.want && err != nil {
637 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
638 }
639 })
640 }
641}
642
643func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
644
645 type args struct {
646 IntfID uint32
647 OnuID uint32
648 UniID uint32
649 }
650 tests := []struct {
651 name string
652 fields *fields
653 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000654 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200655 }{
656 {"GetTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000657 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200658 }
659 for _, tt := range tests {
660 t.Run(tt.name, func(t *testing.T) {
661 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530662 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
663 defer cancel()
664 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 +0200665 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
666 }
667 })
668 }
669}
670
671func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
672 type args struct {
673 ponIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530674 onuID int32
675 uniID int32
cbabuabf02352019-10-15 13:14:56 +0200676 flowStoreCookie uint64
677 }
678 tests := []struct {
679 name string
680 fields *fields
681 args args
682 want bool
683 }{
684 {"IsFlowCookieOnKVStore-1", getResMgr(), args{2, 2, 2, 2}, false},
685 }
686 for _, tt := range tests {
687 t.Run(tt.name, func(t *testing.T) {
688 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530689 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
690 defer cancel()
691 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 +0200692 t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
693 }
694 })
695 }
696}
697
698func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
699
700 type args struct {
701 Direction string
702 IntfID uint32
703 OnuID uint32
704 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000705 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200706 }
707 tests := []struct {
708 name string
709 fields *fields
710 args args
711 wantErr error
712 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000713 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200714 errors.New("failed to delete meter id %s from kvstore")},
715 }
716 for _, tt := range tests {
717 t.Run(tt.name, func(t *testing.T) {
718 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530719 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
720 defer cancel()
721 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000722 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200723 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
724 }
725 })
726 }
727}
728
729func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
730 type args struct {
731 IntfID uint32
732 OnuID uint32
733 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000734 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200735 }
736 tests := []struct {
737 name string
738 fields *fields
739 args args
740 wantErr error
741 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000742 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200743 errors.New("failed to delete techprofile id resource %s in KV store")},
744 }
745 for _, tt := range tests {
746 t.Run(tt.name, func(t *testing.T) {
747 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530748 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
749 defer cancel()
750 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000751 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200752 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
753 }
754 })
755 }
756}
757
758func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
759 type args struct {
760 ponPort uint32
761 onuID uint32
762 uniID uint32
763 allocID []uint32
764 }
765 tests := []struct {
766 name string
767 fields *fields
768 args args
769 wantErr error
770 }{
771 {"UpdateAllocIdsForOnu-1", getResMgr(), args{2, 2, 2, []uint32{1, 2}},
772 errors.New("")},
773 }
774 for _, tt := range tests {
775 t.Run(tt.name, func(t *testing.T) {
776 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530777 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
778 defer cancel()
779 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 +0200780 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
781 }
782 })
783 }
784}
785
786func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
787 type args struct {
788 ponIntfID int32
789 onuID int32
790 uniID int32
791 flowID uint32
792 flowData *[]FlowInfo
793 }
794 tests := []struct {
795 name string
796 fields *fields
797 args args
798 wantErr error
799 }{
800 {"UpdateFlowIDInfo-1", getResMgr(), args{2, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
801 }
802 for _, tt := range tests {
803 t.Run(tt.name, func(t *testing.T) {
804 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530805 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
806 defer cancel()
807 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 +0200808 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
809 }
810 })
811 }
812}
813
814func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
815
816 type args struct {
817 ponPort uint32
818 onuID uint32
819 uniID uint32
820 GEMPortList []uint32
821 }
822 tests := []struct {
823 name string
824 fields *fields
825 args args
826 wantErr error
827 }{
828 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{2, 2, 2,
829 []uint32{1, 2}}, errors.New("failed to update resource")},
830 }
831 for _, tt := range tests {
832 t.Run(tt.name, func(t *testing.T) {
833 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530834 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
835 defer cancel()
836 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 +0200837 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
838 }
839 })
840 }
841}
842
843func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
844 type args struct {
845 gemPorts []uint32
846 PonPort uint32
847 onuID uint32
848 uniID uint32
849 }
850 tests := []struct {
851 name string
852 fields *fields
853 args args
854 wantErr error
855 }{
856 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
857 2, 2, 2}, errors.New("failed to update resource")},
858 }
859 for _, tt := range tests {
860 t.Run(tt.name, func(t *testing.T) {
861 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530862 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
863 defer cancel()
864 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000865 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200866 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
867 }
868 })
869 }
870}
871
872func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
873 type args struct {
874 Direction string
875 IntfID uint32
876 OnuID uint32
877 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000878 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200879 MeterConfig *ofp.OfpMeterConfig
880 }
881 tests := []struct {
882 name string
883 fields *fields
884 args args
885 wantErr error
886 }{
887 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 2, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000888 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200889 }
890 for _, tt := range tests {
891 t.Run(tt.name, func(t *testing.T) {
892 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530893 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
894 defer cancel()
895 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000896 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200897 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
898 }
899 })
900 }
901}
902
903func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
904 type args struct {
905 IntfID uint32
906 OnuID uint32
907 UniID uint32
908 TpID uint32
909 }
910 tests := []struct {
911 name string
912 fields *fields
913 args args
914 wantErr error
915 }{
916 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2,
917 2}, errors.New("failed to update resource")},
918 }
919 for _, tt := range tests {
920 t.Run(tt.name, func(t *testing.T) {
921 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530922 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
923 defer cancel()
924 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 +0200925 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
926 }
927 })
928 }
929}
930
931func TestSetKVClient(t *testing.T) {
932 type args struct {
933 backend string
934 Host string
935 Port int
936 DeviceID string
937 }
938 tests := []struct {
939 name string
940 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500941 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200942 }{
sbarbaria8910ba2019-11-05 10:12:23 -0500943 {"setKVClient-1", args{"consul", "1.1.1.1", 1, "olt1"}, &db.Backend{}},
944 {"setKVClient-1", args{"etcd", "2.2.2.2", 2, "olt2"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200945 }
946 for _, tt := range tests {
947 t.Run(tt.name, func(t *testing.T) {
948 if got := SetKVClient(tt.args.backend, tt.args.Host, tt.args.Port, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
949 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
950 }
951 })
952 }
953}
954
955func Test_getFlowIDFromFlowInfo(t *testing.T) {
956 type args struct {
957 FlowInfo *[]FlowInfo
958 flowID uint32
959 gemportID uint32
960 flowStoreCookie uint64
961 flowCategory string
962 vlanPcp []uint32
963 }
964 flowInfo := &[]FlowInfo{
965 {
966 &openolt.Flow{
967 FlowId: 1,
968 GemportId: 1,
969 Classifier: &openolt.Classifier{
970 OPbits: 1,
971 }},
972 1,
973 "HSIA_FLOW",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530974 2000,
cbabuabf02352019-10-15 13:14:56 +0200975 },
976 {
977 &openolt.Flow{
978 GemportId: 1,
979 },
980 1,
981 "EAPOL",
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530982 3000,
cbabuabf02352019-10-15 13:14:56 +0200983 },
984 }
985 tests := []struct {
986 name string
987 args args
988 wantErr error
989 }{
990 {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
991 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
992 "HSIA_FLOW", []uint32{1, 2}}, errors.New("invalid flow-info")},
993 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
994 "EAPOL", []uint32{1, 2}}, errors.New("invalid flow-info")},
995 }
996 for _, tt := range tests {
997 t.Run(tt.name, func(t *testing.T) {
998 err := getFlowIDFromFlowInfo(tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...)
999 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
1000 t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
1001 }
1002 if err == nil {
1003 t.Log("return'd nil")
1004 }
1005 })
1006 }
1007}
1008
1009func Test_newKVClient(t *testing.T) {
1010 type args struct {
1011 storeType string
1012 address string
1013 timeout uint32
1014 }
1015 var kvClient kvstore.Client
1016 tests := []struct {
1017 name string
1018 args args
1019 want kvstore.Client
1020 wantErr error
1021 }{
1022 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
1023 }
1024 for _, tt := range tests {
1025 t.Run(tt.name, func(t *testing.T) {
1026 got, err := newKVClient(tt.args.storeType, tt.args.address, tt.args.timeout)
1027 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
1028 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
1029 }
1030 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
1031 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
1032 return
1033 }
1034
1035 })
1036 }
1037}
Esin Karamanccb714b2019-11-29 15:02:06 +00001038
1039func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
1040 type args struct {
1041 intf uint32
1042 gem uint32
1043 servicePriority uint32
1044 }
1045 tests := []struct {
1046 name string
1047 args args
1048 fields *fields
1049 }{
1050 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1051 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1052 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1053 }
1054 for _, tt := range tests {
1055 t.Run(tt.name, func(t *testing.T) {
1056 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301057 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1058 defer cancel()
1059 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +00001060 if err != nil {
1061 t.Errorf("%s got err= %s wants nil", tt.name, err)
1062 return
1063 }
1064 })
1065 }
1066}
1067
1068func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1069 groupDesc := ofp.OfpGroupDesc{
1070 Type: ofp.OfpGroupType_OFPGT_ALL,
1071 GroupId: groupID,
1072 }
1073 groupEntry := ofp.OfpGroupEntry{
1074 Desc: &groupDesc,
1075 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001076 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001077 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001078 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001079 bucket := ofp.OfpBucket{
1080 Actions: acts,
1081 }
1082 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001083 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001084 return &groupEntry
1085}
1086
1087func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1088 type args struct {
1089 group *ofp.OfpGroupEntry
1090 cached bool
1091 }
1092 //create group 1
1093 group1 := newGroup(1, []uint32{1})
1094 //create group 2
1095 group2 := newGroup(2, []uint32{2})
1096 //define test set
1097 tests := []struct {
1098 name string
1099 args args
1100 fields *fields
1101 }{
1102 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1103 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1104 }
1105 //execute tests
1106 for _, tt := range tests {
1107 t.Run(tt.name, func(t *testing.T) {
1108 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301109 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1110 defer cancel()
1111 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001112 if err != nil {
1113 t.Errorf("%s got err= %s wants nil", tt.name, err)
1114 return
1115 }
1116 })
1117 }
1118}
1119
1120func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1121 type args struct {
1122 groupID uint32
1123 cached bool
1124 }
1125 //define test set
1126 tests := []struct {
1127 name string
1128 args args
1129 fields *fields
1130 }{
1131 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1132 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1133 }
1134 //execute tests
1135 for _, tt := range tests {
1136 t.Run(tt.name, func(t *testing.T) {
1137 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301138 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1139 defer cancel()
1140 success := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001141 if !success {
1142 t.Errorf("%s got false but wants true", tt.name)
1143 return
1144 }
1145 })
1146 }
1147}
1148
1149func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1150 type args struct {
1151 groupID uint32
1152 cached bool
1153 }
1154 //define test set
1155 tests := []struct {
1156 name string
1157 args args
1158 fields *fields
1159 }{
1160 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1161 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1162 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1163 }
1164 //execute tests
1165 for _, tt := range tests {
1166 t.Run(tt.name, func(t *testing.T) {
1167 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301168 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1169 defer cancel()
1170 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001171 if err != nil {
1172 t.Errorf("%s got error but wants nil error", tt.name)
1173 return
1174 } else if exists && (groupInfo.GroupID == 0) {
1175 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1176 return
1177 } else if tt.args.groupID == 3 && exists {
1178 t.Errorf("%s got true but wants false", tt.name)
1179 return
1180 }
1181 })
1182 }
1183}