Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -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 | import os |
| 17 | import sys |
| 18 | |
| 19 | def setup_sync_unit_test(test_path, globals_dict, models, config_fn="test_config.yaml"): |
| 20 | """ Perform the common steps associated with setting up a synchronizer unit test. |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 21 | 1) Import xosconfig.Config and set it up to test_config.yaml in the current dir |
| 22 | 2) Build the mock modelaccessor and import it |
| 23 | 3) Import all model accessor classes into global space |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 24 | |
| 25 | Arguments: |
| 26 | test_path - path to the test case that is being run |
| 27 | globals_dict - a dictionary to add global models to |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 28 | models - a list of pairs (service_name, xproto_name) |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 29 | config_fn - filename of config file) |
| 30 | |
| 31 | Returns: |
| 32 | Dictionary containing the following: |
| 33 | sys_path_save: the original sys.path |
| 34 | model_accessor: model accessor class |
| 35 | Config: the Config object |
| 36 | xos_dir: xos directory |
| 37 | services_dir: services directory |
| 38 | """ |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 39 | sys_path_save = sys.path |
| 40 | |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 41 | # Setting up the config module |
| 42 | from xosconfig import Config |
| 43 | config = os.path.join(test_path, config_fn) |
| 44 | Config.clear() |
| 45 | Config.init(config, "synchronizer-config-schema.yaml") |
| 46 | |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 47 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 48 | mock_modelaccessor_config(test_path, models) |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 49 | |
Scott Baker | c6ed1c4 | 2019-01-29 16:57:10 -0800 | [diff] [blame] | 50 | import xossynchronizer.modelaccessor |
| 51 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous testp |
| 52 | |
| 53 | from xossynchronizer.modelaccessor import model_accessor |
| 54 | |
| 55 | # modelaccessor.py will have ensure mock_modelaccessor is in sys.path |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 56 | from mock_modelaccessor import MockObjectList |
| 57 | |
| 58 | # import all class names to globals |
| 59 | for (k, v) in model_accessor.all_model_classes.items(): |
| 60 | globals_dict[k] = v |
| 61 | |
| 62 | return {"sys_path_save": sys_path_save, |
| 63 | "model_accessor": model_accessor, |
| 64 | "Config": Config, |
Scott Baker | 8b20bb1 | 2018-05-18 10:51:15 -0700 | [diff] [blame] | 65 | "MockObjectList": MockObjectList} |