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 | class TestScheduling(unittest.TestCase): |
| 36 | def setUp(self): |
| 37 | self.sys_path_save = sys.path |
| 38 | self.cwd_save = os.getcwd() |
| 39 | |
| 40 | config = os.path.join(test_path, "test_config.yaml") |
| 41 | from xosconfig import Config |
| 42 | |
| 43 | Config.clear() |
| 44 | Config.init(config, "synchronizer-config-schema.yaml") |
| 45 | |
| 46 | from xossynchronizer.mock_modelaccessor_build import ( |
| 47 | build_mock_modelaccessor, |
| 48 | ) |
| 49 | |
| 50 | build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]) |
| 51 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 52 | # The test config.yaml references files in `xos-synchronizer-tests/` so make sure we're in the parent |
| 53 | # directory of the test directory. |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 54 | os.chdir(os.path.join(test_path, "..")) |
| 55 | |
| 56 | import xossynchronizer.event_loop |
| 57 | reload(xossynchronizer.event_loop) |
| 58 | |
| 59 | import xossynchronizer.backend |
| 60 | reload(xossynchronizer.backend) |
| 61 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 62 | from xossynchronizer.modelaccessor import model_accessor |
| 63 | |
| 64 | b = xossynchronizer.backend.Backend(model_accessor=model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 65 | steps_dir = Config.get("steps_dir") |
| 66 | self.steps = b.load_sync_step_modules(steps_dir) |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 67 | self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 68 | |
| 69 | def tearDown(self): |
| 70 | sys.path = self.sys_path_save |
| 71 | os.chdir(self.cwd_save) |
| 72 | |
| 73 | def test_load_steps(self): |
| 74 | step_names = [s.__name__ for s in self.steps] |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 75 | self.assertIn("SyncPort", step_names) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 76 | |
| 77 | def test_load_deps(self): |
| 78 | self.synchronizer.load_dependency_graph() |
| 79 | graph = self.synchronizer.model_dependency_graph |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 80 | self.assertTrue(graph[False].has_edge("Slice", "Site")) |
| 81 | self.assertTrue(graph[True].has_edge("Site", "Slice")) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 82 | |
| 83 | def test_load_sync_steps(self): |
| 84 | self.synchronizer.load_sync_steps() |
| 85 | model_to_step = self.synchronizer.model_to_step |
| 86 | step_lookup = self.synchronizer.step_lookup |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 87 | self.assertIn(("Port", ["SyncPort"]), list(model_to_step.items())) |
| 88 | self.assertIn(("Image", ["SyncImages"]), list(model_to_step.items())) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 89 | |
| 90 | for k, v in model_to_step.items(): |
| 91 | val = v[0] |
| 92 | observes = step_lookup[val].observes |
| 93 | if not isinstance(observes, list): |
| 94 | observes = [observes] |
| 95 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 96 | observed_names = [] |
| 97 | for o in observes: |
| 98 | if isinstance(o,str): |
| 99 | observed_names.append(o) |
| 100 | else: |
| 101 | observed_names.append(o.__name__) |
| 102 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 103 | self.assertIn(k, observed_names) |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | unittest.main() |