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 | |
| 15 | import unittest |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import requests_mock |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | # Hack to load synchronizer framework |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | xos_dir=os.path.join(test_path, "../../..") |
| 24 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 25 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 26 | sys.path.append(xos_dir) |
| 27 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 28 | # END Hack to load synchronizer framework |
| 29 | |
| 30 | # Setting up the config module |
| 31 | from xosconfig import Config |
| 32 | config = os.path.join(test_path, "../model_policies/test_config.yaml") |
| 33 | Config.clear() |
| 34 | Config.init(config, "synchronizer-config-schema.yaml") |
| 35 | # END Setting up the config module |
| 36 | |
| 37 | from sync_olt_device import SyncOLTDevice |
| 38 | |
| 39 | class TestSyncOLTDevice(unittest.TestCase): |
| 40 | |
| 41 | def setUp(self): |
| 42 | # create a mock service instance |
| 43 | o = Mock() |
| 44 | o.volt_service.voltha_url = "voltha_url" |
| 45 | o.volt_service.voltha_user = "voltha_user" |
| 46 | o.volt_service.voltha_pass = "voltha_pass" |
| 47 | o.volt_service.p_onos_url = "p_onos_url" |
| 48 | o.volt_service.p_onos_user = "p_onos_user" |
| 49 | o.volt_service.p_onos_pass = "p_onos_pass" |
| 50 | |
| 51 | o.device_type = "ponsim_olt" |
| 52 | o.host = "172.17.0.1" |
| 53 | o.port = "50060" |
| 54 | o.uplink = "129" |
| 55 | o.vlan = "3" |
| 56 | o.driver = "pmc-olt" |
| 57 | |
| 58 | o.tologdict.return_value = {'name': "Mock VOLTServiceInstance"} |
| 59 | |
| 60 | o.save.return_value = "Saved" |
| 61 | |
| 62 | self.o = o |
| 63 | |
| 64 | def tearDown(self): |
| 65 | self.o = None |
| 66 | |
| 67 | def test_format_url(self): |
| 68 | url = SyncOLTDevice.format_url("onf.com") |
| 69 | self.assertEqual(url, "http://onf.com") |
| 70 | url = SyncOLTDevice.format_url("http://onf.com") |
| 71 | self.assertEqual(url, "http://onf.com") |
| 72 | |
| 73 | def test_get_voltha_info(self): |
| 74 | voltha_dict = SyncOLTDevice.get_voltha_info(self.o) |
| 75 | |
| 76 | self.assertEqual(voltha_dict["url"], "http://voltha_url") |
| 77 | self.assertEqual(voltha_dict["user"], "voltha_user") |
| 78 | self.assertEqual(voltha_dict["pass"], "voltha_pass") |
| 79 | |
| 80 | def test_get_onos_info(self): |
| 81 | p_onos_dict = SyncOLTDevice.get_p_onos_info(self.o) |
| 82 | |
| 83 | self.assertEqual(p_onos_dict["url"], "http://p_onos_url") |
| 84 | self.assertEqual(p_onos_dict["user"], "p_onos_user") |
| 85 | self.assertEqual(p_onos_dict["pass"], "p_onos_pass") |
| 86 | |
| 87 | @requests_mock.Mocker() |
| 88 | def test_get_of_id_from_device(self, m): |
| 89 | logical_devices = { |
| 90 | "items": [ |
| 91 | {"root_device_id": "123", "id": "abc"}, |
| 92 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 93 | ] |
| 94 | } |
| 95 | m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 96 | self.o.device_id = "123" |
| 97 | of_id = SyncOLTDevice.get_of_id_from_device(self.o) |
| 98 | self.assertEqual(of_id, "abc") |
| 99 | |
| 100 | with self.assertRaises(Exception) as e: |
| 101 | self.o.device_id = "idonotexist" |
| 102 | SyncOLTDevice.get_of_id_from_device(self.o) |
| 103 | self.assertEqual(e.exception.message, "Can't find a logical device for device id: idonotexist") |
| 104 | |
| 105 | @requests_mock.Mocker() |
| 106 | def test_sync_record_fail_add(self, m): |
| 107 | """ |
| 108 | Should print an error if we can't add the device in VOLTHA |
| 109 | """ |
| 110 | m.post("http://voltha_url/api/v1/devices", status_code=500, text="MockError") |
| 111 | |
| 112 | with self.assertRaises(Exception) as e: |
| 113 | SyncOLTDevice().sync_record(self.o) |
| 114 | self.assertEqual(e.exception.message, "Failed to add device: MockError") |
| 115 | |
| 116 | @requests_mock.Mocker() |
| 117 | def test_sync_record_fail_no_id(self, m): |
| 118 | """ |
| 119 | Should print an error if VOLTHA does not return the device id |
| 120 | """ |
| 121 | m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": ""}) |
| 122 | |
| 123 | with self.assertRaises(Exception) as e: |
| 124 | SyncOLTDevice().sync_record(self.o) |
| 125 | self.assertEqual(e.exception.message, "VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA") |
| 126 | |
| 127 | @requests_mock.Mocker() |
| 128 | def test_sync_record_fail_enable(self, m): |
| 129 | """ |
| 130 | Should print an error if device.enable fails |
| 131 | """ |
| 132 | m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"}) |
| 133 | m.post("http://voltha_url/api/v1/devices/123/enable", status_code=500, text="EnableError") |
| 134 | |
| 135 | with self.assertRaises(Exception) as e: |
| 136 | SyncOLTDevice().sync_record(self.o) |
| 137 | self.assertEqual(e.exception.message, "Failed to enable device: EnableError") |
| 138 | |
| 139 | @requests_mock.Mocker() |
| 140 | def test_sync_record_success(self, m): |
| 141 | """ |
| 142 | If device.enable succed should fetch the state, retrieve the of_id and push it to ONOS |
| 143 | """ |
| 144 | m.post("http://voltha_url/api/v1/devices", status_code=200, json={"id": "123"}) |
| 145 | m.post("http://voltha_url/api/v1/devices/123/enable", status_code=200) |
| 146 | m.get("http://voltha_url/api/v1/devices/123", json={"oper_status": "ENABLED", "admin_state": "ACTIVE"}) |
| 147 | logical_devices = { |
| 148 | "items": [ |
| 149 | {"root_device_id": "123", "id": "abc"}, |
| 150 | {"root_device_id": "0001cc4974a62b87", "id": "0001000000000001"} |
| 151 | ] |
| 152 | } |
| 153 | m.get("http://voltha_url/api/v1/logical_devices", status_code=200, json=logical_devices) |
| 154 | |
| 155 | def match_onos_req(req): |
| 156 | r = req.json()['devices'] |
| 157 | if not r['abc']: |
| 158 | return False |
| 159 | else: |
| 160 | if not r['abc']['basic']['driver'] == 'pmc-olt': |
| 161 | return False |
| 162 | if not r['abc']['accessDevice']['vlan'] == "3" or not r['abc']['accessDevice']['uplink'] == "129": |
| 163 | return False |
| 164 | return True |
| 165 | |
| 166 | m.post("http://p_onos_url/onos/v1/network/configuration/", status_code=200, additional_matcher=match_onos_req, json={}) |
| 167 | |
| 168 | SyncOLTDevice().sync_record(self.o) |
| 169 | self.assertEqual(self.o.admin_state, "ACTIVE") |
| 170 | self.assertEqual(self.o.oper_status, "ENABLED") |
| 171 | self.assertEqual(self.o.of_id, "abc") |
| 172 | self.o.save.assert_called_once() |
| 173 | |
| 174 | @requests_mock.Mocker() |
| 175 | def test_delete_record(self, m): |
| 176 | self.o.of_id = "abc" |
| 177 | self.o.device_id = "123" |
| 178 | |
| 179 | m.delete("http://p_onos_url/onos/v1/network/configuration/devices/abc", status_code=200) |
| 180 | m.post("http://voltha_url/api/v1/devices/123/disable", status_code=200) |
| 181 | m.delete("http://voltha_url/api/v1/devices/123/delete", status_code=200) |
| 182 | |
| 183 | SyncOLTDevice().delete_record(self.o) |
| 184 | |
| 185 | # we don't need to assert here, if there are no exceptions it succeded |
| 186 | |
| 187 | if __name__ == "__main__": |
| 188 | unittest.main() |