Rewrite the HA test case (2)
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_command.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import logging
10 import subprocess
11 import traceback
12
13 import basemonitor as basemonitor
14
15 LOG = logging.getLogger(__name__)
16
17
18 def _execute_shell_command(command):
19     '''execute shell script with error handling'''
20     exitcode = 0
21     output = []
22     try:
23         output = subprocess.check_output(command, shell=True)
24     except Exception:
25         exitcode = -1
26         output = traceback.format_exc()
27         LOG.error("exec command '%s' error:\n " % command)
28         LOG.error(traceback.format_exc())
29
30     return exitcode, output
31
32
33 class MonitorOpenstackCmd(basemonitor.BaseMonitor):
34     """docstring for MonitorApi"""
35
36     __monitor_type__ = "openstack-cmd"
37
38     def monitor_func(self):
39         cmd = self._config["command_name"]
40         exit_status, stdout = _execute_shell_command(cmd)
41         if exit_status:
42             return False
43         return True
44
45     def verify_SLA(self):
46         outage_time = self._result.get('outage_time', None)
47         LOG.debug("the _result:%s" % self._result)
48         max_outage_time = self._config["sla"]["max_outage_time"]
49         if outage_time > max_outage_time:
50             LOG.info("SLA failure: %f > %f" % (outage_time, max_outage_time))
51             return False
52         else:
53             LOG.info("the sla is passed")
54             return True
55
56
57 def _test():    # pragma: no cover
58     host = {
59         "ip": "10.20.0.5",
60         "user": "root",
61         "key_filename": "/root/.ssh/id_rsa"
62     }
63     context = {"node1": host}
64     monitor_configs = []
65     config = {
66         'monitor_type': 'openstack-cmd',
67         'command_name': 'nova image-list',
68         'monitor_time': 1,
69         'SLA': {'max_outage_time': 5}
70     }
71     monitor_configs.append(config)
72
73     p = basemonitor.MonitorMgr()
74     p.init_monitors(monitor_configs, context)
75     p.start_monitors()
76     p.wait_monitors()
77     p.verify()
78
79
80 if __name__ == '__main__':    # pragma: no cover
81     _test()