1 ##############################################################################
2 # Copyright (c) 2017 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.runners import base as runner_base
16 from yardstick.benchmark.runners import iteration
17 from yardstick.tests.unit import base as ut_base
20 class ActionTestCase(ut_base.BaseUnitTestCase):
23 self._mock_log = mock.patch.object(runner_base.log, 'error')
24 self.mock_log = self._mock_log.start()
25 self.addCleanup(self._stop_mocks)
27 def _stop_mocks(self):
30 @mock.patch.object(subprocess, 'check_output')
31 def test__execute_shell_command(self, mock_subprocess):
32 mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
33 self.assertEqual(runner_base._execute_shell_command("")[0], -1)
35 @mock.patch.object(subprocess, 'check_output')
36 def test__single_action(self, mock_subprocess):
37 mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
38 runner_base._single_action(0, 'echo', mock.Mock())
40 @mock.patch.object(subprocess, 'check_output')
41 def test__periodic_action(self, mock_subprocess):
42 mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
43 runner_base._periodic_action(0, 'echo', mock.Mock())
46 class ScenarioOutputTestCase(ut_base.BaseUnitTestCase):
49 self.output_queue = mock.Mock()
50 self.scenario_output = runner_base.ScenarioOutput(self.output_queue,
53 @mock.patch.object(time, 'time')
54 def test_push(self, mock_time):
55 mock_time.return_value = 2
57 self.scenario_output.push(data)
58 self.output_queue.put.assert_called_once_with({'timestamp': 2,
60 'data': data}, True, 10)
62 def test_push_no_timestamp(self):
63 self.scenario_output["value1"] = 1
64 self.scenario_output.push(None, False)
65 self.output_queue.put.assert_called_once_with({'sequence': 1,
66 'value1': 1}, True, 10)
69 class RunnerTestCase(ut_base.BaseUnitTestCase):
79 self.runner = iteration.IterationRunner(config)
81 @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
82 def test_get_output(self, *args):
83 self.runner.output_queue.put({'case': 'opnfv_yardstick_tc002'})
84 self.runner.output_queue.put({'criteria': 'PASS'})
87 'case': 'opnfv_yardstick_tc002',
93 if not self.runner.output_queue.empty():
95 actual_result = self.runner.get_output()
96 self.assertEqual(idle_result, actual_result)
98 @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
99 def test_get_result(self, *args):
100 self.runner.result_queue.put({'case': 'opnfv_yardstick_tc002'})
101 self.runner.result_queue.put({'criteria': 'PASS'})
104 {'case': 'opnfv_yardstick_tc002'},
108 for _ in range(1000):
110 if not self.runner.result_queue.empty():
112 actual_result = self.runner.get_result()
113 self.assertEqual(idle_result, actual_result)
115 def test__run_benchmark(self):
116 runner = runner_base.Runner(mock.Mock())
118 with self.assertRaises(NotImplementedError):
119 runner._run_benchmark(mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock())