3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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.monitor.monitor_command
17 from yardstick.benchmark.scenarios.availability.monitor import monitor_command
19 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.subprocess')
20 class ExecuteShellTestCase(unittest.TestCase):
22 def test__fun_execute_shell_command_successful(self, mock_subprocess):
24 mock_subprocess.check_output.return_value = (0, 'unittest')
25 exitcode, output = monitor_command._execute_shell_command(cmd)
26 self.assertEqual(exitcode, 0)
28 def test__fun_execute_shell_command_fail_cmd_exception(self, mock_subprocess):
30 mock_subprocess.check_output.side_effect = RuntimeError
31 exitcode, output = monitor_command._execute_shell_command(cmd)
32 self.assertEqual(exitcode, -1)
34 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.subprocess')
35 class MonitorOpenstackCmdTestCase(unittest.TestCase):
41 "key_filename": "/root/.ssh/id_rsa"
43 self.context = {"node1": host}
45 'monitor_type': 'openstack-api',
46 'command_name': 'nova image-list',
48 'sla': {'max_outage_time': 5}
52 def test__monitor_command_monitor_func_successful(self, mock_subprocess):
54 instance = monitor_command.MonitorOpenstackCmd(self.config, None)
56 mock_subprocess.check_output.return_value = (0, 'unittest')
57 ret = instance.monitor_func()
58 self.assertEqual(ret, True)
59 instance._result = {"outage_time": 0}
62 def test__monitor_command_monitor_func_failure(self, mock_subprocess):
63 mock_subprocess.check_output.return_value = (1, 'unittest')
64 instance = monitor_command.MonitorOpenstackCmd(self.config, None)
66 mock_subprocess.check_output.side_effect = RuntimeError
67 ret = instance.monitor_func()
68 self.assertEqual(ret, False)
69 instance._result = {"outage_time": 10}
72 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.ssh')
73 def test__monitor_command_ssh_monitor_successful(self, mock_ssh, mock_subprocess):
75 self.config["host"] = "node1"
76 instance = monitor_command.MonitorOpenstackCmd(self.config, self.context)
78 mock_ssh.SSH().execute.return_value = (0, "0", '')
79 ret = instance.monitor_func()