3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # Unittest for yardstick.benchmark.core.task
14 from __future__ import print_function
16 from __future__ import absolute_import
21 from unittest import mock
26 from yardstick.benchmark.core import task
27 from yardstick.common import constants as consts
30 # pylint: disable=unused-argument
31 # disable this for now because I keep forgetting mock patch arg ordering
34 class TaskTestCase(unittest.TestCase):
36 @mock.patch('yardstick.benchmark.core.task.Context')
37 def test_parse_nodes_host_target_same_context(self, mock_context):
42 scenario_cfg = {"nodes": nodes}
46 "key_filename": "/root/.ssh/id_rsa"
48 mock_context.get_server.return_value = server_info
49 context_cfg = task.parse_nodes_with_context(scenario_cfg)
51 self.assertEqual(context_cfg["host"], server_info)
52 self.assertEqual(context_cfg["target"], server_info)
54 def test_set_dispatchers(self):
56 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
57 t._set_dispatchers(output_config)
58 self.assertEqual(output_config, output_config)
60 @mock.patch('yardstick.benchmark.core.task.DispatcherBase')
61 def test__do_output(self, mock_dispatcher):
63 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
64 mock_dispatcher.get = mock.MagicMock(return_value=[mock.MagicMock(),
66 self.assertEqual(None, t._do_output(output_config, {}))
68 @mock.patch('yardstick.benchmark.core.task.Context')
69 def test_parse_networks_from_nodes(self, mock_context):
74 'network_name': 'mgmt',
77 'network_name': 'uplink_0',
80 'network_name': 'downlink_0',
87 'network_name': 'mgmt',
90 'network_name': 'uplink_0',
93 'network_name': 'downlink_0',
99 mock_context.get_network.side_effect = iter([
103 'network_type': 'flat',
108 'subnet_cidr': '10.20.0.0/16',
111 'name': 'downlink_0',
112 'segmentation_id': '1001',
119 # one for each interface
120 expected_get_network_calls = 6
122 'mgmt': {'name': 'mgmt', 'network_type': 'flat'},
123 'uplink_0': {'name': 'uplink_0', 'subnet_cidr': '10.20.0.0/16'},
124 'uplink_1': {'name': 'uplink_1'},
125 'downlink_0': {'name': 'downlink_0', 'segmentation_id': '1001'},
128 networks = task.get_networks_from_nodes(nodes)
129 self.assertEqual(mock_context.get_network.call_count, expected_get_network_calls)
130 self.assertDictEqual(networks, expected)
132 @mock.patch('yardstick.benchmark.core.task.Context')
133 @mock.patch('yardstick.benchmark.core.task.base_runner')
134 def test_run(self, mock_base_runner, mock_ctx):
136 'host': 'athena.demo',
137 'target': 'ares.demo',
148 runner.join.return_value = 0
149 runner.get_output.return_value = {}
150 runner.get_result.return_value = []
151 mock_base_runner.Runner.get.return_value = runner
152 t._run([scenario], False, "yardstick.out")
153 self.assertTrue(runner.run.called)
155 @mock.patch('yardstick.benchmark.core.task.os')
156 def test_check_precondition(self, mock_os):
159 'installer_type': 'compass',
160 'deploy_scenarios': 'os-nosdn',
161 'pod_name': 'huawei-pod1'
165 t = task.TaskParser('/opt')
166 mock_os.environ.get.side_effect = ['compass',
169 result = t._check_precondition(cfg)
170 self.assertTrue(result)
172 def test_parse_suite_no_constraint_no_args(self):
173 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
174 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
175 with mock.patch('yardstick.benchmark.core.task.os.environ',
176 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
177 task_files, task_args, task_args_fnames = t.parse_suite()
178 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
180 self.assertEqual(task_files[0], self.change_to_abspath(
181 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
182 self.assertEqual(task_files[1], self.change_to_abspath(
183 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
184 self.assertEqual(task_args[0], None)
185 self.assertEqual(task_args[1], None)
186 self.assertEqual(task_args_fnames[0], None)
187 self.assertEqual(task_args_fnames[1], None)
189 @mock.patch('yardstick.benchmark.core.task.os.environ')
190 def test_parse_suite_no_constraint_with_args(self, mock_environ):
191 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
192 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
193 with mock.patch('yardstick.benchmark.core.task.os.environ',
194 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
195 task_files, task_args, task_args_fnames = t.parse_suite()
196 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
198 self.assertEqual(task_files[0], self.change_to_abspath(
199 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
200 self.assertEqual(task_files[1], self.change_to_abspath(
201 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
202 self.assertEqual(task_args[0], None)
203 self.assertEqual(task_args[1],
204 '{"host": "node1.LF","target": "node2.LF"}')
205 self.assertEqual(task_args_fnames[0], None)
206 self.assertEqual(task_args_fnames[1], None)
208 @mock.patch('yardstick.benchmark.core.task.os.environ')
209 def test_parse_suite_with_constraint_no_args(self, mock_environ):
210 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
211 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
212 with mock.patch('yardstick.benchmark.core.task.os.environ',
213 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
214 task_files, task_args, task_args_fnames = t.parse_suite()
215 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
217 self.assertEqual(task_files[0], self.change_to_abspath(
218 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
219 self.assertEqual(task_files[1], self.change_to_abspath(
220 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
221 self.assertEqual(task_args[0], None)
222 self.assertEqual(task_args[1], None)
223 self.assertEqual(task_args_fnames[0], None)
224 self.assertEqual(task_args_fnames[1], None)
226 @mock.patch('yardstick.benchmark.core.task.os.environ')
227 def test_parse_suite_with_constraint_with_args(self, mock_environ):
228 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
229 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
230 with mock.patch('yardstick.benchmark.core.task.os.environ',
231 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
232 task_files, task_args, task_args_fnames = t.parse_suite()
233 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
235 self.assertEqual(task_files[0], self.change_to_abspath(
236 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
237 self.assertEqual(task_files[1], self.change_to_abspath(
238 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
239 self.assertEqual(task_args[0], None)
240 self.assertEqual(task_args[1],
241 '{"host": "node1.LF","target": "node2.LF"}')
242 self.assertEqual(task_args_fnames[0], None)
243 self.assertEqual(task_args_fnames[1], None)
245 def test_parse_options(self):
248 'EXTERNAL_NETWORK': '$network'
250 'ndoes': ['node1', '$node'],
256 'network': 'ext-net',
258 'host': 'server.yardstick'
263 'EXTERNAL_NETWORK': 'ext-net'
265 'ndoes': ['node1', 'node2'],
266 'host': 'server.yardstick'
269 actual_result = t._parse_options(options)
270 self.assertEqual(idle_result, actual_result)
272 def test_change_server_name_host_str(self):
273 scenario = {'host': 'demo'}
275 task.change_server_name(scenario, suffix)
276 self.assertTrue(scenario['host'], 'demo-8')
278 def test_change_server_name_host_dict(self):
279 scenario = {'host': {'name': 'demo'}}
281 task.change_server_name(scenario, suffix)
282 self.assertTrue(scenario['host']['name'], 'demo-8')
284 def test_change_server_name_target_str(self):
285 scenario = {'target': 'demo'}
287 task.change_server_name(scenario, suffix)
288 self.assertTrue(scenario['target'], 'demo-8')
290 def test_change_server_name_target_dict(self):
291 scenario = {'target': {'name': 'demo'}}
293 task.change_server_name(scenario, suffix)
294 self.assertTrue(scenario['target']['name'], 'demo-8')
296 @mock.patch('yardstick.benchmark.core.task.utils')
297 @mock.patch('yardstick.benchmark.core.task.logging')
298 def test_set_log(self, mock_logging, mock_utils):
299 task_obj = task.Task()
300 task_obj.task_id = 'task_id'
302 self.assertTrue(mock_logging.root.addHandler.called)
304 def _get_file_abspath(self, filename):
305 curr_path = os.path.dirname(os.path.abspath(__file__))
306 file_path = os.path.join(curr_path, filename)
309 def change_to_abspath(self, filepath):
310 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
317 if __name__ == '__main__':