blob: 2456c271cb8ecdd89454f92bec1d084e7725061b [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
28
29class TestServices(unittest.TestCase):
30 def setUp(self):
31 self.sys_path_save = sys.path
32 self.cwd_save = os.getcwd()
33
34 config = os.path.join(test_path, "test_config.yaml")
35 from xosconfig import Config
36
37 Config.clear()
38 Config.init(config, "synchronizer-config-schema.yaml")
39
40 from xossynchronizer.mock_modelaccessor_build import (
41 build_mock_modelaccessor,
42 )
43
44 build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[])
45
46 os.chdir(os.path.join(test_path, "..")) # config references tests/model-deps
47
48 import xossynchronizer.event_loop
49
50 reload(xossynchronizer.event_loop)
51 import xossynchronizer.backend
52
53 reload(xossynchronizer.backend)
54 from xossynchronizer.modelaccessor import model_accessor
55
56 # import all class names to globals
57 for (k, v) in model_accessor.all_model_classes.items():
58 globals()[k] = v
59
60 b = xossynchronizer.backend.Backend()
61 steps_dir = Config.get("steps_dir")
62 self.steps = b.load_sync_step_modules(steps_dir)
63 self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps)
64
65 def tearDown(self):
66 sys.path = self.sys_path_save
67 os.chdir(self.cwd_save)
68
69 def test_service_models(self):
70 s = Service()
71 a = ServiceInstance(owner=s)
72
73 cohorts = self.synchronizer.compute_dependent_cohorts([a, s], False)
74 self.assertIn([s, a], cohorts)
75
76 cohorts = self.synchronizer.compute_dependent_cohorts([s, a], False)
77 self.assertIn([s, a], cohorts)
78
79 cohorts = self.synchronizer.compute_dependent_cohorts([a, s], True)
80 self.assertIn([a, s], cohorts)
81
82 cohorts = self.synchronizer.compute_dependent_cohorts([s, a], True)
83 self.assertIn([a, s], cohorts)
84
85
86if __name__ == "__main__":
87 unittest.main()