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__))) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 23 | |
| 24 | class TestModelPolicyRCORDSubscriber(unittest.TestCase): |
| 25 | def setUp(self): |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 26 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 27 | self.sys_path_save = sys.path |
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 | config = os.path.join(test_path, "../test_config.yaml") |
| 30 | from xosconfig import Config |
| 31 | Config.clear() |
| 32 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 33 | |
Scott Baker | 8b1a885 | 2019-02-04 09:39:17 -0800 | [diff] [blame] | 34 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 35 | mock_modelaccessor_config(test_path, [ |
Matteo Scandolo | c9527a8 | 2019-02-14 17:02:53 -0800 | [diff] [blame^] | 36 | ("rcord", "rcord.xproto"), |
Scott Baker | 8b1a885 | 2019-02-04 09:39:17 -0800 | [diff] [blame] | 37 | ("olt-service", "volt.xproto")]) |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 38 | |
Scott Baker | 8b1a885 | 2019-02-04 09:39:17 -0800 | [diff] [blame] | 39 | import xossynchronizer.modelaccessor |
| 40 | import mock_modelaccessor |
| 41 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 42 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 43 | |
| 44 | from xossynchronizer.modelaccessor import model_accessor |
| 45 | self.model_accessor = model_accessor |
| 46 | |
| 47 | from model_policy_rcordsubscriber import RCORDSubscriberPolicy |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 48 | |
| 49 | from mock_modelaccessor import MockObjectList |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -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 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 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() |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 58 | |
Scott Baker | 8b1a885 | 2019-02-04 09:39:17 -0800 | [diff] [blame] | 59 | self.policy = RCORDSubscriberPolicy(model_accessor=self.model_accessor) |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 60 | self.si = Mock(name="myTestSubscriber") |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 61 | |
| 62 | def tearDown(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 63 | sys.path = self.sys_path_save |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 64 | |
Matteo Scandolo | c322df5 | 2018-06-22 14:43:59 -0700 | [diff] [blame] | 65 | def test_update_pre_provisione(self): |
| 66 | si = self.si |
| 67 | si.status = "pre-provisioned" |
| 68 | self.policy.handle_create(si) |
| 69 | |
| 70 | with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \ |
| 71 | patch.object(ServiceInstanceLink, "save", autospec=True) as save_link: |
| 72 | |
| 73 | self.policy.handle_create(si) |
| 74 | self.assertEqual(save_link.call_count, 0) |
| 75 | self.assertEqual(save_volt.call_count, 0) |
| 76 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 77 | def test_update_and_do_nothing(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 78 | si = self.si |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 79 | si.is_new = False |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 80 | si.subscribed_links.all.return_value = ["already", "have", "a", "chain"] |
Matteo Scandolo | c322df5 | 2018-06-22 14:43:59 -0700 | [diff] [blame] | 81 | |
| 82 | with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \ |
| 83 | patch.object(ServiceInstanceLink, "save", autospec=True) as save_link: |
| 84 | |
| 85 | self.policy.handle_create(si) |
| 86 | self.assertEqual(save_link.call_count, 0) |
| 87 | self.assertEqual(save_volt.call_count, 0) |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 88 | |
Matteo Scandolo | ace9dd7 | 2018-08-16 16:12:43 -0700 | [diff] [blame] | 89 | def test_create_chain(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 90 | volt = Mock() |
| 91 | volt.get_service_instance_class_name.return_value = "VOLTServiceInstance" |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 92 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 93 | service_dependency = Mock() |
| 94 | service_dependency.provider_service = volt |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 95 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 96 | si = self.si |
| 97 | si.is_new = True |
Matteo Scandolo | fab45ef | 2018-08-17 16:33:29 -0700 | [diff] [blame] | 98 | si.status = "enabled" |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 99 | si.subscribed_links.all.return_value = [] |
| 100 | si.owner.subscribed_dependencies.all.return_value = [service_dependency] |
| 101 | |
| 102 | with patch.object(VOLTServiceInstance, "save", autospec=True) as save_volt, \ |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 103 | patch.object(ServiceInstanceLink, "save", autospec=True) as save_link: |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 104 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 105 | self.policy.handle_create(si) |
| 106 | self.assertEqual(save_link.call_count, 1) |
| 107 | self.assertEqual(save_volt.call_count, 1) |
| 108 | |
Matteo Scandolo | ace9dd7 | 2018-08-16 16:12:43 -0700 | [diff] [blame] | 109 | def test_remove_chain(self): |
| 110 | volt = VOLTServiceInstance() |
| 111 | volt.name = "volt" |
| 112 | |
| 113 | link = ServiceInstanceLink() |
| 114 | link.subscriber_service_instance= self.si |
| 115 | link.provider_service_instance = volt |
| 116 | link.provider_service_instance.leaf_model = volt |
| 117 | |
| 118 | |
| 119 | si = self.si |
| 120 | si.is_new = False |
| 121 | si.status = "awaiting-auth" |
| 122 | si.subscribed_links.all.return_value = [link] |
| 123 | |
| 124 | with patch.object(VOLTServiceInstance, "delete", autospec=True) as delete_volt, \ |
| 125 | patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link: |
| 126 | |
| 127 | self.policy.handle_create(si) |
| 128 | self.assertEqual(delete_link.call_count, 1) |
| 129 | self.assertEqual(delete_volt.call_count, 1) |
| 130 | |
Matteo Scandolo | c6230b4 | 2018-02-26 15:27:57 -0800 | [diff] [blame] | 131 | |
| 132 | if __name__ == '__main__': |
Scott Baker | 9d9ddf6 | 2018-03-20 20:44:27 -0700 | [diff] [blame] | 133 | unittest.main() |