Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import unittest |
| 18 | from mock import patch, call, Mock, PropertyMock |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 19 | |
| 20 | import os, sys |
| 21 | |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | service_dir=os.path.join(test_path, "../../../..") |
| 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 | |
Scott Baker | 27e4fcb | 2018-01-09 10:24:52 -0800 | [diff] [blame] | 29 | # While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check |
| 30 | # both possible locations... |
| 31 | def get_models_fn(service_name, xproto_name): |
| 32 | name = os.path.join(service_name, "xos", xproto_name) |
| 33 | if os.path.exists(os.path.join(services_dir, name)): |
| 34 | return name |
| 35 | else: |
| 36 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 37 | if os.path.exists(os.path.join(services_dir, name)): |
| 38 | return name |
| 39 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 40 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 41 | class TestModelPolicyVOLTServiceInstance(unittest.TestCase): |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 42 | def setUp(self): |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 43 | global VOLTServiceInstancePolicy, MockObjectList |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 44 | |
| 45 | self.sys_path_save = sys.path |
| 46 | sys.path.append(xos_dir) |
| 47 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 48 | |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 49 | config = os.path.join(test_path, "../test_config.yaml") |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 50 | from xosconfig import Config |
| 51 | Config.clear() |
| 52 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 53 | |
| 54 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Scott Baker | 27e4fcb | 2018-01-09 10:24:52 -0800 | [diff] [blame] | 55 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 56 | get_models_fn("vsg", "vsg.xproto"), |
| 57 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 58 | |
| 59 | import synchronizers.new_base.modelaccessor |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 60 | import model_policy_voltserviceinstance |
| 61 | from model_policy_voltserviceinstance import VOLTServiceInstancePolicy, model_accessor |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 62 | |
| 63 | from mock_modelaccessor import MockObjectList |
| 64 | |
| 65 | # import all class names to globals |
| 66 | for (k, v) in model_accessor.all_model_classes.items(): |
| 67 | globals()[k] = v |
| 68 | |
| 69 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 70 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 71 | model_accessor.reset_all_object_stores() |
| 72 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 73 | self.policy = VOLTServiceInstancePolicy() |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 74 | self.si = Mock() |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 75 | |
| 76 | def tearDown(self): |
| 77 | sys.path = self.sys_path_save |
| 78 | |
| 79 | def test_handle_create(self): |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 80 | with patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_eastbound_instance, \ |
| 81 | patch.object(VOLTServiceInstancePolicy, "associate_onu_device") as associate_onu_device: |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 82 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 83 | self.policy.handle_create(self.si) |
| 84 | create_eastbound_instance.assert_called_with(self.si) |
| 85 | associate_onu_device.assert_called_with(self.si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 86 | |
| 87 | def test_create_vsg(self): |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 88 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 89 | patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg: |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 90 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 91 | subscriber_si = Mock() |
| 92 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 93 | link = Mock() |
| 94 | link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance" |
Scott Baker | c50a350 | 2018-08-27 11:36:36 -0700 | [diff] [blame] | 95 | link.provider_service.name = "FabricCrossconnect" |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 96 | link.provider_service.validate_links = Mock(return_value=[]) |
| 97 | link.provider_service.acquire_service_instance = Mock(return_value=subscriber_si) |
| 98 | link.provider_service.leaf_model = link.provider_service |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 99 | |
| 100 | si = Mock() |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 101 | si.subscribed_links.all.return_value = [] |
| 102 | si.owner.subscribed_dependencies.all.return_value = [link] |
| 103 | |
| 104 | self.policy.create_eastbound_instance(si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 105 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 106 | link.provider_service.validate_links.assert_called_with(si) |
| 107 | link.provider_service.acquire_service_instance.assert_called_with(si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 108 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 109 | def test_create_vsg_already_exists(self): |
| 110 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
| 111 | patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg: |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 112 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 113 | subscriber_si = Mock() |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 114 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 115 | link = Mock() |
| 116 | link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance" |
| 117 | link.provider_service.name = "FabricCrossconnect" |
| 118 | link.provider_service.validate_links = Mock(return_value=subscriber_si) |
| 119 | link.provider_service.acquire_service_instance = Mock() |
| 120 | link.provider_service.leaf_model = link.provider_service |
| 121 | |
| 122 | si = Mock() |
| 123 | si.subscribed_links.all.return_value = [] |
| 124 | si.owner.subscribed_dependencies.all.return_value = [link] |
| 125 | |
| 126 | self.policy.create_eastbound_instance(si) |
| 127 | |
| 128 | link.provider_service.validate_links.assert_called_with(si) |
| 129 | link.provider_service.acquire_service_instance.assert_not_called() |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 130 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 131 | def test_associate_onu(self): |
| 132 | with patch.object(ServiceInstance.objects, "get") as get_si, \ |
| 133 | patch.object(ONUDevice.objects, "get") as get_onu: |
| 134 | |
| 135 | mock_si = Mock() |
| 136 | mock_si.get_westbound_service_instance_properties.return_value = "BRCM1234" |
| 137 | get_si.return_value = mock_si |
| 138 | |
| 139 | mock_onu = Mock() |
| 140 | mock_onu.id = 12 |
| 141 | get_onu.return_value = mock_onu |
| 142 | |
| 143 | self.policy.associate_onu_device(self.si) |
| 144 | |
| 145 | self.assertEqual(self.si.onu_device_id, mock_onu.id) |
| 146 | self.si.save.assert_called() |
| 147 | |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 148 | def test_handle_delete(self): |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 149 | self.policy.handle_delete(self.si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 150 | # handle delete does nothing, and should trivially succeed |
| 151 | |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 152 | if __name__ == '__main__': |
| 153 | unittest.main() |
| 154 | |