blob: 96fd5340be95b5bfc2ea2e956723d6c9f490b4bf [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
35
36class TestServices(unittest.TestCase):
37 def setUp(self):
38 self.sys_path_save = sys.path
39 self.cwd_save = os.getcwd()
40
41 config = os.path.join(test_path, "test_config.yaml")
42 from xosconfig import Config
43
44 Config.clear()
45 Config.init(config, "synchronizer-config-schema.yaml")
46
Zack Williams5c2ea232019-01-30 15:23:01 -070047 from xossynchronizer.mock_modelaccessor_build import build_mock_modelaccessor
48
49 build_mock_modelaccessor(
50 sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]
Scott Bakerbba67b62019-01-28 17:38:21 -080051 )
52
Zack Williams5c2ea232019-01-30 15:23:01 -070053 os.chdir(
54 os.path.join(test_path, "..")
55 ) # config references xos-synchronizer-tests/model-deps
Scott Bakerbba67b62019-01-28 17:38:21 -080056
57 import xossynchronizer.event_loop
58
59 reload(xossynchronizer.event_loop)
60 import xossynchronizer.backend
61
62 reload(xossynchronizer.backend)
63 from xossynchronizer.modelaccessor import model_accessor
64
65 # import all class names to globals
66 for (k, v) in model_accessor.all_model_classes.items():
67 globals()[k] = v
68
Scott Bakerc2fddaa2019-01-30 15:45:03 -080069 b = xossynchronizer.backend.Backend(model_accessor=model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080070 steps_dir = Config.get("steps_dir")
71 self.steps = b.load_sync_step_modules(steps_dir)
Zack Williams5c2ea232019-01-30 15:23:01 -070072 self.synchronizer = xossynchronizer.event_loop.XOSObserver(
73 self.steps, model_accessor
74 )
Scott Bakerbba67b62019-01-28 17:38:21 -080075
76 def tearDown(self):
77 sys.path = self.sys_path_save
78 os.chdir(self.cwd_save)
79
80 def test_service_models(self):
81 s = Service()
82 a = ServiceInstance(owner=s)
83
84 cohorts = self.synchronizer.compute_dependent_cohorts([a, s], False)
85 self.assertIn([s, a], cohorts)
86
87 cohorts = self.synchronizer.compute_dependent_cohorts([s, a], False)
88 self.assertIn([s, a], cohorts)
89
90 cohorts = self.synchronizer.compute_dependent_cohorts([a, s], True)
91 self.assertIn([a, s], cohorts)
92
93 cohorts = self.synchronizer.compute_dependent_cohorts([s, a], True)
94 self.assertIn([a, s], cohorts)
95
96
97if __name__ == "__main__":
98 unittest.main()