Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -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 | |
| 16 | import unittest |
| 17 | from mock import patch, MagicMock |
| 18 | import mock |
| 19 | from xosconfig import Config |
| 20 | |
| 21 | import os, sys |
| 22 | |
| 23 | cwd=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | xos_dir=os.path.abspath(os.path.join(cwd, "../../../../../../orchestration/xos/xos")) |
| 25 | # services_dir=os.path.join(xos_dir, "../../xos_services") |
| 26 | config_file = os.path.join(cwd, "test_config.yaml") |
| 27 | |
| 28 | # NOTE this have to start for xos_services |
| 29 | # RCORD_XPROTO = "../profiles/rcord/xos/synchronizer/models/rcord.xproto" |
| 30 | # OLT_XPROTO = "olt-service/xos/synchronizer/models/volt.xproto" |
| 31 | |
| 32 | Config.clear() |
| 33 | Config.init(config_file, 'synchronizer-config-schema.yaml') |
| 34 | |
| 35 | # FIXME move the synchronizer framework into a library |
| 36 | sys.path.append(xos_dir) |
| 37 | # from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 38 | from synchronizers.new_base.modelaccessor import model_accessor |
| 39 | |
| 40 | from model_policy_rcordsubscriber import RCORDSubscriberPolicy |
| 41 | |
| 42 | class MockSubscriber: |
| 43 | |
| 44 | def __init__(self, *args, **kwargs): |
| 45 | self.name = kwargs['name'] |
| 46 | self.is_new = True |
| 47 | self.id = 1 |
| 48 | |
| 49 | def subscribed_links(self): |
| 50 | pass |
| 51 | |
| 52 | class MockAll: |
| 53 | @staticmethod |
| 54 | def all(): |
| 55 | pass |
| 56 | |
| 57 | class MockService: |
| 58 | |
| 59 | def __init__(self): |
| 60 | self.subscribed_dependencies = MockAll |
| 61 | |
| 62 | |
| 63 | class MockModel: |
| 64 | def __init__(self): |
| 65 | self.model_name = "VOLTService" |
| 66 | |
| 67 | class MockLeaf: |
| 68 | def __init__(self): |
| 69 | self.leaf_model = MockModel() |
| 70 | |
| 71 | class MockLink: |
| 72 | def __init__(self): |
| 73 | self.provider_service = MockLeaf() |
| 74 | |
| 75 | class TestModelPolicyRCORDSubscriber(unittest.TestCase): |
| 76 | def setUp(self): |
| 77 | self.original_sys_path = sys.path |
| 78 | |
| 79 | # Generate a fake model accessor (emulate the client library) |
| 80 | # build_mock_modelaccessor(xos_dir, services_dir, [RCORD_XPROTO, OLT_XPROTO]) |
| 81 | |
| 82 | # import all class names to globals |
| 83 | for (k, v) in model_accessor.all_model_classes.items(): |
| 84 | globals()[k] = v |
| 85 | |
| 86 | # model_accessor.reset_all_object_stores() |
| 87 | |
| 88 | # create base objects for testing |
| 89 | self.policy = RCORDSubscriberPolicy() |
| 90 | |
| 91 | def tearDown(self): |
| 92 | sys.path = self.original_sys_path |
| 93 | |
| 94 | @patch.object(MockSubscriber, 'subscribed_links', MockAll) |
| 95 | @patch.object(MockAll, 'all', MagicMock(return_value=['foo'])) |
| 96 | def test_update_and_do_nothing(self): |
| 97 | si = MockSubscriber(name="myTestSubscriber") |
| 98 | si.is_new = False |
| 99 | self.policy.handle_create(si) |
| 100 | # NOTE assert that no models are created |
| 101 | |
| 102 | @patch.object(MockSubscriber, 'subscribed_links', MockAll) |
| 103 | @patch.object(MockAll, 'all', MagicMock(return_value=[MockLink])) |
| 104 | def test_create(self): |
| 105 | si = MockSubscriber(name="myTestSubscriber") |
| 106 | owner = MockService() |
| 107 | |
| 108 | si.owner = owner |
| 109 | |
| 110 | with patch.object(owner.subscribed_dependencies, 'all', MagicMock(return_value=[MockLink()])), \ |
Matteo Scandolo | 6f5c896 | 2018-03-01 13:40:49 -0800 | [diff] [blame] | 111 | patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \ |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 112 | patch.object(ServiceInstanceLink, "save", autospec=True) as save_link: |
| 113 | self.policy.handle_create(si) |
| 114 | self.assertEqual(save_link.call_count, 1) |
| 115 | self.assertEqual(save_volt.call_count, 1) |
| 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | unittest.main() |