Merge changes from topics 'YARDSTICK-1250', 'YARDSTICK-1249'
[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         self.sla_pass = False
33
34     def setup(self):
35         """scenario setup"""
36         nodes = self.context_cfg.get("nodes", None)
37         if nodes is None:
38             LOG.error("The nodes info is none")
39             return
40
41         self.attackers = []
42         attacker_cfgs = self.scenario_cfg["options"]["attackers"]
43         for attacker_cfg in attacker_cfgs:
44             attacker_cls = baseattacker.BaseAttacker.get_attacker_cls(
45                 attacker_cfg)
46             attacker_ins = attacker_cls(attacker_cfg, nodes)
47             attacker_ins.setup()
48             self.attackers.append(attacker_ins)
49             self.data = dict(self.data.items() + attacker_ins.data.items())
50
51         monitor_cfgs = self.scenario_cfg["options"]["monitors"]
52
53         self.monitorMgr = basemonitor.MonitorMgr(self.data)
54         self.monitorMgr.init_monitors(monitor_cfgs, nodes)
55
56         self.setup_done = True
57
58     def run(self, result):
59         """execute the benchmark"""
60         if not self.setup_done:
61             LOG.error("The setup is not finished!")
62             return
63
64         self.monitorMgr.start_monitors()
65         LOG.info("Monitor '%s' start!", self.__scenario_type__)
66
67         for attacker in self.attackers:
68             attacker.inject_fault()
69
70         self.monitorMgr.wait_monitors()
71         LOG.info("Monitor '%s' stop!", self.__scenario_type__)
72
73         self.sla_pass = self.monitorMgr.verify_SLA()
74         service_not_found = False
75         for k, v in self.data.items():
76             if v == 0:
77                 self.sla_pass = False
78                 service_not_found = True
79                 LOG.info("The service process (%s) not found in the host envrioment", k)
80
81         result['sla_pass'] = 1 if self.sla_pass else 0
82         self.monitorMgr.store_result(result)
83
84         self.verify_SLA(
85             self.sla_pass, ("a service process was not found in the host "
86                             "environment" if service_not_found
87                             else "MonitorMgr.verify_SLA() failed"))
88
89     def teardown(self):
90         """scenario teardown"""
91         # only recover when sla not pass
92         if not self.sla_pass:
93             for attacker in self.attackers:
94                 attacker.recover()
95
96
97 def _test():    # pragma: no cover
98     """internal test function"""
99     host = {
100         "ip": "10.20.0.5",
101         "user": "root",
102         "key_filename": "/root/.ssh/id_rsa"
103     }
104     ctx = {"nodes": {"node1": host}}
105     attacker_cfg = {
106         "fault_type": "kill-process",
107         "process_name": "nova-api",
108         "host": "node1"
109     }
110     attacker_cfgs = []
111     attacker_cfgs.append(attacker_cfg)
112     monitor_cfg = {
113         "monitor_cmd": "nova image-list"
114     }
115     monitor_cfgs = []
116     monitor_cfgs.append(monitor_cfg)
117
118     options = {
119         "attackers": attacker_cfgs,
120         "wait_time": 10,
121         "monitors": monitor_cfgs
122     }
123     sla = {"outage_time": 5}
124     args = {"options": options, "sla": sla}
125
126     print("create instance")
127     terstInstance = ServiceHA(args, ctx)
128
129     terstInstance.setup()
130     result = {}
131     terstInstance.run(result)
132     print(result)
133
134     terstInstance.teardown()
135
136
137 if __name__ == '__main__':    # pragma: no cover
138     _test()