bugfix: tc038 ssh default wait 3600s
[yardstick.git] / tests / unit / benchmark / scenarios / availability / test_monitor_command.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
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
13 # yardstick.benchmark.scenarios.availability.monitor.monitor_command
14
15 from __future__ import absolute_import
16 import mock
17 import unittest
18
19 from yardstick.benchmark.scenarios.availability.monitor import monitor_command
20
21
22 @mock.patch(
23     'yardstick.benchmark.scenarios.availability.monitor.monitor_command'
24     '.subprocess')
25 class ExecuteShellTestCase(unittest.TestCase):
26
27     def test__fun_execute_shell_command_successful(self, mock_subprocess):
28         cmd = "env"
29         mock_subprocess.check_output.return_value = (0, 'unittest')
30         exitcode, output = monitor_command._execute_shell_command(cmd)
31         self.assertEqual(exitcode, 0)
32
33     @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.LOG')
34     def test__fun_execute_shell_command_fail_cmd_exception(self, mock_log,
35                                                            mock_subprocess):
36         cmd = "env"
37         mock_subprocess.check_output.side_effect = RuntimeError
38         exitcode, output = monitor_command._execute_shell_command(cmd)
39         self.assertEqual(exitcode, -1)
40         mock_log.error.assert_called_once()
41
42
43 @mock.patch(
44     'yardstick.benchmark.scenarios.availability.monitor.monitor_command'
45     '.subprocess')
46 class MonitorOpenstackCmdTestCase(unittest.TestCase):
47
48     def setUp(self):
49         host = {
50             "ip": "10.20.0.5",
51             "user": "root",
52             "key_filename": "/root/.ssh/id_rsa"
53         }
54         self.context = {"node1": host}
55         self.config = {
56             'monitor_type': 'openstack-api',
57             'command_name': 'nova image-list',
58             'monitor_time': 1,
59             'sla': {'max_outage_time': 5}
60         }
61
62     def test__monitor_command_monitor_func_successful(self, mock_subprocess):
63
64         instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
65         instance.setup()
66         mock_subprocess.check_output.return_value = (0, 'unittest')
67         ret = instance.monitor_func()
68         self.assertEqual(ret, True)
69         instance._result = {"outage_time": 0}
70         instance.verify_SLA()
71
72     @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.LOG')
73     def test__monitor_command_monitor_func_failure(self, mock_log, mock_subprocess):
74         mock_subprocess.check_output.return_value = (1, 'unittest')
75         instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
76         instance.setup()
77         mock_subprocess.check_output.side_effect = RuntimeError
78         ret = instance.monitor_func()
79         self.assertEqual(ret, False)
80         mock_log.error.assert_called_once()
81         instance._result = {"outage_time": 10}
82         instance.verify_SLA()
83
84     @mock.patch(
85         'yardstick.benchmark.scenarios.availability.monitor.monitor_command'
86         '.ssh')
87     def test__monitor_command_ssh_monitor_successful(self, mock_ssh,
88                                                      mock_subprocess):
89
90         self.config["host"] = "node1"
91         instance = monitor_command.MonitorOpenstackCmd(
92             self.config, self.context, {"nova-api": 10})
93         instance.setup()
94         mock_ssh.SSH.from_node().execute.return_value = (0, "0", '')
95         ret = instance.monitor_func()