Merge "Add UT: DurationRunner worker normal operation"
[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 import uuid
12
13 import mock
14 from oslo_config import cfg
15 import oslo_messaging
16 import subprocess
17
18 from yardstick.benchmark.runners import base as runner_base
19 from yardstick.benchmark.runners import iteration
20 from yardstick.common import messaging
21 from yardstick.common.messaging import payloads
22 from yardstick.tests.unit import base as ut_base
23
24
25 class ActionTestCase(ut_base.BaseUnitTestCase):
26
27     def setUp(self):
28         self._mock_log = mock.patch.object(runner_base.log, 'error')
29         self.mock_log = self._mock_log.start()
30         self.addCleanup(self._stop_mocks)
31
32     def _stop_mocks(self):
33         self._mock_log.stop()
34
35     @mock.patch.object(subprocess, 'check_output')
36     def test__execute_shell_command(self, mock_subprocess):
37         mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
38         self.assertEqual(runner_base._execute_shell_command("")[0], -1)
39
40     @mock.patch.object(subprocess, 'check_output')
41     def test__single_action(self, mock_subprocess):
42         mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
43         runner_base._single_action(0, 'echo', mock.Mock())
44
45     @mock.patch.object(subprocess, 'check_output')
46     def test__periodic_action(self, mock_subprocess):
47         mock_subprocess.side_effect = subprocess.CalledProcessError(-1, '')
48         runner_base._periodic_action(0, 'echo', mock.Mock())
49
50
51 class RunnerTestCase(ut_base.BaseUnitTestCase):
52
53     def setUp(self):
54         config = {
55             'output_config': {
56                 'DEFAULT': {
57                     'dispatcher': 'file'
58                 }
59             }
60         }
61         self.runner = iteration.IterationRunner(config)
62
63     @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
64     def test_get_output(self, *args):
65         self.runner.output_queue.put({'case': 'opnfv_yardstick_tc002'})
66         self.runner.output_queue.put({'criteria': 'PASS'})
67
68         idle_result = {
69             'case': 'opnfv_yardstick_tc002',
70             'criteria': 'PASS'
71         }
72
73         for _ in range(1000):
74             time.sleep(0.01)
75             if not self.runner.output_queue.empty():
76                 break
77         actual_result = self.runner.get_output()
78         self.assertEqual(idle_result, actual_result)
79
80     @mock.patch("yardstick.benchmark.runners.iteration.multiprocessing")
81     def test_get_result(self, *args):
82         self.runner.result_queue.put({'case': 'opnfv_yardstick_tc002'})
83         self.runner.result_queue.put({'criteria': 'PASS'})
84
85         idle_result = [
86             {'case': 'opnfv_yardstick_tc002'},
87             {'criteria': 'PASS'}
88         ]
89
90         for _ in range(1000):
91             time.sleep(0.01)
92             if not self.runner.result_queue.empty():
93                 break
94         actual_result = self.runner.get_result()
95         self.assertEqual(idle_result, actual_result)
96
97     def test__run_benchmark(self):
98         runner = runner_base.Runner(mock.Mock())
99
100         with self.assertRaises(NotImplementedError):
101             runner._run_benchmark(mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock())
102
103
104 class RunnerProducerTestCase(ut_base.BaseUnitTestCase):
105
106     @mock.patch.object(oslo_messaging, 'Target', return_value='rpc_target')
107     @mock.patch.object(oslo_messaging, 'RPCClient')
108     @mock.patch.object(oslo_messaging, 'get_rpc_transport',
109                        return_value='rpc_transport')
110     @mock.patch.object(cfg, 'CONF')
111     def test__init(self, mock_config, mock_transport, mock_rpcclient,
112                    mock_target):
113         _id = uuid.uuid1().int
114         runner_producer = runner_base.RunnerProducer(_id)
115         mock_transport.assert_called_once_with(
116             mock_config, url='rabbit://yardstick:yardstick@localhost:5672/')
117         mock_target.assert_called_once_with(topic=messaging.TOPIC_RUNNER,
118                                             fanout=True,
119                                             server=messaging.SERVER)
120         mock_rpcclient.assert_called_once_with('rpc_transport', 'rpc_target')
121         self.assertEqual(_id, runner_producer._id)
122         self.assertEqual(messaging.TOPIC_RUNNER, runner_producer._topic)
123
124     @mock.patch.object(oslo_messaging, 'Target', return_value='rpc_target')
125     @mock.patch.object(oslo_messaging, 'RPCClient')
126     @mock.patch.object(oslo_messaging, 'get_rpc_transport',
127                        return_value='rpc_transport')
128     @mock.patch.object(payloads, 'RunnerPayload', return_value='runner_pload')
129     def test_start_iteration(self, mock_runner_payload, *args):
130         runner_producer = runner_base.RunnerProducer(uuid.uuid1().int)
131         with mock.patch.object(runner_producer,
132                                'send_message') as mock_message:
133             runner_producer.start_iteration(version=10)
134
135         mock_message.assert_called_once_with(
136             messaging.RUNNER_METHOD_START_ITERATION, 'runner_pload')
137         mock_runner_payload.assert_called_once_with(version=10, data={})
138
139     @mock.patch.object(oslo_messaging, 'Target', return_value='rpc_target')
140     @mock.patch.object(oslo_messaging, 'RPCClient')
141     @mock.patch.object(oslo_messaging, 'get_rpc_transport',
142                        return_value='rpc_transport')
143     @mock.patch.object(payloads, 'RunnerPayload', return_value='runner_pload')
144     def test_stop_iteration(self, mock_runner_payload, *args):
145         runner_producer = runner_base.RunnerProducer(uuid.uuid1().int)
146         with mock.patch.object(runner_producer,
147                                'send_message') as mock_message:
148             runner_producer.stop_iteration(version=15)
149
150         mock_message.assert_called_once_with(
151             messaging.RUNNER_METHOD_STOP_ITERATION, 'runner_pload')
152         mock_runner_payload.assert_called_once_with(version=15, data={})