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 class TaskTestCase(unittest.TestCase):
32 @mock.patch('yardstick.benchmark.core.task.Context')
33 def test_parse_nodes_host_target_same_context(self, mock_context):
38 scenario_cfg = {"nodes": nodes}
42 "key_filename": "/root/.ssh/id_rsa"
44 mock_context.get_server.return_value = server_info
45 context_cfg = task.parse_nodes_with_context(scenario_cfg)
47 self.assertEqual(context_cfg["host"], server_info)
48 self.assertEqual(context_cfg["target"], server_info)
50 def test_set_dispatchers(self):
52 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
53 t._set_dispatchers(output_config)
54 self.assertEqual(output_config, output_config)
56 @mock.patch('yardstick.benchmark.core.task.DispatcherBase')
57 def test__do_output(self, mock_dispatcher):
59 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
60 mock_dispatcher.get = mock.MagicMock(return_value=[mock.MagicMock(),
62 self.assertEqual(None, t._do_output(output_config, {}))
64 @mock.patch('yardstick.benchmark.core.task.Context')
65 def test_parse_networks_from_nodes(self, mock_context):
99 mock_context.get_network.side_effect = iter([
103 'network_type': 'private',
109 'subnet_cidr': '10.20.0.0/16',
121 expected_get_network_calls = 4 # once for each vld_id in the nodes dict
123 'a': {'name': 'a', 'network_type': 'private'},
124 'b': {'name': 'b', 'vld_id': 'y', 'subnet_cidr': '10.20.0.0/16'},
127 networks = task.get_networks_from_nodes(nodes)
128 self.assertEqual(mock_context.get_network.call_count, expected_get_network_calls)
129 self.assertDictEqual(networks, expected)
131 @mock.patch('yardstick.benchmark.core.task.Context')
132 @mock.patch('yardstick.benchmark.core.task.base_runner')
133 def test_run(self, mock_base_runner, mock_ctx):
135 'host': 'athena.demo',
136 'target': 'ares.demo',
147 runner.join.return_value = 0
148 runner.get_output.return_value = {}
149 runner.get_result.return_value = []
150 mock_base_runner.Runner.get.return_value = runner
151 t._run([scenario], False, "yardstick.out")
152 self.assertTrue(runner.run.called)
154 @mock.patch('yardstick.benchmark.core.task.os')
155 def test_check_precondition(self, mock_os):
158 'installer_type': 'compass',
159 'deploy_scenarios': 'os-nosdn',
160 'pod_name': 'huawei-pod1'
164 t = task.TaskParser('/opt')
165 mock_os.environ.get.side_effect = ['compass',
168 result = t._check_precondition(cfg)
169 self.assertTrue(result)
171 @mock.patch('yardstick.benchmark.core.task.os.environ')
172 def test_parse_suite_no_constraint_no_args(self, mock_environ):
173 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
174 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
175 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
176 task_files, task_args, task_args_fnames = t.parse_suite()
177 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
179 self.assertEqual(task_files[0], self.change_to_abspath(
180 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
181 self.assertEqual(task_files[1], self.change_to_abspath(
182 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
183 self.assertEqual(task_args[0], None)
184 self.assertEqual(task_args[1], None)
185 self.assertEqual(task_args_fnames[0], None)
186 self.assertEqual(task_args_fnames[1], None)
188 @mock.patch('yardstick.benchmark.core.task.os.environ')
189 def test_parse_suite_no_constraint_with_args(self, mock_environ):
190 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
191 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
192 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
193 task_files, task_args, task_args_fnames = t.parse_suite()
194 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
196 self.assertEqual(task_files[0], self.change_to_abspath(
197 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
198 self.assertEqual(task_files[1], self.change_to_abspath(
199 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
200 self.assertEqual(task_args[0], None)
201 self.assertEqual(task_args[1],
202 '{"host": "node1.LF","target": "node2.LF"}')
203 self.assertEqual(task_args_fnames[0], None)
204 self.assertEqual(task_args_fnames[1], None)
206 @mock.patch('yardstick.benchmark.core.task.os.environ')
207 def test_parse_suite_with_constraint_no_args(self, mock_environ):
208 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
209 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
210 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
211 task_files, task_args, task_args_fnames = t.parse_suite()
212 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
214 self.assertEqual(task_files[0], self.change_to_abspath(
215 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
216 self.assertEqual(task_files[1], self.change_to_abspath(
217 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
218 self.assertEqual(task_args[0], None)
219 self.assertEqual(task_args[1], None)
220 self.assertEqual(task_args_fnames[0], None)
221 self.assertEqual(task_args_fnames[1], None)
223 @mock.patch('yardstick.benchmark.core.task.os.environ')
224 def test_parse_suite_with_constraint_with_args(self, mock_environ):
225 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
226 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
227 mock_environ.get.side_effect = ['huawei-pod1', 'compass']
228 task_files, task_args, task_args_fnames = t.parse_suite()
229 print("files=%s, args=%s, fnames=%s" % (task_files, task_args,
231 self.assertEqual(task_files[0], self.change_to_abspath(
232 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
233 self.assertEqual(task_files[1], self.change_to_abspath(
234 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
235 self.assertEqual(task_args[0], None)
236 self.assertEqual(task_args[1],
237 '{"host": "node1.LF","target": "node2.LF"}')
238 self.assertEqual(task_args_fnames[0], None)
239 self.assertEqual(task_args_fnames[1], None)
241 def test_parse_options(self):
244 'EXTERNAL_NETWORK': '$network'
246 'ndoes': ['node1', '$node'],
252 'network': 'ext-net',
254 'host': 'server.yardstick'
259 'EXTERNAL_NETWORK': 'ext-net'
261 'ndoes': ['node1', 'node2'],
262 'host': 'server.yardstick'
265 actual_result = t._parse_options(options)
266 self.assertEqual(idle_result, actual_result)
268 def test_change_server_name_host_str(self):
269 scenario = {'host': 'demo'}
271 task.change_server_name(scenario, suffix)
272 self.assertTrue(scenario['host'], 'demo-8')
274 def test_change_server_name_host_dict(self):
275 scenario = {'host': {'name': 'demo'}}
277 task.change_server_name(scenario, suffix)
278 self.assertTrue(scenario['host']['name'], 'demo-8')
280 def test_change_server_name_target_str(self):
281 scenario = {'target': 'demo'}
283 task.change_server_name(scenario, suffix)
284 self.assertTrue(scenario['target'], 'demo-8')
286 def test_change_server_name_target_dict(self):
287 scenario = {'target': {'name': 'demo'}}
289 task.change_server_name(scenario, suffix)
290 self.assertTrue(scenario['target']['name'], 'demo-8')
292 def _get_file_abspath(self, filename):
293 curr_path = os.path.dirname(os.path.abspath(__file__))
294 file_path = os.path.join(curr_path, filename)
297 def change_to_abspath(self, filepath):
298 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
305 if __name__ == '__main__':