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 ##############################################################################
13 # yardstick.benchmark.scenarios.availability.monitor.monitor_command
15 from __future__ import absolute_import
19 from yardstick.benchmark.scenarios.availability.monitor import monitor_command
22 @mock.patch('subprocess.check_output')
23 class ExecuteShellTestCase(unittest.TestCase):
25 def test__fun_execute_shell_command_successful(self, mock_subprocess_check_output):
27 mock_subprocess_check_output.return_value = (0, 'unittest')
28 exitcode, _ = monitor_command._execute_shell_command(cmd)
29 self.assertEqual(exitcode, 0)
31 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.LOG')
32 def test__fun_execute_shell_command_fail_cmd_exception(self, mock_log,
33 mock_subprocess_check_output):
35 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 @mock.patch('subprocess.check_output')
42 class MonitorOpenstackCmdTestCase(unittest.TestCase):
48 "key_filename": "/root/.ssh/id_rsa"
50 self.context = {"node1": host}
52 'monitor_type': 'openstack-api',
53 'command_name': 'nova image-list',
55 'sla': {'max_outage_time': 5}
58 def test__monitor_command_monitor_func_successful(self, mock_subprocess_check_output):
60 instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
62 mock_subprocess_check_output.return_value = (0, 'unittest')
63 ret = instance.monitor_func()
65 instance._result = {"outage_time": 0}
68 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.LOG')
69 def test__monitor_command_monitor_func_failure(self, mock_log, mock_subprocess_check_output):
70 mock_subprocess_check_output.return_value = (1, 'unittest')
71 instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
73 mock_subprocess_check_output.side_effect = RuntimeError
74 ret = instance.monitor_func()
76 mock_log.error.assert_called_once()
77 instance._result = {"outage_time": 10}
81 'yardstick.benchmark.scenarios.availability.monitor.monitor_command'
83 def test__monitor_command_ssh_monitor_successful(self, mock_ssh, mock_subprocess_check_output):
85 mock_subprocess_check_output.return_value = (0, 'unittest')
86 self.config["host"] = "node1"
87 instance = monitor_command.MonitorOpenstackCmd(
88 self.config, self.context, {"nova-api": 10})
90 mock_ssh.SSH.from_node().execute.return_value = (0, "0", '')
91 ret = instance.monitor_func()