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