The secondi HA test case-shutdown controller
[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 yardstick.benchmark.scenarios.availability.monitor.monitor_command
13
14 import mock
15 import unittest
16
17 from yardstick.benchmark.scenarios.availability.monitor import monitor_command
18
19 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.subprocess')
20 class ExecuteShellTestCase(unittest.TestCase):
21
22     def test__fun_execute_shell_command_successful(self, mock_subprocess):
23         cmd = "env"
24         mock_subprocess.check_output.return_value = (0, 'unittest')
25         exitcode, output = monitor_command._execute_shell_command(cmd)
26         self.assertEqual(exitcode, 0)
27
28     def test__fun_execute_shell_command_fail_cmd_exception(self, mock_subprocess):
29         cmd = "env"
30         mock_subprocess.check_output.side_effect = RuntimeError
31         exitcode, output = monitor_command._execute_shell_command(cmd)
32         self.assertEqual(exitcode, -1)
33
34 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.subprocess')
35 class MonitorOpenstackCmdTestCase(unittest.TestCase):
36
37     def setUp(self):
38         host = {
39             "ip": "10.20.0.5",
40             "user": "root",
41             "key_filename": "/root/.ssh/id_rsa"
42         }
43         self.context = {"node1": host}
44         self.config = {
45             'monitor_type': 'openstack-api',
46             'command_name': 'nova image-list',
47             'monitor_time': 1,
48             'sla': {'max_outage_time': 5}
49         }
50
51
52     def test__monitor_command_monitor_func_successful(self, mock_subprocess):
53
54         instance = monitor_command.MonitorOpenstackCmd(self.config, None)
55         instance.setup()
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}
60         instance.verify_SLA()
61
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)
65         instance.setup()
66         mock_subprocess.check_output.side_effect = RuntimeError
67         ret = instance.monitor_func()
68         self.assertEqual(ret, False)
69         instance._result = {"outage_time": 10}
70         instance.verify_SLA()
71
72     @mock.patch('yardstick.benchmark.scenarios.availability.monitor.monitor_command.ssh')
73     def test__monitor_command_ssh_monitor_successful(self, mock_ssh, mock_subprocess):
74
75         self.config["host"] = "node1"
76         instance = monitor_command.MonitorOpenstackCmd(self.config, self.context)
77         instance.setup()
78         mock_ssh.SSH().execute.return_value = (0, "0", '')
79         ret = instance.monitor_func()