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 | |
| 45 | # The test config.yaml references files in `test/` so make sure we're in the parent directory of the |
| 46 | # test directory. |
| 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 | |
| 55 | b = xossynchronizer.backend.Backend() |
| 56 | steps_dir = Config.get("steps_dir") |
| 57 | self.steps = b.load_sync_step_modules(steps_dir) |
| 58 | self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps) |
| 59 | |
| 60 | def tearDown(self): |
| 61 | sys.path = self.sys_path_save |
| 62 | os.chdir(self.cwd_save) |
| 63 | |
| 64 | def test_load_steps(self): |
| 65 | step_names = [s.__name__ for s in self.steps] |
| 66 | self.assertIn("SyncControllerSlices", step_names) |
| 67 | |
| 68 | def test_load_deps(self): |
| 69 | self.synchronizer.load_dependency_graph() |
| 70 | graph = self.synchronizer.model_dependency_graph |
| 71 | self.assertTrue(graph[False].has_edge("Instance", "Slice")) |
| 72 | self.assertTrue(graph[True].has_edge("Slice", "Instance")) |
| 73 | self.assertTrue(graph[False].has_edge("Slice", "ControllerSlice")) |
| 74 | self.assertTrue(graph[True].has_edge("ControllerSlice", "Slice")) |
| 75 | |
| 76 | def test_load_dep_accessors(self): |
| 77 | self.synchronizer.load_dependency_graph() |
| 78 | graph = self.synchronizer.model_dependency_graph |
| 79 | self.assertDictContainsSubset( |
| 80 | {"src_accessor": "controllerslices"}, |
| 81 | graph[False]["Slice"]["ControllerSlice"], |
| 82 | ) |
| 83 | self.assertDictContainsSubset( |
| 84 | {"src_accessor": "slice", "dst_accessor": "controllerslices"}, |
| 85 | graph[True]["Slice"]["ControllerSlice"], |
| 86 | ) |
| 87 | |
| 88 | def test_load_sync_steps(self): |
| 89 | self.synchronizer.load_sync_steps() |
| 90 | model_to_step = self.synchronizer.model_to_step |
| 91 | step_lookup = self.synchronizer.step_lookup |
| 92 | self.assertIn( |
| 93 | ("ControllerSlice", ["SyncControllerSlices"]), model_to_step.items() |
| 94 | ) |
| 95 | self.assertIn(("SiteRole", ["SyncRoles"]), model_to_step.items()) |
| 96 | |
| 97 | for k, v in model_to_step.items(): |
| 98 | val = v[0] |
| 99 | observes = step_lookup[val].observes |
| 100 | if not isinstance(observes, list): |
| 101 | observes = [observes] |
| 102 | |
| 103 | observed_names = [o.__name__ for o in observes] |
| 104 | self.assertIn(k, observed_names) |
| 105 | |
| 106 | |
| 107 | if __name__ == "__main__": |
| 108 | unittest.main() |