blob: 774bc97dd06622ee174f04b2527799a9f980f6a9 [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]
Scott Baker4839dec2019-02-27 16:50:37 -080068 self.assertIn("SyncPort", step_names)
Scott Bakerbba67b62019-01-28 17:38:21 -080069
70 def test_load_deps(self):
71 self.synchronizer.load_dependency_graph()
72 graph = self.synchronizer.model_dependency_graph
Scott Baker4839dec2019-02-27 16:50:37 -080073 self.assertTrue(graph[False].has_edge("Slice", "Site"))
74 self.assertTrue(graph[True].has_edge("Site", "Slice"))
Scott Bakerbba67b62019-01-28 17:38:21 -080075
76 def test_load_sync_steps(self):
77 self.synchronizer.load_sync_steps()
78 model_to_step = self.synchronizer.model_to_step
79 step_lookup = self.synchronizer.step_lookup
80 self.assertIn(
Scott Bakerc2fddaa2019-01-30 15:45:03 -080081 ("Port", ["SyncPort"]), model_to_step.items()
82 )
Scott Baker4839dec2019-02-27 16:50:37 -080083 self.assertIn(("Image", ["SyncImages"]), model_to_step.items())
Scott Bakerbba67b62019-01-28 17:38:21 -080084
85 for k, v in model_to_step.items():
86 val = v[0]
87 observes = step_lookup[val].observes
88 if not isinstance(observes, list):
89 observes = [observes]
90
Scott Bakerc2fddaa2019-01-30 15:45:03 -080091 observed_names = []
92 for o in observes:
93 if isinstance(o,str):
94 observed_names.append(o)
95 else:
96 observed_names.append(o.__name__)
97
Scott Bakerbba67b62019-01-28 17:38:21 -080098 self.assertIn(k, observed_names)
99
100
101if __name__ == "__main__":
102 unittest.main()