Merge "Add test case sample to enable mixed network"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / availability / test_util.py
1 ##############################################################################
2 # Copyright (c) 2016 Kanglin Yin and others
3 # 14_ykl@tongji.edu.cn
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 # Unittest for yardstick.benchmark.scenarios.availability.utils
11
12 import mock
13 import unittest
14
15 from yardstick.benchmark.scenarios.availability import util
16
17
18 class ExecuteShellTestCase(unittest.TestCase):
19
20     def setUp(self):
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'}
25
26         self._mock_subprocess = mock.patch.object(util, 'subprocess')
27         self.mock_subprocess = self._mock_subprocess.start()
28         self.addCleanup(self._stop_mock)
29
30     def _stop_mock(self):
31         self._mock_subprocess.stop()
32
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)
37
38     def test_read_stdout_item(self):
39         result = util.read_stdout_item(self.std_output, 'id')
40         self.assertEqual('1', result)
41
42     def test_buildshellparams(self):
43         result = util.buildshellparams(self.cmd_config, True)
44         self.assertEqual('/bin/bash -s {0} {1}', result)
45
46     def test__fun_execute_shell_command_successful(self):
47         cmd = "env"
48         self.mock_subprocess.check_output.return_value = (0, 'unittest')
49         exitcode, _ = util.execute_shell_command(cmd)
50         self.assertEqual(exitcode, 0)
51
52     def test__fun_execute_shell_command_fail_cmd_exception(self):
53         cmd = "env"
54         self.mock_subprocess.check_output.side_effect = RuntimeError
55         exitcode, _ = util.execute_shell_command(cmd)
56         self.assertEqual(exitcode, -1)