3 ##############################################################################
4 # Copyright (c) 2016 Kanglin Yin and others
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # Unittest for yardstick.benchmark.scenarios.availability.utils
17 from yardstick.benchmark.scenarios.availability import util
20 class ExecuteShellTestCase(unittest.TestCase):
23 self.param_config = {'serviceName': '@serviceName', 'value': 1}
24 self.intermediate_variables = {'@serviceName': 'nova-api'}
25 self.std_output = '| id | 1 |'
26 self.cmd_config = {'cmd': 'ls', 'param': '-a'}
28 self._mock_subprocess = mock.patch.object(util, 'subprocess')
29 self.mock_subprocess = self._mock_subprocess.start()
30 self.addCleanup(self._stop_mock)
33 self._mock_subprocess.stop()
35 def test_util_build_command_shell(self):
36 result = util.build_shell_command(self.param_config, True,
37 self.intermediate_variables)
38 self.assertIn("nova-api", result)
40 def test_read_stdout_item(self):
41 result = util.read_stdout_item(self.std_output, 'id')
42 self.assertEqual('1', result)
44 def test_buildshellparams(self):
45 result = util.buildshellparams(self.cmd_config, True)
46 self.assertEqual('/bin/bash -s {0} {1}', result)
48 def test__fun_execute_shell_command_successful(self):
50 self.mock_subprocess.check_output.return_value = (0, 'unittest')
51 exitcode, _ = util.execute_shell_command(cmd)
52 self.assertEqual(exitcode, 0)
54 def test__fun_execute_shell_command_fail_cmd_exception(self):
56 self.mock_subprocess.check_output.side_effect = RuntimeError
57 exitcode, _ = util.execute_shell_command(cmd)
58 self.assertEqual(exitcode, -1)