Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [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 |
| 17 | import mock |
| 18 | import pdb |
| 19 | import networkx as nx |
| 20 | |
| 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 25 | sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer") |
| 26 | xos_dir = os.path.join(test_path, "..", "..", "..", "xos") |
| 27 | |
| 28 | |
| 29 | class TestServices(unittest.TestCase): |
| 30 | def setUp(self): |
| 31 | self.sys_path_save = sys.path |
| 32 | self.cwd_save = os.getcwd() |
| 33 | |
| 34 | config = os.path.join(test_path, "test_config.yaml") |
| 35 | from xosconfig import Config |
| 36 | |
| 37 | Config.clear() |
| 38 | Config.init(config, "synchronizer-config-schema.yaml") |
| 39 | |
| 40 | from xossynchronizer.mock_modelaccessor_build import ( |
| 41 | build_mock_modelaccessor, |
| 42 | ) |
| 43 | |
| 44 | build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]) |
| 45 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 46 | os.chdir(os.path.join(test_path, "..")) # config references xos-synchronizer-tests/model-deps |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 47 | |
| 48 | import xossynchronizer.event_loop |
| 49 | |
| 50 | reload(xossynchronizer.event_loop) |
| 51 | import xossynchronizer.backend |
| 52 | |
| 53 | reload(xossynchronizer.backend) |
| 54 | from xossynchronizer.modelaccessor import model_accessor |
| 55 | |
| 56 | # import all class names to globals |
| 57 | for (k, v) in model_accessor.all_model_classes.items(): |
| 58 | globals()[k] = v |
| 59 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 60 | b = xossynchronizer.backend.Backend(model_accessor=model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 61 | steps_dir = Config.get("steps_dir") |
| 62 | self.steps = b.load_sync_step_modules(steps_dir) |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 63 | self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 64 | |
| 65 | def tearDown(self): |
| 66 | sys.path = self.sys_path_save |
| 67 | os.chdir(self.cwd_save) |
| 68 | |
| 69 | def test_service_models(self): |
| 70 | s = Service() |
| 71 | a = ServiceInstance(owner=s) |
| 72 | |
| 73 | cohorts = self.synchronizer.compute_dependent_cohorts([a, s], False) |
| 74 | self.assertIn([s, a], cohorts) |
| 75 | |
| 76 | cohorts = self.synchronizer.compute_dependent_cohorts([s, a], False) |
| 77 | self.assertIn([s, a], cohorts) |
| 78 | |
| 79 | cohorts = self.synchronizer.compute_dependent_cohorts([a, s], True) |
| 80 | self.assertIn([a, s], cohorts) |
| 81 | |
| 82 | cohorts = self.synchronizer.compute_dependent_cohorts([s, a], True) |
| 83 | self.assertIn([a, s], cohorts) |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | unittest.main() |