bugfix: serviceha do not provide result to influxdb
[yardstick.git] / yardstick / benchmark / scenarios / availability / serviceha.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 from yardstick.benchmark.scenarios import base
11 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
12 from yardstick.benchmark.scenarios.availability.attacker import baseattacker
13
14 LOG = logging.getLogger(__name__)
15
16
17 class ServiceHA(base.Scenario):
18     """TODO: docstring of ServiceHA
19     """
20     __scenario_type__ = "ServiceHA"
21
22     def __init__(self, scenario_cfg, context_cfg):
23         LOG.debug(
24             "scenario_cfg:%s context_cfg:%s" %
25             (scenario_cfg, context_cfg))
26         self.scenario_cfg = scenario_cfg
27         self.context_cfg = context_cfg
28         self.setup_done = False
29
30     def setup(self):
31         '''scenario setup'''
32         nodes = self.context_cfg.get("nodes", None)
33         if nodes is None:
34             LOG.error("the nodes info is none")
35             return
36
37         self.attackers = []
38         attacker_cfgs = self.scenario_cfg["options"]["attackers"]
39         for attacker_cfg in attacker_cfgs:
40             attacker_cls = baseattacker.BaseAttacker.get_attacker_cls(
41                 attacker_cfg)
42             attacker_ins = attacker_cls(attacker_cfg, nodes)
43             attacker_ins.setup()
44             self.attackers.append(attacker_ins)
45
46         monitor_cfgs = self.scenario_cfg["options"]["monitors"]
47
48         self.monitorMgr = basemonitor.MonitorMgr()
49         self.monitorMgr.init_monitors(monitor_cfgs, nodes)
50
51         self.setup_done = True
52
53     def run(self, result):
54         """execute the benchmark"""
55         if not self.setup_done:
56             LOG.error("The setup not finished!")
57             return
58
59         self.monitorMgr.start_monitors()
60         LOG.info("monitor start!")
61
62         for attacker in self.attackers:
63             attacker.inject_fault()
64
65         self.monitorMgr.wait_monitors()
66         LOG.info("monitor stop!")
67
68         sla_pass = self.monitorMgr.verify_SLA()
69         if sla_pass:
70             result['sla_pass'] = 1
71         else:
72             result['sla_pass'] = 0
73         assert sla_pass is True, "the test cases is not pass the SLA"
74
75         return
76
77     def teardown(self):
78         '''scenario teardown'''
79         for attacker in self.attackers:
80             attacker.recover()
81
82
83 def _test():    # pragma: no cover
84     '''internal test function'''
85     host = {
86         "ip": "10.20.0.5",
87         "user": "root",
88         "key_filename": "/root/.ssh/id_rsa"
89     }
90     ctx = {"nodes": {"node1": host}}
91     attacker_cfg = {
92         "fault_type": "kill-process",
93         "process_name": "nova-api",
94         "host": "node1"
95     }
96     attacker_cfgs = []
97     attacker_cfgs.append(attacker_cfg)
98     monitor_cfg = {
99         "monitor_cmd": "nova image-list"
100     }
101     monitor_cfgs = []
102     monitor_cfgs.append(monitor_cfg)
103
104     options = {
105         "attackers": attacker_cfgs,
106         "wait_time": 10,
107         "monitors": monitor_cfgs
108     }
109     sla = {"outage_time": 5}
110     args = {"options": options, "sla": sla}
111
112     print "create instance"
113     terstInstance = ServiceHA(args, ctx)
114
115     terstInstance.setup()
116     result = {}
117     terstInstance.run(result)
118     print result
119
120     terstInstance.teardown()
121
122 if __name__ == '__main__':    # pragma: no cover
123     _test()