blob: c2cdde8b22afd5b541e80e194a0475e3988a8c18 [file] [log] [blame]
Scott Baker8b20bb12018-05-18 10:51:15 -07001
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
16import os
17import sys
18
19def 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 Bakerc6ed1c42019-01-29 16:57:10 -080021 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 Baker8b20bb12018-05-18 10:51:15 -070024
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 Bakerc6ed1c42019-01-29 16:57:10 -080028 models - a list of pairs (service_name, xproto_name)
Scott Baker8b20bb12018-05-18 10:51:15 -070029 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 Baker8b20bb12018-05-18 10:51:15 -070039 sys_path_save = sys.path
40
Scott Baker8b20bb12018-05-18 10:51:15 -070041 # 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 Bakerc6ed1c42019-01-29 16:57:10 -080047 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
48 mock_modelaccessor_config(test_path, models)
Scott Baker8b20bb12018-05-18 10:51:15 -070049
Scott Bakerc6ed1c42019-01-29 16:57:10 -080050 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 Baker8b20bb12018-05-18 10:51:15 -070056 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 Baker8b20bb12018-05-18 10:51:15 -070065 "MockObjectList": MockObjectList}