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