Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -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 | def get_models_fn(service_name, xproto_name): |
| 30 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 31 | if os.path.exists(os.path.join(services_dir, name)): |
| 32 | return name |
| 33 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 34 | |
| 35 | class TestModelPolicyAttWorkflowDriverServiceInstance(unittest.TestCase): |
| 36 | def setUp(self): |
| 37 | |
| 38 | self.sys_path_save = sys.path |
| 39 | sys.path.append(xos_dir) |
| 40 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 41 | |
| 42 | config = os.path.join(test_path, "../test_config.yaml") |
| 43 | from xosconfig import Config |
| 44 | Config.clear() |
| 45 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 46 | |
| 47 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 48 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 49 | get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"), |
| 50 | get_models_fn("olt-service", "volt.xproto"), |
| 51 | get_models_fn("../profiles/rcord", "rcord.xproto") |
| 52 | ]) |
| 53 | |
| 54 | import synchronizers.new_base.modelaccessor |
| 55 | from model_policy_att_workflow_driver_serviceinstance import AttWorkflowDriverServiceInstancePolicy, model_accessor |
| 56 | |
| 57 | from mock_modelaccessor import MockObjectList |
| 58 | |
| 59 | # import all class names to globals |
| 60 | for (k, v) in model_accessor.all_model_classes.items(): |
| 61 | globals()[k] = v |
| 62 | |
| 63 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 64 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 65 | model_accessor.reset_all_object_stores() |
| 66 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 67 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 68 | self.policy = AttWorkflowDriverServiceInstancePolicy() |
| 69 | self.si = AttWorkflowDriverServiceInstance() |
| 70 | self.si.owner = AttWorkflowDriverService() |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 71 | self.si.serial_number = "BRCM1234" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 72 | |
| 73 | def tearDown(self): |
| 74 | sys.path = self.sys_path_save |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 75 | |
| 76 | def test_enable_onu(self): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 77 | from helpers import AttHelpers |
| 78 | with patch.object(AttHelpers, "validate_onu") as validate_onu, \ |
| 79 | patch.object(self.policy, "update_onu") as update_onu, \ |
| 80 | patch.object(self.si, "save") as save_si: |
| 81 | validate_onu.return_value = [True, "valid onu"] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 83 | self.policy.validate_onu_state(self.si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 84 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 85 | update_onu.assert_called_once() |
| 86 | update_onu.assert_called_with("BRCM1234", "ENABLED") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 87 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 88 | self.assertIn("valid onu", self.si.status_message) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 89 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 90 | def test_disable_onu(self): |
| 91 | from helpers import AttHelpers |
| 92 | with patch.object(AttHelpers, "validate_onu") as validate_onu, \ |
| 93 | patch.object(self.policy, "update_onu") as update_onu, \ |
| 94 | patch.object(self.si, "save") as save_si: |
| 95 | validate_onu.return_value = [False, "invalid onu"] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 96 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 97 | self.policy.validate_onu_state(self.si) |
| 98 | |
| 99 | update_onu.assert_called_once() |
| 100 | update_onu.assert_called_with("BRCM1234", "DISABLED") |
| 101 | |
| 102 | self.assertIn("invalid onu", self.si.status_message) |
| 103 | |
| 104 | def test_handle_update_validate_onu(self): |
| 105 | """ |
| 106 | Testing that handle_update calls validate_onu with the correct parameters |
| 107 | when necessary |
| 108 | """ |
| 109 | with patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 110 | patch.object(self.policy, "update_onu") as update_onu, \ |
| 111 | patch.object(self.policy, "get_subscriber") as get_subscriber: |
| 112 | update_onu.return_value = None |
| 113 | get_subscriber.return_value = None |
| 114 | |
| 115 | self.si.onu_state = "AWAITING" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 116 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 117 | validate_onu_state.assert_called_with(self.si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 118 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 119 | self.si.onu_state = "ENABLED" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 120 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 121 | validate_onu_state.assert_called_with(self.si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 122 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 123 | self.si.onu_state = "DISABLED" |
| 124 | self.policy.handle_update(self.si) |
| 125 | self.assertEqual(validate_onu_state.call_count, 2) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 126 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 127 | def test_get_subscriber(self): |
| 128 | |
| 129 | sub = RCORDSubscriber( |
| 130 | onu_device="BRCM1234" |
| 131 | ) |
| 132 | |
| 133 | with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers: |
| 134 | get_subscribers.return_value = [sub] |
| 135 | |
| 136 | res = self.policy.get_subscriber("BRCM1234") |
| 137 | self.assertEqual(res, sub) |
| 138 | |
| 139 | res = self.policy.get_subscriber("brcm1234") |
| 140 | self.assertEqual(res, sub) |
| 141 | |
| 142 | res = self.policy.get_subscriber("foo") |
| 143 | self.assertEqual(res, None) |
| 144 | |
| 145 | def test_update_subscriber(self): |
| 146 | |
| 147 | sub = RCORDSubscriber( |
| 148 | onu_device="BRCM1234" |
| 149 | ) |
| 150 | |
| 151 | self.si.status_message = "some content" |
| 152 | |
| 153 | with patch.object(sub, "save") as sub_save: |
| 154 | self.si.authentication_state = "AWAITING" |
| 155 | self.policy.update_subscriber(sub, self.si) |
| 156 | self.assertEqual(sub.status, "awaiting-auth") |
| 157 | self.assertIn("Awaiting Authentication", self.si.status_message) |
| 158 | sub_save.assert_called() |
| 159 | sub_save.reset_mock() |
| 160 | |
| 161 | self.si.authentication_state = "REQUESTED" |
| 162 | self.policy.update_subscriber(sub, self.si) |
| 163 | self.assertEqual(sub.status, "awaiting-auth") |
| 164 | self.assertIn("Authentication requested", self.si.status_message) |
| 165 | sub_save.assert_called() |
| 166 | sub_save.reset_mock() |
| 167 | |
| 168 | self.si.authentication_state = "STARTED" |
| 169 | self.policy.update_subscriber(sub, self.si) |
| 170 | self.assertEqual(sub.status, "awaiting-auth") |
| 171 | self.assertIn("Authentication started", self.si.status_message) |
| 172 | sub_save.assert_called() |
| 173 | sub_save.reset_mock() |
| 174 | |
| 175 | self.si.authentication_state = "APPROVED" |
| 176 | self.policy.update_subscriber(sub, self.si) |
| 177 | self.assertEqual(sub.status, "enabled") |
| 178 | self.assertIn("Authentication succeded", self.si.status_message) |
| 179 | sub_save.assert_called() |
| 180 | sub_save.reset_mock() |
| 181 | |
| 182 | self.si.authentication_state = "DENIED" |
| 183 | self.policy.update_subscriber(sub, self.si) |
| 184 | self.assertEqual(sub.status, "auth-failed") |
| 185 | self.assertIn("Authentication denied", self.si.status_message) |
| 186 | sub_save.assert_called() |
| 187 | sub_save.reset_mock() |
| 188 | |
| 189 | def test_handle_update_subscriber(self): |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 190 | self.si.onu_state = "DISABLED" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 191 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 192 | sub = RCORDSubscriber( |
| 193 | onu_device="BRCM1234" |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 194 | ) |
| 195 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 196 | with patch.object(self.policy, "get_subscriber") as get_subscriber, \ |
| 197 | patch.object(self.policy, "update_subscriber") as update_subscriber: |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 198 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 199 | get_subscriber.return_value = None |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 200 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 201 | self.assertEqual(update_subscriber.call_count, 0) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 202 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 203 | get_subscriber.return_value = sub |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 204 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame^] | 205 | update_subscriber.assert_called_with(sub, self.si) |
| 206 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 207 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 208 | if __name__ == '__main__': |
| 209 | unittest.main() |
| 210 | |