1 ##############################################################################
2 # Copyright (c) 2015 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 ##############################################################################
13 from yardstick.benchmark.scenarios.availability.monitor import monitor_command
16 class ExecuteShellTestCase(unittest.TestCase):
19 self._mock_subprocess = mock.patch.object(monitor_command, 'subprocess')
20 self.mock_subprocess = self._mock_subprocess.start()
21 self.addCleanup(self._stop_mock)
24 self._mock_subprocess.stop()
26 def test__fun_execute_shell_command_successful(self):
28 self.mock_subprocess.check_output.return_value = (0, 'unittest')
29 exitcode, _t = monitor_command._execute_shell_command(cmd)
30 self.assertEqual(exitcode, 0)
32 @mock.patch.object(monitor_command, 'LOG')
33 def test__fun_execute_shell_command_fail_cmd_exception(self, mock_log):
35 self.mock_subprocess.check_output.side_effect = RuntimeError
36 exitcode, _ = monitor_command._execute_shell_command(cmd)
37 self.assertEqual(exitcode, -1)
38 mock_log.error.assert_called_once()
41 class MonitorOpenstackCmdTestCase(unittest.TestCase):
47 "key_filename": "/root/.ssh/id_rsa"
49 self.context = {"node1": host}
51 'monitor_type': 'openstack-api',
52 'command_name': 'nova image-list',
54 'sla': {'max_outage_time': 5}
56 self._mock_subprocess = mock.patch.object(monitor_command, 'subprocess')
57 self.mock_subprocess = self._mock_subprocess.start()
58 self.addCleanup(self._stop_mock)
61 self._mock_subprocess.stop()
63 def test__monitor_command_monitor_func_successful(self):
65 instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
67 self.mock_subprocess.check_output.return_value = (0, 'unittest')
68 ret = instance.monitor_func()
70 instance._result = {"outage_time": 0}
73 @mock.patch.object(monitor_command, 'LOG')
74 def test__monitor_command_monitor_func_failure(self, mock_log):
75 self.mock_subprocess.check_output.return_value = (1, 'unittest')
76 instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
78 self.mock_subprocess.check_output.side_effect = RuntimeError
79 ret = instance.monitor_func()
81 mock_log.error.assert_called_once()
82 instance._result = {"outage_time": 10}
85 @mock.patch.object(monitor_command, 'ssh')
86 def test__monitor_command_ssh_monitor_successful(self, mock_ssh):
88 self.mock_subprocess.check_output.return_value = (0, 'unittest')
89 self.config["host"] = "node1"
90 instance = monitor_command.MonitorOpenstackCmd(
91 self.config, self.context, {"nova-api": 10})
93 mock_ssh.SSH.from_node().execute.return_value = (0, "0", '')
94 ret = instance.monitor_func()