blob: 720861e441fec440c4ad3369b1169af10bb1de0a [file] [log] [blame]
cbabua9e04cc2019-10-03 12:35:45 +05301/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2018-2024 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
Joey Armstrong3f0e2422023-07-05 18:25:41 -040022// Package core provides the utility for olt devices, flows and statistics
Scott Bakerdbd960e2020-02-28 08:57:51 -080023package 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 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
43 "github.com/opencord/voltha-protos/v5/go/voltha"
cbabua9e04cc2019-10-03 12:35:45 +053044)
45
cbabubef89432019-10-18 11:47:27 +020046// mocks the OpenOLT struct.
cbabua9e04cc2019-10-03 12:35:45 +053047type fields struct {
Kent Hagermane6ff1012020-07-14 15:07:53 -040048 deviceHandlers map[string]*DeviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -040049 coreClient *vgrpc.Client
Himani Chawlacd407802020-12-10 12:08:59 +053050 eventProxy *events.EventProxy
Kent Hagermane6ff1012020-07-14 15:07:53 -040051 numOnus int
52 KVStoreAddress string
53 KVStoreType string
khenaidooefff76e2021-12-15 16:51:30 -050054 exitChannel chan struct{}
Kent Hagermane6ff1012020-07-14 15:07:53 -040055 ctx context.Context
cbabua9e04cc2019-10-03 12:35:45 +053056}
57
cbabubef89432019-10-18 11:47:27 +020058// mockOlt mocks OpenOLT struct.
cbabua9e04cc2019-10-03 12:35:45 +053059func mockOlt() *fields {
cbabua9e04cc2019-10-03 12:35:45 +053060 dh := newMockDeviceHandler()
61 newOlt := &fields{}
62 newOlt.deviceHandlers = map[string]*DeviceHandler{}
63 newOlt.deviceHandlers[dh.device.Id] = dh
64 return newOlt
65}
66
cbabubef89432019-10-18 11:47:27 +020067// testOltObject maps fields type to OpenOLt type.
cbabua9e04cc2019-10-03 12:35:45 +053068func testOltObject(testOlt *fields) *OpenOLT {
69 return &OpenOLT{
70 deviceHandlers: testOlt.deviceHandlers,
cbabua9e04cc2019-10-03 12:35:45 +053071 eventProxy: testOlt.eventProxy,
cbabua9e04cc2019-10-03 12:35:45 +053072 numOnus: testOlt.numOnus,
Neha Sharma3f221ae2020-04-29 19:02:12 +000073 KVStoreAddress: testOlt.KVStoreAddress,
cbabua9e04cc2019-10-03 12:35:45 +053074 KVStoreType: testOlt.KVStoreType,
75 exitChannel: testOlt.exitChannel,
76 }
77}
78
cbabubef89432019-10-18 11:47:27 +020079// mockDevice mocks Device.
cbabua9e04cc2019-10-03 12:35:45 +053080func mockDevice() *voltha.Device {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040081 return &voltha.Device{
cbabua9e04cc2019-10-03 12:35:45 +053082 Id: "olt",
83 Root: true,
84 ParentId: "logical_device",
cbabua9e04cc2019-10-03 12:35:45 +053085 ProxyAddress: &voltha.Device_ProxyAddress{
86 DeviceId: "olt",
87 DeviceType: "onu",
88 ChannelId: 1,
89 ChannelGroupId: 1,
90 },
91 ConnectStatus: 1,
92 }
cbabua9e04cc2019-10-03 12:35:45 +053093}
94
95func TestNewOpenOLT(t *testing.T) {
96 tests := []struct {
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053097 name string
98 fields *fields
99 configFlags *config.AdapterFlags
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800100 cm *conf.ConfigManager
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +0530101 want *OpenOLT
cbabua9e04cc2019-10-03 12:35:45 +0530102 }{
serkant.uluderya7b8211e2021-02-24 16:39:18 +0300103 {"newopenolt-1", &fields{}, &config.AdapterFlags{OnuNumber: 1, KVStoreAddress: "1.1.1.1:1", KVStoreType: "etcd"}, &conf.ConfigManager{},
104 &OpenOLT{numOnus: 1, KVStoreAddress: "1.1.1.1:1", KVStoreType: "etcd"}},
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800105 {"newopenolt-2", &fields{}, &config.AdapterFlags{OnuNumber: 2, KVStoreAddress: "2.2.2.2:2", KVStoreType: "etcd"}, &conf.ConfigManager{},
Neha Sharma3f221ae2020-04-29 19:02:12 +0000106 &OpenOLT{numOnus: 2, KVStoreAddress: "2.2.2.2:2", KVStoreType: "etcd"}},
cbabua9e04cc2019-10-03 12:35:45 +0530107 }
108 for _, tt := range tests {
109 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400110 if got := NewOpenOLT(tt.fields.ctx, tt.fields.coreClient,
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800111 tt.fields.eventProxy, tt.configFlags, tt.cm); reflect.TypeOf(got) != reflect.TypeOf(tt.want) && got != nil {
cbabua9e04cc2019-10-03 12:35:45 +0530112 t.Errorf("NewOpenOLT() error = %v, wantErr %v", got, tt.want)
113 }
114 })
115 }
116}
117
khenaidoo106c61a2021-08-11 18:05:46 -0400118func TestOpenOLT_ActivateImageUpdate(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530119 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400120 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530121 }
122 tests := []struct {
123 name string
124 fields *fields
125 args args
126 want *voltha.ImageDownload
127 wantErr error
128 }{
129 {"activate_image_upate-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530130 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530131 {"activate_image_upate-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123CDE"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530132 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530133 {"activate_image_upate-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123EFG"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530134 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530135 }
136 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
138 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400139 got, err := oo.ActivateImageUpdate(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800140 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530141 t.Errorf("Activate_image_update() error = %v, wantErr %v", err, tt.wantErr)
142 }
143 })
144 }
145}
146
khenaidoo106c61a2021-08-11 18:05:46 -0400147func TestOpenOLT_AdoptDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530148 type args struct {
149 device *voltha.Device
150 }
151 var device = mockDevice()
kesavand39e0aa32020-01-28 20:58:50 -0500152 device.Id = "olt"
Thomas Lee S94109f12020-03-03 16:39:29 +0530153 nilDevice := olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
cbabua9e04cc2019-10-03 12:35:45 +0530154 tests := []struct {
155 name string
156 fields *fields
157 args args
158 wantErr error
159 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800160 {"adopt_device-1", mockOlt(), args{}, nilDevice},
161 {"adopt_device-2", mockOlt(), args{device}, nilDevice},
162 {"adopt_device-3", mockOlt(), args{mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530163 }
164 for _, tt := range tests {
165 t.Run(tt.name, func(t *testing.T) {
166 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400167 _, err := oo.AdoptDevice(context.Background(), tt.args.device)
cbabua9e04cc2019-10-03 12:35:45 +0530168 if (err != nil) && (reflect.TypeOf(err) !=
169 reflect.TypeOf(tt.wantErr)) && (tt.args.device == nil) {
170 t.Errorf("Adopt_device() error = %v, wantErr %v", err, tt.wantErr)
171 }
172 if err == nil {
173 t.Log("return'd nil")
174 }
175 })
176 }
177}
178
khenaidoo106c61a2021-08-11 18:05:46 -0400179func TestOpenOLT_CancelImageDownload(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530180 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400181 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530182 }
183 tests := []struct {
184 name string
185 fields *fields
186 args args
187 want *voltha.ImageDownload
188 wantErr error
189 }{
190 {"cancel_image_download-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530191 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530192 {"cancel_image_download-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123IJK"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530193 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530194 {"cancel_image_download-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123KLM"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530195 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530196 }
197 for _, tt := range tests {
198 t.Run(tt.name, func(t *testing.T) {
199 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400200 got, err := oo.CancelImageDownload(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800201 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530202 t.Errorf("Cancel_image_download() error = %v, wantErr %v", err, tt.wantErr)
203 }
204 })
205 }
206}
207
khenaidoo106c61a2021-08-11 18:05:46 -0400208func TestOpenOLT_DeleteDevice(t *testing.T) {
Gustavo Silva41af9122022-10-11 11:05:13 -0300209 oo1 := testOltObject(&fields{})
210 oo2 := testOltObject(&fields{})
211 oo2.deviceHandlers = make(map[string]*DeviceHandler)
212 oo2.deviceHandlers[mockDevice().Id] = newMockDeviceHandler()
213 oo3 := testOltObject(&fields{})
214 oo3.deviceHandlers = make(map[string]*DeviceHandler)
215 oo3.deviceHandlers[mockDevice().Id] = newMockDeviceHandler()
216 _, err := oo3.deviceHandlers[mockDevice().Id].Client.Reboot(context.Background(), &openolt.Empty{})
217 assert.Nil(t, err)
218
cbabua9e04cc2019-10-03 12:35:45 +0530219 type args struct {
Gustavo Silva41af9122022-10-11 11:05:13 -0300220 oo *OpenOLT
cbabua9e04cc2019-10-03 12:35:45 +0530221 device *voltha.Device
222 }
223 tests := []struct {
224 name string
225 fields *fields
226 args args
227 wantErr error
228 }{
Gustavo Silva41af9122022-10-11 11:05:13 -0300229 {"delete_device-1", &fields{}, args{oo1, mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530230 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
Gustavo Silva41af9122022-10-11 11:05:13 -0300231 {"delete_device-2", &fields{}, args{oo2, mockDevice()}, nil},
Akash Kankanala041a2122024-10-16 15:49:22 +0530232 {"delete_device-3", &fields{}, args{oo3, mockDevice()}, errors.New("reboot failed")},
cbabua9e04cc2019-10-03 12:35:45 +0530233 }
234 for _, tt := range tests {
235 t.Run(tt.name, func(t *testing.T) {
Gustavo Silva41af9122022-10-11 11:05:13 -0300236 if _, err := tt.args.oo.DeleteDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530237 t.Errorf("Delete_device() error = %v, wantErr %v", err, tt.wantErr)
238 }
239 })
240 }
241}
242
khenaidoo106c61a2021-08-11 18:05:46 -0400243func TestOpenOLT_DisableDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530244 type args struct {
245 device *voltha.Device
246 }
247 tests := []struct {
248 name string
249 fields *fields
250 args args
251 wantErr error
252 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800253 {"disable_device-1", mockOlt(), args{mockDevice()}, nil},
254 {"disable_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530255 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530256 }
257 for _, tt := range tests {
258 t.Run(tt.name, func(t *testing.T) {
259 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400260 if _, err := oo.DisableDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530261 t.Errorf("Disable_device() error = %v, wantErr %v", err, tt.wantErr)
262 }
263 })
264 }
265}
266
khenaidoo106c61a2021-08-11 18:05:46 -0400267func TestOpenOLT_DownloadImage(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530268 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400269 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530270 }
271 tests := []struct {
272 name string
273 fields *fields
274 args args
275 want *voltha.ImageDownload
276 wantErr error
277 }{
278 {"download_image-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530279 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530280 {"download_image-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123LKJ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530281 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530282 {"download_image-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123RTY"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530283 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530284 }
285 for _, tt := range tests {
286 t.Run(tt.name, func(t *testing.T) {
287 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400288 got, err := oo.DownloadImage(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800289 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530290 t.Errorf("Download_image() error = %v, wantErr %v", err, tt.wantErr)
291 }
292 })
293 }
294}
295
khenaidoo106c61a2021-08-11 18:05:46 -0400296func TestOpenOLT_GetImageDownloadStatus(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530297 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400298 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530299 }
300 tests := []struct {
301 name string
302 fields *fields
303 args args
304 want *voltha.ImageDownload
305 wantErr error
306 }{
307 {"get_image_download_status-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530308 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530309 {"get_image_download_status-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123LKJ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530310 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530311 {"get_image_download_status-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123DFG"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530312 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530313 }
314 for _, tt := range tests {
315 t.Run(tt.name, func(t *testing.T) {
316 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400317 got, err := oo.GetImageDownloadStatus(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800318 if err != tt.wantErr && got == nil {
319 t.Errorf("Get_image_download_status() got = %v want = %v error = %v, wantErr %v",
320 got, tt.want, err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530321 }
322 })
323 }
324}
325
khenaidoo106c61a2021-08-11 18:05:46 -0400326func TestOpenOLT_GetOfpDeviceInfo(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530327 type args struct {
328 device *voltha.Device
329 }
330 tests := []struct {
331 name string
332 fields *fields
333 args args
khenaidoodc2116e2021-10-19 17:33:19 -0400334 want *ca.SwitchCapability
cbabua9e04cc2019-10-03 12:35:45 +0530335 wantErr error
336 }{
khenaidoodc2116e2021-10-19 17:33:19 -0400337 {"get_ofp_device_info-1", mockOlt(), args{mockDevice()}, &ca.SwitchCapability{
mgouda86543582025-10-29 20:58:16 +0530338 Desc: &ofp.OfpDesc{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800339 MfrDesc: "VOLTHA Project",
340 HwDesc: "open_pon",
341 SwDesc: "open_pon",
342 },
mgouda86543582025-10-29 20:58:16 +0530343 SwitchFeatures: &ofp.OfpSwitchFeatures{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800344 NBuffers: uint32(256),
345 NTables: uint32(2),
346 Capabilities: uint32(15),
347 },
348 }, nil},
349 {"get_ofp_device_info-2", &fields{}, args{mockDevice()}, nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530350 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530351 }
352 for _, tt := range tests {
353 t.Run(tt.name, func(t *testing.T) {
354 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400355 got, err := oo.GetOfpDeviceInfo(context.Background(), tt.args.device)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800356 if !reflect.DeepEqual(err, tt.wantErr) || !reflect.DeepEqual(got, tt.want) {
357 t.Errorf("Get_ofp_device_info() got = %v want = %v error = %v, wantErr = %v",
358 got, tt.want, err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530359 }
360 })
361 }
362}
363
khenaidoo106c61a2021-08-11 18:05:46 -0400364func TestOpenOLT_RebootDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530365 type args struct {
366 device *voltha.Device
367 }
368 tests := []struct {
369 name string
370 fields *fields
371 args args
372 wantErr error
373 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800374 {"reboot_device-1", mockOlt(), args{mockDevice()}, nil},
375 {"reboot_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530376 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530377 }
378 for _, tt := range tests {
379 t.Run(tt.name, func(t *testing.T) {
380 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400381 if _, err := oo.RebootDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530382 t.Errorf("Reboot_device() error = %v, wantErr %v", err, tt.wantErr)
383 }
384 })
385 }
386}
387
khenaidoo106c61a2021-08-11 18:05:46 -0400388func TestOpenOLT_SendPacketOut(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530389 acts := []*ofp.OfpAction{
390 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
391 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
392 fu.Output(1),
393 }
394 type args struct {
395 deviceID string
396 egressPortNo int
mgouda86543582025-10-29 20:58:16 +0530397 packet *ofp.OfpPacketOut
cbabua9e04cc2019-10-03 12:35:45 +0530398 }
399 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUx" +
400 "BgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+" +
401 "GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
402 tests := []struct {
403 name string
404 fields *fields
405 args args
406 wantErr error
407 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800408 {"receive_packet_out-1", mockOlt(), args{mockDevice().Id, 1, pktout}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530409 {"receive_packet_out-2", mockOlt(), args{"1234", 1, pktout},
Thomas Lee S94109f12020-03-03 16:39:29 +0530410 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "1234"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530411 }
412 for _, tt := range tests {
413 t.Run(tt.name, func(t *testing.T) {
414 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400415 if _, err := oo.SendPacketOut(context.Background(), &ca.PacketOut{
khenaidoo106c61a2021-08-11 18:05:46 -0400416 DeviceId: tt.args.deviceID,
417 EgressPortNo: uint32(tt.args.egressPortNo),
418 Packet: tt.args.packet}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530419 t.Errorf("Receive_packet_out() error = %v, wantErr %v", err, tt.wantErr)
420 }
421 })
422 }
423}
424
khenaidoo106c61a2021-08-11 18:05:46 -0400425func TestOpenOLT_ReconcileDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530426 type args struct {
427 device *voltha.Device
428 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530429 expectedError := olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
cbabua9e04cc2019-10-03 12:35:45 +0530430 tests := []struct {
431 name string
432 fields *fields
433 args args
434 wantErr error
435 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800436 {"reconcile_device-1", &fields{}, args{}, expectedError},
437 {"reconcile_device-2", &fields{}, args{}, expectedError},
438 {"reconcile_device-3", &fields{}, args{}, expectedError},
cbabua9e04cc2019-10-03 12:35:45 +0530439 }
440 for _, tt := range tests {
441 t.Run(tt.name, func(t *testing.T) {
442 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400443 if _, err := oo.ReconcileDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530444 t.Errorf("Reconcile_device() error = %v, wantErr %v", err, tt.wantErr)
445 }
446 })
447 }
448}
449
khenaidoo106c61a2021-08-11 18:05:46 -0400450func TestOpenOLT_ReEnableDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530451 type args struct {
452 device *voltha.Device
453 }
454 tests := []struct {
455 name string
456 fields *fields
457 args args
458 wantErr error
459 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800460 {"reenable_device-1", mockOlt(), args{mockDevice()}, nil},
461 {"reenable_device-2", &fields{}, args{mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530462 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
cbabua9e04cc2019-10-03 12:35:45 +0530463 }
464 for _, tt := range tests {
465 t.Run(tt.name, func(t *testing.T) {
466 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400467 if _, err := oo.ReEnableDevice(context.Background(), tt.args.device); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530468 t.Errorf("Reenable_device() error = %v, wantErr %v", err, tt.wantErr)
469 }
470 })
471 }
472}
473
khenaidoo106c61a2021-08-11 18:05:46 -0400474func TestOpenOLT_RevertImageUpdate(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530475 type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400476 request *ca.ImageDownloadMessage
cbabua9e04cc2019-10-03 12:35:45 +0530477 }
478 tests := []struct {
479 name string
480 fields *fields
481 args args
482 want *voltha.ImageDownload
483 wantErr error
484 }{
485 {"revert_image_update-1", &fields{}, args{}, &voltha.ImageDownload{Id: "Image1-ABC123XYZ"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530486 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530487 {"revert_image_update-2", &fields{}, args{}, &voltha.ImageDownload{Id: "Image2-ABC123TYU"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530488 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530489 {"revert_image_update-3", &fields{}, args{}, &voltha.ImageDownload{Id: "Image3-ABC123GTH"},
Thomas Lee S94109f12020-03-03 16:39:29 +0530490 olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530491 }
492 for _, tt := range tests {
493 t.Run(tt.name, func(t *testing.T) {
494 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400495 got, err := oo.RevertImageUpdate(context.Background(), tt.args.request)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800496 if err != tt.wantErr && got == nil {
cbabua9e04cc2019-10-03 12:35:45 +0530497 t.Log("error :", err)
498 }
499 })
500 }
501}
502
khenaidoo106c61a2021-08-11 18:05:46 -0400503func TestOpenOLT_SelfTestDevice(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530504 type args struct {
505 device *voltha.Device
506 }
507 tests := []struct {
508 name string
509 fields *fields
510 args args
511 wantErr error
512 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530513 {"self_test_device-1", &fields{}, args{}, olterrors.ErrNotImplemented},
514 {"self_test_device-2", &fields{}, args{}, olterrors.ErrNotImplemented},
515 {"self_test_device-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530516 }
517 for _, tt := range tests {
518 t.Run(tt.name, func(t *testing.T) {
519 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400520 if _, err := oo.SelfTestDevice(context.Background(), tt.args.device); err != tt.wantErr {
cbabua9e04cc2019-10-03 12:35:45 +0530521 t.Errorf("Self_test_device() error = %v, wantErr %v", err, tt.wantErr)
522 }
523 })
524 }
525}
526
527func TestOpenOLT_Start(t *testing.T) {
528 type args struct {
529 ctx context.Context
530 }
531 tests := []struct {
532 name string
533 fields *fields
534 args args
535 wantErr error
536 }{
537 {"start-1", &fields{}, args{}, errors.New("start error")},
538 }
539 for _, tt := range tests {
540 t.Run(tt.name, func(t *testing.T) {
541 oo := testOltObject(tt.fields)
542 if err := oo.Start(tt.args.ctx); err != nil {
543 t.Errorf("Start() error = %v, wantErr %v", err, tt.wantErr)
544 }
545 })
546 }
547}
548
549func TestOpenOLT_Stop(t *testing.T) {
550 type args struct {
551 ctx context.Context
552 }
553 tests := []struct {
554 name string
555 fields *fields
556 args args
557 wantErr error
558 }{
khenaidooefff76e2021-12-15 16:51:30 -0500559 {"stop-1", &fields{exitChannel: make(chan struct{})}, args{}, errors.New("stop error")},
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)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400564 if err := oo.Start(tt.args.ctx); err != nil {
565 t.Error(err)
566 }
cbabua9e04cc2019-10-03 12:35:45 +0530567 if err := oo.Stop(tt.args.ctx); err != nil {
568 t.Errorf("Stop() error = %v, wantErr %v", err, tt.wantErr)
569 }
570 })
571 }
572}
573
khenaidoo106c61a2021-08-11 18:05:46 -0400574func TestOpenOLT_SuppressEvent(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530575 type args struct {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000576 filter *voltha.EventFilter
cbabua9e04cc2019-10-03 12:35:45 +0530577 }
578 tests := []struct {
579 name string
580 fields *fields
581 args args
582 wantErr error
583 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530584 {"suppress_event-1", &fields{}, args{}, olterrors.ErrNotImplemented},
585 {"suppress_event-2", &fields{}, args{}, olterrors.ErrNotImplemented},
586 {"suppress_event-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530587 }
588 for _, tt := range tests {
589 t.Run(tt.name, func(t *testing.T) {
590 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400591 if _, err := oo.SuppressEvent(context.Background(), tt.args.filter); err != tt.wantErr {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000592 t.Errorf("Suppress_event() error = %v, wantErr %v", err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530593 }
594 })
595 }
596}
597
khenaidoo106c61a2021-08-11 18:05:46 -0400598func TestOpenOLT_UnSuppressEvent(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530599 type args struct {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000600 filter *voltha.EventFilter
cbabua9e04cc2019-10-03 12:35:45 +0530601 }
602 tests := []struct {
603 name string
604 fields *fields
605 args args
606 wantErr error
607 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530608 {"unsupress_event-1", &fields{}, args{}, olterrors.ErrNotImplemented},
609 {"unsupress_event-2", &fields{}, args{}, olterrors.ErrNotImplemented},
610 {"unsupress_event-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530611 }
612 for _, tt := range tests {
613 t.Run(tt.name, func(t *testing.T) {
614 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400615 if _, err := oo.UnSuppressEvent(context.Background(), tt.args.filter); err != tt.wantErr {
Devmalya Pauldd23a992019-11-14 07:06:31 +0000616 t.Errorf("Unsuppress_event() error = %v, wantErr %v", err, tt.wantErr)
cbabua9e04cc2019-10-03 12:35:45 +0530617 }
618 })
619 }
620}
621
khenaidoo106c61a2021-08-11 18:05:46 -0400622func TestOpenOLT_UpdateFlowsBulk(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530623 type args struct {
624 device *voltha.Device
khenaidoodc2116e2021-10-19 17:33:19 -0400625 flows *ofp.Flows
626 groups *ofp.FlowGroups
627 flowMetadata *ofp.FlowMetadata
cbabua9e04cc2019-10-03 12:35:45 +0530628 }
629 tests := []struct {
630 name string
631 fields *fields
632 args args
633 wantErr error
634 }{
Thomas Lee S94109f12020-03-03 16:39:29 +0530635 {"update_flows_bulk-1", &fields{}, args{}, olterrors.ErrNotImplemented},
636 {"update_flows_bulk-2", &fields{}, args{}, olterrors.ErrNotImplemented},
637 {"update_flows_bulk-3", &fields{}, args{}, olterrors.ErrNotImplemented},
cbabua9e04cc2019-10-03 12:35:45 +0530638 }
639 for _, tt := range tests {
640 t.Run(tt.name, func(t *testing.T) {
641 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400642 if _, err := oo.UpdateFlowsBulk(context.Background(), &ca.BulkFlows{
khenaidoo106c61a2021-08-11 18:05:46 -0400643 Device: tt.args.device,
644 Flows: tt.args.flows,
645 Groups: tt.args.groups,
646 FlowMetadata: tt.args.flowMetadata,
647 }); err != tt.wantErr {
cbabua9e04cc2019-10-03 12:35:45 +0530648 t.Errorf("Update_flows_bulk() error = %v, wantErr %v", err, tt.wantErr)
649 }
650 })
651 }
652}
653
khenaidoo106c61a2021-08-11 18:05:46 -0400654func TestOpenOLT_UpdateFlowsIncrementally(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530655 type args struct {
656 device *voltha.Device
mgouda86543582025-10-29 20:58:16 +0530657 flows *ofp.FlowChanges
658 groups *ofp.FlowGroupChanges
khenaidoodc2116e2021-10-19 17:33:19 -0400659 flowMetadata *ofp.FlowMetadata
cbabua9e04cc2019-10-03 12:35:45 +0530660 }
661
662 tests := []struct {
663 name string
664 fields *fields
665 args args
666 wantErr error
667 }{
668 {"update_flows_incrementally-1", &fields{}, args{device: mockDevice()},
Thomas Lee S94109f12020-03-03 16:39:29 +0530669 olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": "olt"}, nil)},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800670 {"update_flows_incrementally-2", mockOlt(), args{device: mockDevice()}, nil},
cbabua9e04cc2019-10-03 12:35:45 +0530671 }
672 for _, tt := range tests {
673 t.Run(tt.name, func(t *testing.T) {
674 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400675 if _, err := oo.UpdateFlowsIncrementally(context.Background(), &ca.IncrementalFlows{
khenaidoo106c61a2021-08-11 18:05:46 -0400676 Device: tt.args.device,
677 Flows: tt.args.flows,
678 Groups: tt.args.groups,
679 FlowMetadata: tt.args.flowMetadata}); !reflect.DeepEqual(err, tt.wantErr) {
cbabua9e04cc2019-10-03 12:35:45 +0530680 t.Errorf("Update_flows_incrementally() error = %v, wantErr %v", err, tt.wantErr)
681 }
682 })
683 }
684}
685
khenaidoo106c61a2021-08-11 18:05:46 -0400686func TestOpenOLT_UpdatePmConfig(t *testing.T) {
cbabua9e04cc2019-10-03 12:35:45 +0530687 type args struct {
688 device *voltha.Device
689 pmConfigs *voltha.PmConfigs
690 }
691 tests := []struct {
692 name string
693 fields *fields
694 args args
695 wantErr error
696 }{
Rohan Agrawalda5e0b22020-05-20 11:10:26 +0000697 {"update_pm_config-1", mockOlt(), args{device: mockDevice(), pmConfigs: &voltha.PmConfigs{DefaultFreq: 150, Grouped: false, FreqOverride: false}}, nil},
698 {"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 +0530699 }
700 for _, tt := range tests {
701 t.Run(tt.name, func(t *testing.T) {
702 oo := testOltObject(tt.fields)
khenaidoodc2116e2021-10-19 17:33:19 -0400703 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 +0530704 t.Errorf("Update_pm_config() error = %v, wantErr %v", err, tt.wantErr)
705 }
cbabua9e04cc2019-10-03 12:35:45 +0530706 })
707 }
708}
709
710func TestOpenOLT_deleteDeviceHandlerToMap(t *testing.T) {
711 type args struct {
712 agent *DeviceHandler
713 }
714 tests := []struct {
715 name string
716 fields *fields
717 args args
718 }{
719 {"delete_device_handler_map-1", mockOlt(), args{newMockDeviceHandler()}},
720 }
721 for _, tt := range tests {
722 t.Run(tt.name, func(t *testing.T) {
723 oo := testOltObject(tt.fields)
724 oo.deleteDeviceHandlerToMap(tt.args.agent)
725 if len(oo.deviceHandlers) > 0 {
726 t.Errorf("delete device manager failed")
727 }
728 })
729 }
730}
kesavand39e0aa32020-01-28 20:58:50 -0500731
khenaidoo106c61a2021-08-11 18:05:46 -0400732func TestOpenOLT_EnablePort(t *testing.T) {
kesavand39e0aa32020-01-28 20:58:50 -0500733 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400734 port *voltha.Port
kesavand39e0aa32020-01-28 20:58:50 -0500735 }
736 tests := []struct {
737 name string
738 fields *fields
739 args args
740 wantErr bool
741 }{
742 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400743 {"Enable_port-1", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_PON_OLT, PortNo: 1, DeviceId: "olt"}}, false},
744 {"Enable_port-2", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_ETHERNET_NNI, PortNo: 1, DeviceId: "olt"}}, true},
kesavand39e0aa32020-01-28 20:58:50 -0500745 }
746 for _, tt := range tests {
747 t.Run(tt.name, func(t *testing.T) {
748 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400749 if _, err := oo.EnablePort(context.Background(), tt.args.port); (err != nil) != tt.wantErr {
kesavand39e0aa32020-01-28 20:58:50 -0500750 t.Errorf("OpenOLT.Enable_port() error = %v, wantErr %v", err, tt.wantErr)
751 }
752 })
753 }
754}
755
khenaidoo106c61a2021-08-11 18:05:46 -0400756func TestOpenOLT_DisablePort(t *testing.T) {
kesavand39e0aa32020-01-28 20:58:50 -0500757 type args struct {
khenaidoo106c61a2021-08-11 18:05:46 -0400758 port *voltha.Port
kesavand39e0aa32020-01-28 20:58:50 -0500759 }
760 tests := []struct {
761 name string
762 fields *fields
763 args args
764 wantErr bool
765 }{
766 // TODO: Add test cases.
khenaidoo106c61a2021-08-11 18:05:46 -0400767 {"Disable_port-1", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_PON_OLT, PortNo: 1, DeviceId: "olt"}}, false},
768 {"Disable_port-2", mockOlt(), args{port: &voltha.Port{Type: voltha.Port_ETHERNET_NNI, PortNo: 1, DeviceId: "olt"}}, true},
kesavand39e0aa32020-01-28 20:58:50 -0500769 }
770 for _, tt := range tests {
771 t.Run(tt.name, func(t *testing.T) {
772 oo := testOltObject(tt.fields)
khenaidoo106c61a2021-08-11 18:05:46 -0400773 if _, err := oo.DisablePort(context.Background(), tt.args.port); (err != nil) != tt.wantErr {
kesavand39e0aa32020-01-28 20:58:50 -0500774 t.Errorf("OpenOLT.Disable_port() error = %v, wantErr %v", err, tt.wantErr)
775 }
776 })
777 }
778}