Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 1 | # 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 Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 15 | from requests import ConnectionError |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 16 | import unittest |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 17 | import functools |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 18 | from mock import patch, call, Mock, PropertyMock |
| 19 | import requests_mock |
| 20 | |
| 21 | import os, sys |
| 22 | |
| 23 | # Hack to load synchronizer framework |
| 24 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 25 | xos_dir=os.path.join(test_path, "../../..") |
| 26 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 27 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 28 | services_dir = os.path.join(xos_dir, "../../xos_services") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 29 | sys.path.append(xos_dir) |
| 30 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 31 | # END of hack to load synchronizer framework |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 32 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 33 | # Generate model from xproto |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 34 | def get_models_fn(service_name, xproto_name): |
| 35 | name = os.path.join(service_name, "xos", xproto_name) |
| 36 | if os.path.exists(os.path.join(services_dir, name)): |
| 37 | return name |
| 38 | else: |
| 39 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 40 | if os.path.exists(os.path.join(services_dir, name)): |
| 41 | return name |
| 42 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 43 | # END generate model from xproto |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 44 | |
Scott Baker | 0bbbfd1 | 2018-12-11 07:01:06 +0000 | [diff] [blame] | 45 | def match_onos_req(req): |
| 46 | request = req.json()['devices'] |
| 47 | if not request['of:0000000ce2314000']: |
| 48 | return False |
| 49 | else: |
| 50 | if not request['of:0000000ce2314000']['basic']['driver'] == 'voltha': |
| 51 | return False |
| 52 | if not request['of:0000000ce2314000']['accessDevice']['vlan'] == 1 or not request['of:0000000ce2314000']['accessDevice']['uplink'] == "129": |
| 53 | return False |
| 54 | return True |
| 55 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 56 | def match_json(desired, req): |
| 57 | if desired!=req.json(): |
| 58 | raise Exception("Got request %s, but body is not matching" % req.url) |
| 59 | return False |
| 60 | return True |
| 61 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 62 | class TestSyncOLTDevice(unittest.TestCase): |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 63 | def setUp(self): |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 64 | global DeferredException |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 65 | self.sys_path_save = sys.path |
| 66 | sys.path.append(xos_dir) |
| 67 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 68 | |
| 69 | # Setting up the config module |
| 70 | from xosconfig import Config |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 71 | config = os.path.join(test_path, "../test_config.yaml") |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 72 | Config.clear() |
| 73 | Config.init(config, "synchronizer-config-schema.yaml") |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 74 | # END setting up the config module |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 75 | |
| 76 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 77 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 78 | |
| 79 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 80 | # and apparently it is not overriding the generated model accessor |
| 81 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 82 | get_models_fn("vsg", "vsg.xproto"), |
| 83 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
| 84 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 85 | import synchronizers.new_base.modelaccessor |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 86 | from sync_olt_device import SyncOLTDevice, DeferredException |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 87 | self.sync_step = SyncOLTDevice |
| 88 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 89 | pon_port = Mock() |
| 90 | pon_port.port_id = "00ff00" |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 91 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 92 | # Create a mock OLTDevice |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 93 | o = Mock() |
| 94 | o.volt_service.voltha_url = "voltha_url" |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 95 | o.volt_service.voltha_port = 1234 |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 96 | o.volt_service.voltha_user = "voltha_user" |
| 97 | o.volt_service.voltha_pass = "voltha_pass" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 98 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 99 | o.volt_service.onos_voltha_port = 4321 |
| 100 | o.volt_service.onos_voltha_url = "onos" |
| 101 | o.volt_service.onos_voltha_user = "karaf" |
| 102 | o.volt_service.onos_voltha_pass = "karaf" |
| 103 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 104 | o.device_type = "ponsim_olt" |
| 105 | o.host = "172.17.0.1" |
| 106 | o.port = "50060" |
Scott Baker | 0bbbfd1 | 2018-12-11 07:01:06 +0000 | [diff] [blame] | 107 | o.uplink = "129" |
Luca Prete | 244e6ec | 2018-07-02 14:30:24 +0200 | [diff] [blame] | 108 | o.driver = "voltha" |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 109 | o.name = "Test Device" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 110 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 111 | # feedback state |
| 112 | o.device_id = None |
| 113 | o.admin_state = None |
| 114 | o.oper_status = None |
| 115 | o.of_id = None |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 116 | o.id = 1 |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 117 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 118 | o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"} |
| 119 | |
| 120 | o.save.return_value = "Saved" |
| 121 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 122 | o.pon_ports.all.return_value = [pon_port] |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 123 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 124 | self.o = o |
| 125 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 126 | self.voltha_devices_response = {"id": "123", "serial_number": "foobar"} |
| 127 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 128 | def tearDown(self): |
| 129 | self.o = None |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 130 | sys.path = self.sys_path_save |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 131 | |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 132 | @requests_mock.Mocker() |
| 133 | def test_get_of_id_from_device(self, m): |
| 134 | logical_devices = { |
| 135 | "items": [ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 136 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 137 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 138 | ] |
| 139 | } |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 140 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 141 | self.o.device_id = "123" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 142 | self.o = self.sync_step.get_ids_from_logical_device(self.o) |
| 143 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
| 144 | self.assertEqual(self.o.dp_id, "of:0000000ce2314000") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 145 | |
| 146 | with self.assertRaises(Exception) as e: |
| 147 | self.o.device_id = "idonotexist" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 148 | self.sync_step.get_ids_from_logical_device(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 149 | self.assertEqual(e.exception.message, "Can't find a logical_device for OLT device id: idonotexist") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 150 | |
| 151 | @requests_mock.Mocker() |
| 152 | def test_sync_record_fail_add(self, m): |
| 153 | """ |
| 154 | Should print an error if we can't add the device in VOLTHA |
| 155 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 156 | m.post("http://voltha_url:1234/api/v1/devices", status_code=500, text="MockError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 157 | |
| 158 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 159 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 160 | self.assertEqual(e.exception.message, "Failed to add OLT device: MockError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 161 | |
| 162 | @requests_mock.Mocker() |
| 163 | def test_sync_record_fail_no_id(self, m): |
| 164 | """ |
| 165 | Should print an error if VOLTHA does not return the device id |
| 166 | """ |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 167 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json={"id": ""}) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 168 | |
| 169 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 170 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 171 | self.assertEqual(e.exception.message, "VOLTHA Device Id is empty. This probably means that the OLT device is already provisioned in VOLTHA") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 172 | |
| 173 | @requests_mock.Mocker() |
| 174 | def test_sync_record_fail_enable(self, m): |
| 175 | """ |
| 176 | Should print an error if device.enable fails |
| 177 | """ |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 178 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 179 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=500, text="EnableError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 180 | |
| 181 | with self.assertRaises(Exception) as e: |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 182 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 183 | |
| 184 | self.assertEqual(e.exception.message, "Failed to enable OLT device: EnableError") |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 185 | |
| 186 | @requests_mock.Mocker() |
| 187 | def test_sync_record_success(self, m): |
| 188 | """ |
| 189 | If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS |
| 190 | """ |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 191 | |
| 192 | expected_conf = { |
| 193 | "type": self.o.device_type, |
| 194 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 195 | } |
| 196 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 197 | 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 Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 198 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 199 | m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"}) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 200 | logical_devices = { |
| 201 | "items": [ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 202 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 203 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 204 | ] |
| 205 | } |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 206 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 207 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 208 | onos_expected_conf = { |
| 209 | "devices": { |
| 210 | "of:0000000ce2314000": { |
| 211 | "basic": { |
| 212 | "name": self.o.name |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf, |
| 218 | additional_matcher=functools.partial(match_json, onos_expected_conf)) |
| 219 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 220 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 221 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 222 | self.assertEqual(self.o.oper_status, "ACTIVE") |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 223 | self.assertEqual(self.o.serial_number, "foobar") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 224 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 225 | self.assertEqual(self.o.save.call_count, 2) # we're updating the backend_status when activating and then adding logical device ids |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 226 | |
| 227 | @requests_mock.Mocker() |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 228 | def test_sync_record_success_mac_address(self, m): |
| 229 | """ |
| 230 | A device should be pre-provisioned via mac_address, the the process is the same |
| 231 | """ |
| 232 | |
| 233 | del self.o.host |
| 234 | del self.o.port |
| 235 | self.o.mac_address = "00:0c:e2:31:40:00" |
| 236 | |
| 237 | expected_conf = { |
| 238 | "type": self.o.device_type, |
| 239 | "mac_address": self.o.mac_address |
| 240 | } |
| 241 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 242 | onos_expected_conf = { |
| 243 | "devices": { |
| 244 | "of:0000000ce2314000": { |
| 245 | "basic": { |
| 246 | "name": self.o.name |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=onos_expected_conf, |
| 252 | additional_matcher=functools.partial(match_json, onos_expected_conf)) |
| 253 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 254 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 255 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 256 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 257 | m.get("http://voltha_url:1234/api/v1/devices/123", json={"oper_status": "ACTIVE", "admin_state": "ENABLED", "serial_number": "foobar"}) |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 258 | logical_devices = { |
| 259 | "items": [ |
| 260 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
| 261 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 262 | ] |
| 263 | } |
| 264 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 265 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 266 | self.sync_step().sync_record(self.o) |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 267 | self.assertEqual(self.o.admin_state, "ENABLED") |
| 268 | self.assertEqual(self.o.oper_status, "ACTIVE") |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 269 | self.assertEqual(self.o.of_id, "0001000ce2314000") |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 270 | self.assertEqual(self.o.save.call_count, 2) |
| 271 | |
| 272 | @requests_mock.Mocker() |
| 273 | def test_sync_record_enable_timeout(self, m): |
| 274 | """ |
| 275 | If device.enable fails we need to tell the suer |
| 276 | """ |
| 277 | |
| 278 | expected_conf = { |
| 279 | "type": self.o.device_type, |
| 280 | "host_and_port": "%s:%s" % (self.o.host, self.o.port) |
| 281 | } |
| 282 | |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 283 | m.post("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.voltha_devices_response, |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 284 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 285 | m.post("http://voltha_url:1234/api/v1/devices/123/enable", status_code=200) |
| 286 | m.get("http://voltha_url:1234/api/v1/devices/123", [ |
Matteo Scandolo | 4587665 | 2018-10-16 16:03:17 -0700 | [diff] [blame] | 287 | {"json": {"oper_status": "ACTIVATING", "admin_state": "ENABLED", "serial_number": "foobar"}, "status_code": 200}, |
| 288 | {"json": {"oper_status": "ERROR", "admin_state": "FAILED", "serial_number": "foobar"}, "status_code": 200} |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 289 | ]) |
| 290 | |
| 291 | logical_devices = { |
| 292 | "items": [ |
| 293 | {"root_device_id": "123", "id": "0001000ce2314000", "datapath_id": "55334486016"}, |
| 294 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 295 | ] |
| 296 | } |
| 297 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 298 | |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 299 | with self.assertRaises(Exception) as e: |
| 300 | self.sync_step().sync_record(self.o) |
| 301 | |
| 302 | self.assertEqual(e.exception.message, "It was not possible to activate OLTDevice with id 1") |
| 303 | self.assertEqual(self.o.oper_status, "ERROR") |
| 304 | self.assertEqual(self.o.admin_state, "FAILED") |
| 305 | self.assertEqual(self.o.save.call_count, 1) |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 306 | |
| 307 | @requests_mock.Mocker() |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 308 | def test_sync_record_already_existing_in_voltha(self, m): |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 309 | # mock device feedback state |
| 310 | self.o.device_id = "123" |
Matteo Scandolo | 096a3cf | 2018-06-20 13:56:13 -0700 | [diff] [blame] | 311 | self.o.admin_state = "ENABLED" |
| 312 | self.o.oper_status = "ACTIVE" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 313 | self.o.dp_id = "of:0000000ce2314000" |
Matteo Scandolo | 2c14493 | 2018-05-04 14:06:24 -0700 | [diff] [blame] | 314 | self.o.of_id = "0001000ce2314000" |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 315 | |
Matteo Scandolo | a79395f | 2018-10-08 13:34:49 -0700 | [diff] [blame] | 316 | expected_conf = { |
| 317 | "devices": { |
| 318 | self.o.dp_id: { |
| 319 | "basic": { |
| 320 | "name": self.o.name |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | m.post("http://onos:4321/onos/v1/network/configuration/", status_code=200, json=expected_conf, |
| 326 | additional_matcher=functools.partial(match_json, expected_conf)) |
| 327 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 328 | self.sync_step().sync_record(self.o) |
| 329 | self.o.save.assert_not_called() |
| 330 | |
Matteo Scandolo | b8621cd | 2018-04-04 17:12:37 -0700 | [diff] [blame] | 331 | @requests_mock.Mocker() |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 332 | def test_delete_record(self, m): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 333 | self.o.of_id = "0001000ce2314000" |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 334 | self.o.device_id = "123" |
| 335 | |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 336 | m.post("http://voltha_url:1234/api/v1/devices/123/disable", status_code=200) |
| 337 | m.delete("http://voltha_url:1234/api/v1/devices/123/delete", status_code=200) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 338 | |
Matteo Scandolo | ce27e9c | 2018-04-06 10:06:53 -0700 | [diff] [blame] | 339 | self.sync_step().delete_record(self.o) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 340 | |
Matteo Scandolo | 563891c | 2018-08-21 11:56:32 -0700 | [diff] [blame] | 341 | self.assertEqual(m.call_count, 2) |
| 342 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 343 | @patch('requests.post') |
| 344 | def test_delete_record_connectionerror(self, m): |
| 345 | self.o.of_id = "0001000ce2314000" |
| 346 | self.o.device_id = "123" |
| 347 | |
| 348 | m.side_effect = ConnectionError() |
| 349 | |
| 350 | self.sync_step().delete_record(self.o) |
| 351 | |
| 352 | # No exception thrown, as ConnectionError will be caught |
| 353 | |
| 354 | |
Matteo Scandolo | 563891c | 2018-08-21 11:56:32 -0700 | [diff] [blame] | 355 | @requests_mock.Mocker() |
| 356 | def test_delete_unsynced_record(self, m): |
| 357 | |
| 358 | self.sync_step().delete_record(self.o) |
| 359 | |
| 360 | self.assertEqual(m.call_count, 0) |
Matteo Scandolo | 4a8b4d6 | 2018-03-06 17:18:46 -0800 | [diff] [blame] | 361 | |
| 362 | if __name__ == "__main__": |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 363 | unittest.main() |