blob: cdce9ab7d1f79c49d13bc111c6a47b08cfaf39f9 [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 (
27 "encoding/json"
28 "errors"
Scott Baker51290152019-10-24 14:23:20 -070029 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
30 "github.com/opencord/voltha-lib-go/v2/pkg/db/model"
31 "github.com/opencord/voltha-lib-go/v2/pkg/log"
32 ponrmgr "github.com/opencord/voltha-lib-go/v2/pkg/ponresourcemanager"
cbabuabf02352019-10-15 13:14:56 +020033 ofp "github.com/opencord/voltha-protos/go/openflow_13"
34 "github.com/opencord/voltha-protos/go/openolt"
35 "reflect"
36 "strconv"
37 "strings"
38 "testing"
39)
40
41func init() {
42 log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
43}
44
45const (
46 // MeterConfig meter to extract meter
47 MeterConfig = "meter_id"
48 // TpIDSuffixPath to extract Techprofile
49 TpIDSuffixPath = "tp_id"
50 // FlowIDInfo to extract flows
51 FlowIDInfo = "flow_id_info"
52 // FlowIds to extract flows
53 FlowIDs = "flow_ids"
54 // GemportIDs to gemport_ids
55 GemportIDs = "gemport_ids"
56 // AllocIDs to extract alloc_ids
57 AllocIDs = "alloc_ids"
58 // GemportIDPool to extract gemport
59 GemportIDPool = "gemport_id_pool"
60 // AllocIDPool to extract allocid
61 AllocIDPool = "alloc_id_pool"
62 // FlowIDpool to extract Flow ids
63 FlowIDpool = "flow_id_pool"
64)
65
cbabubef89432019-10-18 11:47:27 +020066// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020067type fields struct {
68 DeviceID string
69 HostAndPort string
70 Args string
71 KVStore *model.Backend
72 DeviceType string
73 Host string
74 Port int
75 DevInfo *openolt.DeviceInfo
76 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
77}
cbabubef89432019-10-18 11:47:27 +020078
79// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020080type MockResKVClient struct {
81}
82
cbabubef89432019-10-18 11:47:27 +020083// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020084func getResMgr() *fields {
85 var resMgr fields
86 resMgr.KVStore = &model.Backend{
87 Client: &MockResKVClient{},
88 }
89 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
90 ranges := make(map[string]interface{})
91 sharedIdxByType := make(map[string]string)
92 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
93 sharedIdxByType["ONU_ID"] = "ONU_ID"
94 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
95 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
96 ranges["ONU_ID"] = uint32(0)
97 ranges["GEMPORT_ID"] = uint32(0)
98 ranges["ALLOC_ID"] = uint32(0)
99 ranges["FLOW_ID"] = uint32(0)
100 ranges["onu_id_shared"] = uint32(0)
101 ranges["alloc_id_shared"] = uint32(0)
102 ranges["gemport_id_shared"] = uint32(0)
103 ranges["flow_id_shared"] = uint32(0)
104 ponMgr := &ponrmgr.PONResourceManager{
105 DeviceID: "onu-1",
106 IntfIDs: []uint32{1, 2},
107 KVStore: &model.Backend{
108 Client: &MockResKVClient{},
109 },
110 PonResourceRanges: ranges,
111 SharedIdxByType: sharedIdxByType,
112 }
113 resMgr.ResourceMgrs[1] = ponMgr
114 resMgr.ResourceMgrs[2] = ponMgr
115 return &resMgr
116}
cbabubef89432019-10-18 11:47:27 +0200117
118// List function implemented for KVClient.
cbabuabf02352019-10-15 13:14:56 +0200119func (kvclient *MockResKVClient) List(key string, timeout int, lock ...bool) (map[string]*kvstore.KVPair, error) {
120 return nil, errors.New("key didn't find")
121}
122
123// Get mock function implementation for KVClient
124func (kvclient *MockResKVClient) Get(key string, timeout int, lock ...bool) (*kvstore.KVPair, error) {
125 log.Debugw("Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
126 if key != "" {
127 if strings.Contains(key, MeterConfig) {
128 var bands []*ofp.OfpMeterBandHeader
129 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
130 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
131
132 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
133 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
134
Gamze Abakafee36392019-10-03 11:17:24 +0000135 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200136 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
137 if uint32(val) > 1 {
138 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
139 str, _ := json.Marshal(meterConfig)
140
141 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
142 }
143 return nil, errors.New("invalid meter")
144 }
145 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
146 log.Debug("Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
147 data := make(map[string]interface{})
148 data["pool"] = "1024"
149 data["start_idx"] = 1
150 data["end_idx"] = 1024
151 str, _ := json.Marshal(data)
152 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
153 }
154 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
155 log.Debug("Error Error Error Key:", FlowIDs, FlowIDInfo)
156 str, _ := json.Marshal([]uint32{1, 2})
157 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
158 }
159 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
160 log.Debug("Error Error Error Key:", AllocIDs, GemportIDs)
161 str, _ := json.Marshal(1)
162 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
163 }
164 maps := make(map[string]*kvstore.KVPair)
165 maps[key] = &kvstore.KVPair{Key: key}
166 return maps[key], nil
167 }
168 return nil, errors.New("key didn't find")
169}
170
171// Put mock function implementation for KVClient
172func (kvclient *MockResKVClient) Put(key string, value interface{}, timeout int, lock ...bool) error {
173 if key != "" {
174 return nil
175 }
176 return errors.New("key didn't find")
177}
178
179// Delete mock function implementation for KVClient
180func (kvclient *MockResKVClient) Delete(key string, timeout int, lock ...bool) error {
181 return nil
182}
183
184// Reserve mock function implementation for KVClient
185func (kvclient *MockResKVClient) Reserve(key string, value interface{}, ttl int64) (interface{}, error) {
186 return nil, errors.New("key didn't find")
187}
188
189// ReleaseReservation mock function implementation for KVClient
190func (kvclient *MockResKVClient) ReleaseReservation(key string) error {
191 return nil
192}
193
194// ReleaseAllReservations mock function implementation for KVClient
195func (kvclient *MockResKVClient) ReleaseAllReservations() error {
196 return nil
197}
198
199// RenewReservation mock function implementation for KVClient
200func (kvclient *MockResKVClient) RenewReservation(key string) error {
201 return nil
202}
203
204// Watch mock function implementation for KVClient
205func (kvclient *MockResKVClient) Watch(key string) chan *kvstore.Event {
206 return nil
207}
208
209// AcquireLock mock function implementation for KVClient
210func (kvclient *MockResKVClient) AcquireLock(lockName string, timeout int) error {
211 return nil
212}
213
214// ReleaseLock mock function implementation for KVClient
215func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
216 return nil
217}
218
219// IsConnectionUp mock function implementation for KVClient
220func (kvclient *MockResKVClient) IsConnectionUp(timeout int) bool { // timeout in second
221 return true
222}
223
224// CloseWatch mock function implementation for KVClient
225func (kvclient *MockResKVClient) CloseWatch(key string, ch chan *kvstore.Event) {
226}
227
228// Close mock function implementation for KVClient
229func (kvclient *MockResKVClient) Close() {
230}
231
cbabubef89432019-10-18 11:47:27 +0200232// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200233func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
234 return &OpenOltResourceMgr{
235 DeviceID: testResMgr.DeviceID,
236 HostAndPort: testResMgr.HostAndPort,
237 Args: testResMgr.Args,
238 KVStore: testResMgr.KVStore,
239 DeviceType: testResMgr.DeviceType,
240 Host: testResMgr.Host,
241 Port: testResMgr.Port,
242 DevInfo: testResMgr.DevInfo,
243 ResourceMgrs: testResMgr.ResourceMgrs,
244 }
245}
246
247func TestNewResourceMgr(t *testing.T) {
248 type args struct {
249 deviceID string
250 KVStoreHostPort string
251 kvStoreType string
252 deviceType string
253 devInfo *openolt.DeviceInfo
254 }
255 tests := []struct {
256 name string
257 args args
258 want *OpenOltResourceMgr
259 }{
260 {"NewResourceMgr-1", args{"olt1", "1:2", "consul",
261 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
262 {"NewResourceMgr-2", args{"olt2", "3:4", "etcd",
263 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}}, &OpenOltResourceMgr{}},
264 }
265 for _, tt := range tests {
266 t.Run(tt.name, func(t *testing.T) {
267 if got := NewResourceMgr(tt.args.deviceID, tt.args.KVStoreHostPort, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
268 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
269 }
270 })
271 }
272}
273
274func TestOpenOltResourceMgr_Delete(t *testing.T) {
275 tests := []struct {
276 name string
277 fields *fields
278 wantErr error
279 }{
280 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
281 }
282 for _, tt := range tests {
283 t.Run(tt.name, func(t *testing.T) {
284 RsrcMgr := testResMgrObject(tt.fields)
285 if err := RsrcMgr.Delete(); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
286 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
287 }
288 })
289 }
290}
291
292func TestOpenOltResourceMgr_FreeFlowID(t *testing.T) {
293 type args struct {
294 IntfID uint32
295 onuID int32
296 uniID int32
297 FlowID uint32
298 }
299 tests := []struct {
300 name string
301 fields *fields
302 args args
303 }{
304 {"FreeFlowID-1", getResMgr(), args{2, 2, 2, 2}},
305 }
306 for _, tt := range tests {
307 t.Run(tt.name, func(t *testing.T) {
308 RsrcMgr := testResMgrObject(tt.fields)
309 RsrcMgr.FreeFlowID(tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
310 })
311 }
312}
313
314func TestOpenOltResourceMgr_FreeFlowIDs(t *testing.T) {
315
316 type args struct {
317 IntfID uint32
318 onuID uint32
319 uniID uint32
320 FlowID []uint32
321 }
322 tests := []struct {
323 name string
324 fields *fields
325 args args
326 }{
327 {"FreeFlowIDs-1", getResMgr(), args{2, 2, 2, []uint32{1, 2}}},
328 }
329 for _, tt := range tests {
330 t.Run(tt.name, func(t *testing.T) {
331 RsrcMgr := testResMgrObject(tt.fields)
332 RsrcMgr.FreeFlowIDs(tt.args.IntfID, tt.args.onuID, tt.args.uniID, tt.args.FlowID)
333 })
334 }
335}
336
337func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
338 type args struct {
339 intfID uint32
340 onuID uint32
341 uniID uint32
342 }
343 tests := []struct {
344 name string
345 fields *fields
346 args args
347 }{
348 {"FreePONResourcesForONU-1", getResMgr(), args{2, 0, 2}},
349 }
350 for _, tt := range tests {
351 t.Run(tt.name, func(t *testing.T) {
352 RsrcMgr := testResMgrObject(tt.fields)
353 RsrcMgr.FreePONResourcesForONU(tt.args.intfID, tt.args.onuID, tt.args.uniID)
354 })
355 }
356}
357
358func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
359 type args struct {
360 intfID uint32
361 onuID []uint32
362 }
363 tests := []struct {
364 name string
365 fields *fields
366 args args
367 }{
368 {"FreeOnuID-1", getResMgr(), args{2, []uint32{1, 2}}},
369 }
370 for _, tt := range tests {
371 t.Run(tt.name, func(t *testing.T) {
372 RsrcMgr := testResMgrObject(tt.fields)
373 RsrcMgr.FreeonuID(tt.args.intfID, tt.args.onuID)
374 })
375 }
376}
377
378func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
379
380 type args struct {
381 intfID uint32
382 onuID uint32
383 uniID uint32
384 }
385 tests := []struct {
386 name string
387 fields *fields
388 args args
389 want uint32
390 }{
391 {"GetAllocID-1", getResMgr(), args{2, 2, 2}, 0},
392 }
393 for _, tt := range tests {
394 t.Run(tt.name, func(t *testing.T) {
395 RsrcMgr := testResMgrObject(tt.fields)
396 if got := RsrcMgr.GetAllocID(tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
397 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
398 }
399 })
400 }
401}
402
403func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
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
Gamze Abakafee36392019-10-03 11:17:24 +0000413 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200414 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000415 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200416 }
417 for _, tt := range tests {
418 t.Run(tt.name, func(t *testing.T) {
419 RsrcMgr := testResMgrObject(tt.fields)
Gamze Abakafee36392019-10-03 11:17:24 +0000420 if got := RsrcMgr.GetCurrentAllocIDsForOnu(tt.args.intfID, tt.args.onuID, tt.args.uniID); !reflect.DeepEqual(got, tt.want) {
421 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200422 }
423 })
424 }
425}
426
427func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
428
429 type args struct {
430 PONIntfID uint32
431 ONUID uint32
432 UNIID uint32
433 }
434 tests := []struct {
435 name string
436 fields *fields
437 args args
438 want []uint32
439 }{
440 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}},
441 }
442 for _, tt := range tests {
443 t.Run(tt.name, func(t *testing.T) {
444 RsrcMgr := testResMgrObject(tt.fields)
445 if got := RsrcMgr.GetCurrentFlowIDsForOnu(tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
446 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
447 }
448 })
449 }
450}
451
452func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
453 type args struct {
454 intfID uint32
455 onuID uint32
456 uniID uint32
457 }
458 tests := []struct {
459 name string
460 fields *fields
461 args args
462 want []uint32
463 }{
464 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{2, 2, 2}, []uint32{}},
465 }
466 for _, tt := range tests {
467 t.Run(tt.name, func(t *testing.T) {
468 RsrcMgr := testResMgrObject(tt.fields)
469 if got := RsrcMgr.GetCurrentGEMPortIDsForOnu(tt.args.intfID, tt.args.onuID, tt.args.uniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
470 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
471 }
472 })
473 }
474}
475
476func TestOpenOltResourceMgr_GetFlowID(t *testing.T) {
477
478 type args struct {
479 ponIntfID uint32
480 ONUID uint32
481 uniID uint32
482 gemportID uint32
483 flowStoreCookie uint64
484 flowCategory string
485 vlanPcp []uint32
486 }
487 tests := []struct {
488 name string
489 fields *fields
490 args args
491 want uint32
492 wantErr error
493 }{
494 {"GetFlowID-1", getResMgr(), args{2, 2, 2, 2, 2,
495 "HSIA", nil}, 0, errors.New("failed to get flows")},
496 }
497 for _, tt := range tests {
498 t.Run(tt.name, func(t *testing.T) {
499 RsrcMgr := testResMgrObject(tt.fields)
500 got, err := RsrcMgr.GetFlowID(tt.args.ponIntfID, tt.args.ONUID, tt.args.uniID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...)
501 if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
502 t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr)
503 return
504 }
505 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
506 t.Errorf("GetFlowID() got = %v, want %v", got, tt.want)
507 }
508 })
509 }
510}
511
512func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
513 type args struct {
514 ponPort uint32
515 onuID uint32
516 uniID uint32
517 NumOfPorts uint32
518 }
519 tests := []struct {
520 name string
521 fields *fields
522 args args
523 want []uint32
524 wantErr error
525 }{
526 {"GetGEMPortID-1", getResMgr(), args{2, 2, 2, 2}, []uint32{},
527 errors.New("failed to get gem port")},
528 }
529 for _, tt := range tests {
530 t.Run(tt.name, func(t *testing.T) {
531 RsrcMgr := testResMgrObject(tt.fields)
532 got, err := RsrcMgr.GetGEMPortID(tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
533 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
534 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
535 return
536 }
537 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
538 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
539 }
540 })
541 }
542}
543
544func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
545 type args struct {
546 Direction string
547 IntfID uint32
548 OnuID uint32
549 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000550 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200551 }
552 tests := []struct {
553 name string
554 fields *fields
555 args args
556 want *ofp.OfpMeterConfig
557 wantErr error
558 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000559 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200560 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Gamze Abakafee36392019-10-03 11:17:24 +0000561 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 2, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200562 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
563 }
564 for _, tt := range tests {
565 t.Run(tt.name, func(t *testing.T) {
566 RsrcMgr := testResMgrObject(tt.fields)
Gamze Abakafee36392019-10-03 11:17:24 +0000567 got, err := RsrcMgr.GetMeterIDForOnu(tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200568 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
569 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
570 }
571 })
572 }
573}
574
575func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
576 type args struct {
577 ponIntfID uint32
578 }
579 tests := []struct {
580 name string
581 fields *fields
582 args args
583 want uint32
584 wantErr error
585 }{
586 {"GetONUID-1", getResMgr(), args{2}, uint32(0), errors.New("json errors")},
587 }
588 for _, tt := range tests {
589 t.Run(tt.name, func(t *testing.T) {
590 RsrcMgr := testResMgrObject(tt.fields)
591 got, err := RsrcMgr.GetONUID(tt.args.ponIntfID)
592 if got != tt.want && err != nil {
593 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
594 }
595 })
596 }
597}
598
599func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
600
601 type args struct {
602 IntfID uint32
603 OnuID uint32
604 UniID uint32
605 }
606 tests := []struct {
607 name string
608 fields *fields
609 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000610 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200611 }{
612 {"GetTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000613 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200614 }
615 for _, tt := range tests {
616 t.Run(tt.name, func(t *testing.T) {
617 RsrcMgr := testResMgrObject(tt.fields)
618 if got := RsrcMgr.GetTechProfileIDForOnu(tt.args.IntfID, tt.args.OnuID, tt.args.UniID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
619 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
620 }
621 })
622 }
623}
624
625func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) {
626 type args struct {
627 ponIntfID uint32
628 onuID uint32
629 uniID uint32
630 flowStoreCookie uint64
631 }
632 tests := []struct {
633 name string
634 fields *fields
635 args args
636 want bool
637 }{
638 {"IsFlowCookieOnKVStore-1", getResMgr(), args{2, 2, 2, 2}, false},
639 }
640 for _, tt := range tests {
641 t.Run(tt.name, func(t *testing.T) {
642 RsrcMgr := testResMgrObject(tt.fields)
643 if got := RsrcMgr.IsFlowCookieOnKVStore(tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowStoreCookie); got != tt.want {
644 t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want)
645 }
646 })
647 }
648}
649
650func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
651
652 type args struct {
653 Direction string
654 IntfID uint32
655 OnuID uint32
656 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000657 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200658 }
659 tests := []struct {
660 name string
661 fields *fields
662 args args
663 wantErr error
664 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000665 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200666 errors.New("failed to delete meter id %s from kvstore")},
667 }
668 for _, tt := range tests {
669 t.Run(tt.name, func(t *testing.T) {
670 RsrcMgr := testResMgrObject(tt.fields)
Gamze Abakafee36392019-10-03 11:17:24 +0000671 if err := RsrcMgr.RemoveMeterIDForOnu(tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
672 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200673 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
674 }
675 })
676 }
677}
678
679func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
680 type args struct {
681 IntfID uint32
682 OnuID uint32
683 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000684 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200685 }
686 tests := []struct {
687 name string
688 fields *fields
689 args args
690 wantErr error
691 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000692 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200693 errors.New("failed to delete techprofile id resource %s in KV store")},
694 }
695 for _, tt := range tests {
696 t.Run(tt.name, func(t *testing.T) {
697 RsrcMgr := testResMgrObject(tt.fields)
Gamze Abakafee36392019-10-03 11:17:24 +0000698 if err := RsrcMgr.RemoveTechProfileIDForOnu(tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
699 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200700 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
701 }
702 })
703 }
704}
705
706func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
707 type args struct {
708 ponPort uint32
709 onuID uint32
710 uniID uint32
711 allocID []uint32
712 }
713 tests := []struct {
714 name string
715 fields *fields
716 args args
717 wantErr error
718 }{
719 {"UpdateAllocIdsForOnu-1", getResMgr(), args{2, 2, 2, []uint32{1, 2}},
720 errors.New("")},
721 }
722 for _, tt := range tests {
723 t.Run(tt.name, func(t *testing.T) {
724 RsrcMgr := testResMgrObject(tt.fields)
725 if err := RsrcMgr.UpdateAllocIdsForOnu(tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.allocID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
726 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
727 }
728 })
729 }
730}
731
732func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
733 type args struct {
734 ponIntfID int32
735 onuID int32
736 uniID int32
737 flowID uint32
738 flowData *[]FlowInfo
739 }
740 tests := []struct {
741 name string
742 fields *fields
743 args args
744 wantErr error
745 }{
746 {"UpdateFlowIDInfo-1", getResMgr(), args{2, 2, 2, 2, &[]FlowInfo{}}, errors.New("")},
747 }
748 for _, tt := range tests {
749 t.Run(tt.name, func(t *testing.T) {
750 RsrcMgr := testResMgrObject(tt.fields)
751 if err := RsrcMgr.UpdateFlowIDInfo(tt.args.ponIntfID, tt.args.onuID, tt.args.uniID, tt.args.flowID, tt.args.flowData); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
752 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
753 }
754 })
755 }
756}
757
758func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
759
760 type args struct {
761 ponPort uint32
762 onuID uint32
763 uniID uint32
764 GEMPortList []uint32
765 }
766 tests := []struct {
767 name string
768 fields *fields
769 args args
770 wantErr error
771 }{
772 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{2, 2, 2,
773 []uint32{1, 2}}, errors.New("failed to update resource")},
774 }
775 for _, tt := range tests {
776 t.Run(tt.name, func(t *testing.T) {
777 RsrcMgr := testResMgrObject(tt.fields)
778 if err := RsrcMgr.UpdateGEMPortIDsForOnu(tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.GEMPortList); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
779 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
780 }
781 })
782 }
783}
784
785func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
786 type args struct {
787 gemPorts []uint32
788 PonPort uint32
789 onuID uint32
790 uniID uint32
791 }
792 tests := []struct {
793 name string
794 fields *fields
795 args args
796 wantErr error
797 }{
798 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
799 2, 2, 2}, errors.New("failed to update resource")},
800 }
801 for _, tt := range tests {
802 t.Run(tt.name, func(t *testing.T) {
803 RsrcMgr := testResMgrObject(tt.fields)
Gamze Abakafee36392019-10-03 11:17:24 +0000804 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(tt.args.gemPorts, tt.args.PonPort,
805 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200806 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
807 }
808 })
809 }
810}
811
812func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
813 type args struct {
814 Direction string
815 IntfID uint32
816 OnuID uint32
817 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000818 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200819 MeterConfig *ofp.OfpMeterConfig
820 }
821 tests := []struct {
822 name string
823 fields *fields
824 args args
825 wantErr error
826 }{
827 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 2, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000828 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200829 }
830 for _, tt := range tests {
831 t.Run(tt.name, func(t *testing.T) {
832 RsrcMgr := testResMgrObject(tt.fields)
Gamze Abakafee36392019-10-03 11:17:24 +0000833 if err := RsrcMgr.UpdateMeterIDForOnu(tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
834 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200835 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
836 }
837 })
838 }
839}
840
841func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
842 type args struct {
843 IntfID uint32
844 OnuID uint32
845 UniID uint32
846 TpID uint32
847 }
848 tests := []struct {
849 name string
850 fields *fields
851 args args
852 wantErr error
853 }{
854 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{2, 2, 2,
855 2}, errors.New("failed to update resource")},
856 }
857 for _, tt := range tests {
858 t.Run(tt.name, func(t *testing.T) {
859 RsrcMgr := testResMgrObject(tt.fields)
860 if err := RsrcMgr.UpdateTechProfileIDForOnu(tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.TpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
861 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
862 }
863 })
864 }
865}
866
867func TestSetKVClient(t *testing.T) {
868 type args struct {
869 backend string
870 Host string
871 Port int
872 DeviceID string
873 }
874 tests := []struct {
875 name string
876 args args
877 want *model.Backend
878 }{
879 {"setKVClient-1", args{"consul", "1.1.1.1", 1, "olt1"}, &model.Backend{}},
880 {"setKVClient-1", args{"etcd", "2.2.2.2", 2, "olt2"}, &model.Backend{}},
881 }
882 for _, tt := range tests {
883 t.Run(tt.name, func(t *testing.T) {
884 if got := SetKVClient(tt.args.backend, tt.args.Host, tt.args.Port, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
885 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
886 }
887 })
888 }
889}
890
891func Test_getFlowIDFromFlowInfo(t *testing.T) {
892 type args struct {
893 FlowInfo *[]FlowInfo
894 flowID uint32
895 gemportID uint32
896 flowStoreCookie uint64
897 flowCategory string
898 vlanPcp []uint32
899 }
900 flowInfo := &[]FlowInfo{
901 {
902 &openolt.Flow{
903 FlowId: 1,
904 GemportId: 1,
905 Classifier: &openolt.Classifier{
906 OPbits: 1,
907 }},
908 1,
909 "HSIA_FLOW",
910 },
911 {
912 &openolt.Flow{
913 GemportId: 1,
914 },
915 1,
916 "EAPOL",
917 },
918 }
919 tests := []struct {
920 name string
921 args args
922 wantErr error
923 }{
924 {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")},
925 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
926 "HSIA_FLOW", []uint32{1, 2}}, errors.New("invalid flow-info")},
927 {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1,
928 "EAPOL", []uint32{1, 2}}, errors.New("invalid flow-info")},
929 }
930 for _, tt := range tests {
931 t.Run(tt.name, func(t *testing.T) {
932 err := getFlowIDFromFlowInfo(tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...)
933 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
934 t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr)
935 }
936 if err == nil {
937 t.Log("return'd nil")
938 }
939 })
940 }
941}
942
943func Test_newKVClient(t *testing.T) {
944 type args struct {
945 storeType string
946 address string
947 timeout uint32
948 }
949 var kvClient kvstore.Client
950 tests := []struct {
951 name string
952 args args
953 want kvstore.Client
954 wantErr error
955 }{
956 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
957 }
958 for _, tt := range tests {
959 t.Run(tt.name, func(t *testing.T) {
960 got, err := newKVClient(tt.args.storeType, tt.args.address, tt.args.timeout)
961 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
962 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
963 }
964 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
965 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
966 return
967 }
968
969 })
970 }
971}