Merge "Add "securityContext" parameter in Kubernetes context"
[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 RunnerTestCase(ut_base.BaseUnitTestCase):
47
48     def setUp(self):
49         config = {
50             'output_config': {
51                 'DEFAULT': {
52                     'dispatcher': 'file'
53                 }
54             }
55         }
56         self.runner = iteration.IterationRunner(config)
57
58     @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
59     def test_get_output(self, *args):
60         self.runner.output_queue.put({'case': 'opnfv_yardstick_tc002'})
61         self.runner.output_queue.put({'criteria': 'PASS'})
62
63         idle_result = {
64             'case': 'opnfv_yardstick_tc002',
65             'criteria': 'PASS'
66         }
67
68         for _ in range(1000):
69             time.sleep(0.01)
70             if not self.runner.output_queue.empty():
71                 break
72         actual_result = self.runner.get_output()
73         self.assertEqual(idle_result, actual_result)
74
75     @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
76     def test_get_result(self, *args):
77         self.runner.result_queue.put({'case': 'opnfv_yardstick_tc002'})
78         self.runner.result_queue.put({'criteria': 'PASS'})
79
80         idle_result = [
81             {'case': 'opnfv_yardstick_tc002'},
82             {'criteria': 'PASS'}
83         ]
84
85         for _ in range(1000):
86             time.sleep(0.01)
87             if not self.runner.result_queue.empty():
88                 break
89         actual_result = self.runner.get_result()
90         self.assertEqual(idle_result, actual_result)
91
92     def test__run_benchmark(self):
93         runner = runner_base.Runner(mock.Mock())
94
95         with self.assertRaises(NotImplementedError):
96             runner._run_benchmark(mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock())