Matteo Scandolo | 5a0eed2 | 2018-06-01 14:42:43 -0700 | [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 |
| 19 | |
| 20 | import os, sys |
| 21 | |
| 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") |
| 28 | |
| 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)) |
| 40 | |
| 41 | class TestModelPolicyHippieOssServiceInstance(unittest.TestCase): |
| 42 | def setUp(self): |
| 43 | global VOLTServiceInstancePolicy, MockObjectList |
| 44 | |
| 45 | self.sys_path_save = sys.path |
| 46 | sys.path.append(xos_dir) |
| 47 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 48 | |
| 49 | config = os.path.join(test_path, "../test_config.yaml") |
| 50 | from xosconfig import Config |
| 51 | Config.clear() |
| 52 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 53 | |
| 54 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 55 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 56 | get_models_fn("hippie-oss", "hippie-oss.xproto"), |
| 57 | get_models_fn("olt-service", "volt.xproto"), |
| 58 | get_models_fn("../profiles/rcord", "rcord.xproto") |
| 59 | ]) |
| 60 | |
| 61 | import synchronizers.new_base.modelaccessor |
| 62 | from model_policy_hippieossserviceinstance import OSSServiceInstancePolicy, RCORDSubscriber, ONUDevice, model_accessor |
| 63 | |
| 64 | from mock_modelaccessor import MockObjectList |
| 65 | |
| 66 | # import all class names to globals |
| 67 | for (k, v) in model_accessor.all_model_classes.items(): |
| 68 | globals()[k] = v |
| 69 | |
| 70 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 71 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 72 | model_accessor.reset_all_object_stores() |
| 73 | |
| 74 | self.policy = OSSServiceInstancePolicy() |
| 75 | self.si = Mock() |
| 76 | |
| 77 | def tearDown(self): |
| 78 | sys.path = self.sys_path_save |
| 79 | self.si = None |
| 80 | |
| 81 | def test_skip_update(self): |
| 82 | self.si.valid = "awaiting" |
| 83 | |
| 84 | with patch.object(RCORDSubscriber, "save") as subscriber_save, \ |
| 85 | patch.object(ONUDevice, "save") as onu_save: |
| 86 | |
| 87 | self.policy.handle_update(self.si) |
| 88 | subscriber_save.assert_not_called() |
| 89 | onu_save.assert_not_called() |
| 90 | |
| 91 | def test_disable_onu(self): |
| 92 | self.si.valid = "invalid" |
| 93 | self.si.serial_number = "BRCM1234" |
| 94 | |
| 95 | onu = ONUDevice( |
| 96 | serial_number=self.si.serial_number |
| 97 | ) |
| 98 | |
| 99 | with patch.object(ONUDevice.objects, "get_items") as onu_objects, \ |
| 100 | patch.object(RCORDSubscriber, "save") as subscriber_save, \ |
| 101 | patch.object(ONUDevice, "save") as onu_save: |
| 102 | |
| 103 | onu_objects.return_value = [onu] |
| 104 | |
| 105 | self.policy.handle_update(self.si) |
| 106 | subscriber_save.assert_not_called() |
| 107 | self.assertEqual(onu.admin_state, "DISABLED") |
| 108 | onu_save.assert_called() |
| 109 | |
| 110 | def test_create_subscriber(self): |
| 111 | self.si.valid = "valid" |
| 112 | self.si.serial_number = "BRCM1234" |
| 113 | self.si.uni_port_id = 16 |
| 114 | |
| 115 | with patch.object(RCORDSubscriber, "save", autospec=True) as subscriber_save, \ |
| 116 | patch.object(ONUDevice, "save") as onu_save: |
| 117 | |
| 118 | self.policy.handle_update(self.si) |
| 119 | self.assertEqual(subscriber_save.call_count, 1) |
| 120 | |
| 121 | subscriber = subscriber_save.call_args[0][0] |
| 122 | self.assertEqual(subscriber.onu_device, self.si.serial_number) |
| 123 | self.assertEqual(subscriber.uni_port_id, self.si.uni_port_id) |
| 124 | |
| 125 | onu_save.assert_not_called() |
| 126 | |
| 127 | def test_create_subscriber_with_ctag(self): |
| 128 | self.si.valid = "valid" |
| 129 | self.si.serial_number = "BRCM1234" |
| 130 | self.si.uni_port_id = 16 |
| 131 | self.si.c_tag = 111 |
| 132 | |
| 133 | with patch.object(RCORDSubscriber, "save", autospec=True) as subscriber_save, \ |
| 134 | patch.object(ONUDevice, "save") as onu_save: |
| 135 | |
| 136 | self.policy.handle_update(self.si) |
| 137 | self.assertEqual(subscriber_save.call_count, 1) |
| 138 | |
| 139 | subscriber = subscriber_save.call_args[0][0] |
| 140 | self.assertEqual(subscriber.onu_device, self.si.serial_number) |
| 141 | self.assertEqual(subscriber.uni_port_id, self.si.uni_port_id) |
| 142 | self.assertEqual(subscriber.c_tag, self.si.c_tag) |
| 143 | |
| 144 | onu_save.assert_not_called() |
| 145 | |
| 146 | if __name__ == '__main__': |
| 147 | unittest.main() |
| 148 | |