blob: 81e0fa47695af51cd6ef3a3ab7fd6e75431f96a8 [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001/*
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
17/*
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
24package resourcemanager
25
26import (
27 "context"
28 "encoding/json"
29 "errors"
30 "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"
37 "reflect"
38 "strconv"
39 "strings"
40 "sync"
41 "testing"
42 "time"
43)
44
45func init() {
46 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
47}
48
49const (
50 MeterConfig = "meter_id"
51 FlowIDInfo = "flow_id_info"
52 FlowIDs = "flow_ids"
53 GemportIDs = "gemport_ids"
54 AllocIDs = "alloc_ids"
55 GemportIDPool = "gemport_id_pool"
56 AllocIDPool = "alloc_id_pool"
57 FlowIDpool = "flow_id_pool"
58)
59
60// fields mocks OpenOltResourceMgr struct.
61type fields struct {
62 DeviceID string
63 Address string
64 Args string
65 KVStore *db.Backend
66 DeviceType string
67 DevInfo *openolt.DeviceInfo
68 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
69 NumOfPonPorts uint32
70}
71
72// MockKVClient mocks the AdapterProxy interface.
73type MockResKVClient struct {
74}
75
76// getResMgr mocks OpenOltResourceMgr struct.
77func getResMgr() *fields {
78 var resMgr fields
79 resMgr.KVStore = &db.Backend{
80 Client: &MockResKVClient{},
81 }
82 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
83 ranges := make(map[string]interface{})
84 sharedIdxByType := make(map[string]string)
85 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
86 sharedIdxByType["ONU_ID"] = "ONU_ID"
87 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
88 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
89 ranges["ONU_ID"] = uint32(0)
90 ranges["GEMPORT_ID"] = uint32(0)
91 ranges["ALLOC_ID"] = uint32(0)
92 ranges["FLOW_ID"] = uint32(0)
93 ranges["onu_id_shared"] = uint32(0)
94 ranges["alloc_id_shared"] = uint32(0)
95 ranges["gemport_id_shared"] = uint32(0)
96 ranges["flow_id_shared"] = uint32(0)
97 resMgr.NumOfPonPorts = 2
98 ponMgr := &ponrmgr.PONResourceManager{
99 DeviceID: "onu-1",
100 IntfIDs: []uint32{1, 2},
101 KVStore: &db.Backend{
102 Client: &MockResKVClient{},
103 },
104 PonResourceRanges: ranges,
105 SharedIdxByType: sharedIdxByType,
106 }
107 resMgr.ResourceMgrs[1] = ponMgr
108 resMgr.ResourceMgrs[2] = ponMgr
109
110 return &resMgr
111}
112
113// List function implemented for KVClient.
114func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
115 return nil, errors.New("key didn't find")
116}
117
118// Get mock function implementation for KVClient
119func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
120 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
121 if key != "" {
122 if strings.Contains(key, MeterConfig) {
123 var bands []*ofp.OfpMeterBandHeader
124 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
125 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
126
127 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
128 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
129
130 sep := strings.Split(key, "/")[1]
131 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
132 if uint32(val) > 1 {
133 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
134 str, _ := json.Marshal(meterConfig)
135
136 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
137 }
138 return nil, errors.New("invalid meter")
139 }
140 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
141 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
142 data := make(map[string]interface{})
143 data["pool"] = "1024"
144 data["start_idx"] = 1
145 data["end_idx"] = 1024
146 str, _ := json.Marshal(data)
147 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
148 }
149 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
150 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
151 str, _ := json.Marshal([]uint32{1, 2})
152 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
153 }
154 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
155 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
156 str, _ := json.Marshal(1)
157 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
158 }
159 if strings.Contains(key, McastQueuesForIntf) {
160 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
161 mcastQueues := make(map[uint32][]uint32)
162 mcastQueues[10] = []uint32{4000, 0}
163 str, _ := json.Marshal(mcastQueues)
164 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
165 }
166 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
167 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
168 str, _ := json.Marshal(groupInfo)
169 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
170 }
171
172 maps := make(map[string]*kvstore.KVPair)
173 maps[key] = &kvstore.KVPair{Key: key}
174 return maps[key], nil
175 }
176 return nil, errors.New("key didn't find")
177}
178
179// Put mock function implementation for KVClient
180func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
181 if key != "" {
182 return nil
183 }
184 return errors.New("key didn't find")
185}
186
187// Delete mock function implementation for KVClient
188func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
189 return nil
190}
191
192// Reserve mock function implementation for KVClient
193func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
194 return nil, errors.New("key didn't find")
195}
196
197// ReleaseReservation mock function implementation for KVClient
198func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
199 return nil
200}
201
202// ReleaseAllReservations mock function implementation for KVClient
203func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
204 return nil
205}
206
207// RenewReservation mock function implementation for KVClient
208func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
209 return nil
210}
211
212// Watch mock function implementation for KVClient
213func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
214 return nil
215}
216
217// AcquireLock mock function implementation for KVClient
218func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
219 return nil
220}
221
222// ReleaseLock mock function implementation for KVClient
223func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
224 return nil
225}
226
227// IsConnectionUp mock function implementation for KVClient
228func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
229 return true
230}
231
232// CloseWatch mock function implementation for KVClient
233func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
234}
235
236// Close mock function implementation for KVClient
237func (kvclient *MockResKVClient) Close(ctx context.Context) {
238}
239
240// testResMgrObject maps fields type to OpenOltResourceMgr type.
241func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
242 var rsrMgr = OpenOltResourceMgr{
243 DeviceID: testResMgr.DeviceID,
244 Args: testResMgr.Args,
245 KVStore: testResMgr.KVStore,
246 DeviceType: testResMgr.DeviceType,
247 Address: testResMgr.Address,
248 DevInfo: testResMgr.DevInfo,
249 ResourceMgrs: testResMgr.ResourceMgrs,
250 }
251
252 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
253 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
254 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
255
256 return &rsrMgr
257}
258
259func TestNewResourceMgr(t *testing.T) {
260 type args struct {
261 deviceID string
262 KVStoreAddress string
263 kvStoreType string
264 deviceType string
265 devInfo *openolt.DeviceInfo
266 }
267 tests := []struct {
268 name string
269 args args
270 want *OpenOltResourceMgr
271 }{
272 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
273 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
274 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
275 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
276 }
277 for _, tt := range tests {
278 t.Run(tt.name, func(t *testing.T) {
279 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
280 defer cancel()
281 if got := NewResourceMgr(ctx, tt.args.deviceID, tt.args.KVStoreAddress, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
282 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
283 }
284 })
285 }
286}
287
288func TestOpenOltResourceMgr_Delete(t *testing.T) {
289 tests := []struct {
290 name string
291 fields *fields
292 wantErr error
293 }{
294 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
295 }
296 for _, tt := range tests {
297 t.Run(tt.name, func(t *testing.T) {
298 RsrcMgr := testResMgrObject(tt.fields)
299 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
300 defer cancel()
301 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
302 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
303 }
304 })
305 }
306}
307
308func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) {
309 type args struct {
310 IntfID uint32
311 onuID int32
312 uniID int32
313 FlowID uint32
314 }
315 tests := []struct {
316 name string
317 fields *fields
318 args args
319 }{
320 {"FreeFlowID-1", getResMgr(), args{1, 2, 2, 2}},
321 }
322 for _, tt := range tests {
323 t.Run(tt.name, func(t *testing.T) {
324 RsrcMgr := testResMgrObject(tt.fields)
325 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
326 defer cancel()
327 RsrcMgr.FreeFlowID(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
328 })
329 }
330}
331
332func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) {
333
334 type args struct {
335 IntfID uint32
336 onuID uint32
337 uniID uint32
338 FlowID []uint32
339 }
340 tests := []struct {
341 name string
342 fields *fields
343 args args
344 }{
345 {"FreeFlowIDs-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}},
346 }
347 for _, tt := range tests {
348 t.Run(tt.name, func(t *testing.T) {
349 RsrcMgr := testResMgrObject(tt.fields)
350 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
351 defer cancel()
352 RsrcMgr.FreeFlowIDs(ctx, tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
353 })
354 }
355}
356
357func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
358 type args struct {
359 intfID uint32
360 onuID uint32
361 uniID uint32
362 }
363 tests := []struct {
364 name string
365 fields *fields
366 args args
367 }{
368 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
369 }
370 for _, tt := range tests {
371 t.Run(tt.name, func(t *testing.T) {
372 RsrcMgr := testResMgrObject(tt.fields)
373 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
374 defer cancel()
375 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
376 })
377 }
378}
379
380func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
381 type args struct {
382 intfID uint32
383 onuID []uint32
384 }
385 tests := []struct {
386 name string
387 fields *fields
388 args args
389 }{
390 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
391 }
392 for _, tt := range tests {
393 t.Run(tt.name, func(t *testing.T) {
394 RsrcMgr := testResMgrObject(tt.fields)
395 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
396 defer cancel()
397 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
398 })
399 }
400}
401
402func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
403
404 type args struct {
405 intfID uint32
406 onuID uint32
407 uniID uint32
408 }
409 tests := []struct {
410 name string
411 fields *fields
412 args args
413 want uint32
414 }{
415 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
416 }
417 for _, tt := range tests {
418 t.Run(tt.name, func(t *testing.T) {
419 RsrcMgr := testResMgrObject(tt.fields)
420 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
421 defer cancel()
422 if got := RsrcMgr.GetAllocID(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
423 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
424 }
425 })
426 }
427}
428
429func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
430 type args struct {
431 intfID uint32
432 onuID uint32
433 uniID uint32
434 }
435 tests := []struct {
436 name string
437 fields *fields
438 args args
439 want []uint32
440 }{
441 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
442 }
443 for _, tt := range tests {
444 t.Run(tt.name, func(t *testing.T) {
445 RsrcMgr := testResMgrObject(tt.fields)
446 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
447 defer cancel()
448 if got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); !reflect.DeepEqual(got, tt.want) {
449 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
450 }
451 })
452 }
453}
454
455func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
456
457 type args struct {
458 PONIntfID uint32
459 ONUID int32
460 UNIID int32
461 }
462 tests := []struct {
463 name string
464 fields *fields
465 args args
466 want []uint32
467 }{
468 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
469 }
470 for _, tt := range tests {
471 t.Run(tt.name, func(t *testing.T) {
472 RsrcMgr := testResMgrObject(tt.fields)
473 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
474 defer cancel()
475 if got := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
476 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
477 }
478 })
479 }
480}
481
482func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
483 type args struct {
484 intfID uint32
485 onuID uint32
486 uniID uint32
487 }
488 tests := []struct {
489 name string
490 fields *fields
491 args args
492 want []uint32
493 }{
494 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
495 }
496 for _, tt := range tests {
497 t.Run(tt.name, func(t *testing.T) {
498 RsrcMgr := testResMgrObject(tt.fields)
499 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
500 defer cancel()
501 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
502 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
503 }
504 })
505 }
506}
507
508func TestOpenOltResourceMgr_GetFlowID(t *testing.T) {
509
510 type args struct {
511 ponIntfID uint32
512 ONUID int32
513 uniID int32
514 gemportID uint32
515 flowStoreCookie uint64
516 flowCategory string
517 vlanVid uint32
518 vlanPcp []uint32
519 }
520 tests := []struct {
521 name string
522 fields *fields
523 args args
524 want uint32
525 wantErr error
526 }{
527 {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2,
528 "HSIA", 33, nil}, 0, errors.New("failed to get flows")},
529 }
530 for _, tt := range tests {
531 t.Run(tt.name, func(t *testing.T) {
532 RsrcMgr := testResMgrObject(tt.fields)
533 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
534 defer cancel()
535 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...)
536 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
537 t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
538 return
539 }
540 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
541 t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
542 }
543 })
544 }
545}
546
547func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
548 type args struct {
549 ponPort uint32
550 onuID uint32
551 uniID uint32
552 NumOfPorts uint32
553 }
554 tests := []struct {
555 name string
556 fields *fields
557 args args
558 want []uint32
559 wantErr error
560 }{
561 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
562 errors.New("failed to get gem port")},
563 }
564 for _, tt := range tests {
565 t.Run(tt.name, func(t *testing.T) {
566 RsrcMgr := testResMgrObject(tt.fields)
567 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
568 defer cancel()
569 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
570 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
571 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
572 return
573 }
574 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
575 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
576 }
577 })
578 }
579}
580
581func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
582 type args struct {
583 Direction string
584 IntfID uint32
585 OnuID uint32
586 UniID uint32
587 tpID uint32
588 }
589 tests := []struct {
590 name string
591 fields *fields
592 args args
593 want *ofp.OfpMeterConfig
594 wantErr error
595 }{
596 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
597 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
598 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
599 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
600 }
601 for _, tt := range tests {
602 t.Run(tt.name, func(t *testing.T) {
603 RsrcMgr := testResMgrObject(tt.fields)
604 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
605 defer cancel()
606 got, err := RsrcMgr.GetMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
607 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
608 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
609 }
610 })
611 }
612}
613
614func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
615 type args struct {
616 ponIntfID uint32
617 }
618 tests := []struct {
619 name string
620 fields *fields
621 args args
622 want uint32
623 wantErr error
624 }{
625 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
626 }
627 for _, tt := range tests {
628 t.Run(tt.name, func(t *testing.T) {
629 RsrcMgr := testResMgrObject(tt.fields)
630 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
631 defer cancel()
632 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
633 if got != tt.want && err != nil {
634 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
635 }
636 })
637 }
638}
639
640func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
641
642 type args struct {
643 IntfID uint32
644 OnuID uint32
645 UniID uint32
646 }
647 tests := []struct {
648 name string
649 fields *fields
650 args args
651 want []uint32
652 }{
653 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
654 []uint32{1}},
655 }
656 for _, tt := range tests {
657 t.Run(tt.name, func(t *testing.T) {
658 RsrcMgr := testResMgrObject(tt.fields)
659 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
660 defer cancel()
661 if got := RsrcMgr.GetTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
662 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
663 }
664 })
665 }
666}
667
668func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
669 type args struct {
670 ponIntfID uint32
671 onuID int32
672 uniID int32
673 flowStoreCookie uint64
674 }
675 tests := []struct {
676 name string
677 fields *fields
678 args args
679 want bool
680 }{
681 {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false},
682 }
683 for _, tt := range tests {
684 t.Run(tt.name, func(t *testing.T) {
685 RsrcMgr := testResMgrObject(tt.fields)
686 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
687 defer cancel()
688 if got := RsrcMgr.IsFlowCookieOnKVStore(ctx, tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowStoreCookie); got != tt.want {
689 t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
690 }
691 })
692 }
693}
694
695func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
696
697 type args struct {
698 Direction string
699 IntfID uint32
700 OnuID uint32
701 UniID uint32
702 tpID uint32
703 }
704 tests := []struct {
705 name string
706 fields *fields
707 args args
708 wantErr error
709 }{
710 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
711 errors.New("failed to delete meter id %s from kvstore")},
712 }
713 for _, tt := range tests {
714 t.Run(tt.name, func(t *testing.T) {
715 RsrcMgr := testResMgrObject(tt.fields)
716 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
717 defer cancel()
718 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
719 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
720 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
721 }
722 })
723 }
724}
725
726func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
727 type args struct {
728 IntfID uint32
729 OnuID uint32
730 UniID uint32
731 tpID uint32
732 }
733 tests := []struct {
734 name string
735 fields *fields
736 args args
737 wantErr error
738 }{
739 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
740 errors.New("failed to delete techprofile id resource %s in KV store")},
741 }
742 for _, tt := range tests {
743 t.Run(tt.name, func(t *testing.T) {
744 RsrcMgr := testResMgrObject(tt.fields)
745 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
746 defer cancel()
747 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
748 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
749 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
750 }
751 })
752 }
753}
754
755func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
756 type args struct {
757 ponPort uint32
758 onuID uint32
759 uniID uint32
760 allocID []uint32
761 }
762 tests := []struct {
763 name string
764 fields *fields
765 args args
766 wantErr error
767 }{
768 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
769 errors.New("")},
770 }
771 for _, tt := range tests {
772 t.Run(tt.name, func(t *testing.T) {
773 RsrcMgr := testResMgrObject(tt.fields)
774 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
775 defer cancel()
776 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) {
777 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
778 }
779 })
780 }
781}
782
783func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
784 type args struct {
785 ponIntfID int32
786 onuID int32
787 uniID int32
788 flowID uint32
789 flowData *[]FlowInfo
790 }
791 tests := []struct {
792 name string
793 fields *fields
794 args args
795 wantErr error
796 }{
797 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
798 }
799 for _, tt := range tests {
800 t.Run(tt.name, func(t *testing.T) {
801 RsrcMgr := testResMgrObject(tt.fields)
802 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
803 defer cancel()
804 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) {
805 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
806 }
807 })
808 }
809}
810
811func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
812
813 type args struct {
814 ponPort uint32
815 onuID uint32
816 uniID uint32
817 GEMPortList []uint32
818 }
819 tests := []struct {
820 name string
821 fields *fields
822 args args
823 wantErr error
824 }{
825 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
826 []uint32{1, 2}}, errors.New("failed to update resource")},
827 }
828 for _, tt := range tests {
829 t.Run(tt.name, func(t *testing.T) {
830 RsrcMgr := testResMgrObject(tt.fields)
831 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
832 defer cancel()
833 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) {
834 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
835 }
836 })
837 }
838}
839
840func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
841 type args struct {
842 gemPorts []uint32
843 PonPort uint32
844 onuID uint32
845 uniID uint32
846 }
847 tests := []struct {
848 name string
849 fields *fields
850 args args
851 wantErr error
852 }{
853 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
854 1, 2, 2}, errors.New("failed to update resource")},
855 }
856 for _, tt := range tests {
857 t.Run(tt.name, func(t *testing.T) {
858 RsrcMgr := testResMgrObject(tt.fields)
859 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
860 defer cancel()
861 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
862 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
863 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
864 }
865 })
866 }
867}
868
869func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
870 type args struct {
871 Direction string
872 IntfID uint32
873 OnuID uint32
874 UniID uint32
875 tpID uint32
876 MeterConfig *ofp.OfpMeterConfig
877 }
878 tests := []struct {
879 name string
880 fields *fields
881 args args
882 wantErr error
883 }{
884 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
885 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
886 }
887 for _, tt := range tests {
888 t.Run(tt.name, func(t *testing.T) {
889 RsrcMgr := testResMgrObject(tt.fields)
890 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
891 defer cancel()
892 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
893 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
894 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
895 }
896 })
897 }
898}
899
900func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
901 type args struct {
902 IntfID uint32
903 OnuID uint32
904 UniID uint32
905 TpID uint32
906 }
907 tests := []struct {
908 name string
909 fields *fields
910 args args
911 wantErr error
912 }{
913 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
914 2}, errors.New("failed to update resource")},
915 }
916 for _, tt := range tests {
917 t.Run(tt.name, func(t *testing.T) {
918 RsrcMgr := testResMgrObject(tt.fields)
919 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
920 defer cancel()
921 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 {
922 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
923 }
924 })
925 }
926}
927
928func TestSetKVClient(t *testing.T) {
929 type args struct {
930 backend string
931 address string
932 DeviceID string
933 }
934 tests := []struct {
935 name string
936 args args
937 want *db.Backend
938 }{
939 {"setKVClient-1", args{"consul", "1.1.1.1:1", "olt1"}, &db.Backend{}},
940 {"setKVClient-1", args{"etcd", "2.2.2.2:2", "olt2"}, &db.Backend{}},
941 }
942 for _, tt := range tests {
943 t.Run(tt.name, func(t *testing.T) {
944 if got := SetKVClient(context.Background(), tt.args.backend, tt.args.address, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
945 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
946 }
947 })
948 }
949}
950
951func Test_getFlowIDFromFlowInfo(t *testing.T) {
952 type args struct {
953 FlowInfo *[]FlowInfo
954 flowID uint32
955 gemportID uint32
956 flowStoreCookie uint64
957 flowCategory string
958 vlanVid uint32
959 vlanPcp []uint32
960 }
961 flowInfo := &[]FlowInfo{
962 {
963 &openolt.Flow{
964 FlowId: 1,
965 GemportId: 1,
966 Classifier: &openolt.Classifier{
967 OPbits: 1,
968 OVid: 33,
969 },
970 Action: &openolt.Action{
971 Cmd: &openolt.ActionCmd{
972 AddOuterTag: true,
973 },
974 OVid: 7,
975 },
976 },
977 1,
978 "HSIA_FLOW",
979 2000,
980 },
981 {
982 &openolt.Flow{
983 GemportId: 1,
984 Classifier: &openolt.Classifier{
985 OVid: 0,
986 },
987 Action: &openolt.Action{
988 Cmd: &openolt.ActionCmd{
989 TrapToHost: true,
990 },
991 },
992 },
993 1,
994 "EAPOL",
995 3000,
996 },
997 }
998 tests := []struct {
999 name string
1000 args args
1001 wantErr error
1002 }{
1003 {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
1004 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
1005 "HSIA_FLOW", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
1006 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
1007 "EAPOL", 33, []uint32{1, 2}}, errors.New("invalid flow-info")},
1008 }
1009 for _, tt := range tests {
1010 t.Run(tt.name, func(t *testing.T) {
1011 err := getFlowIDFromFlowInfo(context.Background(), tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanVid, tt.args.vlanPcp...)
1012 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
1013 t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
1014 }
1015 if err == nil {
1016 t.Log("return'd nil")
1017 }
1018 })
1019 }
1020}
1021
1022func Test_newKVClient(t *testing.T) {
1023 type args struct {
1024 storeType string
1025 address string
1026 timeout time.Duration
1027 }
1028 var kvClient kvstore.Client
1029 tests := []struct {
1030 name string
1031 args args
1032 want kvstore.Client
1033 wantErr error
1034 }{
1035 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
1036 }
1037 for _, tt := range tests {
1038 t.Run(tt.name, func(t *testing.T) {
1039 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
1040 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
1041 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
1042 }
1043 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
1044 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
1045 return
1046 }
1047
1048 })
1049 }
1050}
1051
1052func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
1053 type args struct {
1054 intf uint32
1055 gem uint32
1056 servicePriority uint32
1057 }
1058 tests := []struct {
1059 name string
1060 args args
1061 fields *fields
1062 }{
1063 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
1064 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
1065 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
1066 }
1067 for _, tt := range tests {
1068 t.Run(tt.name, func(t *testing.T) {
1069 RsrcMgr := testResMgrObject(tt.fields)
1070 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1071 defer cancel()
1072 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
1073 if err != nil {
1074 t.Errorf("%s got err= %s wants nil", tt.name, err)
1075 return
1076 }
1077 })
1078 }
1079}
1080
1081func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1082 groupDesc := ofp.OfpGroupDesc{
1083 Type: ofp.OfpGroupType_OFPGT_ALL,
1084 GroupId: groupID,
1085 }
1086 groupEntry := ofp.OfpGroupEntry{
1087 Desc: &groupDesc,
1088 }
1089 for i := 0; i < len(outPorts); i++ {
1090 var acts []*ofp.OfpAction
1091 acts = append(acts, fu.Output(outPorts[i]))
1092 bucket := ofp.OfpBucket{
1093 Actions: acts,
1094 }
1095 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
1096 }
1097 return &groupEntry
1098}
1099
1100func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1101 type args struct {
1102 group *ofp.OfpGroupEntry
1103 cached bool
1104 }
1105 group1 := newGroup(1, []uint32{1})
1106 group2 := newGroup(2, []uint32{2})
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 for _, tt := range tests {
1116 t.Run(tt.name, func(t *testing.T) {
1117 RsrcMgr := testResMgrObject(tt.fields)
1118 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1119 defer cancel()
1120 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
1121 if err != nil {
1122 t.Errorf("%s got err= %s wants nil", tt.name, err)
1123 return
1124 }
1125 })
1126 }
1127}
1128
1129func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1130 type args struct {
1131 groupID uint32
1132 cached bool
1133 }
1134 tests := []struct {
1135 name string
1136 args args
1137 fields *fields
1138 }{
1139 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1140 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1141 }
1142 for _, tt := range tests {
1143 t.Run(tt.name, func(t *testing.T) {
1144 RsrcMgr := testResMgrObject(tt.fields)
1145 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1146 defer cancel()
1147 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1148 if err != nil {
1149 t.Errorf("%s got false but wants true", tt.name)
1150 return
1151 }
1152 })
1153 }
1154}
1155
1156func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1157 type args struct {
1158 groupID uint32
1159 cached bool
1160 }
1161 tests := []struct {
1162 name string
1163 args args
1164 fields *fields
1165 }{
1166 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1167 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1168 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1169 }
1170 for _, tt := range tests {
1171 t.Run(tt.name, func(t *testing.T) {
1172 RsrcMgr := testResMgrObject(tt.fields)
1173 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1174 defer cancel()
1175 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1176 if err != nil {
1177 t.Errorf("%s got error but wants nil error", tt.name)
1178 return
1179 } else if exists && (groupInfo.GroupID == 0) {
1180 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1181 return
1182 } else if tt.args.groupID == 3 && exists {
1183 t.Errorf("%s got true but wants false", tt.name)
1184 return
1185 }
1186 })
1187 }
1188}