1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
15 from yardstick.benchmark.core import task
16 from yardstick.common import constants as consts
19 class TaskTestCase(unittest.TestCase):
21 @mock.patch.object(task, 'Context')
22 def test_parse_nodes_with_context_same_context(self, mock_context):
32 "key_filename": "/root/.ssh/id_rsa"
34 mock_context.get_server.return_value = server_info
36 context_cfg = task.parse_nodes_with_context(scenario_cfg)
38 self.assertEqual(context_cfg["host"], server_info)
39 self.assertEqual(context_cfg["target"], server_info)
41 def test_set_dispatchers(self):
43 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
44 t._set_dispatchers(output_config)
45 self.assertEqual(output_config, output_config)
47 @mock.patch.object(task, 'DispatcherBase')
48 def test__do_output(self, mock_dispatcher):
50 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
52 dispatcher1 = mock.MagicMock()
53 dispatcher1.__dispatcher_type__ = 'file'
55 dispatcher2 = mock.MagicMock()
56 dispatcher2.__dispatcher_type__ = 'http'
58 mock_dispatcher.get = mock.MagicMock(return_value=[dispatcher1,
60 self.assertEqual(None, t._do_output(output_config, {}))
62 @mock.patch.object(task, 'Context')
63 def test_parse_networks_from_nodes(self, mock_context):
68 'network_name': 'mgmt',
71 'network_name': 'uplink_0',
74 'network_name': 'downlink_0',
81 'network_name': 'mgmt',
84 'network_name': 'uplink_0',
87 'network_name': 'downlink_0',
93 mock_context.get_network.side_effect = iter([
97 'network_type': 'flat',
102 'subnet_cidr': '10.20.0.0/16',
105 'name': 'downlink_0',
106 'segmentation_id': '1001',
113 # one for each interface
114 expected_get_network_calls = 6
116 'mgmt': {'name': 'mgmt', 'network_type': 'flat'},
117 'uplink_0': {'name': 'uplink_0', 'subnet_cidr': '10.20.0.0/16'},
118 'uplink_1': {'name': 'uplink_1'},
119 'downlink_0': {'name': 'downlink_0', 'segmentation_id': '1001'},
122 networks = task.get_networks_from_nodes(nodes)
123 self.assertEqual(mock_context.get_network.call_count, expected_get_network_calls)
124 self.assertDictEqual(networks, expected)
126 @mock.patch.object(task, 'Context')
127 @mock.patch.object(task, 'base_runner')
128 def test_run(self, mock_base_runner, *args):
130 'host': 'athena.demo',
131 'target': 'ares.demo',
142 runner.join.return_value = 0
143 runner.get_output.return_value = {}
144 runner.get_result.return_value = []
145 mock_base_runner.Runner.get.return_value = runner
146 t._run([scenario], False, "yardstick.out")
147 self.assertTrue(runner.run.called)
149 @mock.patch.object(os, 'environ')
150 def test_check_precondition(self, mock_os_environ):
153 'installer_type': 'compass',
154 'deploy_scenarios': 'os-nosdn',
155 'pod_name': 'huawei-pod1'
159 t = task.TaskParser('/opt')
160 mock_os_environ.get.side_effect = ['compass',
163 result = t._check_precondition(cfg)
164 self.assertTrue(result)
166 def test_parse_suite_no_constraint_no_args(self):
167 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
168 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
169 with mock.patch.object(os, 'environ',
170 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
171 task_files, task_args, task_args_fnames = t.parse_suite()
173 self.assertEqual(task_files[0], self.change_to_abspath(
174 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
175 self.assertEqual(task_files[1], self.change_to_abspath(
176 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
178 self.assertIsNone(task_args[0])
179 self.assertIsNone(task_args[1])
180 self.assertIsNone(task_args_fnames[0])
181 self.assertIsNone(task_args_fnames[1])
183 def test_parse_suite_no_constraint_with_args(self):
184 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
185 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
186 with mock.patch.object(os, 'environ',
187 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
188 task_files, task_args, task_args_fnames = t.parse_suite()
190 self.assertEqual(task_files[0], self.change_to_abspath(
191 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
192 self.assertEqual(task_files[1], self.change_to_abspath(
193 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
194 self.assertIsNone(task_args[0])
195 self.assertEqual(task_args[1],
196 '{"host": "node1.LF","target": "node2.LF"}')
197 self.assertIsNone(task_args_fnames[0])
198 self.assertIsNone(task_args_fnames[1])
200 def test_parse_suite_with_constraint_no_args(self):
201 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
202 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
203 with mock.patch.object(os, 'environ',
204 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
205 task_files, task_args, task_args_fnames = t.parse_suite()
206 self.assertEqual(task_files[0], self.change_to_abspath(
207 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
208 self.assertEqual(task_files[1], self.change_to_abspath(
209 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
210 self.assertIsNone(task_args[0])
211 self.assertIsNone(task_args[1])
212 self.assertIsNone(task_args_fnames[0])
213 self.assertIsNone(task_args_fnames[1])
215 def test_parse_suite_with_constraint_with_args(self):
216 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
217 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
218 with mock.patch('os.environ',
219 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
220 task_files, task_args, task_args_fnames = t.parse_suite()
222 self.assertEqual(task_files[0], self.change_to_abspath(
223 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
224 self.assertEqual(task_files[1], self.change_to_abspath(
225 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
226 self.assertIsNone(task_args[0])
227 self.assertEqual(task_args[1],
228 '{"host": "node1.LF","target": "node2.LF"}')
229 self.assertIsNone(task_args_fnames[0])
230 self.assertIsNone(task_args_fnames[1])
232 def test_parse_options(self):
235 'EXTERNAL_NETWORK': '$network'
237 'nodes': ['node1', '$node'],
243 'network': 'ext-net',
245 'host': 'server.yardstick'
250 'EXTERNAL_NETWORK': 'ext-net'
252 'nodes': ['node1', 'node2'],
253 'host': 'server.yardstick'
256 actual_result = t._parse_options(options)
257 self.assertEqual(expected_result, actual_result)
260 def test_change_server_name_host_str(self):
261 scenario = {'host': 'demo'}
263 task.change_server_name(scenario, suffix)
264 self.assertEqual('demo-8', scenario['host'])
266 def test_change_server_name_host_dict(self):
267 scenario = {'host': {'name': 'demo'}}
269 task.change_server_name(scenario, suffix)
270 self.assertEqual('demo-8', scenario['host']['name'])
272 def test_change_server_name_target_str(self):
273 scenario = {'target': 'demo'}
275 task.change_server_name(scenario, suffix)
276 self.assertEqual('demo-8', scenario['target'])
278 def test_change_server_name_target_dict(self):
279 scenario = {'target': {'name': 'demo'}}
281 task.change_server_name(scenario, suffix)
282 self.assertEqual('demo-8', scenario['target']['name'])
284 @mock.patch('six.moves.builtins.open', side_effect=mock.mock_open())
285 @mock.patch.object(task, 'utils')
286 @mock.patch('logging.root')
287 def test_set_log(self, mock_logging_root, *args):
288 task_obj = task.Task()
289 task_obj.task_id = 'task_id'
291 mock_logging_root.addHandler.assert_called()
293 def _get_file_abspath(self, filename):
294 curr_path = os.path.dirname(os.path.abspath(__file__))
295 file_path = os.path.join(curr_path, filename)
298 def change_to_abspath(self, filepath):
299 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
306 if __name__ == '__main__':