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 ##############################################################################
16 from yardstick.benchmark.contexts import dummy
17 from yardstick.benchmark.core import task
18 from yardstick.common import constants as consts
19 from yardstick.common import exceptions
22 class TaskTestCase(unittest.TestCase):
24 @mock.patch.object(task, 'Context')
25 def test_parse_nodes_with_context_same_context(self, mock_context):
35 "key_filename": "/root/.ssh/id_rsa"
37 mock_context.get_server.return_value = server_info
39 context_cfg = task.parse_nodes_with_context(scenario_cfg)
41 self.assertEqual(context_cfg["host"], server_info)
42 self.assertEqual(context_cfg["target"], server_info)
44 def test_set_dispatchers(self):
46 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
47 t._set_dispatchers(output_config)
48 self.assertEqual(output_config, output_config)
50 @mock.patch.object(task, 'DispatcherBase')
51 def test__do_output(self, mock_dispatcher):
53 output_config = {"DEFAULT": {"dispatcher": "file, http"}}
54 mock_dispatcher.get = mock.MagicMock(return_value=[mock.MagicMock(),
56 self.assertEqual(None, t._do_output(output_config, {}))
58 @mock.patch.object(task, 'Context')
59 def test_parse_networks_from_nodes(self, mock_context):
64 'network_name': 'mgmt',
67 'network_name': 'uplink_0',
70 'network_name': 'downlink_0',
77 'network_name': 'mgmt',
80 'network_name': 'uplink_0',
83 'network_name': 'downlink_0',
89 mock_context.get_network.side_effect = iter([
93 'network_type': 'flat',
98 'subnet_cidr': '10.20.0.0/16',
101 'name': 'downlink_0',
102 'segmentation_id': '1001',
109 # one for each interface
110 expected_get_network_calls = 6
112 'mgmt': {'name': 'mgmt', 'network_type': 'flat'},
113 'uplink_0': {'name': 'uplink_0', 'subnet_cidr': '10.20.0.0/16'},
114 'uplink_1': {'name': 'uplink_1'},
115 'downlink_0': {'name': 'downlink_0', 'segmentation_id': '1001'},
118 networks = task.get_networks_from_nodes(nodes)
119 self.assertEqual(mock_context.get_network.call_count, expected_get_network_calls)
120 self.assertDictEqual(networks, expected)
122 @mock.patch.object(task, 'Context')
123 @mock.patch.object(task, 'base_runner')
124 def test_run(self, mock_base_runner, *args):
126 'host': 'athena.demo',
127 'target': 'ares.demo',
138 runner.join.return_value = 0
139 runner.get_output.return_value = {}
140 runner.get_result.return_value = []
141 mock_base_runner.Runner.get.return_value = runner
142 t._run([scenario], False, "yardstick.out")
143 self.assertTrue(runner.run.called)
145 @mock.patch.object(os, 'environ')
146 def test_check_precondition(self, mock_os_environ):
149 'installer_type': 'compass',
150 'deploy_scenarios': 'os-nosdn',
151 'pod_name': 'huawei-pod1'
155 t = task.TaskParser('/opt')
156 mock_os_environ.get.side_effect = ['compass',
159 result = t._check_precondition(cfg)
160 self.assertTrue(result)
162 def test_parse_suite_no_constraint_no_args(self):
163 SAMPLE_SCENARIO_PATH = "no_constraint_no_args_scenario_sample.yaml"
164 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
165 with mock.patch.object(os, 'environ',
166 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
167 task_files, task_args, task_args_fnames = t.parse_suite()
169 self.assertEqual(task_files[0], self.change_to_abspath(
170 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
171 self.assertEqual(task_files[1], self.change_to_abspath(
172 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
174 self.assertIsNone(task_args[0])
175 self.assertIsNone(task_args[1])
176 self.assertIsNone(task_args_fnames[0])
177 self.assertIsNone(task_args_fnames[1])
179 def test_parse_suite_no_constraint_with_args(self):
180 SAMPLE_SCENARIO_PATH = "no_constraint_with_args_scenario_sample.yaml"
181 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
182 with mock.patch.object(os, 'environ',
183 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
184 task_files, task_args, task_args_fnames = t.parse_suite()
186 self.assertEqual(task_files[0], self.change_to_abspath(
187 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
188 self.assertEqual(task_files[1], self.change_to_abspath(
189 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
190 self.assertIsNone(task_args[0])
191 self.assertEqual(task_args[1],
192 '{"host": "node1.LF","target": "node2.LF"}')
193 self.assertIsNone(task_args_fnames[0])
194 self.assertIsNone(task_args_fnames[1])
196 def test_parse_suite_with_constraint_no_args(self):
197 SAMPLE_SCENARIO_PATH = "with_constraint_no_args_scenario_sample.yaml"
198 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
199 with mock.patch.object(os, 'environ',
200 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
201 task_files, task_args, task_args_fnames = t.parse_suite()
202 self.assertEqual(task_files[0], self.change_to_abspath(
203 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
204 self.assertEqual(task_files[1], self.change_to_abspath(
205 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
206 self.assertIsNone(task_args[0])
207 self.assertIsNone(task_args[1])
208 self.assertIsNone(task_args_fnames[0])
209 self.assertIsNone(task_args_fnames[1])
211 def test_parse_suite_with_constraint_with_args(self):
212 SAMPLE_SCENARIO_PATH = "with_constraint_with_args_scenario_sample.yaml"
213 t = task.TaskParser(self._get_file_abspath(SAMPLE_SCENARIO_PATH))
214 with mock.patch('os.environ',
215 new={'NODE_NAME': 'huawei-pod1', 'INSTALLER_TYPE': 'compass'}):
216 task_files, task_args, task_args_fnames = t.parse_suite()
218 self.assertEqual(task_files[0], self.change_to_abspath(
219 'tests/opnfv/test_cases/opnfv_yardstick_tc037.yaml'))
220 self.assertEqual(task_files[1], self.change_to_abspath(
221 'tests/opnfv/test_cases/opnfv_yardstick_tc043.yaml'))
222 self.assertIsNone(task_args[0])
223 self.assertEqual(task_args[1],
224 '{"host": "node1.LF","target": "node2.LF"}')
225 self.assertIsNone(task_args_fnames[0])
226 self.assertIsNone(task_args_fnames[1])
228 def test_parse_options(self):
231 'EXTERNAL_NETWORK': '$network'
233 'nodes': ['node1', '$node'],
239 'network': 'ext-net',
241 'host': 'server.yardstick'
246 'EXTERNAL_NETWORK': 'ext-net'
248 'nodes': ['node1', 'node2'],
249 'host': 'server.yardstick'
252 actual_result = t._parse_options(options)
253 self.assertEqual(expected_result, actual_result)
255 @mock.patch('six.moves.builtins.open', side_effect=mock.mock_open())
256 @mock.patch.object(task, 'utils')
257 @mock.patch('logging.root')
258 def test_set_log(self, mock_logging_root, *args):
259 task_obj = task.Task()
260 task_obj.task_id = 'task_id'
262 mock_logging_root.addHandler.assert_called()
264 def _get_file_abspath(self, filename):
265 curr_path = os.path.dirname(os.path.abspath(__file__))
266 file_path = os.path.join(curr_path, filename)
269 def change_to_abspath(self, filepath):
270 return os.path.join(consts.YARDSTICK_ROOT_PATH, filepath)
273 class TaskParserTestCase(unittest.TestCase):
276 self.parser = task.TaskParser('fake/path')
278 'host': 'athena.demo',
279 'target': 'kratos.demo',
281 'ares.demo', 'mars.demo'
285 'host': 'jupiter.demo',
286 'target': 'saturn.demo',
290 'tg__0': 'tg_0.demo',
291 'vnf__0': 'vnf_0.demo',
295 def test__change_node_names(self):
299 'task_id': '1234567890',
308 my_context = dummy.DummyContext()
309 my_context.init(ctx_attrs)
311 expected_scenario = {
312 'host': 'athena.demo-12345678',
313 'target': 'kratos.demo-12345678',
315 'ares.demo-12345678', 'mars.demo-12345678'
319 'host': 'jupiter.demo-12345678',
320 'target': 'saturn.demo-12345678',
324 'tg__0': 'tg_0.demo-12345678',
325 'vnf__0': 'vnf_0.demo-12345678',
329 scenario = copy.deepcopy(self.scenario)
331 self.parser._change_node_names(scenario, [my_context])
332 self.assertEqual(scenario, expected_scenario)
334 def test__change_node_names_context_not_found(self):
335 scenario = copy.deepcopy(self.scenario)
336 self.assertRaises(exceptions.ScenarioConfigContextNameNotFound,
337 self.parser._change_node_names,
340 def test__change_node_names_context_name_unchanged(self):
343 'task_id': '1234567890',
350 my_context = dummy.DummyContext()
351 my_context.init(ctx_attrs)
353 scenario = copy.deepcopy(self.scenario)
354 expected_scenario = copy.deepcopy(self.scenario)
356 self.parser._change_node_names(scenario, [my_context])
357 self.assertEqual(scenario, expected_scenario)