blob: f21428ee785989bf9150d3cc78533b528c3f6573 [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
33ANSIBLE_FILE = "/tmp/payload_test"
34
35
36def run_fake_ansible_template(*args, **kwargs):
37 opts = args[1]
38 open(ANSIBLE_FILE, "w").write(json.dumps(opts))
39
40
41def get_ansible_output():
42 ansible_str = open(ANSIBLE_FILE).read()
43 return json.loads(ansible_str)
44
45
46class TestRun(unittest.TestCase):
47 def setUp(self):
48 self.sys_path_save = sys.path
49 self.cwd_save = os.getcwd()
50
51 config = os.path.join(test_path, "test_config.yaml")
52 from xosconfig import Config
53
54 Config.clear()
55 Config.init(config, "synchronizer-config-schema.yaml")
56
Zack Williams5c2ea232019-01-30 15:23:01 -070057 from xossynchronizer.mock_modelaccessor_build import build_mock_modelaccessor
58
59 build_mock_modelaccessor(
60 sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]
Scott Bakerbba67b62019-01-28 17:38:21 -080061 )
62
Zack Williams5c2ea232019-01-30 15:23:01 -070063 os.chdir(
64 os.path.join(test_path, "..")
65 ) # config references xos-synchronizer-tests/model-deps
Scott Bakerbba67b62019-01-28 17:38:21 -080066
67 import xossynchronizer.event_loop
68
69 reload(xossynchronizer.event_loop)
70 import xossynchronizer.backend
71
72 reload(xossynchronizer.backend)
73 from xossynchronizer.modelaccessor import model_accessor
74
75 # import all class names to globals
76 for (k, v) in model_accessor.all_model_classes.items():
77 globals()[k] = v
78
Scott Bakerc2fddaa2019-01-30 15:45:03 -080079 from xossynchronizer.modelaccessor import model_accessor
80
81 b = xossynchronizer.backend.Backend(model_accessor=model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080082 steps_dir = Config.get("steps_dir")
83 self.steps = b.load_sync_step_modules(steps_dir)
Zack Williams5c2ea232019-01-30 15:23:01 -070084 self.synchronizer = xossynchronizer.event_loop.XOSObserver(
85 self.steps, model_accessor
86 )
Scott Bakerbba67b62019-01-28 17:38:21 -080087 try:
88 os.remove("/tmp/sync_ports")
89 except OSError:
90 pass
91 try:
92 os.remove("/tmp/delete_ports")
93 except OSError:
94 pass
95
96 def tearDown(self):
97 sys.path = self.sys_path_save
98 os.chdir(self.cwd_save)
99
Scott Baker4839dec2019-02-27 16:50:37 -0800100 def test_run_once(self):
Scott Bakerbba67b62019-01-28 17:38:21 -0800101 pending_objects, pending_steps = self.synchronizer.fetch_pending()
102 pending_objects2 = list(pending_objects)
103
Scott Bakerbba67b62019-01-28 17:38:21 -0800104 slice = Slice()
Scott Bakerbba67b62019-01-28 17:38:21 -0800105
106 self.synchronizer.run_once()
107
108 sync_ports = open("/tmp/sync_ports").read()
109 delete_ports = open("/tmp/delete_ports").read()
110
111 self.assertIn("successful", sync_ports)
112 self.assertIn("successful", delete_ports)
113
114
115if __name__ == "__main__":
116 unittest.main()