Merge "Add new scenario NSPerf-RFC2544"
[yardstick.git] / yardstick / tests / unit / benchmark / runner / test_base.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 import time
11
12 import mock
13 import subprocess
14
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
18
19
20 class ActionTestCase(ut_base.BaseUnitTestCase):
21
22     def setUp(self):
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)
26
27     def _stop_mocks(self):
28         self._mock_log.stop()
29
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)
34
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())
39
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())
44
45
46 class ScenarioOutputTestCase(ut_base.BaseUnitTestCase):
47
48     def setUp(self):
49         self.output_queue = mock.Mock()
50         self.scenario_output = runner_base.ScenarioOutput(self.output_queue,
51                                                           sequence=1)
52
53     @mock.patch.object(time, 'time')
54     def test_push(self, mock_time):
55         mock_time.return_value = 2
56         data = {"value1": 1}
57         self.scenario_output.push(data)
58         self.output_queue.put.assert_called_once_with({'timestamp': 2,
59                                                        'sequence': 1,
60                                                        'data': data}, True, 10)
61
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)
67
68
69 class RunnerTestCase(ut_base.BaseUnitTestCase):
70
71     def setUp(self):
72         config = {
73             'output_config': {
74                 'DEFAULT': {
75                     'dispatcher': 'file'
76                 }
77             }
78         }
79         self.runner = iteration.IterationRunner(config)
80
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'})
85
86         idle_result = {
87             'case': 'opnfv_yardstick_tc002',
88             'criteria': 'PASS'
89         }
90
91         for _ in range(1000):
92             time.sleep(0.01)
93             if not self.runner.output_queue.empty():
94                 break
95         actual_result = self.runner.get_output()
96         self.assertEqual(idle_result, actual_result)
97
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'})
102
103         idle_result = [
104             {'case': 'opnfv_yardstick_tc002'},
105             {'criteria': 'PASS'}
106         ]
107
108         for _ in range(1000):
109             time.sleep(0.01)
110             if not self.runner.result_queue.empty():
111                 break
112         actual_result = self.runner.get_result()
113         self.assertEqual(idle_result, actual_result)
114
115     def test__run_benchmark(self):
116         runner = runner_base.Runner(mock.Mock())
117
118         with self.assertRaises(NotImplementedError):
119             runner._run_benchmark(mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock())