blob: 6fc58167f7138162d41bee4b6cd55c30510ae1cb [file] [log] [blame]
Matteo Scandolod44ca992018-05-17 15:02:10 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Baker22b46c52018-11-15 15:15:29 -080015import functools
Matteo Scandolod44ca992018-05-17 15:02:10 -070016import unittest
17from mock import patch, call, Mock, PropertyMock
18import requests_mock
19
20import os, sys
21
22# Hack to load synchronizer framework
23test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
24xos_dir=os.path.join(test_path, "../../..")
25if not os.path.exists(os.path.join(test_path, "new_base")):
26 xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos")
27 services_dir = os.path.join(xos_dir, "../../xos_services")
28sys.path.append(xos_dir)
29sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
30# END Hack to load synchronizer framework
31
32# generate model from xproto
33def get_models_fn(service_name, xproto_name):
34 name = os.path.join(service_name, "xos", xproto_name)
35 if os.path.exists(os.path.join(services_dir, name)):
36 return name
37 else:
38 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
39 if os.path.exists(os.path.join(services_dir, name)):
40 return name
41 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
42# END generate model from xproto
43
44class TestPullONUDevice(unittest.TestCase):
45
46 def setUp(self):
47 global DeferredException
48
49 self.sys_path_save = sys.path
50 sys.path.append(xos_dir)
51 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
52
53 # Setting up the config module
54 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070055 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolod44ca992018-05-17 15:02:10 -070056 Config.clear()
57 Config.init(config, "synchronizer-config-schema.yaml")
58 # END Setting up the config module
59
60 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
61 # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")])
62
63 # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order
64 # and apparently it is not overriding the generated model accessor
65 build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"),
66 get_models_fn("vsg", "vsg.xproto"),
67 get_models_fn("../profiles/rcord", "rcord.xproto")])
68 import synchronizers.new_base.modelaccessor
69 from pull_onus import ONUDevicePullStep, model_accessor
70
71 # import all class names to globals
72 for (k, v) in model_accessor.all_model_classes.items():
73 globals()[k] = v
74
75 self.sync_step = ONUDevicePullStep
76
77 # mock volt service
78 self.volt_service = Mock()
79 self.volt_service.id = "volt_service_id"
80 self.volt_service.voltha_url = "voltha_url"
81 self.volt_service.voltha_user = "voltha_user"
82 self.volt_service.voltha_pass = "voltha_pass"
83 self.volt_service.voltha_port = 1234
84
85 # mock OLTDevice
86 self.olt = Mock()
87 self.olt.id = 1
88
Scott Baker22b46c52018-11-15 15:15:29 -080089 # second mock OLTDevice
90 self.olt2 = Mock()
91 self.olt2.id = 2
92
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070093 # mock pon port
94 self.pon_port = Mock()
95 self.pon_port.id = 1
96
Scott Baker22b46c52018-11-15 15:15:29 -080097 # mock pon port
98 self.pon_port2 = Mock()
99 self.pon_port2.id = 2
100
Matteo Scandolod44ca992018-05-17 15:02:10 -0700101 # mock voltha responses
102 self.devices = {
103 "items": [
104 {
105 "id": "0001130158f01b2d",
106 "type": "broadcom_onu",
107 "vendor": "Broadcom",
108 "serial_number": "BRCM22222222",
109 "vendor_id": "BRCM",
110 "adapter": "broadcom_onu",
111 "vlan": 0,
112 "admin_state": "ENABLED",
113 "oper_status": "ACTIVE",
114 "connect_status": "REACHABLE",
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700115 "parent_id": "00010fc93996afea",
116 "parent_port_no": 1
Matteo Scandolod44ca992018-05-17 15:02:10 -0700117 }
118 ]
119 }
120
Scott Baker22b46c52018-11-15 15:15:29 -0800121 self.two_devices = {
122 "items": [
123 {
124 "id": "0001130158f01b2d",
125 "type": "broadcom_onu",
126 "vendor": "Broadcom",
127 "serial_number": "BRCM22222222",
128 "vendor_id": "BRCM",
129 "adapter": "broadcom_onu",
130 "vlan": 0,
131 "admin_state": "ENABLED",
132 "oper_status": "ACTIVE",
133 "connect_status": "REACHABLE",
134 "parent_id": "00010fc93996afea",
135 "parent_port_no": 1
136 },
137 {
138 "id": "0001130158f01b2e",
139 "type": "broadcom_onu",
140 "vendor": "Broadcom",
141 "serial_number": "BRCM22222223",
142 "vendor_id": "BRCM",
143 "adapter": "broadcom_onu",
144 "vlan": 0,
145 "admin_state": "ENABLED",
146 "oper_status": "ACTIVE",
147 "connect_status": "REACHABLE",
148 "parent_id": "00010fc93996afeb",
149 "parent_port_no": 1
150 }
151 ],
152 }
153
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700154 # TODO add ports
155 self.ports = {
156 "items": []
157 }
158
Matteo Scandolod44ca992018-05-17 15:02:10 -0700159 def tearDown(self):
160 sys.path = self.sys_path_save
161
162 @requests_mock.Mocker()
163 def test_missing_volt_service(self, m):
164 self.assertFalse(m.called)
165
166 @requests_mock.Mocker()
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700167 def test_pull(self, m):
Matteo Scandolod44ca992018-05-17 15:02:10 -0700168
169 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
170 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700171 patch.object(PONPort.objects, "get") as mock_pon_port, \
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700172 patch.object(ONUDevice, "save", autospec=True) as mock_save:
Matteo Scandolod44ca992018-05-17 15:02:10 -0700173 olt_service_mock.return_value = [self.volt_service]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700174 mock_pon_port.return_value = self.pon_port
Matteo Scandolod44ca992018-05-17 15:02:10 -0700175 mock_olt_device.return_value = self.olt
176
177 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700178 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700179
180 self.sync_step().pull_records()
181
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700182 saved_onu = mock_save.call_args[0][0]
Matteo Scandolod44ca992018-05-17 15:02:10 -0700183
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700184 self.assertEqual(saved_onu.admin_state, "ENABLED")
185 self.assertEqual(saved_onu.oper_status, "ACTIVE")
186 self.assertEqual(saved_onu.connect_status, "REACHABLE")
187 self.assertEqual(saved_onu.device_type, "broadcom_onu")
188 self.assertEqual(saved_onu.vendor, "Broadcom")
189 self.assertEqual(saved_onu.device_id, "0001130158f01b2d")
Matteo Scandolod44ca992018-05-17 15:02:10 -0700190
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700191 self.assertEqual(mock_save.call_count, 1)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700192
Scott Baker22b46c52018-11-15 15:15:29 -0800193 @requests_mock.Mocker()
194 def test_pull_bad_pon(self, m):
195
196 def olt_side_effect(device_id):
197 # fail the first onu device
198 if device_id=="00010fc93996afea":
199 return self.olt
200 else:
201 return self.olt2
202
203 def pon_port_side_effect(mock_pon_port, port_no, olt_device_id):
204 # fail the first onu device
205 if olt_device_id==1:
206 raise IndexError()
207 return self.pon_port2
208
209 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
210 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
211 patch.object(PONPort.objects, "get") as mock_pon_port, \
212 patch.object(ONUDevice, "save", autospec=True) as mock_save:
213 olt_service_mock.return_value = [self.volt_service]
214 mock_pon_port.side_effect = functools.partial(pon_port_side_effect, self.pon_port)
215 mock_olt_device.side_effect = olt_side_effect
216
217 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.two_devices)
218 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
219 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2e/ports", status_code=200, json=self.ports)
220
221 self.sync_step().pull_records()
222
223 self.assertEqual(mock_save.call_count, 1)
224 saved_onu = mock_save.call_args[0][0]
225
226 # we should get the second onu in self.two_onus
227
228 self.assertEqual(saved_onu.admin_state, "ENABLED")
229 self.assertEqual(saved_onu.oper_status, "ACTIVE")
230 self.assertEqual(saved_onu.connect_status, "REACHABLE")
231 self.assertEqual(saved_onu.device_type, "broadcom_onu")
232 self.assertEqual(saved_onu.vendor, "Broadcom")
233 self.assertEqual(saved_onu.device_id, "0001130158f01b2e")
234
235 self.assertEqual(mock_save.call_count, 1)
236
237 @requests_mock.Mocker()
238 def test_pull_bad_olt(self, m):
239
240 def olt_side_effect(device_id):
241 # fail the first onu device
242 if device_id=="00010fc93996afea":
243 raise IndexError()
244 else:
245 return self.olt2
246
247 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
248 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
249 patch.object(PONPort.objects, "get") as mock_pon_port, \
250 patch.object(ONUDevice, "save", autospec=True) as mock_save:
251 olt_service_mock.return_value = [self.volt_service]
252 mock_pon_port.return_value = self.pon_port2
253 mock_olt_device.side_effect = olt_side_effect
254
255 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.two_devices)
256 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
257 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2e/ports", status_code=200, json=self.ports)
258
259 self.sync_step().pull_records()
260
261 self.assertEqual(mock_save.call_count, 1)
262 saved_onu = mock_save.call_args[0][0]
263
264 # we should get the second onu in self.two_onus
265
266 self.assertEqual(saved_onu.admin_state, "ENABLED")
267 self.assertEqual(saved_onu.oper_status, "ACTIVE")
268 self.assertEqual(saved_onu.connect_status, "REACHABLE")
269 self.assertEqual(saved_onu.device_type, "broadcom_onu")
270 self.assertEqual(saved_onu.vendor, "Broadcom")
271 self.assertEqual(saved_onu.device_id, "0001130158f01b2e")
272
273 self.assertEqual(mock_save.call_count, 1)
274
Matteo Scandolod44ca992018-05-17 15:02:10 -0700275
276if __name__ == "__main__":
277 unittest.main()