Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [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 |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame^] | 16 | import functools |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 17 | from mock import patch, call, Mock, PropertyMock |
| 18 | import requests_mock |
| 19 | |
| 20 | import os, sys |
| 21 | |
| 22 | # Hack to load synchronizer framework |
| 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | xos_dir=os.path.join(test_path, "../../..") |
| 25 | if 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") |
| 28 | sys.path.append(xos_dir) |
| 29 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 30 | # END Hack to load synchronizer framework |
| 31 | |
| 32 | # generate model from xproto |
| 33 | def 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 | |
| 44 | def mock_get_westbound_service_instance_properties(prop): |
| 45 | return prop |
| 46 | |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame^] | 47 | def match_json(desired, req): |
| 48 | if desired!=req.json(): |
| 49 | raise Exception("Got request %s, but body is not matching" % req.url) |
| 50 | return False |
| 51 | return True |
| 52 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 53 | class TestSyncVOLTServiceInstance(unittest.TestCase): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 54 | def setUp(self): |
| 55 | global DeferredException |
| 56 | |
| 57 | self.sys_path_save = sys.path |
| 58 | sys.path.append(xos_dir) |
| 59 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 60 | |
| 61 | # Setting up the config module |
| 62 | from xosconfig import Config |
| 63 | config = os.path.join(test_path, "../model_policies/test_config.yaml") |
| 64 | Config.clear() |
| 65 | Config.init(config, "synchronizer-config-schema.yaml") |
| 66 | # END Setting up the config module |
| 67 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 68 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 69 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 70 | |
| 71 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 72 | # and apparently it is not overriding the generated model accessor |
| 73 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 74 | get_models_fn("vsg", "vsg.xproto"), |
| 75 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 76 | import synchronizers.new_base.modelaccessor |
Scott Baker | 8699e7b | 2018-07-05 13:11:14 -0700 | [diff] [blame] | 77 | from synchronizers.new_base.syncstep import DeferredException |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 78 | from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor |
| 79 | |
| 80 | # import all class names to globals |
| 81 | for (k, v) in model_accessor.all_model_classes.items(): |
| 82 | globals()[k] = v |
| 83 | |
| 84 | self.sync_step = SyncVOLTServiceInstance |
| 85 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 86 | volt_service = Mock() |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 87 | volt_service.onos_voltha_url = "onos_voltha_url" |
| 88 | volt_service.onos_voltha_port = 4321 |
| 89 | volt_service.onos_voltha_user = "onos_voltha_user" |
| 90 | volt_service.onos_voltha_pass = "onos_voltha_pass" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 91 | |
| 92 | si = Mock() |
| 93 | si.get_westbound_service_instance_properties = mock_get_westbound_service_instance_properties |
| 94 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 95 | uni_port = Mock() |
| 96 | uni_port.port_no = "uni_port_id" |
| 97 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 98 | onu_device = Mock() |
| 99 | onu_device.name = "BRCM1234" |
| 100 | onu_device.pon_port.olt_device.dp_id = None |
| 101 | onu_device.pon_port.olt_device.name = "Test OLT Device" |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 102 | onu_device.uni_ports.first.return_value = uni_port |
| 103 | |
| 104 | # create a mock service instance |
| 105 | o = Mock() |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame^] | 106 | o.policy_code = 1 |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 107 | o.id = 1 |
| 108 | o.owner_id = "volt_service" |
| 109 | o.onu_device = onu_device |
| 110 | o.tologdict.return_value = {} |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 111 | |
| 112 | self.o = o |
| 113 | self.si = si |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 114 | self.onu_device = onu_device |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 115 | self.volt_service = volt_service |
| 116 | |
| 117 | def tearDown(self): |
| 118 | self.o = None |
| 119 | sys.path = self.sys_path_save |
| 120 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 121 | @requests_mock.Mocker() |
| 122 | def test_do_not_sync(self, m): |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 123 | self.onu_device.pon_port.olt_device.dp_id = None |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 124 | |
| 125 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 126 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 127 | service_instance_mock.return_value = self.si |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 128 | olt_service_mock.return_value = self.volt_service |
| 129 | |
| 130 | with self.assertRaises(DeferredException) as e: |
| 131 | self.sync_step().sync_record(self.o) |
| 132 | |
| 133 | self.assertFalse(m.called) |
| 134 | self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized") |
| 135 | |
| 136 | @requests_mock.Mocker() |
| 137 | def test_do_sync(self, m): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 138 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 139 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame^] | 140 | |
| 141 | expected_conf = { |
| 142 | "deviceId" : "of:dp_id", |
| 143 | "port" : "uni_port_id", |
| 144 | "sVlan" : "s_tag", |
| 145 | "cVlan" : "c_tag" |
| 146 | } |
| 147 | |
| 148 | m.post("http://onos_voltha_url:4321/onos/olt/oltapp/subscribers", status_code=200, json={}, additional_matcher=functools.partial(match_json, expected_conf)) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 149 | |
| 150 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 151 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 152 | service_instance_mock.return_value = self.si |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 153 | olt_service_mock.return_value = self.volt_service |
| 154 | |
| 155 | self.sync_step().sync_record(self.o) |
| 156 | self.assertTrue(m.called) |
| 157 | |
| 158 | @requests_mock.Mocker() |
| 159 | def test_do_sync_fail(self, m): |
Matteo Scandolo | 8091294 | 2018-07-25 20:51:30 -0700 | [diff] [blame^] | 160 | |
| 161 | expected_conf = { |
| 162 | "deviceId" : "of:dp_id", |
| 163 | "port" : "uni_port_id", |
| 164 | "sVlan" : "s_tag", |
| 165 | "cVlan" : "c_tag" |
| 166 | } |
| 167 | |
| 168 | m.post("http://onos_voltha_url:4321/onos/olt/oltapp/subscribers", status_code=500, text="Mock Error", additional_matcher=functools.partial(match_json, expected_conf)) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 169 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 170 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 171 | |
| 172 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 173 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 174 | service_instance_mock.return_value = self.si |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 175 | olt_service_mock.return_value = self.volt_service |
| 176 | |
| 177 | with self.assertRaises(Exception) as e: |
| 178 | self.sync_step().sync_record(self.o) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 179 | self.assertTrue(m.called) |
| 180 | self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 181 | |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 182 | @requests_mock.Mocker() |
| 183 | def test_delete(self, m): |
| 184 | m.delete("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=204) |
| 185 | |
| 186 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
| 187 | |
| 188 | with patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 189 | olt_service_mock.return_value = self.volt_service |
| 190 | |
| 191 | self.sync_step().delete_record(self.o) |
| 192 | self.assertTrue(m.called) |
| 193 | self.assertEqual(m.call_count, 1) |
| 194 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 195 | if __name__ == "__main__": |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 196 | unittest.main() |