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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 205 | func (kvclient *MockResKVClient) Reserve(ctx context.Context, key string, value interface{}, ttl int64) (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 |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 230 | func (kvclient *MockResKVClient) AcquireLock(ctx context.Context, lockName string, timeout int) 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 | 3f182d8 | 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 |
| 531 | vlanPcp []uint32 |
| 532 | } |
| 533 | tests := []struct { |
| 534 | name string |
| 535 | fields *fields |
| 536 | args args |
| 537 | want uint32 |
| 538 | wantErr error |
| 539 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 540 | {"GetFlowID-1", getResMgr(), args{1, 2, 2, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 541 | "HSIA", nil}, 0, errors.New("failed to get flows")}, |
| 542 | } |
| 543 | for _, tt := range tests { |
| 544 | t.Run(tt.name, func(t *testing.T) { |
| 545 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 546 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 547 | defer cancel() |
| 548 | 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.vlanPcp...) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 549 | if err != nil && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 550 | t.Errorf("GetFlowID() error = %v, wantErr %v", err, tt.wantErr) |
| 551 | return |
| 552 | } |
| 553 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 554 | t.Errorf("GetFlowID() got = %v, want %v", got, tt.want) |
| 555 | } |
| 556 | }) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | func TestOpenOltResourceMgr_GetGEMPortID(t *testing.T) { |
| 561 | type args struct { |
| 562 | ponPort uint32 |
| 563 | onuID uint32 |
| 564 | uniID uint32 |
| 565 | NumOfPorts uint32 |
| 566 | } |
| 567 | tests := []struct { |
| 568 | name string |
| 569 | fields *fields |
| 570 | args args |
| 571 | want []uint32 |
| 572 | wantErr error |
| 573 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 574 | {"GetGEMPortID-1", getResMgr(), args{1, 2, 2, 2}, []uint32{}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 575 | errors.New("failed to get gem port")}, |
| 576 | } |
| 577 | for _, tt := range tests { |
| 578 | t.Run(tt.name, func(t *testing.T) { |
| 579 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 580 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 581 | defer cancel() |
| 582 | 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] | 583 | if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 584 | t.Errorf("GetGEMPortID() error = %v, wantErr %v", err, tt.wantErr) |
| 585 | return |
| 586 | } |
| 587 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 588 | t.Errorf("GetGEMPortID() got = %v, want %v", got, tt.want) |
| 589 | } |
| 590 | }) |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | func TestOpenOltResourceMgr_GetMeterIDForOnu(t *testing.T) { |
| 595 | type args struct { |
| 596 | Direction string |
| 597 | IntfID uint32 |
| 598 | OnuID uint32 |
| 599 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 600 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 601 | } |
| 602 | tests := []struct { |
| 603 | name string |
| 604 | fields *fields |
| 605 | args args |
| 606 | want *ofp.OfpMeterConfig |
| 607 | wantErr error |
| 608 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 609 | {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 0, 1, 1, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 610 | &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")}, |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 611 | {"GetMeterIDOnu", getResMgr(), args{"DOWNSTREAM", 1, 2, 2, 65}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 612 | &ofp.OfpMeterConfig{}, errors.New("failed to get Meter config from kvstore for path")}, |
| 613 | } |
| 614 | for _, tt := range tests { |
| 615 | t.Run(tt.name, func(t *testing.T) { |
| 616 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 617 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 618 | defer cancel() |
| 619 | 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] | 620 | if reflect.TypeOf(got) != reflect.TypeOf(tt.want) && err != nil { |
| 621 | t.Errorf("GetMeterIDForOnu() got = %v, want %v", got, tt.want) |
| 622 | } |
| 623 | }) |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | func TestOpenOltResourceMgr_GetONUID(t *testing.T) { |
| 628 | type args struct { |
| 629 | ponIntfID uint32 |
| 630 | } |
| 631 | tests := []struct { |
| 632 | name string |
| 633 | fields *fields |
| 634 | args args |
| 635 | want uint32 |
| 636 | wantErr error |
| 637 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 638 | {"GetONUID-1", getResMgr(), args{1}, uint32(0), errors.New("json errors")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 639 | } |
| 640 | for _, tt := range tests { |
| 641 | t.Run(tt.name, func(t *testing.T) { |
| 642 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 643 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 644 | defer cancel() |
| 645 | got, err := RsrcMgr.GetONUID(ctx, tt.args.ponIntfID) |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 646 | if got != tt.want && err != nil { |
| 647 | t.Errorf("GetONUID() got = %v, want %v", got, tt.want) |
| 648 | } |
| 649 | }) |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | func TestOpenOltResourceMgr_GetTechProfileIDForOnu(t *testing.T) { |
| 654 | |
| 655 | type args struct { |
| 656 | IntfID uint32 |
| 657 | OnuID uint32 |
| 658 | UniID uint32 |
| 659 | } |
| 660 | tests := []struct { |
| 661 | name string |
| 662 | fields *fields |
| 663 | args args |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 664 | want []uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 665 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 666 | {"GetTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2}, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 667 | []uint32{1}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 668 | } |
| 669 | for _, tt := range tests { |
| 670 | t.Run(tt.name, func(t *testing.T) { |
| 671 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 672 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 673 | defer cancel() |
| 674 | 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] | 675 | t.Errorf("GetTechProfileIDForOnu() = %v, want %v", got, tt.want) |
| 676 | } |
| 677 | }) |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | func TestOpenOltResourceMgr_IsFlowCookieOnKVStore(t *testing.T) { |
| 682 | type args struct { |
| 683 | ponIntfID uint32 |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 684 | onuID int32 |
| 685 | uniID int32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 686 | flowStoreCookie uint64 |
| 687 | } |
| 688 | tests := []struct { |
| 689 | name string |
| 690 | fields *fields |
| 691 | args args |
| 692 | want bool |
| 693 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 694 | {"IsFlowCookieOnKVStore-1", getResMgr(), args{1, 2, 2, 2}, false}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 695 | } |
| 696 | for _, tt := range tests { |
| 697 | t.Run(tt.name, func(t *testing.T) { |
| 698 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 699 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 700 | defer cancel() |
| 701 | 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] | 702 | t.Errorf("IsFlowCookieOnKVStore() = %v, want %v", got, tt.want) |
| 703 | } |
| 704 | }) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | func TestOpenOltResourceMgr_RemoveMeterIDForOnu(t *testing.T) { |
| 709 | |
| 710 | type args struct { |
| 711 | Direction string |
| 712 | IntfID uint32 |
| 713 | OnuID uint32 |
| 714 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 715 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 716 | } |
| 717 | tests := []struct { |
| 718 | name string |
| 719 | fields *fields |
| 720 | args args |
| 721 | wantErr error |
| 722 | }{ |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 723 | {"RemoveMeterIdForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 1, 1, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 724 | errors.New("failed to delete meter id %s from kvstore")}, |
| 725 | } |
| 726 | for _, tt := range tests { |
| 727 | t.Run(tt.name, func(t *testing.T) { |
| 728 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 729 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 730 | defer cancel() |
| 731 | 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] | 732 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 733 | t.Errorf("RemoveMeterIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 734 | } |
| 735 | }) |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | func TestOpenOltResourceMgr_RemoveTechProfileIDForOnu(t *testing.T) { |
| 740 | type args struct { |
| 741 | IntfID uint32 |
| 742 | OnuID uint32 |
| 743 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 744 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 745 | } |
| 746 | tests := []struct { |
| 747 | name string |
| 748 | fields *fields |
| 749 | args args |
| 750 | wantErr error |
| 751 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 752 | {"RemoveTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, 64}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 753 | errors.New("failed to delete techprofile id resource %s in KV store")}, |
| 754 | } |
| 755 | for _, tt := range tests { |
| 756 | t.Run(tt.name, func(t *testing.T) { |
| 757 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 758 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 759 | defer cancel() |
| 760 | 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] | 761 | tt.args.tpID); reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 762 | t.Errorf("RemoveTechProfileIDForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 763 | } |
| 764 | }) |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | func TestOpenOltResourceMgr_UpdateAllocIdsForOnu(t *testing.T) { |
| 769 | type args struct { |
| 770 | ponPort uint32 |
| 771 | onuID uint32 |
| 772 | uniID uint32 |
| 773 | allocID []uint32 |
| 774 | } |
| 775 | tests := []struct { |
| 776 | name string |
| 777 | fields *fields |
| 778 | args args |
| 779 | wantErr error |
| 780 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 781 | {"UpdateAllocIdsForOnu-1", getResMgr(), args{1, 2, 2, []uint32{1, 2}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 782 | errors.New("")}, |
| 783 | } |
| 784 | for _, tt := range tests { |
| 785 | t.Run(tt.name, func(t *testing.T) { |
| 786 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 787 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 788 | defer cancel() |
| 789 | 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] | 790 | t.Errorf("UpdateAllocIdsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 791 | } |
| 792 | }) |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | func TestOpenOltResourceMgr_UpdateFlowIDInfo(t *testing.T) { |
| 797 | type args struct { |
| 798 | ponIntfID int32 |
| 799 | onuID int32 |
| 800 | uniID int32 |
| 801 | flowID uint32 |
| 802 | flowData *[]FlowInfo |
| 803 | } |
| 804 | tests := []struct { |
| 805 | name string |
| 806 | fields *fields |
| 807 | args args |
| 808 | wantErr error |
| 809 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 810 | {"UpdateFlowIDInfo-1", getResMgr(), args{1, 2, 2, 2, &[]FlowInfo{}}, errors.New("")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 811 | } |
| 812 | for _, tt := range tests { |
| 813 | t.Run(tt.name, func(t *testing.T) { |
| 814 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 815 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 816 | defer cancel() |
| 817 | 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] | 818 | t.Errorf("UpdateFlowIDInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 819 | } |
| 820 | }) |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | func 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 Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 838 | {"UpdateGEMPortIDsForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 839 | []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) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 844 | 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) { |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 847 | t.Errorf("UpdateGEMPortIDsForOnu() error = %v, wantErr %v", err, tt.wantErr) |
| 848 | } |
| 849 | }) |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | func TestOpenOltResourceMgr_UpdateGEMportsPonportToOnuMapOnKVStore(t *testing.T) { |
| 854 | type args struct { |
| 855 | gemPorts []uint32 |
| 856 | PonPort uint32 |
| 857 | onuID uint32 |
| 858 | uniID uint32 |
| 859 | } |
| 860 | tests := []struct { |
| 861 | name string |
| 862 | fields *fields |
| 863 | args args |
| 864 | wantErr error |
| 865 | }{ |
| 866 | {"UpdateGEMportsPonportToOnuMapOnKVStore-1", getResMgr(), args{[]uint32{1, 2}, |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 867 | 1, 2, 2}, errors.New("failed to update resource")}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 868 | } |
| 869 | for _, tt := range tests { |
| 870 | t.Run(tt.name, func(t *testing.T) { |
| 871 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 872 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 873 | defer cancel() |
| 874 | if err := RsrcMgr.UpdateGEMportsPonportToOnuMapOnKVStore(ctx, tt.args.gemPorts, tt.args.PonPort, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 875 | 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] | 876 | t.Errorf("UpdateGEMportsPonportToOnuMapOnKVStore() error = %v, wantErr %v", err, tt.wantErr) |
| 877 | } |
| 878 | }) |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | func TestOpenOltResourceMgr_UpdateMeterIDForOnu(t *testing.T) { |
| 883 | type args struct { |
| 884 | Direction string |
| 885 | IntfID uint32 |
| 886 | OnuID uint32 |
| 887 | UniID uint32 |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 888 | tpID uint32 |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 889 | MeterConfig *ofp.OfpMeterConfig |
| 890 | } |
| 891 | tests := []struct { |
| 892 | name string |
| 893 | fields *fields |
| 894 | args args |
| 895 | wantErr error |
| 896 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 897 | {"UpdateMeterIDForOnu-1", getResMgr(), args{"DOWNSTREAM", 1, 2, |
Gamze Abaka | fee3639 | 2019-10-03 11:17:24 +0000 | [diff] [blame] | 898 | 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] | 899 | } |
| 900 | for _, tt := range tests { |
| 901 | t.Run(tt.name, func(t *testing.T) { |
| 902 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 903 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 904 | defer cancel() |
| 905 | 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] | 906 | 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] | 907 | t.Errorf("UpdateMeterIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 908 | } |
| 909 | }) |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | func TestOpenOltResourceMgr_UpdateTechProfileIDForOnu(t *testing.T) { |
| 914 | type args struct { |
| 915 | IntfID uint32 |
| 916 | OnuID uint32 |
| 917 | UniID uint32 |
| 918 | TpID uint32 |
| 919 | } |
| 920 | tests := []struct { |
| 921 | name string |
| 922 | fields *fields |
| 923 | args args |
| 924 | wantErr error |
| 925 | }{ |
Girish Gowdra | 3f182d8 | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 926 | {"UpdateTechProfileIDForOnu-1", getResMgr(), args{1, 2, 2, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 927 | 2}, errors.New("failed to update resource")}, |
| 928 | } |
| 929 | for _, tt := range tests { |
| 930 | t.Run(tt.name, func(t *testing.T) { |
| 931 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 932 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 933 | defer cancel() |
| 934 | 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] | 935 | t.Errorf("UpdateTechProfileIDForOnu() got = %v, want %v", err, tt.wantErr) |
| 936 | } |
| 937 | }) |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | func TestSetKVClient(t *testing.T) { |
| 942 | type args struct { |
| 943 | backend string |
| 944 | Host string |
| 945 | Port int |
| 946 | DeviceID string |
| 947 | } |
| 948 | tests := []struct { |
| 949 | name string |
| 950 | args args |
sbarbari | a8910ba | 2019-11-05 10:12:23 -0500 | [diff] [blame] | 951 | want *db.Backend |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 952 | }{ |
sbarbari | a8910ba | 2019-11-05 10:12:23 -0500 | [diff] [blame] | 953 | {"setKVClient-1", args{"consul", "1.1.1.1", 1, "olt1"}, &db.Backend{}}, |
| 954 | {"setKVClient-1", args{"etcd", "2.2.2.2", 2, "olt2"}, &db.Backend{}}, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 955 | } |
| 956 | for _, tt := range tests { |
| 957 | t.Run(tt.name, func(t *testing.T) { |
| 958 | if got := SetKVClient(tt.args.backend, tt.args.Host, tt.args.Port, tt.args.DeviceID); reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 959 | t.Errorf("SetKVClient() = %v, want %v", got, tt.want) |
| 960 | } |
| 961 | }) |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | func Test_getFlowIDFromFlowInfo(t *testing.T) { |
| 966 | type args struct { |
| 967 | FlowInfo *[]FlowInfo |
| 968 | flowID uint32 |
| 969 | gemportID uint32 |
| 970 | flowStoreCookie uint64 |
| 971 | flowCategory string |
| 972 | vlanPcp []uint32 |
| 973 | } |
| 974 | flowInfo := &[]FlowInfo{ |
| 975 | { |
| 976 | &openolt.Flow{ |
| 977 | FlowId: 1, |
| 978 | GemportId: 1, |
| 979 | Classifier: &openolt.Classifier{ |
| 980 | OPbits: 1, |
| 981 | }}, |
| 982 | 1, |
| 983 | "HSIA_FLOW", |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 984 | 2000, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 985 | }, |
| 986 | { |
| 987 | &openolt.Flow{ |
| 988 | GemportId: 1, |
| 989 | }, |
| 990 | 1, |
| 991 | "EAPOL", |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 992 | 3000, |
cbabu | abf0235 | 2019-10-15 13:14:56 +0200 | [diff] [blame] | 993 | }, |
| 994 | } |
| 995 | tests := []struct { |
| 996 | name string |
| 997 | args args |
| 998 | wantErr error |
| 999 | }{ |
| 1000 | {"getFlowIdFromFlowInfo-1", args{}, errors.New("invalid flow-info")}, |
| 1001 | {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1, |
| 1002 | "HSIA_FLOW", []uint32{1, 2}}, errors.New("invalid flow-info")}, |
| 1003 | {"getFlowIdFromFlowInfo-2", args{flowInfo, 1, 1, 1, |
| 1004 | "EAPOL", []uint32{1, 2}}, errors.New("invalid flow-info")}, |
| 1005 | } |
| 1006 | for _, tt := range tests { |
| 1007 | t.Run(tt.name, func(t *testing.T) { |
| 1008 | err := getFlowIDFromFlowInfo(tt.args.FlowInfo, tt.args.flowID, tt.args.gemportID, tt.args.flowStoreCookie, tt.args.flowCategory, tt.args.vlanPcp...) |
| 1009 | if reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) && err != nil { |
| 1010 | t.Errorf("getFlowIDFromFlowInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 1011 | } |
| 1012 | if err == nil { |
| 1013 | t.Log("return'd nil") |
| 1014 | } |
| 1015 | }) |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | func Test_newKVClient(t *testing.T) { |
| 1020 | type args struct { |
| 1021 | storeType string |
| 1022 | address string |
| 1023 | timeout uint32 |
| 1024 | } |
| 1025 | var kvClient kvstore.Client |
| 1026 | tests := []struct { |
| 1027 | name string |
| 1028 | args args |
| 1029 | want kvstore.Client |
| 1030 | wantErr error |
| 1031 | }{ |
| 1032 | {"newKVClient-1", args{"", "3.3.3.3", 1}, kvClient, errors.New("unsupported-kv-store")}, |
| 1033 | } |
| 1034 | for _, tt := range tests { |
| 1035 | t.Run(tt.name, func(t *testing.T) { |
| 1036 | got, err := newKVClient(tt.args.storeType, tt.args.address, tt.args.timeout) |
| 1037 | if got != nil && reflect.TypeOf(got) != reflect.TypeOf(tt.want) { |
| 1038 | t.Errorf("newKVClient() got = %v, want %v", got, tt.want) |
| 1039 | } |
| 1040 | if (err != nil) && reflect.TypeOf(err) != reflect.TypeOf(tt.wantErr) { |
| 1041 | t.Errorf("newKVClient() error = %v, wantErr %v", err, tt.wantErr) |
| 1042 | return |
| 1043 | } |
| 1044 | |
| 1045 | }) |
| 1046 | } |
| 1047 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1048 | |
| 1049 | func TestOpenOltResourceMgr_AddMcastQueueForIntf(t *testing.T) { |
| 1050 | type args struct { |
| 1051 | intf uint32 |
| 1052 | gem uint32 |
| 1053 | servicePriority uint32 |
| 1054 | } |
| 1055 | tests := []struct { |
| 1056 | name string |
| 1057 | args args |
| 1058 | fields *fields |
| 1059 | }{ |
| 1060 | {"AddMcastQueueForIntf-1", args{0, 4000, 0}, getResMgr()}, |
| 1061 | {"AddMcastQueueForIntf-2", args{1, 4000, 1}, getResMgr()}, |
| 1062 | {"AddMcastQueueForIntf-3", args{2, 4000, 2}, getResMgr()}, |
| 1063 | } |
| 1064 | for _, tt := range tests { |
| 1065 | t.Run(tt.name, func(t *testing.T) { |
| 1066 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1067 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1068 | defer cancel() |
| 1069 | 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] | 1070 | if err != nil { |
| 1071 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 1072 | return |
| 1073 | } |
| 1074 | }) |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | func newGroup(groupID uint32, outPorts []uint32) *ofp.OfpGroupEntry { |
| 1079 | groupDesc := ofp.OfpGroupDesc{ |
| 1080 | Type: ofp.OfpGroupType_OFPGT_ALL, |
| 1081 | GroupId: groupID, |
| 1082 | } |
| 1083 | groupEntry := ofp.OfpGroupEntry{ |
| 1084 | Desc: &groupDesc, |
| 1085 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1086 | for i := 0; i < len(outPorts); i++ { |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 1087 | var acts []*ofp.OfpAction |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1088 | acts = append(acts, fu.Output(outPorts[i])) |
Esin Karaman | 0ebd2a3 | 2020-02-09 18:45:36 +0000 | [diff] [blame] | 1089 | bucket := ofp.OfpBucket{ |
| 1090 | Actions: acts, |
| 1091 | } |
| 1092 | groupDesc.Buckets = append(groupDesc.Buckets, &bucket) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1093 | } |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1094 | return &groupEntry |
| 1095 | } |
| 1096 | |
| 1097 | func TestOpenOltResourceMgr_AddFlowGroupToKVStore(t *testing.T) { |
| 1098 | type args struct { |
| 1099 | group *ofp.OfpGroupEntry |
| 1100 | cached bool |
| 1101 | } |
| 1102 | //create group 1 |
| 1103 | group1 := newGroup(1, []uint32{1}) |
| 1104 | //create group 2 |
| 1105 | group2 := newGroup(2, []uint32{2}) |
| 1106 | //define test set |
| 1107 | tests := []struct { |
| 1108 | name string |
| 1109 | args args |
| 1110 | fields *fields |
| 1111 | }{ |
| 1112 | {"AddFlowGroupToKVStore-1", args{group1, true}, getResMgr()}, |
| 1113 | {"AddFlowGroupToKVStore-2", args{group2, false}, getResMgr()}, |
| 1114 | } |
| 1115 | //execute tests |
| 1116 | for _, tt := range tests { |
| 1117 | t.Run(tt.name, func(t *testing.T) { |
| 1118 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1119 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1120 | defer cancel() |
| 1121 | err := RsrcMgr.AddFlowGroupToKVStore(ctx, tt.args.group, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1122 | if err != nil { |
| 1123 | t.Errorf("%s got err= %s wants nil", tt.name, err) |
| 1124 | return |
| 1125 | } |
| 1126 | }) |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | func TestOpenOltResourceMgr_RemoveFlowGroupFromKVStore(t *testing.T) { |
| 1131 | type args struct { |
| 1132 | groupID uint32 |
| 1133 | cached bool |
| 1134 | } |
| 1135 | //define test set |
| 1136 | tests := []struct { |
| 1137 | name string |
| 1138 | args args |
| 1139 | fields *fields |
| 1140 | }{ |
| 1141 | {"RemoveFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 1142 | {"RemoveFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 1143 | } |
| 1144 | //execute tests |
| 1145 | for _, tt := range tests { |
| 1146 | t.Run(tt.name, func(t *testing.T) { |
| 1147 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1148 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1149 | defer cancel() |
| 1150 | success := RsrcMgr.RemoveFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1151 | if !success { |
| 1152 | t.Errorf("%s got false but wants true", tt.name) |
| 1153 | return |
| 1154 | } |
| 1155 | }) |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | func TestOpenOltResourceMgr_GetFlowGroupFromKVStore(t *testing.T) { |
| 1160 | type args struct { |
| 1161 | groupID uint32 |
| 1162 | cached bool |
| 1163 | } |
| 1164 | //define test set |
| 1165 | tests := []struct { |
| 1166 | name string |
| 1167 | args args |
| 1168 | fields *fields |
| 1169 | }{ |
| 1170 | {"GetFlowGroupFromKVStore-1", args{1, true}, getResMgr()}, |
| 1171 | {"GetFlowGroupFromKVStore-2", args{2, false}, getResMgr()}, |
| 1172 | {"GetFlowGroupFromKVStore-3", args{1000, false}, getResMgr()}, |
| 1173 | } |
| 1174 | //execute tests |
| 1175 | for _, tt := range tests { |
| 1176 | t.Run(tt.name, func(t *testing.T) { |
| 1177 | RsrcMgr := testResMgrObject(tt.fields) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1178 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1179 | defer cancel() |
| 1180 | exists, groupInfo, err := RsrcMgr.GetFlowGroupFromKVStore(ctx, tt.args.groupID, tt.args.cached) |
Esin Karaman | ccb714b | 2019-11-29 15:02:06 +0000 | [diff] [blame] | 1181 | if err != nil { |
| 1182 | t.Errorf("%s got error but wants nil error", tt.name) |
| 1183 | return |
| 1184 | } else if exists && (groupInfo.GroupID == 0) { |
| 1185 | t.Errorf("%s got true and nil group info but expected not nil group info", tt.name) |
| 1186 | return |
| 1187 | } else if tt.args.groupID == 3 && exists { |
| 1188 | t.Errorf("%s got true but wants false", tt.name) |
| 1189 | return |
| 1190 | } |
| 1191 | }) |
| 1192 | } |
| 1193 | } |