Use assertIn(x, y) instead of other variations
[yardstick.git] / tests / unit / benchmark / scenarios / availability / test_util.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Kanglin Yin and others
5 # 14_ykl@tongji.edu.cn
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 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.availability.utils
13
14 import mock
15 import unittest
16
17 from yardstick.benchmark.scenarios.availability import util
18
19 class ExecuteShellTestCase(unittest.TestCase):
20
21     def setUp(self):
22         self.param_config = {'serviceName': '@serviceName', 'value': 1}
23         self.intermediate_variables = {'@serviceName': 'nova-api'}
24         self.std_output = '| id       | 1                     |'
25         self.cmd_config = {'cmd':'ls', 'param':'-a'}
26
27         self._mock_subprocess = mock.patch.object(util, 'subprocess')
28         self.mock_subprocess = self._mock_subprocess.start()
29         self.addCleanup(self._stop_mock)
30
31     def _stop_mock(self):
32         self._mock_subprocess.stop()
33
34     def test_util_build_command_shell(self):
35         result = util.build_shell_command(self.param_config, True,
36                                           self.intermediate_variables)
37         self.assertIn("nova-api", result)
38
39     def test_read_stdout_item(self):
40         result = util.read_stdout_item(self.std_output, 'id')
41         self.assertEquals('1', result)
42
43     def test_buildshellparams(self):
44         result = util.buildshellparams(self.cmd_config, True)
45         self.assertEquals('/bin/bash -s {0} {1}', result)
46
47     def test__fun_execute_shell_command_successful(self):
48         cmd = "env"
49         self.mock_subprocess.check_output.return_value = (0, 'unittest')
50         exitcode, _ = util.execute_shell_command(cmd)
51         self.assertEqual(exitcode, 0)
52
53     def test__fun_execute_shell_command_fail_cmd_exception(self):
54         cmd = "env"
55         self.mock_subprocess.check_output.side_effect = RuntimeError
56         exitcode, _ = util.execute_shell_command(cmd)
57         self.assertEqual(exitcode, -1)