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__))) |
Scott Baker | 27e4fcb | 2018-01-09 10:24:52 -0800 | [diff] [blame] | 23 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 24 | class TestModelPolicyVOLTServiceInstance(unittest.TestCase): |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 25 | def setUp(self): |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 26 | global VOLTServiceInstancePolicy, MockObjectList |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 27 | |
| 28 | self.sys_path_save = sys.path |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 29 | |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 30 | config = os.path.join(test_path, "../test_config.yaml") |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 31 | from xosconfig import Config |
| 32 | Config.clear() |
| 33 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 34 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 35 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 36 | mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), |
| 37 | ("vsg", "vsg.xproto"), |
Matteo Scandolo | e04a488 | 2019-02-14 10:22:24 -0800 | [diff] [blame] | 38 | ("rcord", "rcord.xproto"),]) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 39 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 40 | import xossynchronizer.modelaccessor |
| 41 | import mock_modelaccessor |
| 42 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 43 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 44 | |
| 45 | from xossynchronizer.modelaccessor import model_accessor |
| 46 | self.model_accessor = model_accessor |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 47 | |
| 48 | from mock_modelaccessor import MockObjectList |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 49 | from model_policy_voltserviceinstance import VOLTServiceInstancePolicy |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 50 | |
| 51 | # import all class names to globals |
| 52 | for (k, v) in model_accessor.all_model_classes.items(): |
| 53 | globals()[k] = v |
| 54 | |
| 55 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 56 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 57 | model_accessor.reset_all_object_stores() |
| 58 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 59 | self.policy = VOLTServiceInstancePolicy(model_accessor=self.model_accessor) |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 60 | self.si = Mock() |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 61 | |
| 62 | def tearDown(self): |
| 63 | sys.path = self.sys_path_save |
| 64 | |
| 65 | def test_handle_create(self): |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 66 | with patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_eastbound_instance, \ |
| 67 | patch.object(VOLTServiceInstancePolicy, "associate_onu_device") as associate_onu_device: |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 68 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 69 | self.policy.handle_create(self.si) |
| 70 | create_eastbound_instance.assert_called_with(self.si) |
| 71 | associate_onu_device.assert_called_with(self.si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 72 | |
| 73 | def test_create_vsg(self): |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 74 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 75 | patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg: |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 76 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 77 | subscriber_si = Mock() |
| 78 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 79 | link = Mock() |
| 80 | link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance" |
Scott Baker | c50a350 | 2018-08-27 11:36:36 -0700 | [diff] [blame] | 81 | link.provider_service.name = "FabricCrossconnect" |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 82 | link.provider_service.validate_links = Mock(return_value=[]) |
| 83 | link.provider_service.acquire_service_instance = Mock(return_value=subscriber_si) |
| 84 | link.provider_service.leaf_model = link.provider_service |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 85 | |
| 86 | si = Mock() |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 87 | si.subscribed_links.all.return_value = [] |
| 88 | si.owner.subscribed_dependencies.all.return_value = [link] |
| 89 | |
| 90 | self.policy.create_eastbound_instance(si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 91 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 92 | link.provider_service.validate_links.assert_called_with(si) |
| 93 | link.provider_service.acquire_service_instance.assert_called_with(si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 94 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 95 | def test_create_vsg_already_exists(self): |
| 96 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
| 97 | patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg: |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 98 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 99 | subscriber_si = Mock() |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 100 | |
Scott Baker | 59b1e07 | 2018-08-23 15:59:05 -0700 | [diff] [blame] | 101 | link = Mock() |
| 102 | link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance" |
| 103 | link.provider_service.name = "FabricCrossconnect" |
| 104 | link.provider_service.validate_links = Mock(return_value=subscriber_si) |
| 105 | link.provider_service.acquire_service_instance = Mock() |
| 106 | link.provider_service.leaf_model = link.provider_service |
| 107 | |
| 108 | si = Mock() |
| 109 | si.subscribed_links.all.return_value = [] |
| 110 | si.owner.subscribed_dependencies.all.return_value = [link] |
| 111 | |
| 112 | self.policy.create_eastbound_instance(si) |
| 113 | |
| 114 | link.provider_service.validate_links.assert_called_with(si) |
| 115 | link.provider_service.acquire_service_instance.assert_not_called() |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 116 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 117 | def test_associate_onu(self): |
| 118 | with patch.object(ServiceInstance.objects, "get") as get_si, \ |
| 119 | patch.object(ONUDevice.objects, "get") as get_onu: |
| 120 | |
| 121 | mock_si = Mock() |
| 122 | mock_si.get_westbound_service_instance_properties.return_value = "BRCM1234" |
| 123 | get_si.return_value = mock_si |
| 124 | |
| 125 | mock_onu = Mock() |
| 126 | mock_onu.id = 12 |
| 127 | get_onu.return_value = mock_onu |
| 128 | |
| 129 | self.policy.associate_onu_device(self.si) |
| 130 | |
| 131 | self.assertEqual(self.si.onu_device_id, mock_onu.id) |
| 132 | self.si.save.assert_called() |
| 133 | |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 134 | def test_handle_delete(self): |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 135 | self.policy.handle_delete(self.si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 136 | # handle delete does nothing, and should trivially succeed |
| 137 | |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 138 | if __name__ == '__main__': |
| 139 | unittest.main() |
| 140 | |