1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import print_function
from __future__ import absolute_import
import logging
from yardstick.benchmark.scenarios import base
from yardstick.benchmark.scenarios.availability.monitor import basemonitor
from yardstick.benchmark.scenarios.availability.attacker import baseattacker
LOG = logging.getLogger(__name__)
class ServiceHA(base.Scenario):
"""TODO: docstring of ServiceHA
"""
__scenario_type__ = "ServiceHA"
def __init__(self, scenario_cfg, context_cfg):
LOG.debug(
"scenario_cfg:%s context_cfg:%s",
scenario_cfg, context_cfg)
self.scenario_cfg = scenario_cfg
self.context_cfg = context_cfg
self.setup_done = False
self.data = {}
self.sla_pass = False
def setup(self):
"""scenario setup"""
nodes = self.context_cfg.get("nodes", None)
if nodes is None:
LOG.error("The nodes info is none")
return
self.attackers = []
attacker_cfgs = self.scenario_cfg["options"]["attackers"]
for attacker_cfg in attacker_cfgs:
attacker_cls = baseattacker.BaseAttacker.get_attacker_cls(
attacker_cfg)
attacker_ins = attacker_cls(attacker_cfg, nodes)
attacker_ins.setup()
self.attackers.append(attacker_ins)
self.data = dict(self.data.items() + attacker_ins.data.items())
monitor_cfgs = self.scenario_cfg["options"]["monitors"]
self.monitorMgr = basemonitor.MonitorMgr(self.data)
self.monitorMgr.init_monitors(monitor_cfgs, nodes)
self.setup_done = True
def run(self, result):
"""execute the benchmark"""
if not self.setup_done:
LOG.error("The setup is not finished!")
return
self.monitorMgr.start_monitors()
LOG.info("Monitor '%s' start!", self.__scenario_type__)
for attacker in self.attackers:
attacker.inject_fault()
self.monitorMgr.wait_monitors()
LOG.info("Monitor '%s' stop!", self.__scenario_type__)
self.sla_pass = self.monitorMgr.verify_SLA()
service_not_found = False
for k, v in self.data.items():
if v == 0:
self.sla_pass = False
LOG.info("The service process (%s) not found in the host environment", k)
result['sla_pass'] = 1 if self.sla_pass else 0
self.monitorMgr.store_result(result)
self.verify_SLA(
self.sla_pass, ("a service process was not found in the host "
"environment" if service_not_found
else "MonitorMgr.verify_SLA() failed"))
def teardown(self):
"""scenario teardown"""
# recover when mandatory or sla not pass
for attacker in self.attackers:
if attacker.mandatory or not self.sla_pass:
attacker.recover()
def _test(): # pragma: no cover
"""internal test function"""
host = {
"ip": "10.20.0.5",
"user": "root",
"key_filename": "/root/.ssh/id_rsa"
}
ctx = {"nodes": {"node1": host}}
attacker_cfg = {
"fault_type": "kill-process",
"process_name": "nova-api",
"host": "node1"
}
attacker_cfgs = []
attacker_cfgs.append(attacker_cfg)
monitor_cfg = {
"monitor_cmd": "nova image-list"
}
monitor_cfgs = []
monitor_cfgs.append(monitor_cfg)
options = {
"attackers": attacker_cfgs,
"wait_time": 10,
"monitors": monitor_cfgs
}
sla = {"outage_time": 5}
args = {"options": options, "sla": sla}
print("create instance")
terstInstance = ServiceHA(args, ctx)
terstInstance.setup()
result = {}
terstInstance.run(result)
print(result)
terstInstance.teardown()
if __name__ == '__main__': # pragma: no cover
_test()
|