blob: e2bbbb038bd75d3418d324a12540eed66269c872 [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
15import unittest
16from mock import patch
17import mock
18import pdb
19import networkx as nx
20
21import os
22import sys
23
24test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
25sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
26xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
27
28class 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 Bakerf0d7e5c2019-02-05 08:35:31 -080045 # 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 Bakerbba67b62019-01-28 17:38:21 -080047 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 Bakerc2fddaa2019-01-30 15:45:03 -080055 from xossynchronizer.modelaccessor import model_accessor
56
57 b = xossynchronizer.backend.Backend(model_accessor=model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080058 steps_dir = Config.get("steps_dir")
59 self.steps = b.load_sync_step_modules(steps_dir)
Scott Bakerc2fddaa2019-01-30 15:45:03 -080060 self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080061
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 Bakerc2fddaa2019-01-30 15:45:03 -080097 self.assertIn(
98 ("Port", ["SyncPort"]), model_to_step.items()
99 )
Scott Bakerbba67b62019-01-28 17:38:21 -0800100 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 Bakerc2fddaa2019-01-30 15:45:03 -0800108 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 Bakerbba67b62019-01-28 17:38:21 -0800115 self.assertIn(k, observed_names)
116
117
118if __name__ == "__main__":
119 unittest.main()