31e309714aa64b2d2ede9b0359f36fbfd0e9f531
[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 MonitorOpenstackCmdTestCase(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     def test__monitor_command_monitor_func_successful(self, mock_subprocess):
35         config = {
36             'monitor_type': 'openstack-api',
37             'command_name': 'nova image-list',
38             'monitor_time': 1,
39             'sla': {'max_outage_time': 5}
40         }
41
42         instance = monitor_command.MonitorOpenstackCmd(config, None)
43
44         mock_subprocess.check_output.return_value = (0, 'unittest')
45         ret = instance.monitor_func()
46         self.assertEqual(ret, True)
47         instance._result = {"outage_time": 0}
48         instance.verify_SLA()
49
50     def test__monitor_command_monitor_func_failure(self, mock_subprocess):
51         mock_subprocess.check_output.return_value = (1, 'unittest')
52         config = {
53             'monitor_type': 'openstack-api',
54             'command_name': 'nova image-list',
55             'monitor_time': 1,
56             'sla': {'max_outage_time': 5}
57         }
58         instance = monitor_command.MonitorOpenstackCmd(config, None)
59
60         mock_subprocess.check_output.side_effect = RuntimeError
61         ret = instance.monitor_func()
62         self.assertEqual(ret, False)
63         instance._result = {"outage_time": 10}
64         instance.verify_SLA()