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