blob: e71d07e23a2d5a7b35368029bdad6b15010c8b89 [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"
Matteo Scandolo84585372021-03-18 14:21:22 -070030 tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030031 "reflect"
32 "strconv"
33 "strings"
34 "sync"
35 "testing"
36 "time"
37
Girish Gowdraa09aeab2020-09-14 16:30:52 -070038 "github.com/opencord/voltha-lib-go/v4/pkg/db"
39 "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore"
40 fu "github.com/opencord/voltha-lib-go/v4/pkg/flows"
41 "github.com/opencord/voltha-lib-go/v4/pkg/log"
42 ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager"
43 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
44 "github.com/opencord/voltha-protos/v4/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020045)
46
47func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040048 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020049}
50
51const (
52 // MeterConfig meter to extract meter
53 MeterConfig = "meter_id"
54 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040055 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020056 // FlowIDInfo to extract flows
57 FlowIDInfo = "flow_id_info"
58 // FlowIds to extract flows
59 FlowIDs = "flow_ids"
60 // GemportIDs to gemport_ids
61 GemportIDs = "gemport_ids"
62 // AllocIDs to extract alloc_ids
63 AllocIDs = "alloc_ids"
64 // GemportIDPool to extract gemport
65 GemportIDPool = "gemport_id_pool"
66 // AllocIDPool to extract allocid
67 AllocIDPool = "alloc_id_pool"
68 // FlowIDpool to extract Flow ids
69 FlowIDpool = "flow_id_pool"
70)
71
cbabubef89432019-10-18 11:47:27 +020072// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020073type fields struct {
Girish Gowdra38d533d2020-03-30 20:38:51 -070074 DeviceID string
Neha Sharma3f221ae2020-04-29 19:02:12 +000075 Address string
Girish Gowdra38d533d2020-03-30 20:38:51 -070076 Args string
77 KVStore *db.Backend
78 DeviceType string
Girish Gowdra38d533d2020-03-30 20:38:51 -070079 DevInfo *openolt.DeviceInfo
80 ResourceMgrs map[uint32]*ponrmgr.PONResourceManager
81 NumOfPonPorts uint32
cbabuabf02352019-10-15 13:14:56 +020082}
cbabubef89432019-10-18 11:47:27 +020083
84// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020085type MockResKVClient struct {
86}
87
cbabubef89432019-10-18 11:47:27 +020088// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020089func getResMgr() *fields {
Matteo Scandolo84585372021-03-18 14:21:22 -070090 ctx := context.TODO()
cbabuabf02352019-10-15 13:14:56 +020091 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050092 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020093 Client: &MockResKVClient{},
94 }
95 resMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
96 ranges := make(map[string]interface{})
97 sharedIdxByType := make(map[string]string)
98 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
99 sharedIdxByType["ONU_ID"] = "ONU_ID"
100 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
101 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
102 ranges["ONU_ID"] = uint32(0)
103 ranges["GEMPORT_ID"] = uint32(0)
104 ranges["ALLOC_ID"] = uint32(0)
105 ranges["FLOW_ID"] = uint32(0)
106 ranges["onu_id_shared"] = uint32(0)
107 ranges["alloc_id_shared"] = uint32(0)
108 ranges["gemport_id_shared"] = uint32(0)
109 ranges["flow_id_shared"] = uint32(0)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700110 resMgr.NumOfPonPorts = 16
Matteo Scandolo84585372021-03-18 14:21:22 -0700111 ponMgr := &ponrmgr.PONResourceManager{}
112 tpMgr, err := tp.NewTechProfile(ctx, ponMgr, "etcd", "127.0.0.1", "/")
113 if err != nil {
114 logger.Fatal(ctx, err.Error())
cbabuabf02352019-10-15 13:14:56 +0200115 }
Matteo Scandolo84585372021-03-18 14:21:22 -0700116
117 ponMgr.DeviceID = "onu-1"
118 ponMgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
119 ponMgr.KVStore = &db.Backend{
120 Client: &MockResKVClient{},
121 }
122 ponMgr.PonResourceRanges = ranges
123 ponMgr.SharedIdxByType = sharedIdxByType
124 ponMgr.TechProfileMgr = tpMgr
125
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700126 var ponIntf uint32
127 for ponIntf = 0; ponIntf < resMgr.NumOfPonPorts; ponIntf++ {
128 resMgr.ResourceMgrs[ponIntf] = ponMgr
129 }
cbabuabf02352019-10-15 13:14:56 +0200130 return &resMgr
131}
cbabubef89432019-10-18 11:47:27 +0200132
133// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530134func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200135 return nil, errors.New("key didn't find")
136}
137
138// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530139func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000140 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200141 if key != "" {
142 if strings.Contains(key, MeterConfig) {
143 var bands []*ofp.OfpMeterBandHeader
144 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
145 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
146
147 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
148 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
149
Gamze Abakafee36392019-10-03 11:17:24 +0000150 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200151 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
152 if uint32(val) > 1 {
153 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
154 str, _ := json.Marshal(meterConfig)
155
156 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
157 }
158 return nil, errors.New("invalid meter")
159 }
160 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000161 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200162 data := make(map[string]interface{})
163 data["pool"] = "1024"
164 data["start_idx"] = 1
165 data["end_idx"] = 1024
166 str, _ := json.Marshal(data)
167 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
168 }
169 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000170 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200171 str, _ := json.Marshal([]uint32{1, 2})
172 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
173 }
174 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000175 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200176 str, _ := json.Marshal(1)
177 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
178 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000179 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000180 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000181 mcastQueues := make(map[uint32][]uint32)
182 mcastQueues[10] = []uint32{4000, 0}
183 str, _ := json.Marshal(mcastQueues)
184 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
185 }
186 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
187 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
188 str, _ := json.Marshal(groupInfo)
189 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
190 }
191
cbabuabf02352019-10-15 13:14:56 +0200192 maps := make(map[string]*kvstore.KVPair)
193 maps[key] = &kvstore.KVPair{Key: key}
194 return maps[key], nil
195 }
196 return nil, errors.New("key didn't find")
197}
198
199// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530200func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200201 if key != "" {
202 return nil
203 }
204 return errors.New("key didn't find")
205}
206
207// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530208func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200209 return nil
210}
211
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300212// DeleteWithPrefix mock function implementation for KVClient
213func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
214 return nil
215}
216
cbabuabf02352019-10-15 13:14:56 +0200217// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000218func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200219 return nil, errors.New("key didn't find")
220}
221
222// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530223func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200224 return nil
225}
226
227// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530228func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200229 return nil
230}
231
232// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530233func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200234 return nil
235}
236
237// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800238func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200239 return nil
240}
241
242// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000243func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200244 return nil
245}
246
247// ReleaseLock mock function implementation for KVClient
248func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
249 return nil
250}
251
252// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530253func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200254 return true
255}
256
257// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000258func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200259}
260
261// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000262func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200263}
264
cbabubef89432019-10-18 11:47:27 +0200265// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200266func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700267 var rsrMgr = OpenOltResourceMgr{
cbabuabf02352019-10-15 13:14:56 +0200268 DeviceID: testResMgr.DeviceID,
cbabuabf02352019-10-15 13:14:56 +0200269 Args: testResMgr.Args,
270 KVStore: testResMgr.KVStore,
271 DeviceType: testResMgr.DeviceType,
Neha Sharma3f221ae2020-04-29 19:02:12 +0000272 Address: testResMgr.Address,
cbabuabf02352019-10-15 13:14:56 +0200273 DevInfo: testResMgr.DevInfo,
274 ResourceMgrs: testResMgr.ResourceMgrs,
275 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700276
277 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
278 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
279 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, testResMgr.NumOfPonPorts)
280
281 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200282}
283
284func TestNewResourceMgr(t *testing.T) {
285 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000286 deviceID string
287 KVStoreAddress string
288 kvStoreType string
289 deviceType string
290 devInfo *openolt.DeviceInfo
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800291 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200292 }
293 tests := []struct {
294 name string
295 args args
296 want *OpenOltResourceMgr
297 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300298 {"NewResourceMgr-2", args{"olt1", "1:2", "etcd",
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800299 "onu", &openolt.DeviceInfo{OnuIdStart: 1, OnuIdEnd: 1}, "service/voltha"}, &OpenOltResourceMgr{}},
cbabuabf02352019-10-15 13:14:56 +0200300 }
301 for _, tt := range tests {
302 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530303 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
304 defer cancel()
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800305 if got := NewResourceMgr(ctx, tt.args.deviceID, tt.args.KVStoreAddress, tt.args.kvStoreType, tt.args.deviceType, tt.args.devInfo, tt.args.kvStorePrefix); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200306 t.Errorf("NewResourceMgr() = %v, want %v", got, tt.want)
307 }
308 })
309 }
310}
311
312func TestOpenOltResourceMgr_Delete(t *testing.T) {
313 tests := []struct {
314 name string
315 fields *fields
316 wantErr error
317 }{
318 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool")},
319 }
320 for _, tt := range tests {
321 t.Run(tt.name, func(t *testing.T) {
322 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530323 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
324 defer cancel()
325 if err := RsrcMgr.Delete(ctx); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200326 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
327 }
328 })
329 }
330}
331
cbabuabf02352019-10-15 13:14:56 +0200332func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
333 type args struct {
334 intfID uint32
335 onuID uint32
336 uniID uint32
337 }
338 tests := []struct {
339 name string
340 fields *fields
341 args args
342 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700343 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200344 }
345 for _, tt := range tests {
346 t.Run(tt.name, func(t *testing.T) {
347 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530348 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
349 defer cancel()
350 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200351 })
352 }
353}
354
355func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
356 type args struct {
357 intfID uint32
358 onuID []uint32
359 }
360 tests := []struct {
361 name string
362 fields *fields
363 args args
364 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700365 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200366 }
367 for _, tt := range tests {
368 t.Run(tt.name, func(t *testing.T) {
369 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530370 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
371 defer cancel()
372 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200373 })
374 }
375}
376
377func TestOpenOltResourceMgr_GetAllocID(t *testing.T) {
378
379 type args struct {
380 intfID uint32
381 onuID uint32
382 uniID uint32
383 }
384 tests := []struct {
385 name string
386 fields *fields
387 args args
388 want uint32
389 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700390 {"GetAllocID-1", getResMgr(), args{1, 2, 2}, 0},
cbabuabf02352019-10-15 13:14:56 +0200391 }
392 for _, tt := range tests {
393 t.Run(tt.name, func(t *testing.T) {
394 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530395 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
396 defer cancel()
397 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 +0200398 t.Errorf("GetAllocID() = %v, want %v", got, tt.want)
399 }
400 })
401 }
402}
403
404func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
405 type args struct {
406 intfID uint32
407 onuID uint32
408 uniID uint32
409 }
410 tests := []struct {
411 name string
412 fields *fields
413 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000414 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200415 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700416 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200417 }
418 for _, tt := range tests {
419 t.Run(tt.name, func(t *testing.T) {
420 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530421 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
422 defer cancel()
423 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 +0000424 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200425 }
426 })
427 }
428}
429
430func TestOpenOltResourceMgr_GetCurrentFlowIDsForOnu(t *testing.T) {
431
432 type args struct {
433 PONIntfID uint32
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530434 ONUID int32
435 UNIID int32
cbabuabf02352019-10-15 13:14:56 +0200436 }
437 tests := []struct {
438 name string
439 fields *fields
440 args args
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700441 want []uint64
cbabuabf02352019-10-15 13:14:56 +0200442 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700443 {"GetCurrentFlowIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint64{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200444 }
445 for _, tt := range tests {
446 t.Run(tt.name, func(t *testing.T) {
447 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530448 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
449 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700450 got, err := RsrcMgr.GetCurrentFlowIDsForOnu(ctx, tt.args.PONIntfID, tt.args.ONUID, tt.args.UNIID)
451 if err != nil {
452 t.Errorf("GetCurrentFlowIDsForOnu() returned error")
453 }
454 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200455 t.Errorf("GetCurrentFlowIDsForOnu() = %v, want %v", got, tt.want)
456 }
457 })
458 }
459}
460
461func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
462 type args struct {
463 intfID uint32
464 onuID uint32
465 uniID uint32
466 }
467 tests := []struct {
468 name string
469 fields *fields
470 args args
471 want []uint32
472 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700473 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200474 }
475 for _, tt := range tests {
476 t.Run(tt.name, func(t *testing.T) {
477 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530478 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
479 defer cancel()
480 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 +0200481 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
482 }
483 })
484 }
485}
486
cbabuabf02352019-10-15 13:14:56 +0200487func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) {
488 type args struct {
489 ponPort uint32
490 onuID uint32
491 uniID uint32
492 NumOfPorts uint32
493 }
494 tests := []struct {
495 name string
496 fields *fields
497 args args
498 want []uint32
499 wantErr error
500 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700501 {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{},
cbabuabf02352019-10-15 13:14:56 +0200502 errors.New("failed to get gem port")},
503 }
504 for _, tt := range tests {
505 t.Run(tt.name, func(t *testing.T) {
506 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530507 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
508 defer cancel()
509 got, err := RsrcMgr.GetGEMPortID(ctx, tt.args.ponPort, tt.args.onuID, tt.args.uniID, tt.args.NumOfPorts)
cbabuabf02352019-10-15 13:14:56 +0200510 if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
511 t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr)
512 return
513 }
514 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
515 t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want)
516 }
517 })
518 }
519}
520
521func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) {
522 type args struct {
523 Direction string
524 IntfID uint32
525 OnuID uint32
526 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000527 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200528 }
529 tests := []struct {
530 name string
531 fields *fields
532 args args
533 want *ofp.OfpMeterConfig
534 wantErr error
535 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700536 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200537 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700538 {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
cbabuabf02352019-10-15 13:14:56 +0200539 &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")},
540 }
541 for _, tt := range tests {
542 t.Run(tt.name, func(t *testing.T) {
543 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530544 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
545 defer cancel()
546 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 +0200547 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
548 t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want)
549 }
550 })
551 }
552}
553
554func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
555 type args struct {
556 ponIntfID uint32
557 }
558 tests := []struct {
559 name string
560 fields *fields
561 args args
562 want uint32
563 wantErr error
564 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700565 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200566 }
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.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200573 if got != tt.want && err != nil {
574 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
575 }
576 })
577 }
578}
579
580func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
581
582 type args struct {
583 IntfID uint32
584 OnuID uint32
585 UniID uint32
586 }
587 tests := []struct {
588 name string
589 fields *fields
590 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000591 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200592 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700593 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000594 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200595 }
596 for _, tt := range tests {
597 t.Run(tt.name, func(t *testing.T) {
598 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530599 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
600 defer cancel()
601 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 +0200602 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
603 }
604 })
605 }
606}
607
cbabuabf02352019-10-15 13:14:56 +0200608func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
609
610 type args struct {
611 Direction string
612 IntfID uint32
613 OnuID uint32
614 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000615 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200616 }
617 tests := []struct {
618 name string
619 fields *fields
620 args args
621 wantErr error
622 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000623 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200624 errors.New("failed to delete meter id %s from kvstore")},
625 }
626 for _, tt := range tests {
627 t.Run(tt.name, func(t *testing.T) {
628 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530629 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
630 defer cancel()
631 if err := RsrcMgr.RemoveMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000632 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200633 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
634 }
635 })
636 }
637}
638
639func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
640 type args struct {
641 IntfID uint32
642 OnuID uint32
643 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000644 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200645 }
646 tests := []struct {
647 name string
648 fields *fields
649 args args
650 wantErr error
651 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700652 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200653 errors.New("failed to delete techprofile id resource %s in KV store")},
654 }
655 for _, tt := range tests {
656 t.Run(tt.name, func(t *testing.T) {
657 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530658 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
659 defer cancel()
660 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000661 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200662 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
663 }
664 })
665 }
666}
667
668func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
669 type args struct {
670 ponPort uint32
671 onuID uint32
672 uniID uint32
673 allocID []uint32
674 }
675 tests := []struct {
676 name string
677 fields *fields
678 args args
679 wantErr error
680 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700681 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200682 errors.New("")},
683 }
684 for _, tt := range tests {
685 t.Run(tt.name, func(t *testing.T) {
686 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530687 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
688 defer cancel()
689 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 +0200690 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
691 }
692 })
693 }
694}
695
696func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) {
697 type args struct {
698 ponIntfID int32
699 onuID int32
700 uniID int32
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700701 flowID uint64
702 flowData FlowInfo
cbabuabf02352019-10-15 13:14:56 +0200703 }
704 tests := []struct {
705 name string
706 fields *fields
707 args args
708 wantErr error
709 }{
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700710 {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, FlowInfo{}}, errors.New("")},
cbabuabf02352019-10-15 13:14:56 +0200711 }
712 for _, tt := range tests {
713 t.Run(tt.name, func(t *testing.T) {
714 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530715 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
716 defer cancel()
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700717 if err := RsrcMgr.UpdateFlowIDInfo(ctx, uint32(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 +0200718 t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr)
719 }
720 })
721 }
722}
723
724func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
725
726 type args struct {
727 ponPort uint32
728 onuID uint32
729 uniID uint32
730 GEMPortList []uint32
731 }
732 tests := []struct {
733 name string
734 fields *fields
735 args args
736 wantErr error
737 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700738 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200739 []uint32{1, 2}}, errors.New("failed to update resource")},
740 }
741 for _, tt := range tests {
742 t.Run(tt.name, func(t *testing.T) {
743 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530744 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
745 defer cancel()
746 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 +0200747 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
748 }
749 })
750 }
751}
752
753func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) {
754 type args struct {
755 gemPorts []uint32
756 PonPort uint32
757 onuID uint32
758 uniID uint32
759 }
760 tests := []struct {
761 name string
762 fields *fields
763 args args
764 wantErr error
765 }{
766 {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700767 1, 2, 2}, errors.New("failed to update resource")},
cbabuabf02352019-10-15 13:14:56 +0200768 }
769 for _, tt := range tests {
770 t.Run(tt.name, func(t *testing.T) {
771 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530772 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
773 defer cancel()
774 if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort,
Gamze Abakafee36392019-10-03 11:17:24 +0000775 tt.args.onuID, tt.args.uniID); err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200776 t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr)
777 }
778 })
779 }
780}
781
782func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
783 type args struct {
784 Direction string
785 IntfID uint32
786 OnuID uint32
787 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000788 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200789 MeterConfig *ofp.OfpMeterConfig
790 }
791 tests := []struct {
792 name string
793 fields *fields
794 args args
795 wantErr error
796 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700797 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Gamze Abakafee36392019-10-03 11:17:24 +0000798 2, 64, &ofp.OfpMeterConfig{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200799 }
800 for _, tt := range tests {
801 t.Run(tt.name, func(t *testing.T) {
802 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530803 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
804 defer cancel()
805 if err := RsrcMgr.UpdateMeterIDForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000806 tt.args.tpID, tt.args.MeterConfig); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200807 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
808 }
809 })
810 }
811}
812
813func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
814 type args struct {
815 IntfID uint32
816 OnuID uint32
817 UniID uint32
818 TpID uint32
819 }
820 tests := []struct {
821 name string
822 fields *fields
823 args args
824 wantErr error
825 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700826 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200827 2}, errors.New("failed to update resource")},
828 }
829 for _, tt := range tests {
830 t.Run(tt.name, func(t *testing.T) {
831 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530832 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
833 defer cancel()
834 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 +0200835 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
836 }
837 })
838 }
839}
840
841func TestSetKVClient(t *testing.T) {
842 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800843 backend string
844 address string
845 DeviceID string
846 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200847 }
848 tests := []struct {
849 name string
850 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500851 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200852 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300853 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200854 }
855 for _, tt := range tests {
856 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800857 if got := SetKVClient(context.Background(), tt.args.backend, tt.args.address, tt.args.DeviceID, tt.args.kvStorePrefix); reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
cbabuabf02352019-10-15 13:14:56 +0200858 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
859 }
860 })
861 }
862}
863
cbabuabf02352019-10-15 13:14:56 +0200864func Test_newKVClient(t *testing.T) {
865 type args struct {
866 storeType string
867 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000868 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200869 }
870 var kvClient kvstore.Client
871 tests := []struct {
872 name string
873 args args
874 want kvstore.Client
875 wantErr error
876 }{
877 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
878 }
879 for _, tt := range tests {
880 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000881 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200882 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
883 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
884 }
885 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
886 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
887 return
888 }
889
890 })
891 }
892}
Esin Karamanccb714b2019-11-29 15:02:06 +0000893
894func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
895 type args struct {
896 intf uint32
897 gem uint32
898 servicePriority uint32
899 }
900 tests := []struct {
901 name string
902 args args
903 fields *fields
904 }{
905 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
906 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
907 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
908 }
909 for _, tt := range tests {
910 t.Run(tt.name, func(t *testing.T) {
911 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530912 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
913 defer cancel()
914 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000915 if err != nil {
916 t.Errorf("%s got err= %s wants nil", tt.name, err)
917 return
918 }
919 })
920 }
921}
922
923func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
924 groupDesc := ofp.OfpGroupDesc{
925 Type: ofp.OfpGroupType_OFPGT_ALL,
926 GroupId: groupID,
927 }
928 groupEntry := ofp.OfpGroupEntry{
929 Desc: &groupDesc,
930 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000931 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000932 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +0000933 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +0000934 bucket := ofp.OfpBucket{
935 Actions: acts,
936 }
937 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +0000938 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000939 return &groupEntry
940}
941
942func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
943 type args struct {
944 group *ofp.OfpGroupEntry
945 cached bool
946 }
947 //create group 1
948 group1 := newGroup(1, []uint32{1})
949 //create group 2
950 group2 := newGroup(2, []uint32{2})
951 //define test set
952 tests := []struct {
953 name string
954 args args
955 fields *fields
956 }{
957 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
958 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
959 }
960 //execute tests
961 for _, tt := range tests {
962 t.Run(tt.name, func(t *testing.T) {
963 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530964 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
965 defer cancel()
966 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +0000967 if err != nil {
968 t.Errorf("%s got err= %s wants nil", tt.name, err)
969 return
970 }
971 })
972 }
973}
974
975func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
976 type args struct {
977 groupID uint32
978 cached bool
979 }
980 //define test set
981 tests := []struct {
982 name string
983 args args
984 fields *fields
985 }{
986 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
987 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
988 }
989 //execute tests
990 for _, tt := range tests {
991 t.Run(tt.name, func(t *testing.T) {
992 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530993 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
994 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +0000995 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
996 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +0000997 t.Errorf("%s got false but wants true", tt.name)
998 return
999 }
1000 })
1001 }
1002}
1003
1004func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1005 type args struct {
1006 groupID uint32
1007 cached bool
1008 }
1009 //define test set
1010 tests := []struct {
1011 name string
1012 args args
1013 fields *fields
1014 }{
1015 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1016 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1017 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1018 }
1019 //execute tests
1020 for _, tt := range tests {
1021 t.Run(tt.name, func(t *testing.T) {
1022 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301023 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1024 defer cancel()
1025 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001026 if err != nil {
1027 t.Errorf("%s got error but wants nil error", tt.name)
1028 return
1029 } else if exists && (groupInfo.GroupID == 0) {
1030 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1031 return
1032 } else if tt.args.groupID == 3 && exists {
1033 t.Errorf("%s got true but wants false", tt.name)
1034 return
1035 }
1036 })
1037 }
1038}