blob: 1ecb04f7bb7ddea4fb4271e89ca2dbf7ac3bca62 [file] [log] [blame]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -08001# 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 -080015from requests import ConnectionError
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080016import unittest
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070017import functools
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080018from mock import patch, call, Mock, PropertyMock
19import requests_mock
20
21import os, sys
22
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080023test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080024
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070025def match_json(desired, req):
26 if desired!=req.json():
27 raise Exception("Got request %s, but body is not matching" % req.url)
28 return False
29 return True
30
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080031class TestSyncOLTDevice(unittest.TestCase):
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080032 def setUp(self):
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070033 global DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070034 self.sys_path_save = sys.path
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070035
36 # Setting up the config module
37 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070038 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070039 Config.clear()
40 Config.init(config, "synchronizer-config-schema.yaml")
Luca Preteca974c82018-05-01 18:06:16 -070041 # END setting up the config module
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070042
Scott Baker47b47302019-01-30 16:55:07 -080043 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
44 mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
Matteo Scandolo35207b72019-05-10 08:46:48 -070045 ("rcord", "rcord.xproto")])
Matteo Scandolo19466a02018-05-16 17:43:39 -070046
Scott Baker47b47302019-01-30 16:55:07 -080047 import xossynchronizer.modelaccessor
48 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
Matteo Scandolo19466a02018-05-16 17:43:39 -070049
Scott Baker47b47302019-01-30 16:55:07 -080050 from xossynchronizer.modelaccessor import model_accessor
51 self.model_accessor = model_accessor
52
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070053 from sync_olt_device import SyncOLTDevice, DeferredException
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070054 self.sync_step = SyncOLTDevice
55
Matteo Scandolob8621cd2018-04-04 17:12:37 -070056 pon_port = Mock()
57 pon_port.port_id = "00ff00"
Matteo Scandolob8621cd2018-04-04 17:12:37 -070058
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070059 # Create a mock OLTDevice
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080060 o = Mock()
61 o.volt_service.voltha_url = "voltha_url"
Luca Preteca974c82018-05-01 18:06:16 -070062 o.volt_service.voltha_port = 1234
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080063 o.volt_service.voltha_user = "voltha_user"
64 o.volt_service.voltha_pass = "voltha_pass"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080065
Matteo Scandoloa79395f2018-10-08 13:34:49 -070066 o.volt_service.onos_voltha_port = 4321
67 o.volt_service.onos_voltha_url = "onos"
68 o.volt_service.onos_voltha_user = "karaf"
69 o.volt_service.onos_voltha_pass = "karaf"
70
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080071 o.device_type = "ponsim_olt"
72 o.host = "172.17.0.1"
73 o.port = "50060"
Scott Baker0bbbfd12018-12-11 07:01:06 +000074 o.uplink = "129"
Luca Prete244e6ec2018-07-02 14:30:24 +020075 o.driver = "voltha"
Matteo Scandoloa79395f2018-10-08 13:34:49 -070076 o.name = "Test Device"
Scott Baker09798d82019-01-17 08:34:59 -080077 o.admin_state = "ENABLED"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080078
Matteo Scandolob8621cd2018-04-04 17:12:37 -070079 # feedback state
80 o.device_id = None
Matteo Scandolob8621cd2018-04-04 17:12:37 -070081 o.oper_status = None
82 o.of_id = None
Matteo Scandolo096a3cf2018-06-20 13:56:13 -070083 o.id = 1
Matteo Scandolob8621cd2018-04-04 17:12:37 -070084
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080085 o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"}
86
Andy Bavier00c573c2019-02-08 16:19:11 -070087 o.save_changed_fields.return_value = "Saved"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080088
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070089 o.pon_ports.all.return_value = [pon_port]
Matteo Scandolob8621cd2018-04-04 17:12:37 -070090
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080091 self.o = o
92
Matteo Scandolod6fce512018-10-16 10:35:29 -070093 self.voltha_devices_response = {"id": "123", "serial_number": "foobar"}
94
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080095 def tearDown(self):
96 self.o = None
Matteo Scandoloce27e9c2018-04-06 10:06:53 -070097 sys.path = self.sys_path_save
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080098
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080099 @requests_mock.Mocker()
100 def test_get_of_id_from_device(self, m):
101 logical_devices = {
102 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700103 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800104 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
105 ]
106 }
Luca Preteca974c82018-05-01 18:06:16 -0700107 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800108 self.o.device_id = "123"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700109 self.o = self.sync_step.get_ids_from_logical_device(self.o)
110 self.assertEqual(self.o.of_id, "0001000ce2314000")
111 self.assertEqual(self.o.dp_id, "of:0000000ce2314000")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800112
113 with self.assertRaises(Exception) as e:
114 self.o.device_id = "idonotexist"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700115 self.sync_step.get_ids_from_logical_device(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700116 self.assertEqual(e.exception.message, "Can't find a logical_device for OLT device id: idonotexist")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800117
118 @requests_mock.Mocker()
119 def test_sync_record_fail_add(self, m):
120 """
121 Should print an error if we can't add the device in VOLTHA
122 """
Luca Preteca974c82018-05-01 18:06:16 -0700123 m.post("http://voltha_url:1234/api/v1/devices", status_code=500, text="MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800124
125 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800126 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700127 self.assertEqual(e.exception.message, "Failed to add OLT device: MockError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800128
129 @requests_mock.Mocker()
130 def test_sync_record_fail_no_id(self, m):
131 """
132 Should print an error if VOLTHA does not return the device id
133 """
Luca Preteca974c82018-05-01 18:06:16 -0700134 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": ""})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800135
136 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800137 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700138 self.assertEqual(e.exception.message, "VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800139
140 @requests_mock.Mocker()
141 def test_sync_record_fail_enable(self, m):
142 """
143 Should print an error if device.enable fails
144 """
Matteo Scandolod6fce512018-10-16 10:35:29 -0700145 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response)
Luca Preteca974c82018-05-01 18:06:16 -0700146 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=500, text="EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800147
148 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800149 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo2c144932018-05-04 14:06:24 -0700150
151 self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800152
153 @requests_mock.Mocker()
154 def test_sync_record_success(self, m):
155 """
156 If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS
157 """
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700158
159 expected_conf = {
160 "type": self.o.device_type,
161 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
162 }
163
Matteo Scandolod6fce512018-10-16 10:35:29 -0700164 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, additional_matcher=functools.partial(match_json, expected_conf))
Luca Preteca974c82018-05-01 18:06:16 -0700165 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
Matteo Scandolo45876652018-10-16 16:03:17 -0700166 m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"})
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800167 logical_devices = {
168 "items": [
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700169 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800170 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
171 ]
172 }
Luca Preteca974c82018-05-01 18:06:16 -0700173 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800174
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700175 onos_expected_conf = {
176 "devices": {
177 "of:0000000ce2314000": {
178 "basic": {
179 "name": self.o.name
180 }
181 }
182 }
183 }
184 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf,
185 additional_matcher=functools.partial(match_json, onos_expected_conf))
186
Scott Baker47b47302019-01-30 16:55:07 -0800187 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700188 self.assertEqual(self.o.admin_state, "ENABLED")
189 self.assertEqual(self.o.oper_status, "ACTIVE")
Matteo Scandolo45876652018-10-16 16:03:17 -0700190 self.assertEqual(self.o.serial_number, "foobar")
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700191 self.assertEqual(self.o.of_id, "0001000ce2314000")
Scott Baker09798d82019-01-17 08:34:59 -0800192
193 # One save during preprovision
194 # One save during activation to set backend_status to "Waiting for device to activate"
195 # One save after activation has succeeded
Andy Bavier00c573c2019-02-08 16:19:11 -0700196 self.assertEqual(self.o.save_changed_fields.call_count, 3)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800197
198 @requests_mock.Mocker()
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700199 def test_sync_record_success_mac_address(self, m):
200 """
201 A device should be pre-provisioned via mac_address, the the process is the same
202 """
203
204 del self.o.host
205 del self.o.port
206 self.o.mac_address = "00:0c:e2:31:40:00"
207
208 expected_conf = {
209 "type": self.o.device_type,
210 "mac_address": self.o.mac_address
211 }
212
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700213 onos_expected_conf = {
214 "devices": {
215 "of:0000000ce2314000": {
216 "basic": {
217 "name": self.o.name
218 }
219 }
220 }
221 }
222 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf,
223 additional_matcher=functools.partial(match_json, onos_expected_conf))
224
Matteo Scandolod6fce512018-10-16 10:35:29 -0700225 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response,
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700226 additional_matcher=functools.partial(match_json, expected_conf))
227 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
Matteo Scandolo45876652018-10-16 16:03:17 -0700228 m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"})
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700229 logical_devices = {
230 "items": [
231 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
232 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
233 ]
234 }
235 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
236
Scott Baker47b47302019-01-30 16:55:07 -0800237 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700238 self.assertEqual(self.o.admin_state, "ENABLED")
239 self.assertEqual(self.o.oper_status, "ACTIVE")
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700240 self.assertEqual(self.o.of_id, "0001000ce2314000")
Scott Baker09798d82019-01-17 08:34:59 -0800241
242 # One save during preprovision
243 # One save during activation to set backend_status to "Waiting for device to activate"
244 # One save after activation has succeeded
Andy Bavier00c573c2019-02-08 16:19:11 -0700245 self.assertEqual(self.o.save_changed_fields.call_count, 3)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700246
247 @requests_mock.Mocker()
248 def test_sync_record_enable_timeout(self, m):
249 """
Scott Baker09798d82019-01-17 08:34:59 -0800250 If device activation fails we need to tell the user.
251
252 OLT will be preprovisioned.
253 OLT will return "ERROR" for oper_status during activate and will eventually exceed retries.s
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700254 """
255
256 expected_conf = {
257 "type": self.o.device_type,
258 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
259 }
260
Matteo Scandolod6fce512018-10-16 10:35:29 -0700261 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response,
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700262 additional_matcher=functools.partial(match_json, expected_conf))
263 m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200)
264 m.get("http://voltha_url:1234/api/v1/devices/123", [
Matteo Scandolo45876652018-10-16 16:03:17 -0700265 {"json": {"oper_status": "ACTIVATING", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200},
Scott Baker09798d82019-01-17 08:34:59 -0800266 {"json": {"oper_status": "ERROR", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200}
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700267 ])
268
269 logical_devices = {
270 "items": [
271 {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"},
272 {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"}
273 ]
274 }
275 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices)
276
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700277 with self.assertRaises(Exception) as e:
Scott Baker47b47302019-01-30 16:55:07 -0800278 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700279
280 self.assertEqual(e.exception.message, "It was not possible to activate OLTDevice with id 1")
281 self.assertEqual(self.o.oper_status, "ERROR")
Scott Baker09798d82019-01-17 08:34:59 -0800282 self.assertEqual(self.o.admin_state, "ENABLED")
283 self.assertEqual(self.o.device_id, "123")
284 self.assertEqual(self.o.serial_number, "foobar")
285
286 # One save from preprovision to set device_id, serial_number
287 # One save from activate to set backend_status to "Waiting for device to be activated"
Andy Bavier00c573c2019-02-08 16:19:11 -0700288 self.assertEqual(self.o.save_changed_fields.call_count, 2)
Matteo Scandolo2ed64b92018-06-18 10:32:56 -0700289
290 @requests_mock.Mocker()
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700291 def test_sync_record_already_existing_in_voltha(self, m):
Scott Baker09798d82019-01-17 08:34:59 -0800292 """
293 If device.admin_state == "ENABLED" and oper_status == "ACTIVE", then the OLT should not be reactivated.
294 """
295
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700296 # mock device feedback state
297 self.o.device_id = "123"
Matteo Scandolo096a3cf2018-06-20 13:56:13 -0700298 self.o.admin_state = "ENABLED"
299 self.o.oper_status = "ACTIVE"
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700300 self.o.dp_id = "of:0000000ce2314000"
Matteo Scandolo2c144932018-05-04 14:06:24 -0700301 self.o.of_id = "0001000ce2314000"
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700302
Matteo Scandoloa79395f2018-10-08 13:34:49 -0700303 expected_conf = {
304 "devices": {
305 self.o.dp_id: {
306 "basic": {
307 "name": self.o.name
308 }
309 }
310 }
311 }
312 m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=expected_conf,
313 additional_matcher=functools.partial(match_json, expected_conf))
314
Scott Baker47b47302019-01-30 16:55:07 -0800315 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700316 self.o.save.assert_not_called()
Andy Bavier00c573c2019-02-08 16:19:11 -0700317 self.o.save_changed_fields.assert_not_called()
318
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700319
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700320 @requests_mock.Mocker()
Scott Baker09798d82019-01-17 08:34:59 -0800321 def test_sync_record_deactivate(self, m):
322 """
323 If device.admin_state == "DISABLED" and oper_status == "ACTIVE", then OLT should be deactivated.
324 """
325
326 expected_conf = {
327 "type": self.o.device_type,
328 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
329 }
330
331 # Make it look like we have an active OLT that we are deactivating.
332 self.o.admin_state = "DISABLED"
333 self.o.oper_status = "ACTIVE"
334 self.o.serial_number = "foobar"
335 self.o.device_id = "123"
336 self.o.of_id = "0001000ce2314000"
337
338 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, additional_matcher=functools.partial(match_json, expected_conf))
339 m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200)
340
Scott Baker47b47302019-01-30 16:55:07 -0800341 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Scott Baker09798d82019-01-17 08:34:59 -0800342
343 # No saves as state has not changed (will eventually be saved by synchronizer framework to update backend_status)
344 self.assertEqual(self.o.save.call_count, 0)
Andy Bavier00c573c2019-02-08 16:19:11 -0700345 self.assertEqual(self.o.save_changed_fields.call_count, 0)
346
Scott Baker09798d82019-01-17 08:34:59 -0800347
348 # Make sure disable was called
349 urls = [x.url for x in m.request_history]
350 self.assertIn("http://voltha_url:1234/api/v1/devices/123/disable", urls)
351
352 @requests_mock.Mocker()
353 def test_sync_record_deactivate_already_inactive(self, m):
354 """
355 If device.admin_state == "DISABLED" and device.oper_status == "UNKNOWN", then the device is already deactivated
356 and VOLTHA should not be called.
357 """
358
359 expected_conf = {
360 "type": self.o.device_type,
361 "host_and_port": "%s:%s" % (self.o.host, self.o.port)
362 }
363
364 # Make it look like we have an active OLT that we are deactivating.
365 self.o.admin_state = "DISABLED"
366 self.o.oper_status = "UNKNOWN"
367 self.o.serial_number = "foobar"
368 self.o.device_id = "123"
369 self.o.of_id = "0001000ce2314000"
370
371 m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, additional_matcher=functools.partial(match_json, expected_conf))
372
Scott Baker47b47302019-01-30 16:55:07 -0800373 self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
Scott Baker09798d82019-01-17 08:34:59 -0800374
375 # No saves as state has not changed (will eventually be saved by synchronizer framework to update backend_status)
376 self.assertEqual(self.o.save.call_count, 0)
Andy Bavier00c573c2019-02-08 16:19:11 -0700377 self.assertEqual(self.o.save_changed_fields.call_count, 0)
Scott Baker09798d82019-01-17 08:34:59 -0800378
379 @requests_mock.Mocker()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800380 def test_delete_record(self, m):
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700381 self.o.of_id = "0001000ce2314000"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800382 self.o.device_id = "123"
383
Luca Preteca974c82018-05-01 18:06:16 -0700384 m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200)
385 m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800386
Scott Baker47b47302019-01-30 16:55:07 -0800387 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800388
Matteo Scandolo563891c2018-08-21 11:56:32 -0700389 self.assertEqual(m.call_count, 2)
390
Scott Baker22b46c52018-11-15 15:15:29 -0800391 @patch('requests.post')
392 def test_delete_record_connectionerror(self, m):
393 self.o.of_id = "0001000ce2314000"
394 self.o.device_id = "123"
395
396 m.side_effect = ConnectionError()
397
Scott Baker47b47302019-01-30 16:55:07 -0800398 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Scott Baker22b46c52018-11-15 15:15:29 -0800399
400 # No exception thrown, as ConnectionError will be caught
401
402
Matteo Scandolo563891c2018-08-21 11:56:32 -0700403 @requests_mock.Mocker()
404 def test_delete_unsynced_record(self, m):
405
Scott Baker47b47302019-01-30 16:55:07 -0800406 self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
Matteo Scandolo563891c2018-08-21 11:56:32 -0700407
408 self.assertEqual(m.call_count, 0)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800409
410if __name__ == "__main__":
Luca Preteca974c82018-05-01 18:06:16 -0700411 unittest.main()