blob: 776fef48f0d975d15bda18d0b170c6592d769f72 [file] [log] [blame]
cbabua9e04cc2019-10-03 12:35:45 +05301/*
Joey Armstronga6af1522023-01-17 16:06:16 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
cbabua9e04cc2019-10-03 12:35:45 +05303
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"
Gustavo Silva41af9122022-10-11 11:05:13 -030033 "github.com/opencord/voltha-protos/v5/go/openolt"
34 "github.com/stretchr/testify/assert"
serkant.uluderya7b8211e2021-02-24 16:39:18 +030035
khenaidoo106c61a2021-08-11 18:05:46 -040036 "github.com/opencord/voltha-lib-go/v7/pkg/events"
37 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
38 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Scott Bakerdbd960e2020-02-28 08:57:51 -080039 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053040 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
khenaidoodc2116e2021-10-19 17:33:19 -040041 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo106c61a2021-08-11 18:05:46 -040042 "github.com/opencord/voltha-protos/v5/go/openflow_13"
43 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
44 "github.com/opencord/voltha-protos/v5/go/voltha"
cbabua9e04cc2019-10-03 12:35:45 +053045)
46
cbabubef89432019-10-18 11:47:27 +020047// mocks the OpenOLT struct.
cbabua9e04cc2019-10-03 12:35:45 +053048type fields struct {
Kent Hagermane6ff1012020-07-14 15:07:53 -040049 deviceHandlers map[string]*DeviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -040050 coreClient *vgrpc.Client
Himani Chawlacd407802020-12-10 12:08:59 +053051 eventProxy *events.EventProxy
Kent Hagermane6ff1012020-07-14 15:07:53 -040052 numOnus int
53 KVStoreAddress string
54 KVStoreType string
khenaidooefff76e2021-12-15 16:51:30 -050055 exitChannel chan struct{}
Kent Hagermane6ff1012020-07-14 15:07:53 -040056 ctx context.Context
cbabua9e04cc2019-10-03 12:35:45 +053057}
58
cbabubef89432019-10-18 11:47:27 +020059// mockOlt mocks OpenOLT struct.
cbabua9e04cc2019-10-03 12:35:45 +053060func mockOlt() *fields {
cbabua9e04cc2019-10-03 12:35:45 +053061 dh := newMockDeviceHandler()
62 newOlt := &fields{}
63 newOlt.deviceHandlers = map[string]*DeviceHandler{}
64 newOlt.deviceHandlers[dh.device.Id] = dh
65 return newOlt
66}
67
cbabubef89432019-10-18 11:47:27 +020068// testOltObject maps fields type to OpenOLt type.
cbabua9e04cc2019-10-03 12:35:45 +053069func testOltObject(testOlt *fields) *OpenOLT {
70 return &OpenOLT{
71 deviceHandlers: testOlt.deviceHandlers,
cbabua9e04cc2019-10-03 12:35:45 +053072 eventProxy: testOlt.eventProxy,
cbabua9e04cc2019-10-03 12:35:45 +053073 numOnus: testOlt.numOnus,
Neha Sharma3f221ae2020-04-29 19:02:12 +000074 KVStoreAddress: testOlt.KVStoreAddress,
cbabua9e04cc2019-10-03 12:35:45 +053075 KVStoreType: testOlt.KVStoreType,
76 exitChannel: testOlt.exitChannel,
77 }
78}
79
cbabubef89432019-10-18 11:47:27 +020080// mockDevice mocks Device.
cbabua9e04cc2019-10-03 12:35:45 +053081func mockDevice() *voltha.Device {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040082 return &voltha.Device{
cbabua9e04cc2019-10-03 12:35:45 +053083 Id: "olt",
84 Root: true,
85 ParentId: "logical_device",
cbabua9e04cc2019-10-03 12:35:45 +053086 ProxyAddress: &voltha.Device_ProxyAddress{
87 DeviceId: "olt",
88 DeviceType: "onu",
89 ChannelId: 1,
90 ChannelGroupId: 1,
91 },
92 ConnectStatus: 1,
93 }
cbabua9e04cc2019-10-03 12:35:45 +053094}
95
96func TestNewOpenOLT(t *testing.T) {
97 tests := []struct {
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053098 name string
99 fields *fields
100 configFlags *config.AdapterFlags
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800101 cm *conf.ConfigManager
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530102 want *OpenOLT
cbabua9e04cc2019-10-03 12:35:45 +0530103 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300104 {"newopenolt-1", &fields{}, &config.AdapterFlags{OnuNumber: 1, KVStoreAddress: "1.1.1.1:1", KVStoreType: "etcd"}, &conf.ConfigManager{},
105 &OpenOLT{numOnus: 1, KVStoreAddress: "1.1.1.1:1", KVStoreType: "etcd"}},
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800106 {"newopenolt-2", &fields{}, &config.AdapterFlags{OnuNumber: 2, KVStoreAddress: "2.2.2.2:2", KVStoreType: "etcd"}, &conf.ConfigManager{},
Neha Sharma3f221ae2020-04-29 19:02:12 +0000107 &OpenOLT{numOnus: 2, KVStoreAddress: "2.2.2.2:2", KVStoreType: "etcd"}},
cbabua9e04cc2019-10-03 12:35:45 +0530108 }
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400111 if got := NewOpenOLT(tt.fields.ctx, tt.fields.coreClient,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800112 tt.fields.eventProxy, tt.configFlags, tt.cm); reflect.TypeOf(got) != reflect.TypeOf(tt.want) && got != nil {
cbabua9e04cc2019-10-03 12:35:45 +0530113 t.Errorf("NewOpenOLT() error = %v, wantErr %v", got, tt.want)
114 }
115 })
116 }
117}
118
khenaidoo106c61a2021-08-11 18:05:46 -0400119func TestOpenOLT_ActivateImageUpdate(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530120 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400121 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530122 }
123 tests := []struct {
124 name string
125 fields *fields
126 args args
127 want *voltha.ImageDownload
128 wantErr error
129 }{
130 {"activate_image_upate-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530131 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530132 {"activate_image_upate-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123CDE"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530133 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530134 {"activate_image_upate-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123EFG"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530135 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530136 }
137 for _, tt := range tests {
138 t.Run(tt.name, func(t *testing.T) {
139 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400140 got, err := oo.ActivateImageUpdate(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800141 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530142 t.Errorf("Activate_image_update() error = %v, wantErr %v", err, tt.wantErr)
143 }
144 })
145 }
146}
147
khenaidoo106c61a2021-08-11 18:05:46 -0400148func TestOpenOLT_AdoptDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530149 type args struct {
150 device *voltha.Device
151 }
152 var device = mockDevice()
kesavand39e0aa32020-01-28 20:58:50 -0500153 device.Id = "olt"
Thomas Lee S94109f12020-03-03 16:39:29 +0530154 nilDevice := olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
cbabua9e04cc2019-10-03 12:35:45 +0530155 tests := []struct {
156 name string
157 fields *fields
158 args args
159 wantErr error
160 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800161 {"adopt_device-1", mockOlt(), args{}, nilDevice},
162 {"adopt_device-2", mockOlt(), args{device}, nilDevice},
163 {"adopt_device-3", mockOlt(), args{mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530164 }
165 for _, tt := range tests {
166 t.Run(tt.name, func(t *testing.T) {
167 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400168 _, err := oo.AdoptDevice(context.Background(), tt.args.device)
cbabua9e04cc2019-10-03 12:35:45 +0530169 if (err != nil) && (reflect.TypeOf(err) !=
170 reflect.TypeOf(tt.wantErr)) && (tt.args.device == nil) {
171 t.Errorf("Adopt_device() error = %v, wantErr %v", err, tt.wantErr)
172 }
173 if err == nil {
174 t.Log("return'd nil")
175 }
176 })
177 }
178}
179
khenaidoo106c61a2021-08-11 18:05:46 -0400180func TestOpenOLT_CancelImageDownload(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530181 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400182 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530183 }
184 tests := []struct {
185 name string
186 fields *fields
187 args args
188 want *voltha.ImageDownload
189 wantErr error
190 }{
191 {"cancel_image_download-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530192 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530193 {"cancel_image_download-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123IJK"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530194 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530195 {"cancel_image_download-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123KLM"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530196 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530197 }
198 for _, tt := range tests {
199 t.Run(tt.name, func(t *testing.T) {
200 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400201 got, err := oo.CancelImageDownload(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800202 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530203 t.Errorf("Cancel_image_download() error = %v, wantErr %v", err, tt.wantErr)
204 }
205 })
206 }
207}
208
khenaidoo106c61a2021-08-11 18:05:46 -0400209func TestOpenOLT_DeleteDevice(t *testing.T) {
Gustavo Silva41af9122022-10-11 11:05:13 -0300210 oo1 := testOltObject(&fields{})
211 oo2 := testOltObject(&fields{})
212 oo2.deviceHandlers = make(map[string]*DeviceHandler)
213 oo2.deviceHandlers[mockDevice().Id] = newMockDeviceHandler()
214 oo3 := testOltObject(&fields{})
215 oo3.deviceHandlers = make(map[string]*DeviceHandler)
216 oo3.deviceHandlers[mockDevice().Id] = newMockDeviceHandler()
217 _, err := oo3.deviceHandlers[mockDevice().Id].Client.Reboot(context.Background(), &openolt.Empty{})
218 assert.Nil(t, err)
219
cbabua9e04cc2019-10-03 12:35:45 +0530220 type args struct {
Gustavo Silva41af9122022-10-11 11:05:13 -0300221 oo *OpenOLT
cbabua9e04cc2019-10-03 12:35:45 +0530222 device *voltha.Device
223 }
224 tests := []struct {
225 name string
226 fields *fields
227 args args
228 wantErr error
229 }{
Gustavo Silva41af9122022-10-11 11:05:13 -0300230 {"delete_device-1", &fields{}, args{oo1, mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530231 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
Gustavo Silva41af9122022-10-11 11:05:13 -0300232 {"delete_device-2", &fields{}, args{oo2, mockDevice()}, nil},
233 {"delete_device-3", &fields{}, args{oo3, mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530234 }
235 for _, tt := range tests {
236 t.Run(tt.name, func(t *testing.T) {
Gustavo Silva41af9122022-10-11 11:05:13 -0300237 if _, err := tt.args.oo.DeleteDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530238 t.Errorf("Delete_device() error = %v, wantErr %v", err, tt.wantErr)
239 }
240 })
241 }
242}
243
khenaidoo106c61a2021-08-11 18:05:46 -0400244func TestOpenOLT_DisableDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530245 type args struct {
246 device *voltha.Device
247 }
248 tests := []struct {
249 name string
250 fields *fields
251 args args
252 wantErr error
253 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800254 {"disable_device-1", mockOlt(), args{mockDevice()}, nil},
255 {"disable_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530256 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530257 }
258 for _, tt := range tests {
259 t.Run(tt.name, func(t *testing.T) {
260 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400261 if _, err := oo.DisableDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530262 t.Errorf("Disable_device() error = %v, wantErr %v", err, tt.wantErr)
263 }
264 })
265 }
266}
267
khenaidoo106c61a2021-08-11 18:05:46 -0400268func TestOpenOLT_DownloadImage(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530269 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400270 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530271 }
272 tests := []struct {
273 name string
274 fields *fields
275 args args
276 want *voltha.ImageDownload
277 wantErr error
278 }{
279 {"download_image-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530280 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530281 {"download_image-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123LKJ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530282 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530283 {"download_image-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123RTY"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530284 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530285 }
286 for _, tt := range tests {
287 t.Run(tt.name, func(t *testing.T) {
288 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400289 got, err := oo.DownloadImage(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800290 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530291 t.Errorf("Download_image() error = %v, wantErr %v", err, tt.wantErr)
292 }
293 })
294 }
295}
296
khenaidoo106c61a2021-08-11 18:05:46 -0400297func TestOpenOLT_GetImageDownloadStatus(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530298 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400299 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530300 }
301 tests := []struct {
302 name string
303 fields *fields
304 args args
305 want *voltha.ImageDownload
306 wantErr error
307 }{
308 {"get_image_download_status-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530309 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530310 {"get_image_download_status-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123LKJ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530311 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530312 {"get_image_download_status-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123DFG"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530313 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530314 }
315 for _, tt := range tests {
316 t.Run(tt.name, func(t *testing.T) {
317 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400318 got, err := oo.GetImageDownloadStatus(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800319 if err != tt.wantErr && got == nil {
320 t.Errorf("Get_image_download_status() got = %v want = %v error = %v, wantErr %v",
321 got, tt.want, err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530322 }
323 })
324 }
325}
326
khenaidoo106c61a2021-08-11 18:05:46 -0400327func TestOpenOLT_GetOfpDeviceInfo(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530328 type args struct {
329 device *voltha.Device
330 }
331 tests := []struct {
332 name string
333 fields *fields
334 args args
khenaidoodc2116e2021-10-19 17:33:19 -0400335 want *ca.SwitchCapability
cbabua9e04cc2019-10-03 12:35:45 +0530336 wantErr error
337 }{
khenaidoodc2116e2021-10-19 17:33:19 -0400338 {"get_ofp_device_info-1", mockOlt(), args{mockDevice()}, &ca.SwitchCapability{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800339 Desc: &openflow_13.OfpDesc{
340 MfrDesc: "VOLTHA Project",
341 HwDesc: "open_pon",
342 SwDesc: "open_pon",
343 },
344 SwitchFeatures: &openflow_13.OfpSwitchFeatures{
345 NBuffers: uint32(256),
346 NTables: uint32(2),
347 Capabilities: uint32(15),
348 },
349 }, nil},
350 {"get_ofp_device_info-2", &fields{}, args{mockDevice()}, nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530351 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530352 }
353 for _, tt := range tests {
354 t.Run(tt.name, func(t *testing.T) {
355 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400356 got, err := oo.GetOfpDeviceInfo(context.Background(), tt.args.device)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800357 if !reflect.DeepEqual(err, tt.wantErr) || !reflect.DeepEqual(got, tt.want) {
358 t.Errorf("Get_ofp_device_info() got = %v want = %v error = %v, wantErr = %v",
359 got, tt.want, err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530360 }
361 })
362 }
363}
364
khenaidoo106c61a2021-08-11 18:05:46 -0400365func TestOpenOLT_RebootDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530366 type args struct {
367 device *voltha.Device
368 }
369 tests := []struct {
370 name string
371 fields *fields
372 args args
373 wantErr error
374 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800375 {"reboot_device-1", mockOlt(), args{mockDevice()}, nil},
376 {"reboot_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530377 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530378 }
379 for _, tt := range tests {
380 t.Run(tt.name, func(t *testing.T) {
381 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400382 if _, err := oo.RebootDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530383 t.Errorf("Reboot_device() error = %v, wantErr %v", err, tt.wantErr)
384 }
385 })
386 }
387}
388
khenaidoo106c61a2021-08-11 18:05:46 -0400389func TestOpenOLT_SendPacketOut(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530390 acts := []*ofp.OfpAction{
391 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
392 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
393 fu.Output(1),
394 }
395 type args struct {
396 deviceID string
397 egressPortNo int
398 packet *openflow_13.OfpPacketOut
399 }
400 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUx" +
401 "BgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+" +
402 "GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
403 tests := []struct {
404 name string
405 fields *fields
406 args args
407 wantErr error
408 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800409 {"receive_packet_out-1", mockOlt(), args{mockDevice().Id, 1, pktout}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530410 {"receive_packet_out-2", mockOlt(), args{"1234", 1, pktout},
Thomas Lee S94109f12020-03-03 16:39:29 +0530411 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "1234"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530412 }
413 for _, tt := range tests {
414 t.Run(tt.name, func(t *testing.T) {
415 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400416 if _, err := oo.SendPacketOut(context.Background(), &ca.PacketOut{
khenaidoo106c61a2021-08-11 18:05:46 -0400417 DeviceId: tt.args.deviceID,
418 EgressPortNo: uint32(tt.args.egressPortNo),
419 Packet: tt.args.packet}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530420 t.Errorf("Receive_packet_out() error = %v, wantErr %v", err, tt.wantErr)
421 }
422 })
423 }
424}
425
khenaidoo106c61a2021-08-11 18:05:46 -0400426func TestOpenOLT_ReconcileDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530427 type args struct {
428 device *voltha.Device
429 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530430 expectedError := olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
cbabua9e04cc2019-10-03 12:35:45 +0530431 tests := []struct {
432 name string
433 fields *fields
434 args args
435 wantErr error
436 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800437 {"reconcile_device-1", &fields{}, args{}, expectedError},
438 {"reconcile_device-2", &fields{}, args{}, expectedError},
439 {"reconcile_device-3", &fields{}, args{}, expectedError},
cbabua9e04cc2019-10-03 12:35:45 +0530440 }
441 for _, tt := range tests {
442 t.Run(tt.name, func(t *testing.T) {
443 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400444 if _, err := oo.ReconcileDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530445 t.Errorf("Reconcile_device() error = %v, wantErr %v", err, tt.wantErr)
446 }
447 })
448 }
449}
450
khenaidoo106c61a2021-08-11 18:05:46 -0400451func TestOpenOLT_ReEnableDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530452 type args struct {
453 device *voltha.Device
454 }
455 tests := []struct {
456 name string
457 fields *fields
458 args args
459 wantErr error
460 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800461 {"reenable_device-1", mockOlt(), args{mockDevice()}, nil},
462 {"reenable_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530463 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530464 }
465 for _, tt := range tests {
466 t.Run(tt.name, func(t *testing.T) {
467 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400468 if _, err := oo.ReEnableDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530469 t.Errorf("Reenable_device() error = %v, wantErr %v", err, tt.wantErr)
470 }
471 })
472 }
473}
474
khenaidoo106c61a2021-08-11 18:05:46 -0400475func TestOpenOLT_RevertImageUpdate(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530476 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400477 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530478 }
479 tests := []struct {
480 name string
481 fields *fields
482 args args
483 want *voltha.ImageDownload
484 wantErr error
485 }{
486 {"revert_image_update-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530487 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530488 {"revert_image_update-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123TYU"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530489 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530490 {"revert_image_update-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123GTH"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530491 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530492 }
493 for _, tt := range tests {
494 t.Run(tt.name, func(t *testing.T) {
495 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400496 got, err := oo.RevertImageUpdate(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800497 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530498 t.Log("error :", err)
499 }
500 })
501 }
502}
503
khenaidoo106c61a2021-08-11 18:05:46 -0400504func TestOpenOLT_SelfTestDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530505 type args struct {
506 device *voltha.Device
507 }
508 tests := []struct {
509 name string
510 fields *fields
511 args args
512 wantErr error
513 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530514 {"self_test_device-1", &fields{}, args{}, olterrors.ErrNotImplemented},
515 {"self_test_device-2", &fields{}, args{}, olterrors.ErrNotImplemented},
516 {"self_test_device-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530517 }
518 for _, tt := range tests {
519 t.Run(tt.name, func(t *testing.T) {
520 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400521 if _, err := oo.SelfTestDevice(context.Background(), tt.args.device); err != tt.wantErr {
cbabua9e04cc2019-10-03 12:35:45 +0530522 t.Errorf("Self_test_device() error = %v, wantErr %v", err, tt.wantErr)
523 }
524 })
525 }
526}
527
528func TestOpenOLT_Start(t *testing.T) {
529 type args struct {
530 ctx context.Context
531 }
532 tests := []struct {
533 name string
534 fields *fields
535 args args
536 wantErr error
537 }{
538 {"start-1", &fields{}, args{}, errors.New("start error")},
539 }
540 for _, tt := range tests {
541 t.Run(tt.name, func(t *testing.T) {
542 oo := testOltObject(tt.fields)
543 if err := oo.Start(tt.args.ctx); err != nil {
544 t.Errorf("Start() error = %v, wantErr %v", err, tt.wantErr)
545 }
546 })
547 }
548}
549
550func TestOpenOLT_Stop(t *testing.T) {
551 type args struct {
552 ctx context.Context
553 }
554 tests := []struct {
555 name string
556 fields *fields
557 args args
558 wantErr error
559 }{
khenaidooefff76e2021-12-15 16:51:30 -0500560 {"stop-1", &fields{exitChannel: make(chan struct{})}, args{}, errors.New("stop error")},
cbabua9e04cc2019-10-03 12:35:45 +0530561 }
562 for _, tt := range tests {
563 t.Run(tt.name, func(t *testing.T) {
564 oo := testOltObject(tt.fields)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400565 if err := oo.Start(tt.args.ctx); err != nil {
566 t.Error(err)
567 }
cbabua9e04cc2019-10-03 12:35:45 +0530568 if err := oo.Stop(tt.args.ctx); err != nil {
569 t.Errorf("Stop() error = %v, wantErr %v", err, tt.wantErr)
570 }
571 })
572 }
573}
574
khenaidoo106c61a2021-08-11 18:05:46 -0400575func TestOpenOLT_SuppressEvent(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530576 type args struct {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000577 filter *voltha.EventFilter
cbabua9e04cc2019-10-03 12:35:45 +0530578 }
579 tests := []struct {
580 name string
581 fields *fields
582 args args
583 wantErr error
584 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530585 {"suppress_event-1", &fields{}, args{}, olterrors.ErrNotImplemented},
586 {"suppress_event-2", &fields{}, args{}, olterrors.ErrNotImplemented},
587 {"suppress_event-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530588 }
589 for _, tt := range tests {
590 t.Run(tt.name, func(t *testing.T) {
591 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400592 if _, err := oo.SuppressEvent(context.Background(), tt.args.filter); err != tt.wantErr {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000593 t.Errorf("Suppress_event() error = %v, wantErr %v", err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530594 }
595 })
596 }
597}
598
khenaidoo106c61a2021-08-11 18:05:46 -0400599func TestOpenOLT_UnSuppressEvent(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530600 type args struct {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000601 filter *voltha.EventFilter
cbabua9e04cc2019-10-03 12:35:45 +0530602 }
603 tests := []struct {
604 name string
605 fields *fields
606 args args
607 wantErr error
608 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530609 {"unsupress_event-1", &fields{}, args{}, olterrors.ErrNotImplemented},
610 {"unsupress_event-2", &fields{}, args{}, olterrors.ErrNotImplemented},
611 {"unsupress_event-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530612 }
613 for _, tt := range tests {
614 t.Run(tt.name, func(t *testing.T) {
615 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400616 if _, err := oo.UnSuppressEvent(context.Background(), tt.args.filter); err != tt.wantErr {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000617 t.Errorf("Unsuppress_event() error = %v, wantErr %v", err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530618 }
619 })
620 }
621}
622
khenaidoo106c61a2021-08-11 18:05:46 -0400623func TestOpenOLT_UpdateFlowsBulk(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530624 type args struct {
625 device *voltha.Device
khenaidoodc2116e2021-10-19 17:33:19 -0400626 flows *ofp.Flows
627 groups *ofp.FlowGroups
628 flowMetadata *ofp.FlowMetadata
cbabua9e04cc2019-10-03 12:35:45 +0530629 }
630 tests := []struct {
631 name string
632 fields *fields
633 args args
634 wantErr error
635 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530636 {"update_flows_bulk-1", &fields{}, args{}, olterrors.ErrNotImplemented},
637 {"update_flows_bulk-2", &fields{}, args{}, olterrors.ErrNotImplemented},
638 {"update_flows_bulk-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530639 }
640 for _, tt := range tests {
641 t.Run(tt.name, func(t *testing.T) {
642 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400643 if _, err := oo.UpdateFlowsBulk(context.Background(), &ca.BulkFlows{
khenaidoo106c61a2021-08-11 18:05:46 -0400644 Device: tt.args.device,
645 Flows: tt.args.flows,
646 Groups: tt.args.groups,
647 FlowMetadata: tt.args.flowMetadata,
648 }); err != tt.wantErr {
cbabua9e04cc2019-10-03 12:35:45 +0530649 t.Errorf("Update_flows_bulk() error = %v, wantErr %v", err, tt.wantErr)
650 }
651 })
652 }
653}
654
khenaidoo106c61a2021-08-11 18:05:46 -0400655func TestOpenOLT_UpdateFlowsIncrementally(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530656 type args struct {
657 device *voltha.Device
658 flows *openflow_13.FlowChanges
659 groups *openflow_13.FlowGroupChanges
khenaidoodc2116e2021-10-19 17:33:19 -0400660 flowMetadata *ofp.FlowMetadata
cbabua9e04cc2019-10-03 12:35:45 +0530661 }
662
663 tests := []struct {
664 name string
665 fields *fields
666 args args
667 wantErr error
668 }{
669 {"update_flows_incrementally-1", &fields{}, args{device: mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530670 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800671 {"update_flows_incrementally-2", mockOlt(), args{device: mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530672 }
673 for _, tt := range tests {
674 t.Run(tt.name, func(t *testing.T) {
675 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400676 if _, err := oo.UpdateFlowsIncrementally(context.Background(), &ca.IncrementalFlows{
khenaidoo106c61a2021-08-11 18:05:46 -0400677 Device: tt.args.device,
678 Flows: tt.args.flows,
679 Groups: tt.args.groups,
680 FlowMetadata: tt.args.flowMetadata}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530681 t.Errorf("Update_flows_incrementally() error = %v, wantErr %v", err, tt.wantErr)
682 }
683 })
684 }
685}
686
khenaidoo106c61a2021-08-11 18:05:46 -0400687func TestOpenOLT_UpdatePmConfig(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530688 type args struct {
689 device *voltha.Device
690 pmConfigs *voltha.PmConfigs
691 }
692 tests := []struct {
693 name string
694 fields *fields
695 args args
696 wantErr error
697 }{
Rohan Agrawalda5e0b22020-05-20 11:10:26 +0000698 {"update_pm_config-1", mockOlt(), args{device: mockDevice(), pmConfigs: &voltha.PmConfigs{DefaultFreq: 150, Grouped: false, FreqOverride: false}}, nil},
699 {"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 +0530700 }
701 for _, tt := range tests {
702 t.Run(tt.name, func(t *testing.T) {
703 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400704 if _, err := oo.UpdatePmConfig(context.Background(), &ca.PmConfigsInfo{DeviceId: tt.args.device.Id, PmConfigs: tt.args.pmConfigs}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530705 t.Errorf("Update_pm_config() error = %v, wantErr %v", err, tt.wantErr)
706 }
707
708 })
709 }
710}
711
712func TestOpenOLT_deleteDeviceHandlerToMap(t *testing.T) {
713 type args struct {
714 agent *DeviceHandler
715 }
716 tests := []struct {
717 name string
718 fields *fields
719 args args
720 }{
721 {"delete_device_handler_map-1", mockOlt(), args{newMockDeviceHandler()}},
722 }
723 for _, tt := range tests {
724 t.Run(tt.name, func(t *testing.T) {
725 oo := testOltObject(tt.fields)
726 oo.deleteDeviceHandlerToMap(tt.args.agent)
727 if len(oo.deviceHandlers) > 0 {
728 t.Errorf("delete device manager failed")
729 }
730 })
731 }
732}
kesavand39e0aa32020-01-28 20:58:50 -0500733
khenaidoo106c61a2021-08-11 18:05:46 -0400734func TestOpenOLT_EnablePort(t *testing.T) {
kesavand39e0aa32020-01-28 20:58:50 -0500735 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400736 port *voltha.Port
kesavand39e0aa32020-01-28 20:58:50 -0500737 }
738 tests := []struct {
739 name string
740 fields *fields
741 args args
742 wantErr bool
743 }{
744 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400745 {"Enable_port-1", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_PON_OLT, PortNo: 1, DeviceId: "olt"}}, false},
746 {"Enable_port-2", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_ETHERNET_NNI, PortNo: 1, DeviceId: "olt"}}, true},
kesavand39e0aa32020-01-28 20:58:50 -0500747 }
748 for _, tt := range tests {
749 t.Run(tt.name, func(t *testing.T) {
750 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400751 if _, err := oo.EnablePort(context.Background(), tt.args.port); (err != nil) != tt.wantErr {
kesavand39e0aa32020-01-28 20:58:50 -0500752 t.Errorf("OpenOLT.Enable_port() error = %v, wantErr %v", err, tt.wantErr)
753 }
754 })
755 }
756}
757
khenaidoo106c61a2021-08-11 18:05:46 -0400758func TestOpenOLT_DisablePort(t *testing.T) {
kesavand39e0aa32020-01-28 20:58:50 -0500759 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400760 port *voltha.Port
kesavand39e0aa32020-01-28 20:58:50 -0500761 }
762 tests := []struct {
763 name string
764 fields *fields
765 args args
766 wantErr bool
767 }{
768 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400769 {"Disable_port-1", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_PON_OLT, PortNo: 1, DeviceId: "olt"}}, false},
770 {"Disable_port-2", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_ETHERNET_NNI, PortNo: 1, DeviceId: "olt"}}, true},
kesavand39e0aa32020-01-28 20:58:50 -0500771 }
772 for _, tt := range tests {
773 t.Run(tt.name, func(t *testing.T) {
774 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400775 if _, err := oo.DisablePort(context.Background(), tt.args.port); (err != nil) != tt.wantErr {
kesavand39e0aa32020-01-28 20:58:50 -0500776 t.Errorf("OpenOLT.Disable_port() error = %v, wantErr %v", err, tt.wantErr)
777 }
778 })
779 }
780}