10134ea6d8e6c45a3bfa0e60d21386f555feffb5
[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 import time
11 from yardstick.benchmark.scenarios import base
12 from yardstick.benchmark.scenarios.availability import monitor
13 from yardstick.benchmark.scenarios.availability.attacker import baseattacker
14
15 LOG = logging.getLogger(__name__)
16
17
18 class ServiceHA(base.Scenario):
19     """TODO: docstring of ServiceHA
20     """
21     __scenario_type__ = "ServiceHA"
22
23     def __init__(self, scenario_cfg, context_cfg):
24         LOG.debug(
25             "scenario_cfg:%s context_cfg:%s" %
26             (scenario_cfg, context_cfg))
27         self.scenario_cfg = scenario_cfg
28         self.context_cfg = context_cfg
29         self.setup_done = False
30
31     def setup(self):
32         '''scenario setup'''
33         nodes = self.context_cfg.get("nodes", None)
34         if nodes is None:
35             LOG.error("the nodes info is none")
36             return
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.monitor_ins = monitor.Monitor()
49         self.monitor_ins.setup(monitor_cfgs[0])
50         self.monitor_ins.monitor_time = monitor_cfgs[0]["monitor_time"]
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.monitor_ins.start()
60         LOG.info("monitor start!")
61
62         for attacker in self.attackers:
63             attacker.inject_fault()
64
65         time.sleep(self.monitor_ins.monitor_time)
66
67         self.monitor_ins.stop()
68         LOG.info("monitor stop!")
69
70         ret = self.monitor_ins.get_result()
71         LOG.info("The monitor result:%s" % ret)
72         outage_time = ret.get("outage_time")
73         result["outage_time"] = outage_time
74         LOG.info("the result:%s" % result)
75
76         if "sla" in self.scenario_cfg:
77             sla_outage_time = int(self.scenario_cfg["sla"]["outage_time"])
78             assert outage_time <= sla_outage_time, "outage_time %f > sla:outage_time(%f)" % \
79                 (outage_time, sla_outage_time)
80
81         return
82
83     def teardown(self):
84         '''scenario teardown'''
85         for attacker in self.attackers:
86             if not attacker.check():
87                 attacker.recover()
88
89
90 def _test():    # pragma: no cover
91     '''internal test function'''
92     host = {
93         "ip": "10.20.0.5",
94         "user": "root",
95         "key_filename": "/root/.ssh/id_rsa"
96     }
97     ctx = {"nodes": {"node1": host}}
98     attacker_cfg = {
99         "fault_type": "kill-process",
100         "process_name": "nova-api",
101         "host": "node1"
102     }
103     attacker_cfgs = []
104     attacker_cfgs.append(attacker_cfg)
105     monitor_cfg = {
106         "monitor_cmd": "nova image-list",
107         "monitor_tme": 10
108     }
109     monitor_cfgs = []
110     monitor_cfgs.append(monitor_cfg)
111
112     options = {
113         "attackers": attacker_cfgs,
114         "monitors": monitor_cfgs
115     }
116     sla = {"outage_time": 5}
117     args = {"options": options, "sla": sla}
118
119     print "create instance"
120     terstInstance = ServiceHA(args, ctx)
121
122     terstInstance.setup()
123     result = {}
124     terstInstance.run(result)
125     print result
126
127     terstInstance.teardown()
128
129 if __name__ == '__main__':    # pragma: no cover
130     _test()