HA testcase containerized Compass support
[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     def test__fun_execute_shell_command_fail_cmd_exception(self,
34                                                            mock_subprocess):
35         cmd = "env"
36         mock_subprocess.check_output.side_effect = RuntimeError
37         exitcode, output = monitor_command._execute_shell_command(cmd)
38         self.assertEqual(exitcode, -1)
39
40
41 @mock.patch(
42     'yardstick.benchmark.scenarios.availability.monitor.monitor_command'
43     '.subprocess')
44 class MonitorOpenstackCmdTestCase(unittest.TestCase):
45
46     def setUp(self):
47         host = {
48             "ip": "10.20.0.5",
49             "user": "root",
50             "key_filename": "/root/.ssh/id_rsa"
51         }
52         self.context = {"node1": host}
53         self.config = {
54             'monitor_type': 'openstack-api',
55             'command_name': 'nova image-list',
56             'monitor_time': 1,
57             'sla': {'max_outage_time': 5}
58         }
59
60     def test__monitor_command_monitor_func_successful(self, mock_subprocess):
61
62         instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
63         instance.setup()
64         mock_subprocess.check_output.return_value = (0, 'unittest')
65         ret = instance.monitor_func()
66         self.assertEqual(ret, True)
67         instance._result = {"outage_time": 0}
68         instance.verify_SLA()
69
70     def test__monitor_command_monitor_func_failure(self, mock_subprocess):
71         mock_subprocess.check_output.return_value = (1, 'unittest')
72         instance = monitor_command.MonitorOpenstackCmd(self.config, None, {"nova-api": 10})
73         instance.setup()
74         mock_subprocess.check_output.side_effect = RuntimeError
75         ret = instance.monitor_func()
76         self.assertEqual(ret, False)
77         instance._result = {"outage_time": 10}
78         instance.verify_SLA()
79
80     @mock.patch(
81         'yardstick.benchmark.scenarios.availability.monitor.monitor_command'
82         '.ssh')
83     def test__monitor_command_ssh_monitor_successful(self, mock_ssh,
84                                                      mock_subprocess):
85
86         self.config["host"] = "node1"
87         instance = monitor_command.MonitorOpenstackCmd(
88             self.config, self.context, {"nova-api": 10})
89         instance.setup()
90         mock_ssh.SSH.from_node().execute.return_value = (0, "0", '')
91         ret = instance.monitor_func()