blob: 68f6743b1af59f1cc41914bd00151168df226673 [file] [log] [blame]
Scott Baker62c7eaf2018-05-22 15:59:26 -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.
21 1) Add synchronizers/new_base to sys.path
22 2) Import xosconfig.Config and set it up to test_config.yaml in the current dir
23 3) Build the mock modelaccessor and import it
24 4) Import all model accessor classes into global space
25
26 Arguments:
27 test_path - path to the test case that is being run
28 globals_dict - a dictionary to add global models to
29 models - a list of pairs (service_name, xproto_name,
30 config_fn - filename of config file)
31
32 Returns:
33 Dictionary containing the following:
34 sys_path_save: the original sys.path
35 model_accessor: model accessor class
36 Config: the Config object
37 xos_dir: xos directory
38 services_dir: services directory
39 """
40 def get_models_fn(services_dir, service_name, xproto_name):
41 name = os.path.join(service_name, "xos", xproto_name)
42 if os.path.exists(os.path.join(services_dir, name)):
43 return name
44 else:
45 name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name)
46 if os.path.exists(os.path.join(services_dir, name)):
47 return name
48 raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name))
49
50 sys_path_save = sys.path
51
52 xos_dir = os.path.join(test_path, "../../..")
53 if not os.path.exists(os.path.join(test_path, "new_base")):
54 xos_dir = os.path.join(test_path, "../../../../../../orchestration/xos/xos")
55 services_dir = os.path.join(xos_dir, "../../xos_services")
56 sys.path.append(xos_dir)
57 sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base'))
58
59 # Setting up the config module
60 from xosconfig import Config
61 config = os.path.join(test_path, config_fn)
62 Config.clear()
63 Config.init(config, "synchronizer-config-schema.yaml")
64
65 xprotos = []
66 for (service_name, xproto_name) in models:
67 xprotos.append(get_models_fn(services_dir, service_name, xproto_name))
68
69 from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor
70 build_mock_modelaccessor(xos_dir, services_dir, xprotos)
71 import synchronizers.new_base.modelaccessor
72 from synchronizers.new_base.modelaccessor import model_accessor
73 from mock_modelaccessor import MockObjectList
74
75 # import all class names to globals
76 for (k, v) in model_accessor.all_model_classes.items():
77 globals_dict[k] = v
78
79 return {"sys_path_save": sys_path_save,
80 "model_accessor": model_accessor,
81 "Config": Config,
82 "xos_dir": xos_dir,
83 "services_dir": services_dir,
84 "MockObjectList": MockObjectList}