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 |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 17 | from mock import patch, Mock |
| 18 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 19 | |
| 20 | import os, sys |
| 21 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 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") |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 28 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [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)) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 40 | |
| 41 | class TestModelPolicyRCORDSubscriber(unittest.TestCase): |
| 42 | def setUp(self): |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 43 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 44 | self.sys_path_save = sys.path |
| 45 | sys.path.append(xos_dir) |
| 46 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 47 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 48 | config = os.path.join(test_path, "../test_config.yaml") |
| 49 | from xosconfig import Config |
| 50 | Config.clear() |
| 51 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 52 | |
| 53 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 54 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 55 | get_models_fn("../profiles/rcord", "rcord.xproto"), |
| 56 | get_models_fn("olt-service", "volt.xproto") # in test create we spy on VOLTServiceInstance |
| 57 | ]) |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 58 | |
| 59 | import synchronizers.new_base.modelaccessor |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 60 | from model_policy_rcordsubscriber import RCORDSubscriberPolicy, model_accessor |
| 61 | |
| 62 | from mock_modelaccessor import MockObjectList |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 63 | |
| 64 | # import all class names to globals |
| 65 | for (k, v) in model_accessor.all_model_classes.items(): |
| 66 | globals()[k] = v |
| 67 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 68 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 69 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 70 | model_accessor.reset_all_object_stores() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 71 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 72 | self.policy = RCORDSubscriberPolicy() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 73 | self.si = Mock(name="myTestSubscriber") |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 74 | |
| 75 | def tearDown(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 76 | sys.path = self.sys_path_save |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 77 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 78 | def test_update_and_do_nothing(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 79 | si = self.si |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 80 | si.is_new = False |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 81 | si.subscribed_links.all.return_value = ["already", "have", "a", "chain"] |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 82 | self.policy.handle_create(si) |
| 83 | # NOTE assert that no models are created |
| 84 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 85 | def test_create(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 86 | volt = Mock() |
| 87 | volt.get_service_instance_class_name.return_value = "VOLTServiceInstance" |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 88 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 89 | service_dependency = Mock() |
| 90 | service_dependency.provider_service = volt |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 91 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 92 | si = self.si |
| 93 | si.is_new = True |
| 94 | si.subscribed_links.all.return_value = [] |
| 95 | si.owner.subscribed_dependencies.all.return_value = [service_dependency] |
| 96 | |
| 97 | with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \ |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 98 | patch.object(ServiceInstanceLink, "save", autospec=True) as save_link: |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 99 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 100 | self.policy.handle_create(si) |
| 101 | self.assertEqual(save_link.call_count, 1) |
| 102 | self.assertEqual(save_volt.call_count, 1) |
| 103 | |
| 104 | |
| 105 | if __name__ == '__main__': |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 106 | unittest.main() |