Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [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 | import unittest |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import json |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | # Hack to load synchronizer framework |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | xos_dir=os.path.join(test_path, "../../..") |
| 24 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 25 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 26 | services_dir = os.path.join(xos_dir, "../../xos_services") |
| 27 | sys.path.append(xos_dir) |
| 28 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 29 | # END Hack to load synchronizer framework |
| 30 | |
| 31 | # generate model from xproto |
| 32 | def get_models_fn(service_name, xproto_name): |
| 33 | name = os.path.join(service_name, "xos", xproto_name) |
| 34 | if os.path.exists(os.path.join(services_dir, name)): |
| 35 | return name |
| 36 | else: |
| 37 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 38 | if os.path.exists(os.path.join(services_dir, name)): |
| 39 | return name |
| 40 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 41 | # END generate model from xproto |
| 42 | |
| 43 | class TestSyncOLTDevice(unittest.TestCase): |
| 44 | |
| 45 | def setUp(self): |
| 46 | |
| 47 | self.sys_path_save = sys.path |
| 48 | sys.path.append(xos_dir) |
| 49 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 50 | |
| 51 | # Setting up the config module |
| 52 | from xosconfig import Config |
| 53 | config = os.path.join(test_path, "../test_config.yaml") |
| 54 | Config.clear() |
| 55 | Config.init(config, "synchronizer-config-schema.yaml") |
| 56 | # END Setting up the config module |
| 57 | |
| 58 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 59 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 60 | |
| 61 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 62 | # and apparently it is not overriding the generated model accessor |
| 63 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 64 | get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"), |
| 65 | get_models_fn("olt-service", "volt.xproto"), |
| 66 | get_models_fn("../profiles/rcord", "rcord.xproto") |
| 67 | ]) |
| 68 | import synchronizers.new_base.modelaccessor |
| 69 | from onu_event import ONUEventStep, model_accessor |
| 70 | |
| 71 | # import all class names to globals |
| 72 | for (k, v) in model_accessor.all_model_classes.items(): |
| 73 | globals()[k] = v |
| 74 | |
| 75 | self.log = Mock() |
| 76 | |
| 77 | self.event_step = ONUEventStep(self.log) |
| 78 | |
| 79 | self.event = Mock() |
| 80 | self.event_dict = { |
| 81 | 'status': 'activated', |
| 82 | 'serial_number': 'BRCM1234', |
| 83 | 'of_dpid': 'of:109299321' |
| 84 | } |
| 85 | self.event.value = json.dumps(self.event_dict) |
| 86 | |
| 87 | def tearDown(self): |
| 88 | sys.path = self.sys_path_save |
| 89 | |
| 90 | |
| 91 | def test_create_instance(self): |
| 92 | |
| 93 | with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock , \ |
| 94 | patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
| 95 | |
| 96 | att_si_mock.side_effect = IndexError |
| 97 | |
| 98 | self.event_step.process_event(self.event) |
| 99 | |
| 100 | att_si = mock_save.call_args[0][0] |
| 101 | |
| 102 | self.assertEqual(mock_save.call_count, 1) |
| 103 | |
| 104 | self.assertEqual(att_si.serial_number, self.event_dict['serial_number']) |
| 105 | self.assertEqual(att_si.of_dpid, self.event_dict['of_dpid']) |