blob: c9486b6c8c64251b30f132f348d269d8dabd597b [file] [log] [blame]
cbabua9e04cc2019-10-03 12:35:45 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
cbabubef89432019-10-18 11:47:27 +020017/*
18This file contains unit test cases for functions in the file openolt.go.
19This file also implements the fields struct to mock the Openolt and few utility functions.
20*/
21
Scott Bakerdbd960e2020-02-28 08:57:51 -080022//Package core provides the utility for olt devices, flows and statistics
23package core
cbabua9e04cc2019-10-03 12:35:45 +053024
25import (
26 "context"
27 "errors"
Kent Hagermanf1db18b2020-07-08 13:38:15 -040028 "reflect"
Kent Hagermanf1db18b2020-07-08 13:38:15 -040029 "testing"
30
khenaidoo106c61a2021-08-11 18:05:46 -040031 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
32 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030033
khenaidoo106c61a2021-08-11 18:05:46 -040034 "github.com/opencord/voltha-lib-go/v7/pkg/events"
35 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
36 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Scott Bakerdbd960e2020-02-28 08:57:51 -080037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053038 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
khenaidoo106c61a2021-08-11 18:05:46 -040039 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
40 "github.com/opencord/voltha-protos/v5/go/openflow_13"
41 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
42 "github.com/opencord/voltha-protos/v5/go/voltha"
cbabua9e04cc2019-10-03 12:35:45 +053043)
44
cbabubef89432019-10-18 11:47:27 +020045// mocks the OpenOLT struct.
cbabua9e04cc2019-10-03 12:35:45 +053046type fields struct {
Kent Hagermane6ff1012020-07-14 15:07:53 -040047 deviceHandlers map[string]*DeviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -040048 coreClient *vgrpc.Client
Himani Chawlacd407802020-12-10 12:08:59 +053049 eventProxy *events.EventProxy
Kent Hagermane6ff1012020-07-14 15:07:53 -040050 numOnus int
51 KVStoreAddress string
52 KVStoreType string
53 exitChannel chan int
54 ctx context.Context
cbabua9e04cc2019-10-03 12:35:45 +053055}
56
cbabubef89432019-10-18 11:47:27 +020057// mockOlt mocks OpenOLT struct.
cbabua9e04cc2019-10-03 12:35:45 +053058func mockOlt() *fields {
cbabua9e04cc2019-10-03 12:35:45 +053059 dh := newMockDeviceHandler()
60 newOlt := &fields{}
61 newOlt.deviceHandlers = map[string]*DeviceHandler{}
62 newOlt.deviceHandlers[dh.device.Id] = dh
63 return newOlt
64}
65
cbabubef89432019-10-18 11:47:27 +020066// testOltObject maps fields type to OpenOLt type.
cbabua9e04cc2019-10-03 12:35:45 +053067func testOltObject(testOlt *fields) *OpenOLT {
68 return &OpenOLT{
69 deviceHandlers: testOlt.deviceHandlers,
cbabua9e04cc2019-10-03 12:35:45 +053070 eventProxy: testOlt.eventProxy,
cbabua9e04cc2019-10-03 12:35:45 +053071 numOnus: testOlt.numOnus,
Neha Sharma3f221ae2020-04-29 19:02:12 +000072 KVStoreAddress: testOlt.KVStoreAddress,
cbabua9e04cc2019-10-03 12:35:45 +053073 KVStoreType: testOlt.KVStoreType,
74 exitChannel: testOlt.exitChannel,
75 }
76}
77
cbabubef89432019-10-18 11:47:27 +020078// mockDevice mocks Device.
cbabua9e04cc2019-10-03 12:35:45 +053079func mockDevice() *voltha.Device {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040080 return &voltha.Device{
cbabua9e04cc2019-10-03 12:35:45 +053081 Id: "olt",
82 Root: true,
83 ParentId: "logical_device",
cbabua9e04cc2019-10-03 12:35:45 +053084 ProxyAddress: &voltha.Device_ProxyAddress{
85 DeviceId: "olt",
86 DeviceType: "onu",
87 ChannelId: 1,
88 ChannelGroupId: 1,
89 },
90 ConnectStatus: 1,
91 }
cbabua9e04cc2019-10-03 12:35:45 +053092}
93
94func TestNewOpenOLT(t *testing.T) {
95 tests := []struct {
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053096 name string
97 fields *fields
98 configFlags *config.AdapterFlags
Matteo Scandolodfa7a972020-11-06 13:03:40 -080099 cm *conf.ConfigManager
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530100 want *OpenOLT
cbabua9e04cc2019-10-03 12:35:45 +0530101 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300102 {"newopenolt-1", &fields{}, &config.AdapterFlags{OnuNumber: 1, KVStoreAddress: "1.1.1.1:1", KVStoreType: "etcd"}, &conf.ConfigManager{},
103 &OpenOLT{numOnus: 1, KVStoreAddress: "1.1.1.1:1", KVStoreType: "etcd"}},
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800104 {"newopenolt-2", &fields{}, &config.AdapterFlags{OnuNumber: 2, KVStoreAddress: "2.2.2.2:2", KVStoreType: "etcd"}, &conf.ConfigManager{},
Neha Sharma3f221ae2020-04-29 19:02:12 +0000105 &OpenOLT{numOnus: 2, KVStoreAddress: "2.2.2.2:2", KVStoreType: "etcd"}},
cbabua9e04cc2019-10-03 12:35:45 +0530106 }
107 for _, tt := range tests {
108 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400109 if got := NewOpenOLT(tt.fields.ctx, tt.fields.coreClient,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800110 tt.fields.eventProxy, tt.configFlags, tt.cm); reflect.TypeOf(got) != reflect.TypeOf(tt.want) && got != nil {
cbabua9e04cc2019-10-03 12:35:45 +0530111 t.Errorf("NewOpenOLT() error = %v, wantErr %v", got, tt.want)
112 }
113 })
114 }
115}
116
khenaidoo106c61a2021-08-11 18:05:46 -0400117func TestOpenOLT_ActivateImageUpdate(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530118 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400119 request *ic.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530120 }
121 tests := []struct {
122 name string
123 fields *fields
124 args args
125 want *voltha.ImageDownload
126 wantErr error
127 }{
128 {"activate_image_upate-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530129 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530130 {"activate_image_upate-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123CDE"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530131 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530132 {"activate_image_upate-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123EFG"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530133 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530134 }
135 for _, tt := range tests {
136 t.Run(tt.name, func(t *testing.T) {
137 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400138 got, err := oo.ActivateImageUpdate(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800139 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530140 t.Errorf("Activate_image_update() error = %v, wantErr %v", err, tt.wantErr)
141 }
142 })
143 }
144}
145
khenaidoo106c61a2021-08-11 18:05:46 -0400146func TestOpenOLT_AdoptDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530147 type args struct {
148 device *voltha.Device
149 }
150 var device = mockDevice()
kesavand39e0aa32020-01-28 20:58:50 -0500151 device.Id = "olt"
Thomas Lee S94109f12020-03-03 16:39:29 +0530152 nilDevice := olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
cbabua9e04cc2019-10-03 12:35:45 +0530153 tests := []struct {
154 name string
155 fields *fields
156 args args
157 wantErr error
158 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800159 {"adopt_device-1", mockOlt(), args{}, nilDevice},
160 {"adopt_device-2", mockOlt(), args{device}, nilDevice},
161 {"adopt_device-3", mockOlt(), args{mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530162 }
163 for _, tt := range tests {
164 t.Run(tt.name, func(t *testing.T) {
165 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400166 _, err := oo.AdoptDevice(context.Background(), tt.args.device)
cbabua9e04cc2019-10-03 12:35:45 +0530167 if (err != nil) && (reflect.TypeOf(err) !=
168 reflect.TypeOf(tt.wantErr)) && (tt.args.device == nil) {
169 t.Errorf("Adopt_device() error = %v, wantErr %v", err, tt.wantErr)
170 }
171 if err == nil {
172 t.Log("return'd nil")
173 }
174 })
175 }
176}
177
khenaidoo106c61a2021-08-11 18:05:46 -0400178func TestOpenOLT_CancelImageDownload(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530179 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400180 request *ic.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530181 }
182 tests := []struct {
183 name string
184 fields *fields
185 args args
186 want *voltha.ImageDownload
187 wantErr error
188 }{
189 {"cancel_image_download-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530190 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530191 {"cancel_image_download-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123IJK"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530192 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530193 {"cancel_image_download-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123KLM"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530194 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530195 }
196 for _, tt := range tests {
197 t.Run(tt.name, func(t *testing.T) {
198 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400199 got, err := oo.CancelImageDownload(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800200 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530201 t.Errorf("Cancel_image_download() error = %v, wantErr %v", err, tt.wantErr)
202 }
203 })
204 }
205}
206
khenaidoo106c61a2021-08-11 18:05:46 -0400207func TestOpenOLT_DeleteDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530208 type args struct {
209 device *voltha.Device
210 }
211 tests := []struct {
212 name string
213 fields *fields
214 args args
215 wantErr error
216 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800217 {"delete_device-1", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530218 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530219 }
220 for _, tt := range tests {
221 t.Run(tt.name, func(t *testing.T) {
222 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400223 if _, err := oo.DeleteDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530224 t.Errorf("Delete_device() error = %v, wantErr %v", err, tt.wantErr)
225 }
226 })
227 }
228}
229
khenaidoo106c61a2021-08-11 18:05:46 -0400230func TestOpenOLT_DisableDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530231 type args struct {
232 device *voltha.Device
233 }
234 tests := []struct {
235 name string
236 fields *fields
237 args args
238 wantErr error
239 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800240 {"disable_device-1", mockOlt(), args{mockDevice()}, nil},
241 {"disable_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530242 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530243 }
244 for _, tt := range tests {
245 t.Run(tt.name, func(t *testing.T) {
246 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400247 if _, err := oo.DisableDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530248 t.Errorf("Disable_device() error = %v, wantErr %v", err, tt.wantErr)
249 }
250 })
251 }
252}
253
khenaidoo106c61a2021-08-11 18:05:46 -0400254func TestOpenOLT_DownloadImage(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530255 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400256 request *ic.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530257 }
258 tests := []struct {
259 name string
260 fields *fields
261 args args
262 want *voltha.ImageDownload
263 wantErr error
264 }{
265 {"download_image-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530266 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530267 {"download_image-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123LKJ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530268 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530269 {"download_image-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123RTY"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530270 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530271 }
272 for _, tt := range tests {
273 t.Run(tt.name, func(t *testing.T) {
274 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400275 got, err := oo.DownloadImage(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800276 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530277 t.Errorf("Download_image() error = %v, wantErr %v", err, tt.wantErr)
278 }
279 })
280 }
281}
282
khenaidoo106c61a2021-08-11 18:05:46 -0400283func TestOpenOLT_GetImageDownloadStatus(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530284 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400285 request *ic.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530286 }
287 tests := []struct {
288 name string
289 fields *fields
290 args args
291 want *voltha.ImageDownload
292 wantErr error
293 }{
294 {"get_image_download_status-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530295 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530296 {"get_image_download_status-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123LKJ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530297 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530298 {"get_image_download_status-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123DFG"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530299 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530300 }
301 for _, tt := range tests {
302 t.Run(tt.name, func(t *testing.T) {
303 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400304 got, err := oo.GetImageDownloadStatus(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800305 if err != tt.wantErr && got == nil {
306 t.Errorf("Get_image_download_status() got = %v want = %v error = %v, wantErr %v",
307 got, tt.want, err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530308 }
309 })
310 }
311}
312
khenaidoo106c61a2021-08-11 18:05:46 -0400313func TestOpenOLT_GetOfpDeviceInfo(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530314 type args struct {
315 device *voltha.Device
316 }
317 tests := []struct {
318 name string
319 fields *fields
320 args args
321 want *ic.SwitchCapability
322 wantErr error
323 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800324 {"get_ofp_device_info-1", mockOlt(), args{mockDevice()}, &ic.SwitchCapability{
325 Desc: &openflow_13.OfpDesc{
326 MfrDesc: "VOLTHA Project",
327 HwDesc: "open_pon",
328 SwDesc: "open_pon",
329 },
330 SwitchFeatures: &openflow_13.OfpSwitchFeatures{
331 NBuffers: uint32(256),
332 NTables: uint32(2),
333 Capabilities: uint32(15),
334 },
335 }, nil},
336 {"get_ofp_device_info-2", &fields{}, args{mockDevice()}, nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530337 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530338 }
339 for _, tt := range tests {
340 t.Run(tt.name, func(t *testing.T) {
341 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400342 got, err := oo.GetOfpDeviceInfo(context.Background(), tt.args.device)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800343 if !reflect.DeepEqual(err, tt.wantErr) || !reflect.DeepEqual(got, tt.want) {
344 t.Errorf("Get_ofp_device_info() got = %v want = %v error = %v, wantErr = %v",
345 got, tt.want, err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530346 }
347 })
348 }
349}
350
khenaidoo106c61a2021-08-11 18:05:46 -0400351// func TestOpenOLT_Process_inter_adapter_message(t *testing.T) {
352// type args struct {
353// msg *ic.InterAdapterMessage
354// }
355// var message1 = args{
356// msg: &ic.InterAdapterMessage{
357// Header: &ic.InterAdapterHeader{
358// Id: "olt",
359// ProxyDeviceId: "",
360// ToDeviceId: "onu1",
361// },
362// },
363// }
364// var message2 = args{
365// msg: &ic.InterAdapterMessage{
366// Header: &ic.InterAdapterHeader{
367// Id: "olt",
368// ProxyDeviceId: "olt",
369// ToDeviceId: "olt",
370// Type: ic.InterAdapterMessageType_OMCI_REQUEST,
371// },
372// },
373// }
374// var message3 = args{
375// msg: &ic.InterAdapterMessage{
376// Header: &ic.InterAdapterHeader{
377// Id: "olt",
378// ProxyDeviceId: "olt",
379// ToDeviceId: "olt",
380// Type: ic.InterAdapterMessageType_FLOW_REQUEST,
381// },
382// },
383// }
384// tests := []struct {
385// name string
386// fields *fields
387// args args
388// wantErrType reflect.Type
389// }{
390// {"process_inter_adaptor_messgae-1", mockOlt(), message1,
391// reflect.TypeOf(&olterrors.ErrNotFound{})},
392// {"process_inter_adaptor_messgae-2", mockOlt(), message2,
393// reflect.TypeOf(&olterrors.ErrAdapter{})},
394// {"process_inter_adaptor_messgae-3", mockOlt(), message3,
395// reflect.TypeOf(&olterrors.ErrInvalidValue{})},
396// }
397// for _, tt := range tests {
398// t.Run(tt.name, func(t *testing.T) {
399// oo := testOltObject(tt.fields)
400// if err := oo.Process_inter_adapter_message(context.Background(), tt.args.msg); reflect.TypeOf(err) != tt.wantErrType {
401// t.Errorf("Process_inter_adapter_message() error = %v, wantErr %v",
402// reflect.TypeOf(err), tt.wantErrType)
403// }
404// })
405// }
406// }
cbabua9e04cc2019-10-03 12:35:45 +0530407
khenaidoo106c61a2021-08-11 18:05:46 -0400408func TestOpenOLT_RebootDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530409 type args struct {
410 device *voltha.Device
411 }
412 tests := []struct {
413 name string
414 fields *fields
415 args args
416 wantErr error
417 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800418 {"reboot_device-1", mockOlt(), args{mockDevice()}, nil},
419 {"reboot_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530420 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530421 }
422 for _, tt := range tests {
423 t.Run(tt.name, func(t *testing.T) {
424 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400425 if _, err := oo.RebootDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530426 t.Errorf("Reboot_device() error = %v, wantErr %v", err, tt.wantErr)
427 }
428 })
429 }
430}
431
khenaidoo106c61a2021-08-11 18:05:46 -0400432func TestOpenOLT_SendPacketOut(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530433 acts := []*ofp.OfpAction{
434 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
435 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
436 fu.Output(1),
437 }
438 type args struct {
439 deviceID string
440 egressPortNo int
441 packet *openflow_13.OfpPacketOut
442 }
443 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUx" +
444 "BgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+" +
445 "GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
446 tests := []struct {
447 name string
448 fields *fields
449 args args
450 wantErr error
451 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800452 {"receive_packet_out-1", mockOlt(), args{mockDevice().Id, 1, pktout}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530453 {"receive_packet_out-2", mockOlt(), args{"1234", 1, pktout},
Thomas Lee S94109f12020-03-03 16:39:29 +0530454 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "1234"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530455 }
456 for _, tt := range tests {
457 t.Run(tt.name, func(t *testing.T) {
458 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400459 if _, err := oo.SendPacketOut(context.Background(), &ic.PacketOut{
460 DeviceId: tt.args.deviceID,
461 EgressPortNo: uint32(tt.args.egressPortNo),
462 Packet: tt.args.packet}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530463 t.Errorf("Receive_packet_out() error = %v, wantErr %v", err, tt.wantErr)
464 }
465 })
466 }
467}
468
khenaidoo106c61a2021-08-11 18:05:46 -0400469func TestOpenOLT_ReconcileDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530470 type args struct {
471 device *voltha.Device
472 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530473 expectedError := olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
cbabua9e04cc2019-10-03 12:35:45 +0530474 tests := []struct {
475 name string
476 fields *fields
477 args args
478 wantErr error
479 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800480 {"reconcile_device-1", &fields{}, args{}, expectedError},
481 {"reconcile_device-2", &fields{}, args{}, expectedError},
482 {"reconcile_device-3", &fields{}, args{}, expectedError},
cbabua9e04cc2019-10-03 12:35:45 +0530483 }
484 for _, tt := range tests {
485 t.Run(tt.name, func(t *testing.T) {
486 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400487 if _, err := oo.ReconcileDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530488 t.Errorf("Reconcile_device() error = %v, wantErr %v", err, tt.wantErr)
489 }
490 })
491 }
492}
493
khenaidoo106c61a2021-08-11 18:05:46 -0400494func TestOpenOLT_ReEnableDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530495 type args struct {
496 device *voltha.Device
497 }
498 tests := []struct {
499 name string
500 fields *fields
501 args args
502 wantErr error
503 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800504 {"reenable_device-1", mockOlt(), args{mockDevice()}, nil},
505 {"reenable_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530506 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530507 }
508 for _, tt := range tests {
509 t.Run(tt.name, func(t *testing.T) {
510 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400511 if _, err := oo.ReEnableDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530512 t.Errorf("Reenable_device() error = %v, wantErr %v", err, tt.wantErr)
513 }
514 })
515 }
516}
517
khenaidoo106c61a2021-08-11 18:05:46 -0400518func TestOpenOLT_RevertImageUpdate(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530519 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400520 request *ic.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530521 }
522 tests := []struct {
523 name string
524 fields *fields
525 args args
526 want *voltha.ImageDownload
527 wantErr error
528 }{
529 {"revert_image_update-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530530 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530531 {"revert_image_update-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123TYU"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530532 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530533 {"revert_image_update-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123GTH"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530534 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530535 }
536 for _, tt := range tests {
537 t.Run(tt.name, func(t *testing.T) {
538 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400539 got, err := oo.RevertImageUpdate(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800540 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530541 t.Log("error :", err)
542 }
543 })
544 }
545}
546
khenaidoo106c61a2021-08-11 18:05:46 -0400547func TestOpenOLT_SelfTestDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530548 type args struct {
549 device *voltha.Device
550 }
551 tests := []struct {
552 name string
553 fields *fields
554 args args
555 wantErr error
556 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530557 {"self_test_device-1", &fields{}, args{}, olterrors.ErrNotImplemented},
558 {"self_test_device-2", &fields{}, args{}, olterrors.ErrNotImplemented},
559 {"self_test_device-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530560 }
561 for _, tt := range tests {
562 t.Run(tt.name, func(t *testing.T) {
563 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400564 if _, err := oo.SelfTestDevice(context.Background(), tt.args.device); err != tt.wantErr {
cbabua9e04cc2019-10-03 12:35:45 +0530565 t.Errorf("Self_test_device() error = %v, wantErr %v", err, tt.wantErr)
566 }
567 })
568 }
569}
570
571func TestOpenOLT_Start(t *testing.T) {
572 type args struct {
573 ctx context.Context
574 }
575 tests := []struct {
576 name string
577 fields *fields
578 args args
579 wantErr error
580 }{
581 {"start-1", &fields{}, args{}, errors.New("start error")},
582 }
583 for _, tt := range tests {
584 t.Run(tt.name, func(t *testing.T) {
585 oo := testOltObject(tt.fields)
586 if err := oo.Start(tt.args.ctx); err != nil {
587 t.Errorf("Start() error = %v, wantErr %v", err, tt.wantErr)
588 }
589 })
590 }
591}
592
593func TestOpenOLT_Stop(t *testing.T) {
594 type args struct {
595 ctx context.Context
596 }
597 tests := []struct {
598 name string
599 fields *fields
600 args args
601 wantErr error
602 }{
603 {"stop-1", &fields{exitChannel: make(chan int, 1)}, args{}, errors.New("stop error")},
604 }
605 for _, tt := range tests {
606 t.Run(tt.name, func(t *testing.T) {
607 oo := testOltObject(tt.fields)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400608 if err := oo.Start(tt.args.ctx); err != nil {
609 t.Error(err)
610 }
cbabua9e04cc2019-10-03 12:35:45 +0530611 if err := oo.Stop(tt.args.ctx); err != nil {
612 t.Errorf("Stop() error = %v, wantErr %v", err, tt.wantErr)
613 }
614 })
615 }
616}
617
khenaidoo106c61a2021-08-11 18:05:46 -0400618func TestOpenOLT_SuppressEvent(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530619 type args struct {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000620 filter *voltha.EventFilter
cbabua9e04cc2019-10-03 12:35:45 +0530621 }
622 tests := []struct {
623 name string
624 fields *fields
625 args args
626 wantErr error
627 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530628 {"suppress_event-1", &fields{}, args{}, olterrors.ErrNotImplemented},
629 {"suppress_event-2", &fields{}, args{}, olterrors.ErrNotImplemented},
630 {"suppress_event-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530631 }
632 for _, tt := range tests {
633 t.Run(tt.name, func(t *testing.T) {
634 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400635 if _, err := oo.SuppressEvent(context.Background(), tt.args.filter); err != tt.wantErr {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000636 t.Errorf("Suppress_event() error = %v, wantErr %v", err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530637 }
638 })
639 }
640}
641
khenaidoo106c61a2021-08-11 18:05:46 -0400642func TestOpenOLT_UnSuppressEvent(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530643 type args struct {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000644 filter *voltha.EventFilter
cbabua9e04cc2019-10-03 12:35:45 +0530645 }
646 tests := []struct {
647 name string
648 fields *fields
649 args args
650 wantErr error
651 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530652 {"unsupress_event-1", &fields{}, args{}, olterrors.ErrNotImplemented},
653 {"unsupress_event-2", &fields{}, args{}, olterrors.ErrNotImplemented},
654 {"unsupress_event-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530655 }
656 for _, tt := range tests {
657 t.Run(tt.name, func(t *testing.T) {
658 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400659 if _, err := oo.UnSuppressEvent(context.Background(), tt.args.filter); err != tt.wantErr {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000660 t.Errorf("Unsuppress_event() error = %v, wantErr %v", err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530661 }
662 })
663 }
664}
665
khenaidoo106c61a2021-08-11 18:05:46 -0400666func TestOpenOLT_UpdateFlowsBulk(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530667 type args struct {
668 device *voltha.Device
669 flows *voltha.Flows
670 groups *voltha.FlowGroups
671 flowMetadata *voltha.FlowMetadata
672 }
673 tests := []struct {
674 name string
675 fields *fields
676 args args
677 wantErr error
678 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530679 {"update_flows_bulk-1", &fields{}, args{}, olterrors.ErrNotImplemented},
680 {"update_flows_bulk-2", &fields{}, args{}, olterrors.ErrNotImplemented},
681 {"update_flows_bulk-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530682 }
683 for _, tt := range tests {
684 t.Run(tt.name, func(t *testing.T) {
685 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400686 if _, err := oo.UpdateFlowsBulk(context.Background(), &ic.BulkFlows{
687 Device: tt.args.device,
688 Flows: tt.args.flows,
689 Groups: tt.args.groups,
690 FlowMetadata: tt.args.flowMetadata,
691 }); err != tt.wantErr {
cbabua9e04cc2019-10-03 12:35:45 +0530692 t.Errorf("Update_flows_bulk() error = %v, wantErr %v", err, tt.wantErr)
693 }
694 })
695 }
696}
697
khenaidoo106c61a2021-08-11 18:05:46 -0400698func TestOpenOLT_UpdateFlowsIncrementally(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530699 type args struct {
700 device *voltha.Device
701 flows *openflow_13.FlowChanges
702 groups *openflow_13.FlowGroupChanges
703 flowMetadata *voltha.FlowMetadata
704 }
705
706 tests := []struct {
707 name string
708 fields *fields
709 args args
710 wantErr error
711 }{
712 {"update_flows_incrementally-1", &fields{}, args{device: mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530713 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800714 {"update_flows_incrementally-2", mockOlt(), args{device: mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530715 }
716 for _, tt := range tests {
717 t.Run(tt.name, func(t *testing.T) {
718 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400719 if _, err := oo.UpdateFlowsIncrementally(context.Background(), &ic.IncrementalFlows{
720 Device: tt.args.device,
721 Flows: tt.args.flows,
722 Groups: tt.args.groups,
723 FlowMetadata: tt.args.flowMetadata}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530724 t.Errorf("Update_flows_incrementally() error = %v, wantErr %v", err, tt.wantErr)
725 }
726 })
727 }
728}
729
khenaidoo106c61a2021-08-11 18:05:46 -0400730func TestOpenOLT_UpdatePmConfig(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530731 type args struct {
732 device *voltha.Device
733 pmConfigs *voltha.PmConfigs
734 }
735 tests := []struct {
736 name string
737 fields *fields
738 args args
739 wantErr error
740 }{
Rohan Agrawalda5e0b22020-05-20 11:10:26 +0000741 {"update_pm_config-1", mockOlt(), args{device: mockDevice(), pmConfigs: &voltha.PmConfigs{DefaultFreq: 150, Grouped: false, FreqOverride: false}}, nil},
742 {"update_pm_config-2", &fields{}, args{device: mockDevice(), pmConfigs: &voltha.PmConfigs{DefaultFreq: 150, Grouped: false, FreqOverride: false}}, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530743 }
744 for _, tt := range tests {
745 t.Run(tt.name, func(t *testing.T) {
746 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400747 if _, err := oo.UpdatePmConfig(context.Background(), &ic.PmConfigsInfo{DeviceId: tt.args.device.Id, PmConfigs: tt.args.pmConfigs}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530748 t.Errorf("Update_pm_config() error = %v, wantErr %v", err, tt.wantErr)
749 }
750
751 })
752 }
753}
754
755func TestOpenOLT_deleteDeviceHandlerToMap(t *testing.T) {
756 type args struct {
757 agent *DeviceHandler
758 }
759 tests := []struct {
760 name string
761 fields *fields
762 args args
763 }{
764 {"delete_device_handler_map-1", mockOlt(), args{newMockDeviceHandler()}},
765 }
766 for _, tt := range tests {
767 t.Run(tt.name, func(t *testing.T) {
768 oo := testOltObject(tt.fields)
769 oo.deleteDeviceHandlerToMap(tt.args.agent)
770 if len(oo.deviceHandlers) > 0 {
771 t.Errorf("delete device manager failed")
772 }
773 })
774 }
775}
kesavand39e0aa32020-01-28 20:58:50 -0500776
khenaidoo106c61a2021-08-11 18:05:46 -0400777func TestOpenOLT_EnablePort(t *testing.T) {
kesavand39e0aa32020-01-28 20:58:50 -0500778 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400779 port *voltha.Port
kesavand39e0aa32020-01-28 20:58:50 -0500780 }
781 tests := []struct {
782 name string
783 fields *fields
784 args args
785 wantErr bool
786 }{
787 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400788 {"Enable_port-1", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_PON_OLT, PortNo: 1, DeviceId: "olt"}}, false},
789 {"Enable_port-2", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_ETHERNET_NNI, PortNo: 1, DeviceId: "olt"}}, true},
kesavand39e0aa32020-01-28 20:58:50 -0500790 }
791 for _, tt := range tests {
792 t.Run(tt.name, func(t *testing.T) {
793 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400794 if _, err := oo.EnablePort(context.Background(), tt.args.port); (err != nil) != tt.wantErr {
kesavand39e0aa32020-01-28 20:58:50 -0500795 t.Errorf("OpenOLT.Enable_port() error = %v, wantErr %v", err, tt.wantErr)
796 }
797 })
798 }
799}
800
khenaidoo106c61a2021-08-11 18:05:46 -0400801func TestOpenOLT_DisablePort(t *testing.T) {
kesavand39e0aa32020-01-28 20:58:50 -0500802 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400803 port *voltha.Port
kesavand39e0aa32020-01-28 20:58:50 -0500804 }
805 tests := []struct {
806 name string
807 fields *fields
808 args args
809 wantErr bool
810 }{
811 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400812 {"Disable_port-1", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_PON_OLT, PortNo: 1, DeviceId: "olt"}}, false},
813 {"Disable_port-2", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_ETHERNET_NNI, PortNo: 1, DeviceId: "olt"}}, true},
kesavand39e0aa32020-01-28 20:58:50 -0500814 }
815 for _, tt := range tests {
816 t.Run(tt.name, func(t *testing.T) {
817 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400818 if _, err := oo.DisablePort(context.Background(), tt.args.port); (err != nil) != tt.wantErr {
kesavand39e0aa32020-01-28 20:58:50 -0500819 t.Errorf("OpenOLT.Disable_port() error = %v, wantErr %v", err, tt.wantErr)
820 }
821 })
822 }
823}