blob: 73ea213a80a74dbeef018c769797a6bd3710340c [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# 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 Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
Scott Bakerbba67b62019-01-28 17:38:21 -080016import unittest
17from mock import patch
18import mock
19import pdb
20import networkx as nx
21
22import os
23import sys
24
Zack Williams5c2ea232019-01-30 15:23:01 -070025try:
26 # Python 2: "reload" is built-in
27 reload # pylint: disable=reload-builtin
28except NameError:
29 from importlib import reload
30
Scott Bakerbba67b62019-01-28 17:38:21 -080031test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
32sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
33xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
34
35class 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 Bakerf0d7e5c2019-02-05 08:35:31 -080052 # 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 Bakerbba67b62019-01-28 17:38:21 -080054 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 Bakerc2fddaa2019-01-30 15:45:03 -080062 from xossynchronizer.modelaccessor import model_accessor
63
64 b = xossynchronizer.backend.Backend(model_accessor=model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080065 steps_dir = Config.get("steps_dir")
66 self.steps = b.load_sync_step_modules(steps_dir)
Scott Bakerc2fddaa2019-01-30 15:45:03 -080067 self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080068
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 Baker4839dec2019-02-27 16:50:37 -080075 self.assertIn("SyncPort", step_names)
Scott Bakerbba67b62019-01-28 17:38:21 -080076
77 def test_load_deps(self):
78 self.synchronizer.load_dependency_graph()
79 graph = self.synchronizer.model_dependency_graph
Scott Baker4839dec2019-02-27 16:50:37 -080080 self.assertTrue(graph[False].has_edge("Slice", "Site"))
81 self.assertTrue(graph[True].has_edge("Site", "Slice"))
Scott Bakerbba67b62019-01-28 17:38:21 -080082
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 Williams5c2ea232019-01-30 15:23:01 -070087 self.assertIn(("Port", ["SyncPort"]), list(model_to_step.items()))
88 self.assertIn(("Image", ["SyncImages"]), list(model_to_step.items()))
Scott Bakerbba67b62019-01-28 17:38:21 -080089
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 Bakerc2fddaa2019-01-30 15:45:03 -080096 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 Bakerbba67b62019-01-28 17:38:21 -0800103 self.assertIn(k, observed_names)
104
105
106if __name__ == "__main__":
107 unittest.main()