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 | |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 76 | def test_update_onu(self): |
| 77 | |
| 78 | onu = ONUDevice( |
| 79 | serial_number="BRCM1234", |
| 80 | admin_state="ENABLED" |
| 81 | ) |
| 82 | with patch.object(ONUDevice.objects, "get_items") as get_onu, \ |
| 83 | patch.object(onu, "save") as onu_save: |
| 84 | get_onu.return_value = [onu] |
| 85 | |
| 86 | self.policy.update_onu("brcm1234", "ENABLED") |
| 87 | onu_save.assert_not_called() |
| 88 | |
| 89 | self.policy.update_onu("brcm1234", "DISABLED") |
| 90 | self.assertEqual(onu.admin_state, "DISABLED") |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 91 | onu_save.assert_called_with(always_update_timestamp=True, update_fields=['admin_state', 'serial_number', 'updated']) |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 92 | |
| 93 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 94 | def test_enable_onu(self): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 95 | from helpers import AttHelpers |
| 96 | with patch.object(AttHelpers, "validate_onu") as validate_onu, \ |
| 97 | patch.object(self.policy, "update_onu") as update_onu, \ |
| 98 | patch.object(self.si, "save") as save_si: |
| 99 | validate_onu.return_value = [True, "valid onu"] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 100 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 101 | self.policy.validate_onu_state(self.si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 102 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 103 | update_onu.assert_called_once() |
| 104 | update_onu.assert_called_with("BRCM1234", "ENABLED") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 105 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 106 | self.assertIn("valid onu", self.si.status_message) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 107 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 108 | def test_disable_onu(self): |
| 109 | from helpers import AttHelpers |
| 110 | with patch.object(AttHelpers, "validate_onu") as validate_onu, \ |
| 111 | patch.object(self.policy, "update_onu") as update_onu, \ |
| 112 | patch.object(self.si, "save") as save_si: |
| 113 | validate_onu.return_value = [False, "invalid onu"] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 114 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 115 | self.policy.validate_onu_state(self.si) |
| 116 | |
| 117 | update_onu.assert_called_once() |
| 118 | update_onu.assert_called_with("BRCM1234", "DISABLED") |
| 119 | |
| 120 | self.assertIn("invalid onu", self.si.status_message) |
| 121 | |
| 122 | def test_handle_update_validate_onu(self): |
| 123 | """ |
| 124 | Testing that handle_update calls validate_onu with the correct parameters |
| 125 | when necessary |
| 126 | """ |
| 127 | with patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 128 | patch.object(self.policy, "update_onu") as update_onu, \ |
| 129 | patch.object(self.policy, "get_subscriber") as get_subscriber: |
| 130 | update_onu.return_value = None |
| 131 | get_subscriber.return_value = None |
| 132 | |
| 133 | self.si.onu_state = "AWAITING" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 134 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 135 | validate_onu_state.assert_called_with(self.si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 136 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 137 | self.si.onu_state = "ENABLED" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 138 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 139 | validate_onu_state.assert_called_with(self.si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 140 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 141 | self.si.onu_state = "DISABLED" |
| 142 | self.policy.handle_update(self.si) |
| 143 | self.assertEqual(validate_onu_state.call_count, 2) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 144 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 145 | def test_get_subscriber(self): |
| 146 | |
| 147 | sub = RCORDSubscriber( |
| 148 | onu_device="BRCM1234" |
| 149 | ) |
| 150 | |
| 151 | with patch.object(RCORDSubscriber.objects, "get_items") as get_subscribers: |
| 152 | get_subscribers.return_value = [sub] |
| 153 | |
| 154 | res = self.policy.get_subscriber("BRCM1234") |
| 155 | self.assertEqual(res, sub) |
| 156 | |
| 157 | res = self.policy.get_subscriber("brcm1234") |
| 158 | self.assertEqual(res, sub) |
| 159 | |
| 160 | res = self.policy.get_subscriber("foo") |
| 161 | self.assertEqual(res, None) |
| 162 | |
| 163 | def test_update_subscriber(self): |
| 164 | |
| 165 | sub = RCORDSubscriber( |
| 166 | onu_device="BRCM1234" |
| 167 | ) |
| 168 | |
| 169 | self.si.status_message = "some content" |
| 170 | |
| 171 | with patch.object(sub, "save") as sub_save: |
| 172 | self.si.authentication_state = "AWAITING" |
| 173 | self.policy.update_subscriber(sub, self.si) |
| 174 | self.assertEqual(sub.status, "awaiting-auth") |
| 175 | self.assertIn("Awaiting Authentication", self.si.status_message) |
| 176 | sub_save.assert_called() |
| 177 | sub_save.reset_mock() |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 178 | sub.status = None |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 179 | |
| 180 | self.si.authentication_state = "REQUESTED" |
| 181 | self.policy.update_subscriber(sub, self.si) |
| 182 | self.assertEqual(sub.status, "awaiting-auth") |
| 183 | self.assertIn("Authentication requested", self.si.status_message) |
| 184 | sub_save.assert_called() |
| 185 | sub_save.reset_mock() |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 186 | sub.status = None |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 187 | |
| 188 | self.si.authentication_state = "STARTED" |
| 189 | self.policy.update_subscriber(sub, self.si) |
| 190 | self.assertEqual(sub.status, "awaiting-auth") |
| 191 | self.assertIn("Authentication started", self.si.status_message) |
| 192 | sub_save.assert_called() |
| 193 | sub_save.reset_mock() |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 194 | sub.status = None |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 195 | |
| 196 | self.si.authentication_state = "APPROVED" |
| 197 | self.policy.update_subscriber(sub, self.si) |
| 198 | self.assertEqual(sub.status, "enabled") |
| 199 | self.assertIn("Authentication succeded", self.si.status_message) |
| 200 | sub_save.assert_called() |
| 201 | sub_save.reset_mock() |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 202 | sub.status = None |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 203 | |
| 204 | self.si.authentication_state = "DENIED" |
| 205 | self.policy.update_subscriber(sub, self.si) |
| 206 | self.assertEqual(sub.status, "auth-failed") |
| 207 | self.assertIn("Authentication denied", self.si.status_message) |
| 208 | sub_save.assert_called() |
| 209 | sub_save.reset_mock() |
Matteo Scandolo | c6ac74a | 2018-09-14 08:14:51 -0700 | [diff] [blame] | 210 | sub.status = None |
| 211 | |
| 212 | def test_update_subscriber_not(self): |
| 213 | sub = RCORDSubscriber( |
| 214 | onu_device="BRCM1234" |
| 215 | ) |
| 216 | |
| 217 | with patch.object(sub, "save") as sub_save: |
| 218 | sub.status = "awaiting-auth" |
| 219 | self.si.authentication_state = "AWAITING" |
| 220 | self.policy.update_subscriber(sub, self.si) |
| 221 | sub_save.assert_not_called() |
| 222 | |
| 223 | sub.status = "awaiting-auth" |
| 224 | self.si.authentication_state = "REQUESTED" |
| 225 | self.policy.update_subscriber(sub, self.si) |
| 226 | sub_save.assert_not_called() |
| 227 | |
| 228 | sub.status = "awaiting-auth" |
| 229 | self.si.authentication_state = "STARTED" |
| 230 | self.policy.update_subscriber(sub, self.si) |
| 231 | sub_save.assert_not_called() |
| 232 | |
| 233 | sub.status = "enabled" |
| 234 | self.si.authentication_state = "APPROVED" |
| 235 | self.policy.update_subscriber(sub, self.si) |
| 236 | sub_save.assert_not_called() |
| 237 | |
| 238 | sub.status = "auth-failed" |
| 239 | self.si.authentication_state = "DENIED" |
| 240 | self.policy.update_subscriber(sub, self.si) |
| 241 | sub_save.assert_not_called() |
| 242 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 243 | def test_update_subscriber_dhcp_with_exiting_ip(self): |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 244 | sub = RCORDSubscriber( |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 245 | id=10, |
| 246 | onu_device="BRCM1234" |
| 247 | ) |
| 248 | |
| 249 | ip = RCORDIpAddress( |
| 250 | subscriber_id=sub.id, |
| 251 | ip='10.11.2.23' |
| 252 | ) |
| 253 | |
| 254 | self.si.dhcp_state = "DHCPACK" |
| 255 | self.si.ip_address = "10.11.2.23" |
| 256 | self.si.mac_address = "4321" |
| 257 | |
| 258 | with patch.object(sub, "save") as sub_save, \ |
| 259 | patch.object(RCORDIpAddress.objects, "get_items") as get_ips, \ |
| 260 | patch.object(ip, "save_changed_fields") as ip_mock: |
| 261 | |
| 262 | get_ips.return_value = [ip] |
| 263 | ip_mock.return_value = [] |
| 264 | |
| 265 | self.policy.update_subscriber(sub, self.si) |
| 266 | sub_save.assert_called() |
| 267 | self.assertEqual(sub.mac_address, self.si.mac_address) |
| 268 | |
| 269 | ip_mock.assert_called_with() |
| 270 | |
| 271 | def test_update_subscriber_dhcp_with_new_ip(self): |
| 272 | sub = RCORDSubscriber( |
| 273 | id=10, |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 274 | onu_device="BRCM1234" |
| 275 | ) |
| 276 | |
| 277 | self.si.dhcp_state = "DHCPACK" |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 278 | self.si.ip_address = "10.11.2.23" |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 279 | self.si.mac_address = "4321" |
| 280 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 281 | with patch.object(sub, "save") as sub_save, \ |
| 282 | patch.object(RCORDIpAddress, "save", autospec=True) as ip_mock: |
| 283 | |
| 284 | ip_mock.return_value = [] |
| 285 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 286 | self.policy.update_subscriber(sub, self.si) |
| 287 | sub_save.assert_called() |
| 288 | self.assertEqual(sub.mac_address, self.si.mac_address) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 289 | |
Matteo Scandolo | 74f6330 | 2018-11-01 14:05:01 -0700 | [diff] [blame] | 290 | saved_ip = ip_mock.call_args[0][0] |
| 291 | self.assertEqual(saved_ip.ip, self.si.ip_address) |
| 292 | self.assertEqual(saved_ip.subscriber_id, sub.id) |
| 293 | self.assertEqual(saved_ip.description, "DHCP Assigned IP Address") |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 294 | |
| 295 | def test_handle_update_subscriber(self): |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 296 | self.si.onu_state = "DISABLED" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 297 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 298 | sub = RCORDSubscriber( |
| 299 | onu_device="BRCM1234" |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 300 | ) |
| 301 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 302 | with patch.object(self.policy, "get_subscriber") as get_subscriber, \ |
Matteo Scandolo | c5c558f | 2018-11-02 10:33:40 -0700 | [diff] [blame] | 303 | patch.object(self.policy, "update_onu") as update_onu, \ |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 304 | patch.object(self.policy, "update_subscriber") as update_subscriber: |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 305 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 306 | get_subscriber.return_value = None |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 307 | self.policy.handle_update(self.si) |
Matteo Scandolo | c5c558f | 2018-11-02 10:33:40 -0700 | [diff] [blame] | 308 | update_onu.assert_called_with(sub.onu_device, "DISABLED"); |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 309 | self.assertEqual(update_subscriber.call_count, 0) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 310 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 311 | get_subscriber.return_value = sub |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 312 | self.policy.handle_update(self.si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 313 | update_subscriber.assert_called_with(sub, self.si) |
| 314 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 315 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 316 | if __name__ == '__main__': |
| 317 | unittest.main() |
| 318 | |