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