blob: f5815f2bbb7b9bc0a3cd09014b3b9a27fc4579dc [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 json
16import unittest
17from mock import patch
18import mock
19import pdb
20import networkx as nx
21
22import os
23import sys
24
25test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
26sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
27xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
28
29ANSIBLE_FILE = "/tmp/payload_test"
30
31
32def run_fake_ansible_template(*args, **kwargs):
33 opts = args[1]
34 open(ANSIBLE_FILE, "w").write(json.dumps(opts))
35
36
37def get_ansible_output():
38 ansible_str = open(ANSIBLE_FILE).read()
39 return json.loads(ansible_str)
40
41
42class TestRun(unittest.TestCase):
43 def setUp(self):
44 self.sys_path_save = sys.path
45 self.cwd_save = os.getcwd()
46
47 config = os.path.join(test_path, "test_config.yaml")
48 from xosconfig import Config
49
50 Config.clear()
51 Config.init(config, "synchronizer-config-schema.yaml")
52
53 from xossynchronizer.mock_modelaccessor_build import (
54 build_mock_modelaccessor,
55 )
56
57 build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[])
58
59 os.chdir(os.path.join(test_path, "..")) # config references tests/model-deps
60
61 import xossynchronizer.event_loop
62
63 reload(xossynchronizer.event_loop)
64 import xossynchronizer.backend
65
66 reload(xossynchronizer.backend)
67 from xossynchronizer.modelaccessor import model_accessor
68
69 # import all class names to globals
70 for (k, v) in model_accessor.all_model_classes.items():
71 globals()[k] = v
72
73 b = xossynchronizer.backend.Backend()
74 steps_dir = Config.get("steps_dir")
75 self.steps = b.load_sync_step_modules(steps_dir)
76 self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps)
77 try:
78 os.remove("/tmp/sync_ports")
79 except OSError:
80 pass
81 try:
82 os.remove("/tmp/delete_ports")
83 except OSError:
84 pass
85
86 def tearDown(self):
87 sys.path = self.sys_path_save
88 os.chdir(self.cwd_save)
89
90 @mock.patch(
91 "steps.sync_instances.syncstep.run_template",
92 side_effect=run_fake_ansible_template,
93 )
94 @mock.patch("xossynchronizer.event_loop.model_accessor")
95 def test_run_once(self, mock_run_template, mock_accessor, *_other_accessors):
96
97 pending_objects, pending_steps = self.synchronizer.fetch_pending()
98 pending_objects2 = list(pending_objects)
99
100 any_cs = next(
101 obj for obj in pending_objects if obj.leaf_model_name == "ControllerSlice"
102 )
103 any_instance = next(
104 obj for obj in pending_objects2 if obj.leaf_model_name == "Instance"
105 )
106
107 slice = Slice()
108 any_instance.slice = slice
109 any_cs.slice = slice
110
111 self.synchronizer.run_once()
112
113 sync_ports = open("/tmp/sync_ports").read()
114 delete_ports = open("/tmp/delete_ports").read()
115
116 self.assertIn("successful", sync_ports)
117 self.assertIn("successful", delete_ports)
118
119
120if __name__ == "__main__":
121 unittest.main()