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