blob: ae7c116144ab7bb72d6ecdc45e186d74c9b9a332 [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 json
17import unittest
Scott Bakerbba67b62019-01-28 17:38:21 -080018import mock
Scott Bakerbba67b62019-01-28 17:38:21 -080019
20import os
21import sys
22
Zack Williams5c2ea232019-01-30 15:23:01 -070023try:
24 # Python 2: "reload" is built-in
25 reload # pylint: disable=reload-builtin
26except NameError:
27 from importlib import reload
28
Scott Bakerbba67b62019-01-28 17:38:21 -080029test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
30sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
31xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
32
Scott Bakerbba67b62019-01-28 17:38:21 -080033
34class TestRun(unittest.TestCase):
35 def setUp(self):
36 self.sys_path_save = sys.path
37 self.cwd_save = os.getcwd()
38
39 config = os.path.join(test_path, "test_config.yaml")
40 from xosconfig import Config
41
42 Config.clear()
43 Config.init(config, "synchronizer-config-schema.yaml")
44
Zack Williams5c2ea232019-01-30 15:23:01 -070045 from xossynchronizer.mock_modelaccessor_build import build_mock_modelaccessor
46
47 build_mock_modelaccessor(
48 sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]
Scott Bakerbba67b62019-01-28 17:38:21 -080049 )
50
Zack Williams5c2ea232019-01-30 15:23:01 -070051 os.chdir(
52 os.path.join(test_path, "..")
53 ) # config references xos-synchronizer-tests/model-deps
Scott Bakerbba67b62019-01-28 17:38:21 -080054
55 import xossynchronizer.event_loop
56
57 reload(xossynchronizer.event_loop)
58 import xossynchronizer.backend
59
60 reload(xossynchronizer.backend)
61 from xossynchronizer.modelaccessor import model_accessor
62
63 # import all class names to globals
64 for (k, v) in model_accessor.all_model_classes.items():
65 globals()[k] = v
66
Scott Bakerc2fddaa2019-01-30 15:45:03 -080067 from xossynchronizer.modelaccessor import model_accessor
68
69 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 try:
76 os.remove("/tmp/sync_ports")
77 except OSError:
78 pass
79 try:
80 os.remove("/tmp/delete_ports")
81 except OSError:
82 pass
83
84 def tearDown(self):
85 sys.path = self.sys_path_save
86 os.chdir(self.cwd_save)
87
Scott Baker4839dec2019-02-27 16:50:37 -080088 def test_run_once(self):
Scott Bakerbba67b62019-01-28 17:38:21 -080089 pending_objects, pending_steps = self.synchronizer.fetch_pending()
90 pending_objects2 = list(pending_objects)
91
Scott Bakerbba67b62019-01-28 17:38:21 -080092 slice = Slice()
Scott Bakerbba67b62019-01-28 17:38:21 -080093
94 self.synchronizer.run_once()
95
96 sync_ports = open("/tmp/sync_ports").read()
97 delete_ports = open("/tmp/delete_ports").read()
98
99 self.assertIn("successful", sync_ports)
100 self.assertIn("successful", delete_ports)
101
102
103if __name__ == "__main__":
104 unittest.main()