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 = []
37 self.scenario_cfg = scenario_cfg
38 self.context_cfg = context_cfg
39 nodes = self.context_cfg.get("nodes", None)
41 if "attackers" in self.scenario_cfg["options"]:
42 LOG.debug("start init attackers...")
43 attacker_cfgs = self.scenario_cfg["options"]["attackers"]
44 self.attackerMgr = baseattacker.AttackerMgr()
45 self.attackerMgr.init_attackers(attacker_cfgs, nodes)
47 if "monitors" in self.scenario_cfg["options"]:
48 LOG.debug("start init monitors...")
49 monitor_cfgs = self.scenario_cfg["options"]["monitors"]
50 self.monitorMgr = basemonitor.MonitorMgr()
51 self.monitorMgr.init_monitors(monitor_cfgs, nodes)
53 if "operations" in self.scenario_cfg["options"]:
54 LOG.debug("start init operations...")
55 operation_cfgs = self.scenario_cfg["options"]["operations"]
56 self.operationMgr = baseoperation.OperationMgr()
57 self.operationMgr.init_operations(operation_cfgs, nodes)
58 # setup result checker
59 if "resultCheckers" in self.scenario_cfg["options"]:
60 LOG.debug("start init resultCheckers...")
61 result_check_cfgs = self.scenario_cfg["options"]["resultCheckers"]
62 self.resultCheckerMgr = baseresultchecker.ResultCheckerMgr()
63 self.resultCheckerMgr.init_ResultChecker(result_check_cfgs, nodes)
65 def createActionPlayer(self, type, key):
67 "the type of current action is %s, the key is %s", type, key)
68 if type == ActionType.ATTACKER:
69 return actionplayers.AttackerPlayer(self.attackerMgr[key])
70 if type == ActionType.MONITOR:
71 return actionplayers.MonitorPlayer(self.monitorMgr[key])
72 if type == ActionType.RESULTCHECKER:
73 return actionplayers.ResultCheckerPlayer(
74 self.resultCheckerMgr[key])
75 if type == ActionType.OPERATION:
76 return actionplayers.OperationPlayer(self.operationMgr[key])
77 LOG.debug("something run when creatactionplayer")
79 def createActionRollbacker(self, type, key):
81 "the type of current action is %s, the key is %s", type, key)
82 if type == ActionType.ATTACKER:
83 return actionrollbackers.AttackerRollbacker(self.attackerMgr[key])
84 if type == ActionType.OPERATION:
85 return actionrollbackers.OperationRollbacker(
86 self.operationMgr[key])
87 LOG.debug("no rollbacker created for %s", key)
91 if hasattr(self, 'monitorMgr'):
92 result &= self.monitorMgr.verify_SLA()
93 if hasattr(self, 'resultCheckerMgr'):
94 result &= self.resultCheckerMgr.verify()
96 LOG.debug("monitors are passed")
99 def stopMonitors(self):
100 if "monitors" in self.scenario_cfg["options"]:
101 self.monitorMgr.wait_monitors()
104 LOG.debug("knock off ....")
105 while self.executionSteps:
106 singleStep = self.executionSteps.pop()
107 singleStep.rollback()