blob: e3fa49c30d32464b1e34e9da796ce998eff8726a [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"
yasin sapli9e4c5092022-02-01 13:52:33 +000030 "fmt"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030031 "reflect"
32 "strconv"
33 "strings"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030034 "testing"
35 "time"
36
khenaidoo106c61a2021-08-11 18:05:46 -040037 "github.com/opencord/voltha-lib-go/v7/pkg/techprofile"
38 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
39
40 "github.com/opencord/voltha-lib-go/v7/pkg/db"
41 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
42 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
43 "github.com/opencord/voltha-lib-go/v7/pkg/log"
44 ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager"
45 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
46 "github.com/opencord/voltha-protos/v5/go/openolt"
cbabuabf02352019-10-15 13:14:56 +020047)
48
49func init() {
Kent Hagermane6ff1012020-07-14 15:07:53 -040050 _, _ = log.SetDefaultLogger(log.JSON, log.DebugLevel, nil)
cbabuabf02352019-10-15 13:14:56 +020051}
52
53const (
54 // MeterConfig meter to extract meter
55 MeterConfig = "meter_id"
56 // TpIDSuffixPath to extract Techprofile
Kent Hagermane6ff1012020-07-14 15:07:53 -040057 // TpIDSuffixPath = "tp_id"
cbabuabf02352019-10-15 13:14:56 +020058 // FlowIDInfo to extract flows
59 FlowIDInfo = "flow_id_info"
60 // FlowIds to extract flows
61 FlowIDs = "flow_ids"
62 // GemportIDs to gemport_ids
63 GemportIDs = "gemport_ids"
64 // AllocIDs to extract alloc_ids
65 AllocIDs = "alloc_ids"
66 // GemportIDPool to extract gemport
67 GemportIDPool = "gemport_id_pool"
68 // AllocIDPool to extract allocid
69 AllocIDPool = "alloc_id_pool"
70 // FlowIDpool to extract Flow ids
71 FlowIDpool = "flow_id_pool"
72)
73
cbabubef89432019-10-18 11:47:27 +020074// fields mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020075type fields struct {
Girish Gowdra76a1b092021-07-28 10:07:04 -070076 DeviceID string
77 Address string
78 Args string
79 KVStore *db.Backend
80 DeviceType string
81 DevInfo *openolt.DeviceInfo
82 PonRsrMgr *ponrmgr.PONResourceManager
83 NumOfPonPorts uint32
84 TechProfileRef techprofile.TechProfileIf
cbabuabf02352019-10-15 13:14:56 +020085}
cbabubef89432019-10-18 11:47:27 +020086
87// MockKVClient mocks the AdapterProxy interface.
cbabuabf02352019-10-15 13:14:56 +020088type MockResKVClient struct {
89}
90
cbabubef89432019-10-18 11:47:27 +020091// getResMgr mocks OpenOltResourceMgr struct.
cbabuabf02352019-10-15 13:14:56 +020092func getResMgr() *fields {
93 var resMgr fields
sbarbaria8910ba2019-11-05 10:12:23 -050094 resMgr.KVStore = &db.Backend{
cbabuabf02352019-10-15 13:14:56 +020095 Client: &MockResKVClient{},
96 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070097 resMgr.PonRsrMgr = &ponrmgr.PONResourceManager{}
cbabuabf02352019-10-15 13:14:56 +020098 ranges := make(map[string]interface{})
99 sharedIdxByType := make(map[string]string)
100 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
101 sharedIdxByType["ONU_ID"] = "ONU_ID"
102 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
103 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
104 ranges["ONU_ID"] = uint32(0)
105 ranges["GEMPORT_ID"] = uint32(0)
106 ranges["ALLOC_ID"] = uint32(0)
107 ranges["FLOW_ID"] = uint32(0)
108 ranges["onu_id_shared"] = uint32(0)
109 ranges["alloc_id_shared"] = uint32(0)
110 ranges["gemport_id_shared"] = uint32(0)
111 ranges["flow_id_shared"] = uint32(0)
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700112 resMgr.NumOfPonPorts = 16
yasin saplid0566272021-12-21 09:10:30 +0000113 resMgr.DevInfo = &openolt.DeviceInfo{PonPorts: 16}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700114 resMgr.PonRsrMgr.DeviceID = "onu-1"
115 resMgr.PonRsrMgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
116 resMgr.PonRsrMgr.KVStore = &db.Backend{
Matteo Scandolo84585372021-03-18 14:21:22 -0700117 Client: &MockResKVClient{},
118 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700119 resMgr.PonRsrMgr.Technology = "XGS-PON"
120 resMgr.PonRsrMgr.PonResourceRanges = ranges
121 resMgr.PonRsrMgr.SharedIdxByType = sharedIdxByType
Girish Gowdra76a1b092021-07-28 10:07:04 -0700122 resMgr.TechProfileRef = mocks.MockTechProfile{}
123
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700124 /*
125 tpMgr, err := tp.NewTechProfile(ctx, resMgr.PonRsrMgr, "etcd", "127.0.0.1", "/")
126 if err != nil {
127 logger.Fatal(ctx, err.Error())
128 }
129 */
Matteo Scandolo84585372021-03-18 14:21:22 -0700130
cbabuabf02352019-10-15 13:14:56 +0200131 return &resMgr
132}
cbabubef89432019-10-18 11:47:27 +0200133
134// List function implemented for KVClient.
npujarec5762e2020-01-01 14:08:48 +0530135func (kvclient *MockResKVClient) List(ctx context.Context, key string) (map[string]*kvstore.KVPair, error) {
cbabuabf02352019-10-15 13:14:56 +0200136 return nil, errors.New("key didn't find")
137}
138
139// Get mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530140func (kvclient *MockResKVClient) Get(ctx context.Context, key string) (*kvstore.KVPair, error) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000141 logger.Debugw(ctx, "Warning Warning Warning: Get of MockKVClient called", log.Fields{"key": key})
cbabuabf02352019-10-15 13:14:56 +0200142 if key != "" {
143 if strings.Contains(key, MeterConfig) {
144 var bands []*ofp.OfpMeterBandHeader
145 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
146 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 2}}})
147
148 bands = append(bands, &ofp.OfpMeterBandHeader{Type: ofp.OfpMeterBandType_OFPMBT_DSCP_REMARK,
149 Rate: 1024, Data: &ofp.OfpMeterBandHeader_DscpRemark{DscpRemark: &ofp.OfpMeterBandDscpRemark{PrecLevel: 3}}})
150
Gamze Abakafee36392019-10-03 11:17:24 +0000151 sep := strings.Split(key, "/")[1]
cbabuabf02352019-10-15 13:14:56 +0200152 val, _ := strconv.ParseInt(strings.Split(sep, ",")[1], 10, 32)
153 if uint32(val) > 1 {
154 meterConfig := &ofp.OfpMeterConfig{MeterId: uint32(val), Bands: bands}
155 str, _ := json.Marshal(meterConfig)
156
157 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
158 }
159 return nil, errors.New("invalid meter")
160 }
161 if strings.Contains(key, FlowIDpool) || strings.Contains(key, GemportIDPool) || strings.Contains(key, AllocIDPool) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000162 logger.Debug(ctx, "Error Error Error Key:", FlowIDpool, GemportIDPool, AllocIDPool)
cbabuabf02352019-10-15 13:14:56 +0200163 data := make(map[string]interface{})
164 data["pool"] = "1024"
165 data["start_idx"] = 1
166 data["end_idx"] = 1024
167 str, _ := json.Marshal(data)
168 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
169 }
170 if strings.Contains(key, FlowIDInfo) || strings.Contains(key, FlowIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000171 logger.Debug(ctx, "Error Error Error Key:", FlowIDs, FlowIDInfo)
cbabuabf02352019-10-15 13:14:56 +0200172 str, _ := json.Marshal([]uint32{1, 2})
173 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
174 }
175 if strings.Contains(key, AllocIDs) || strings.Contains(key, GemportIDs) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000176 logger.Debug(ctx, "Error Error Error Key:", AllocIDs, GemportIDs)
cbabuabf02352019-10-15 13:14:56 +0200177 str, _ := json.Marshal(1)
178 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
179 }
Esin Karamanccb714b2019-11-29 15:02:06 +0000180 if strings.Contains(key, McastQueuesForIntf) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000181 logger.Debug(ctx, "Error Error Error Key:", McastQueuesForIntf)
Esin Karamanccb714b2019-11-29 15:02:06 +0000182 mcastQueues := make(map[uint32][]uint32)
183 mcastQueues[10] = []uint32{4000, 0}
184 str, _ := json.Marshal(mcastQueues)
185 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
186 }
187 if strings.Contains(key, "flow_groups") && !strings.Contains(key, "1000") {
188 groupInfo := GroupInfo{GroupID: 2, OutPorts: []uint32{2}}
189 str, _ := json.Marshal(groupInfo)
190 return kvstore.NewKVPair(key, str, "mock", 3000, 1), nil
191 }
192
cbabuabf02352019-10-15 13:14:56 +0200193 maps := make(map[string]*kvstore.KVPair)
194 maps[key] = &kvstore.KVPair{Key: key}
195 return maps[key], nil
196 }
197 return nil, errors.New("key didn't find")
198}
199
200// Put mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530201func (kvclient *MockResKVClient) Put(ctx context.Context, key string, value interface{}) error {
cbabuabf02352019-10-15 13:14:56 +0200202 if key != "" {
203 return nil
204 }
205 return errors.New("key didn't find")
206}
207
208// Delete mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530209func (kvclient *MockResKVClient) Delete(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200210 return nil
211}
212
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300213// DeleteWithPrefix mock function implementation for KVClient
214func (kvclient *MockResKVClient) DeleteWithPrefix(ctx context.Context, prefix string) error {
215 return nil
216}
217
cbabuabf02352019-10-15 13:14:56 +0200218// Reserve mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000219func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) {
cbabuabf02352019-10-15 13:14:56 +0200220 return nil, errors.New("key didn't find")
221}
222
223// ReleaseReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530224func (kvclient *MockResKVClient) ReleaseReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200225 return nil
226}
227
228// ReleaseAllReservations mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530229func (kvclient *MockResKVClient) ReleaseAllReservations(ctx context.Context) error {
cbabuabf02352019-10-15 13:14:56 +0200230 return nil
231}
232
233// RenewReservation mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530234func (kvclient *MockResKVClient) RenewReservation(ctx context.Context, key string) error {
cbabuabf02352019-10-15 13:14:56 +0200235 return nil
236}
237
238// Watch mock function implementation for KVClient
Scott Bakere701b862020-02-20 16:19:16 -0800239func (kvclient *MockResKVClient) Watch(ctx context.Context, key string, withPrefix bool) chan *kvstore.Event {
cbabuabf02352019-10-15 13:14:56 +0200240 return nil
241}
242
243// AcquireLock mock function implementation for KVClient
Neha Sharmacc656962020-04-14 14:26:11 +0000244func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error {
cbabuabf02352019-10-15 13:14:56 +0200245 return nil
246}
247
248// ReleaseLock mock function implementation for KVClient
249func (kvclient *MockResKVClient) ReleaseLock(lockName string) error {
250 return nil
251}
252
253// IsConnectionUp mock function implementation for KVClient
npujarec5762e2020-01-01 14:08:48 +0530254func (kvclient *MockResKVClient) IsConnectionUp(ctx context.Context) bool { // timeout in second
cbabuabf02352019-10-15 13:14:56 +0200255 return true
256}
257
258// CloseWatch mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000259func (kvclient *MockResKVClient) CloseWatch(ctx context.Context, key string, ch chan *kvstore.Event) {
cbabuabf02352019-10-15 13:14:56 +0200260}
261
262// Close mock function implementation for KVClient
Neha Sharma96b7bf22020-06-15 10:37:32 +0000263func (kvclient *MockResKVClient) Close(ctx context.Context) {
cbabuabf02352019-10-15 13:14:56 +0200264}
265
cbabubef89432019-10-18 11:47:27 +0200266// testResMgrObject maps fields type to OpenOltResourceMgr type.
cbabuabf02352019-10-15 13:14:56 +0200267func testResMgrObject(testResMgr *fields) *OpenOltResourceMgr {
Girish Gowdra38d533d2020-03-30 20:38:51 -0700268 var rsrMgr = OpenOltResourceMgr{
Girish Gowdra76a1b092021-07-28 10:07:04 -0700269 DeviceID: testResMgr.DeviceID,
270 Args: testResMgr.Args,
271 KVStore: testResMgr.KVStore,
272 DeviceType: testResMgr.DeviceType,
273 Address: testResMgr.Address,
274 DevInfo: testResMgr.DevInfo,
275 PonRsrMgr: testResMgr.PonRsrMgr,
276 TechprofileRef: testResMgr.TechProfileRef,
cbabuabf02352019-10-15 13:14:56 +0200277 }
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700278 rsrMgr.InitLocalCache()
Girish Gowdra38d533d2020-03-30 20:38:51 -0700279
280 return &rsrMgr
cbabuabf02352019-10-15 13:14:56 +0200281}
282
283func TestNewResourceMgr(t *testing.T) {
284 type args struct {
Neha Sharma3f221ae2020-04-29 19:02:12 +0000285 deviceID string
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700286 intfID uint32
Neha Sharma3f221ae2020-04-29 19:02:12 +0000287 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 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700298 {"NewResourceMgr-2", args{"olt1", 0, "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()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700305 if got := NewResourceMgr(ctx, tt.args.intfID, 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) {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700313 type args struct {
314 intfID uint32
315 }
cbabuabf02352019-10-15 13:14:56 +0200316 tests := []struct {
317 name string
318 fields *fields
319 wantErr error
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700320 args args
cbabuabf02352019-10-15 13:14:56 +0200321 }{
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700322 {"Delete-1", getResMgr(), errors.New("failed to clear device resource pool"), args{intfID: 0}},
cbabuabf02352019-10-15 13:14:56 +0200323 }
324 for _, tt := range tests {
325 t.Run(tt.name, func(t *testing.T) {
326 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530327 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
328 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700329 if err := RsrcMgr.Delete(ctx, tt.args.intfID); (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
cbabuabf02352019-10-15 13:14:56 +0200330 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
331 }
332 })
333 }
334}
335
cbabuabf02352019-10-15 13:14:56 +0200336func TestOpenOltResourceMgr_FreePONResourcesForONU(t *testing.T) {
337 type args struct {
338 intfID uint32
339 onuID uint32
340 uniID uint32
341 }
342 tests := []struct {
343 name string
344 fields *fields
345 args args
346 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700347 {"FreePONResourcesForONU-1", getResMgr(), args{1, 0, 2}},
cbabuabf02352019-10-15 13:14:56 +0200348 }
349 for _, tt := range tests {
350 t.Run(tt.name, func(t *testing.T) {
351 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530352 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
353 defer cancel()
354 RsrcMgr.FreePONResourcesForONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
cbabuabf02352019-10-15 13:14:56 +0200355 })
356 }
357}
358
359func TestOpenOltResourceMgr_FreeonuID(t *testing.T) {
360 type args struct {
361 intfID uint32
362 onuID []uint32
363 }
364 tests := []struct {
365 name string
366 fields *fields
367 args args
368 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700369 {"FreeOnuID-1", getResMgr(), args{1, []uint32{1, 2}}},
cbabuabf02352019-10-15 13:14:56 +0200370 }
371 for _, tt := range tests {
372 t.Run(tt.name, func(t *testing.T) {
373 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530374 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
375 defer cancel()
376 RsrcMgr.FreeonuID(ctx, tt.args.intfID, tt.args.onuID)
cbabuabf02352019-10-15 13:14:56 +0200377 })
378 }
379}
380
cbabuabf02352019-10-15 13:14:56 +0200381func TestOpenOltResourceMgr_GetCurrentAllocIDForOnu(t *testing.T) {
382 type args struct {
383 intfID uint32
384 onuID uint32
385 uniID uint32
386 }
387 tests := []struct {
388 name string
389 fields *fields
390 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000391 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200392 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700393 {"GetCurrentAllocIDForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200394 }
395 for _, tt := range tests {
396 t.Run(tt.name, func(t *testing.T) {
397 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530398 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
399 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700400 got := RsrcMgr.GetCurrentAllocIDsForOnu(ctx, tt.args.intfID, tt.args.onuID, tt.args.uniID)
401 if len(got) != len(tt.want) {
Gamze Abakafee36392019-10-03 11:17:24 +0000402 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700403 } else {
404 for i := range tt.want {
405 if got[i] != tt.want[i] {
406 t.Errorf("GetCurrentAllocIDsForOnu() = %v, want %v", got, tt.want)
407 break
408 }
409 }
cbabuabf02352019-10-15 13:14:56 +0200410 }
411 })
412 }
413}
414
Girish Gowdra950326e2021-11-05 12:43:24 -0700415func TestOpenOltResourceMgr_DeleteAllFlowIDsForGemForIntf(t *testing.T) {
416
417 type args struct {
418 PONIntfID uint32
419 }
420 tests := []struct {
421 name string
422 fields *fields
423 args args
424 want error
425 }{
426 {"DeleteAllFlowIDsForGemForIntf-1", getResMgr(), args{0}, nil},
427 }
428 for _, tt := range tests {
429 t.Run(tt.name, func(t *testing.T) {
430 RsrcMgr := testResMgrObject(tt.fields)
431 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
432 defer cancel()
433 err := RsrcMgr.DeleteAllFlowIDsForGemForIntf(ctx, tt.args.PONIntfID)
434 if err != nil {
435 t.Errorf("DeleteAllFlowIDsForGemForIntf() returned error")
436 }
437 })
438 }
439}
440
441func TestOpenOltResourceMgr_DeleteAllOnuGemInfoForIntf(t *testing.T) {
442
443 type args struct {
444 PONIntfID uint32
445 }
446 tests := []struct {
447 name string
448 fields *fields
449 args args
450 want error
451 }{
452 {"DeleteAllOnuGemInfoForIntf-1", getResMgr(), args{0}, nil},
453 }
454 for _, tt := range tests {
455 t.Run(tt.name, func(t *testing.T) {
456 RsrcMgr := testResMgrObject(tt.fields)
457 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
458 defer cancel()
459 err := RsrcMgr.DeleteAllOnuGemInfoForIntf(ctx, tt.args.PONIntfID)
460 if err != nil {
461 t.Errorf("DeleteAllOnuGemInfoForIntf() returned error")
462 }
463 })
464 }
465}
466
yasin sapli9e4c5092022-02-01 13:52:33 +0000467func TestOpenOltResourceMgr_deleteGemPort(t *testing.T) {
468
469 type args struct {
470 intfID uint32
471 onuID uint32
472 gemPortIDs []uint32
473 gemPortIDsToBeDeleted []uint32
474 gemPortIDsRemaining []uint32
475 serialNum string
476 finalLength int
477 }
478 tests := []struct {
479 name string
480 fields *fields
481 args args
482 }{
483 // Add/Delete single gem port
484 {"DeleteGemPortFromLocalCache1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, []uint32{}, "onu1", 0}},
485 // Delete all gemports
486 {"DeleteGemPortFromLocalCache2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, []uint32{}, "onu1", 0}},
487 // Try to delete when there is no gem port
488 {"DeleteGemPortFromLocalCache3", getResMgr(), args{0, 1, []uint32{}, []uint32{1, 2}, nil, "onu1", 0}},
489 // Try to delete non-existent gem port
490 {"DeleteGemPortFromLocalCache4", getResMgr(), args{0, 1, []uint32{1}, []uint32{2}, []uint32{1}, "onu1", 1}},
491 // Try to delete two of the gem ports
492 {"DeleteGemPortFromLocalCache5", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{2, 4}, []uint32{1, 3}, "onu1", 2}},
493 }
494 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
495 defer cancel()
496 for _, tt := range tests {
497 t.Run(tt.name, func(t *testing.T) {
498 RsrcMgr := testResMgrObject(tt.fields)
499 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID); err != nil {
500 t.Errorf("failed to remove onu")
501 }
502 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum); err != nil {
503 t.Errorf("failed to add onu")
504 }
505 for _, gemPort := range tt.args.gemPortIDs {
506 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID, gemPort); err != nil {
507 t.Errorf("failed to add gem to onu")
508 }
509 }
510 for _, gemPortDeleted := range tt.args.gemPortIDsToBeDeleted {
511 if err := RsrcMgr.RemoveGemFromOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID, gemPortDeleted); err != nil {
512 t.Errorf("failed to remove gem from onu")
513 }
514 }
515 lenofGemPorts := 0
516 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID)
517 if err != nil || gP == nil {
518 t.Errorf("failed to get onuGemInfo")
519 }
520 var gemPorts []uint32
521
522 lenofGemPorts = len(gP.GemPorts)
523 gemPorts = gP.GemPorts
524
525 if lenofGemPorts != tt.args.finalLength {
526 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
527 }
528
529 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
530 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
531 }
532 })
533 }
534}
535
536func TestOpenOltResourceMgr_AddNewOnuGemInfo(t *testing.T) {
537
538 type args struct {
539 PONIntfID uint32
540 OnuCount uint32
541 }
542 tests := []struct {
543 name string
544 fields *fields
545 args args
546 want error
547 }{
548 {"AddNewOnuGemInfoForIntf-0", getResMgr(), args{0, 32}, nil},
549 }
550 for _, tt := range tests {
551 t.Run(tt.name, func(t *testing.T) {
552 RsrcMgr := testResMgrObject(tt.fields)
553 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
554 defer cancel()
555 for j := 1; j <= int(tt.args.OnuCount); j++ {
556 go func(i uint32, j uint32) {
557 // TODO: actually verify success
558 _ = RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, i, i, fmt.Sprintf("onu-%d", i))
559 }(tt.args.PONIntfID, uint32(j))
560 }
561 })
562 }
563}
564
565func TestOpenOltFlowMgr_addGemPortToOnuInfoMap(t *testing.T) {
566
567 type args struct {
568 intfID uint32
569 onuID uint32
570 gemPortIDs []uint32
571 gemPortIDsRemaining []uint32
572 serialNum string
573 finalLength int
574 }
575 tests := []struct {
576 name string
577 fields *fields
578 args args
579 }{
580 // Add single gem port
581 {"addGemPortToOnuInfoMap1", getResMgr(), args{0, 1, []uint32{1}, []uint32{1}, "onu1", 1}},
582 // Delete all gemports
583 {"addGemPortToOnuInfoMap2", getResMgr(), args{0, 1, []uint32{1, 2, 3, 4}, []uint32{1, 2, 3, 4}, "onu1", 4}},
584 // Do not add any gemport
585 {"addGemPortToOnuInfoMap3", getResMgr(), args{0, 1, []uint32{}, nil, "onu1", 0}},
586 }
587 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
588 defer cancel()
589 for _, tt := range tests {
590 t.Run(tt.name, func(t *testing.T) {
591 RsrcMgr := testResMgrObject(tt.fields)
592 if err := RsrcMgr.DelOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID); err != nil {
593 t.Errorf("failed to remove onu")
594 }
595 if err := RsrcMgr.AddNewOnuGemInfoToCacheAndKvStore(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum); err != nil {
596 t.Errorf("failed to add onu")
597 }
598 for _, gemPort := range tt.args.gemPortIDs {
599 if err := RsrcMgr.AddGemToOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID, gemPort); err != nil {
600 t.Errorf("failed to add gem to onu")
601 }
602 }
603
604 lenofGemPorts := 0
605 gP, err := RsrcMgr.GetOnuGemInfo(ctx, tt.args.intfID, tt.args.onuID)
606
607 var gemPorts []uint32
608 if err == nil && gP != nil {
609 lenofGemPorts = len(gP.GemPorts)
610 gemPorts = gP.GemPorts
611 }
612 if lenofGemPorts != tt.args.finalLength {
613 t.Errorf("GemPorts length is not as expected len = %d, want %d", lenofGemPorts, tt.args.finalLength)
614 }
615
616 if !reflect.DeepEqual(tt.args.gemPortIDsRemaining, gemPorts) {
617 t.Errorf("GemPorts are not as expected = %v, want %v", gemPorts, tt.args.gemPortIDsRemaining)
618 }
619 })
620 }
621}
622
cbabuabf02352019-10-15 13:14:56 +0200623func TestOpenOltResourceMgr_GetCurrentGEMPortIDsForOnu(t *testing.T) {
624 type args struct {
625 intfID uint32
626 onuID uint32
627 uniID uint32
628 }
629 tests := []struct {
630 name string
631 fields *fields
632 args args
633 want []uint32
634 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700635 {"GetCurrentGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2}, []uint32{}},
cbabuabf02352019-10-15 13:14:56 +0200636 }
637 for _, tt := range tests {
638 t.Run(tt.name, func(t *testing.T) {
639 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530640 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
641 defer cancel()
642 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 +0200643 t.Errorf("GetCurrentGEMPortIDsForOnu() = %v, want %v", got, tt.want)
644 }
645 })
646 }
647}
648
Girish Gowdraa482f272021-03-24 23:04:19 -0700649func TestOpenOltResourceMgr_GetMeterInfoForOnu(t *testing.T) {
cbabuabf02352019-10-15 13:14:56 +0200650 type args struct {
651 Direction string
652 IntfID uint32
653 OnuID uint32
654 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000655 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200656 }
657 tests := []struct {
658 name string
659 fields *fields
660 args args
Girish Gowdraa482f272021-03-24 23:04:19 -0700661 want *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200662 wantErr error
663 }{
Girish Gowdraa482f272021-03-24 23:04:19 -0700664 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64},
665 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
666 {"GetMeterInfoForOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65},
667 &MeterInfo{}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200668 }
669 for _, tt := range tests {
670 t.Run(tt.name, func(t *testing.T) {
671 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530672 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
673 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700674 got, err := RsrcMgr.GetMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID, tt.args.tpID)
cbabuabf02352019-10-15 13:14:56 +0200675 if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil {
Girish Gowdraa482f272021-03-24 23:04:19 -0700676 t.Errorf("GetMeterInfoForOnu() got = %v, want %v", got, tt.want)
cbabuabf02352019-10-15 13:14:56 +0200677 }
678 })
679 }
680}
681
682func TestOpenOltResourceMgr_GetONUID(t *testing.T) {
683 type args struct {
684 ponIntfID uint32
685 }
686 tests := []struct {
687 name string
688 fields *fields
689 args args
690 want uint32
691 wantErr error
692 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700693 {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")},
cbabuabf02352019-10-15 13:14:56 +0200694 }
695 for _, tt := range tests {
696 t.Run(tt.name, func(t *testing.T) {
697 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530698 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
699 defer cancel()
700 got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID)
cbabuabf02352019-10-15 13:14:56 +0200701 if got != tt.want && err != nil {
702 t.Errorf("GetONUID() got = %v, want %v", got, tt.want)
703 }
704 })
705 }
706}
707
708func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) {
709
710 type args struct {
711 IntfID uint32
712 OnuID uint32
713 UniID uint32
714 }
715 tests := []struct {
716 name string
717 fields *fields
718 args args
Gamze Abakafee36392019-10-03 11:17:24 +0000719 want []uint32
cbabuabf02352019-10-15 13:14:56 +0200720 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700721 {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2},
Gamze Abakafee36392019-10-03 11:17:24 +0000722 []uint32{1}},
cbabuabf02352019-10-15 13:14:56 +0200723 }
724 for _, tt := range tests {
725 t.Run(tt.name, func(t *testing.T) {
726 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530727 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
728 defer cancel()
729 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 +0200730 t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want)
731 }
732 })
733 }
734}
735
cbabuabf02352019-10-15 13:14:56 +0200736func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) {
737
738 type args struct {
739 Direction string
740 IntfID uint32
741 OnuID uint32
742 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000743 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200744 }
745 tests := []struct {
746 name string
747 fields *fields
748 args args
749 wantErr error
750 }{
Gamze Abakafee36392019-10-03 11:17:24 +0000751 {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64},
cbabuabf02352019-10-15 13:14:56 +0200752 errors.New("failed to delete meter id %s from kvstore")},
753 }
754 for _, tt := range tests {
755 t.Run(tt.name, func(t *testing.T) {
756 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530757 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
758 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700759 if err := RsrcMgr.RemoveMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000760 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200761 t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
762 }
763 })
764 }
765}
766
767func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) {
768 type args struct {
769 IntfID uint32
770 OnuID uint32
771 UniID uint32
Gamze Abakafee36392019-10-03 11:17:24 +0000772 tpID uint32
cbabuabf02352019-10-15 13:14:56 +0200773 }
774 tests := []struct {
775 name string
776 fields *fields
777 args args
778 wantErr error
779 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700780 {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64},
cbabuabf02352019-10-15 13:14:56 +0200781 errors.New("failed to delete techprofile id resource %s in KV store")},
782 }
783 for _, tt := range tests {
784 t.Run(tt.name, func(t *testing.T) {
785 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530786 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
787 defer cancel()
788 if err := RsrcMgr.RemoveTechProfileIDForOnu(ctx, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
Gamze Abakafee36392019-10-03 11:17:24 +0000789 tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200790 t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr)
791 }
792 })
793 }
794}
795
796func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) {
797 type args struct {
798 ponPort uint32
799 onuID uint32
800 uniID uint32
801 allocID []uint32
802 }
803 tests := []struct {
804 name string
805 fields *fields
806 args args
807 wantErr error
808 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700809 {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}},
cbabuabf02352019-10-15 13:14:56 +0200810 errors.New("")},
811 }
812 for _, tt := range tests {
813 t.Run(tt.name, func(t *testing.T) {
814 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530815 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
816 defer cancel()
817 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 +0200818 t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr)
819 }
820 })
821 }
822}
823
cbabuabf02352019-10-15 13:14:56 +0200824func TestOpenOltResourceMgr_UpdateGEMPortIDsForOnu(t *testing.T) {
825
826 type args struct {
827 ponPort uint32
828 onuID uint32
829 uniID uint32
830 GEMPortList []uint32
831 }
832 tests := []struct {
833 name string
834 fields *fields
835 args args
836 wantErr error
837 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700838 {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200839 []uint32{1, 2}}, errors.New("failed to update resource")},
840 }
841 for _, tt := range tests {
842 t.Run(tt.name, func(t *testing.T) {
843 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530844 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
845 defer cancel()
846 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 +0200847 t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr)
848 }
849 })
850 }
851}
852
cbabuabf02352019-10-15 13:14:56 +0200853func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) {
854 type args struct {
Girish Gowdraa482f272021-03-24 23:04:19 -0700855 Direction string
856 IntfID uint32
857 OnuID uint32
858 UniID uint32
859 tpID uint32
860 MeterInfo *MeterInfo
cbabuabf02352019-10-15 13:14:56 +0200861 }
862 tests := []struct {
863 name string
864 fields *fields
865 args args
866 wantErr error
867 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700868 {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2,
Girish Gowdraa482f272021-03-24 23:04:19 -0700869 2, 64, &MeterInfo{}}, errors.New("failed to get Meter config from kvstore for path")},
cbabuabf02352019-10-15 13:14:56 +0200870 }
871 for _, tt := range tests {
872 t.Run(tt.name, func(t *testing.T) {
873 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530874 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
875 defer cancel()
Girish Gowdraa482f272021-03-24 23:04:19 -0700876 if err := RsrcMgr.StoreMeterInfoForOnu(ctx, tt.args.Direction, tt.args.IntfID, tt.args.OnuID, tt.args.UniID,
877 tt.args.tpID, tt.args.MeterInfo); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil {
cbabuabf02352019-10-15 13:14:56 +0200878 t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr)
879 }
880 })
881 }
882}
883
884func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) {
885 type args struct {
886 IntfID uint32
887 OnuID uint32
888 UniID uint32
889 TpID uint32
890 }
891 tests := []struct {
892 name string
893 fields *fields
894 args args
895 wantErr error
896 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700897 {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2,
cbabuabf02352019-10-15 13:14:56 +0200898 2}, errors.New("failed to update resource")},
899 }
900 for _, tt := range tests {
901 t.Run(tt.name, func(t *testing.T) {
902 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530903 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
904 defer cancel()
905 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 +0200906 t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr)
907 }
908 })
909 }
910}
911
912func TestSetKVClient(t *testing.T) {
913 type args struct {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800914 backend string
915 address string
916 DeviceID string
917 kvStorePrefix string
cbabuabf02352019-10-15 13:14:56 +0200918 }
919 tests := []struct {
920 name string
921 args args
sbarbaria8910ba2019-11-05 10:12:23 -0500922 want *db.Backend
cbabuabf02352019-10-15 13:14:56 +0200923 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300924 {"setKVClient-1", args{"etcd", "1.1.1.1:1", "olt1", "service/voltha"}, &db.Backend{}},
cbabuabf02352019-10-15 13:14:56 +0200925 }
926 for _, tt := range tests {
927 t.Run(tt.name, func(t *testing.T) {
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800928 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 +0200929 t.Errorf("SetKVClient() = %v, want %v", got, tt.want)
930 }
931 })
932 }
933}
934
cbabuabf02352019-10-15 13:14:56 +0200935func Test_newKVClient(t *testing.T) {
936 type args struct {
937 storeType string
938 address string
Neha Sharmacc656962020-04-14 14:26:11 +0000939 timeout time.Duration
cbabuabf02352019-10-15 13:14:56 +0200940 }
941 var kvClient kvstore.Client
942 tests := []struct {
943 name string
944 args args
945 want kvstore.Client
946 wantErr error
947 }{
948 {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")},
949 }
950 for _, tt := range tests {
951 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000952 got, err := newKVClient(context.Background(), tt.args.storeType, tt.args.address, tt.args.timeout)
cbabuabf02352019-10-15 13:14:56 +0200953 if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) {
954 t.Errorf("newKVClient() got = %v, want %v", got, tt.want)
955 }
956 if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) {
957 t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr)
958 return
959 }
960
961 })
962 }
963}
Esin Karamanccb714b2019-11-29 15:02:06 +0000964
965func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) {
966 type args struct {
967 intf uint32
968 gem uint32
969 servicePriority uint32
970 }
971 tests := []struct {
972 name string
973 args args
974 fields *fields
975 }{
976 {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()},
977 {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()},
978 {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()},
979 }
980 for _, tt := range tests {
981 t.Run(tt.name, func(t *testing.T) {
982 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +0530983 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
984 defer cancel()
985 err := RsrcMgr.AddMcastQueueForIntf(ctx, tt.args.intf, tt.args.gem, tt.args.servicePriority)
Esin Karamanccb714b2019-11-29 15:02:06 +0000986 if err != nil {
987 t.Errorf("%s got err= %s wants nil", tt.name, err)
988 return
989 }
990 })
991 }
992}
993
Girish Gowdraf3728b12022-02-02 21:46:51 -0800994func TestOpenOltResourceMgr_DeleteMcastQueueForIntf(t *testing.T) {
995 tests := []struct {
996 name string
997 fields *fields
998 }{
999 {"DeleteMcastQueueForIntf-1", getResMgr()},
1000 }
1001 for _, tt := range tests {
1002 t.Run(tt.name, func(t *testing.T) {
1003 RsrcMgr := testResMgrObject(tt.fields)
1004 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1005 defer cancel()
1006 RsrcMgr.DeleteMcastQueueForIntf(ctx)
1007 })
1008 }
1009}
1010
Esin Karamanccb714b2019-11-29 15:02:06 +00001011func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry {
1012 groupDesc := ofp.OfpGroupDesc{
1013 Type: ofp.OfpGroupType_OFPGT_ALL,
1014 GroupId: groupID,
1015 }
1016 groupEntry := ofp.OfpGroupEntry{
1017 Desc: &groupDesc,
1018 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001019 for i := 0; i < len(outPorts); i++ {
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001020 var acts []*ofp.OfpAction
Esin Karamanccb714b2019-11-29 15:02:06 +00001021 acts = append(acts, fu.Output(outPorts[i]))
Esin Karaman0ebd2a32020-02-09 18:45:36 +00001022 bucket := ofp.OfpBucket{
1023 Actions: acts,
1024 }
1025 groupDesc.Buckets = append(groupDesc.Buckets, &bucket)
Esin Karamanccb714b2019-11-29 15:02:06 +00001026 }
Esin Karamanccb714b2019-11-29 15:02:06 +00001027 return &groupEntry
1028}
1029
1030func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) {
1031 type args struct {
1032 group *ofp.OfpGroupEntry
1033 cached bool
1034 }
1035 //create group 1
1036 group1 := newGroup(1, []uint32{1})
1037 //create group 2
1038 group2 := newGroup(2, []uint32{2})
1039 //define test set
1040 tests := []struct {
1041 name string
1042 args args
1043 fields *fields
1044 }{
1045 {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()},
1046 {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()},
1047 }
1048 //execute tests
1049 for _, tt := range tests {
1050 t.Run(tt.name, func(t *testing.T) {
1051 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301052 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1053 defer cancel()
1054 err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001055 if err != nil {
1056 t.Errorf("%s got err= %s wants nil", tt.name, err)
1057 return
1058 }
1059 })
1060 }
1061}
1062
1063func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) {
1064 type args struct {
1065 groupID uint32
1066 cached bool
1067 }
1068 //define test set
1069 tests := []struct {
1070 name string
1071 args args
1072 fields *fields
1073 }{
1074 {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1075 {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1076 }
1077 //execute tests
1078 for _, tt := range tests {
1079 t.Run(tt.name, func(t *testing.T) {
1080 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301081 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1082 defer cancel()
Esin Karamand519bbf2020-07-01 11:16:03 +00001083 err := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
1084 if err != nil {
Esin Karamanccb714b2019-11-29 15:02:06 +00001085 t.Errorf("%s got false but wants true", tt.name)
1086 return
1087 }
1088 })
1089 }
1090}
1091
1092func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) {
1093 type args struct {
1094 groupID uint32
1095 cached bool
1096 }
1097 //define test set
1098 tests := []struct {
1099 name string
1100 args args
1101 fields *fields
1102 }{
1103 {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()},
1104 {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()},
1105 {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()},
1106 }
1107 //execute tests
1108 for _, tt := range tests {
1109 t.Run(tt.name, func(t *testing.T) {
1110 RsrcMgr := testResMgrObject(tt.fields)
npujarec5762e2020-01-01 14:08:48 +05301111 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1112 defer cancel()
1113 exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached)
Esin Karamanccb714b2019-11-29 15:02:06 +00001114 if err != nil {
1115 t.Errorf("%s got error but wants nil error", tt.name)
1116 return
1117 } else if exists && (groupInfo.GroupID == 0) {
1118 t.Errorf("%s got true and nil group info but expected not nil group info", tt.name)
1119 return
1120 } else if tt.args.groupID == 3 && exists {
1121 t.Errorf("%s got true but wants false", tt.name)
1122 return
1123 }
1124 })
1125 }
1126}