1 ##############################################################################
2 # Copyright (c) 2016 Juan Qiu and others
3 # juan_ qiu@tongji.edu.cn
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 absolute_import
12 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
13 from yardstick.benchmark.scenarios.availability.attacker import baseattacker
14 from yardstick.benchmark.scenarios.availability.operation import baseoperation
15 from yardstick.benchmark.scenarios.availability.result_checker \
16 import baseresultchecker
17 from yardstick.benchmark.scenarios.availability import ActionType
18 from yardstick.benchmark.scenarios.availability import actionplayers
19 from yardstick.benchmark.scenarios.availability import actionrollbackers
21 LOG = logging.getLogger(__name__)
24 class Director(object):
26 Director is used to direct a test scenaio
27 including the creation of action players, test result verification
28 and rollback of actions.
31 def __init__(self, scenario_cfg, context_cfg):
33 # A stack store Rollbacker that will be called after
34 # all actionplayers finish.
35 self.executionSteps = []
38 self.scenario_cfg = scenario_cfg
39 self.context_cfg = context_cfg
40 nodes = self.context_cfg.get("nodes", None)
42 if "attackers" in self.scenario_cfg["options"]:
43 LOG.debug("Start init attackers...")
44 attacker_cfgs = self.scenario_cfg["options"]["attackers"]
45 self.attackerMgr = baseattacker.AttackerMgr()
46 self.data = self.attackerMgr.init_attackers(attacker_cfgs,
50 if "monitors" in self.scenario_cfg["options"]:
51 LOG.debug("Start init monitors...")
52 monitor_cfgs = self.scenario_cfg["options"]["monitors"]
53 self.monitorMgr = basemonitor.MonitorMgr(self.data)
54 self.monitorMgr.init_monitors(monitor_cfgs, nodes)
56 if "operations" in self.scenario_cfg["options"]:
57 LOG.debug("Start init operations...")
58 operation_cfgs = self.scenario_cfg["options"]["operations"]
59 self.operationMgr = baseoperation.OperationMgr()
60 self.operationMgr.init_operations(operation_cfgs, nodes)
61 # setup result checker
62 if "resultCheckers" in self.scenario_cfg["options"]:
63 LOG.debug("Start init resultCheckers...")
64 result_check_cfgs = self.scenario_cfg["options"]["resultCheckers"]
65 self.resultCheckerMgr = baseresultchecker.ResultCheckerMgr()
66 self.resultCheckerMgr.init_ResultChecker(result_check_cfgs, nodes)
68 def createActionPlayer(self, type, key, intermediate_variables=None):
69 if intermediate_variables is None:
70 intermediate_variables = {}
72 "The type of current action is %s, the key is %s", type, key)
73 if type == ActionType.ATTACKER:
74 return actionplayers.AttackerPlayer(self.attackerMgr[key], intermediate_variables)
75 if type == ActionType.MONITOR:
76 return actionplayers.MonitorPlayer(self.monitorMgr[key], intermediate_variables)
77 if type == ActionType.RESULTCHECKER:
78 return actionplayers.ResultCheckerPlayer(
79 self.resultCheckerMgr[key], intermediate_variables)
80 if type == ActionType.OPERATION:
81 return actionplayers.OperationPlayer(self.operationMgr[key],
82 intermediate_variables)
83 LOG.debug("The type is not recognized by createActionPlayer")
85 def createActionRollbacker(self, type, key):
87 "The type of current action is %s, the key is %s", type, key)
88 if type == ActionType.ATTACKER:
89 return actionrollbackers.AttackerRollbacker(self.attackerMgr[key])
90 if type == ActionType.OPERATION:
91 return actionrollbackers.OperationRollbacker(
92 self.operationMgr[key])
93 LOG.debug("No rollbacker created for key: %s", key)
97 if hasattr(self, 'monitorMgr'):
98 result &= self.monitorMgr.verify_SLA()
99 if hasattr(self, 'resultCheckerMgr'):
100 result &= self.resultCheckerMgr.verify()
102 LOG.debug("Monitor results are passed")
105 def stopMonitors(self):
106 if "monitors" in self.scenario_cfg["options"]:
107 self.monitorMgr.wait_monitors()
110 LOG.debug("Knock off ....")
111 while self.executionSteps:
112 singleStep = self.executionSteps.pop()
113 singleStep.rollback()
115 def store_result(self, result):
116 LOG.debug("Store result ....")
117 if hasattr(self, 'monitorMgr'):
118 self.monitorMgr.store_result(result)