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 ##############################################################################
14 from oslo_config import cfg
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
25 class ActionTestCase(ut_base.BaseUnitTestCase):
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)
32 def _stop_mocks(self):
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)
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())
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())
51 class RunnerTestCase(ut_base.BaseUnitTestCase):
61 self.runner = iteration.IterationRunner(config)
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'})
69 'case': 'opnfv_yardstick_tc002',
75 if not self.runner.output_queue.empty():
77 actual_result = self.runner.get_output()
78 self.assertEqual(idle_result, actual_result)
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'})
86 {'case': 'opnfv_yardstick_tc002'},
92 if not self.runner.result_queue.empty():
94 actual_result = self.runner.get_result()
95 self.assertEqual(idle_result, actual_result)
97 def test__run_benchmark(self):
98 runner = runner_base.Runner(mock.Mock())
100 with self.assertRaises(NotImplementedError):
101 runner._run_benchmark(mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock())
104 class RunnerProducerTestCase(ut_base.BaseUnitTestCase):
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,
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,
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)
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)
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={})
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)
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={})