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