1 ##############################################################################
2 # Copyright (c) 2016 Kanglin Yin 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 ##############################################################################
10 # Unittest for yardstick.benchmark.scenarios.availability.utils
15 from yardstick.benchmark.scenarios.availability import util
18 class ExecuteShellTestCase(unittest.TestCase):
21 self.param_config = {'serviceName': '@serviceName', 'value': 1}
22 self.intermediate_variables = {'@serviceName': 'nova-api'}
23 self.std_output = '| id | 1 |'
24 self.cmd_config = {'cmd': 'ls', 'param': '-a'}
26 self._mock_subprocess = mock.patch.object(util, 'subprocess')
27 self.mock_subprocess = self._mock_subprocess.start()
28 self.addCleanup(self._stop_mock)
31 self._mock_subprocess.stop()
33 def test_util_build_command_shell(self):
34 result = util.build_shell_command(self.param_config, True,
35 self.intermediate_variables)
36 self.assertIn("nova-api", result)
38 def test_read_stdout_item(self):
39 result = util.read_stdout_item(self.std_output, 'id')
40 self.assertEqual('1', result)
42 def test_buildshellparams(self):
43 result = util.buildshellparams(self.cmd_config, True)
44 self.assertEqual('/bin/bash -s {0} {1}', result)
46 def test__fun_execute_shell_command_successful(self):
48 self.mock_subprocess.check_output.return_value = (0, 'unittest')
49 exitcode, _ = util.execute_shell_command(cmd)
50 self.assertEqual(exitcode, 0)
52 def test__fun_execute_shell_command_fail_cmd_exception(self):
54 self.mock_subprocess.check_output.side_effect = RuntimeError
55 exitcode, _ = util.execute_shell_command(cmd)
56 self.assertEqual(exitcode, -1)