Merge "fix SSH object examples to use correct context manager form"
[yardstick.git] / yardstick / benchmark / scenarios / availability / director.py
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 import logging
10
11 from yardstick.benchmark.scenarios.availability.monitor import basemonitor
12 from yardstick.benchmark.scenarios.availability.attacker import baseattacker
13 from yardstick.benchmark.scenarios.availability.operation import baseoperation
14 from yardstick.benchmark.scenarios.availability.result_checker \
15     import baseresultchecker
16 from yardstick.benchmark.scenarios.availability import ActionType
17 from yardstick.benchmark.scenarios.availability import actionplayers
18 from yardstick.benchmark.scenarios.availability import actionrollbackers
19
20 LOG = logging.getLogger(__name__)
21
22
23 class Director(object):
24     """
25     Director is used to direct a test scenaio
26     including the creation of  action players, test result verification
27     and rollback of actions.
28     """
29
30     def __init__(self, scenario_cfg, context_cfg):
31
32         # A stack store Rollbacker that will be called after
33         # all actionplayers finish.
34         self.executionSteps = []
35
36         self.scenario_cfg = scenario_cfg
37         self.context_cfg = context_cfg
38         nodes = self.context_cfg.get("nodes", None)
39         # setup attackers
40         if "attackers" in self.scenario_cfg["options"]:
41             LOG.debug("start init attackers...")
42             attacker_cfgs = self.scenario_cfg["options"]["attackers"]
43             self.attackerMgr = baseattacker.AttackerMgr()
44             self.attackerMgr.init_attackers(attacker_cfgs, nodes)
45         # setup monitors
46         if "monitors" in self.scenario_cfg["options"]:
47             LOG.debug("start init monitors...")
48             monitor_cfgs = self.scenario_cfg["options"]["monitors"]
49             self.monitorMgr = basemonitor.MonitorMgr()
50             self.monitorMgr.init_monitors(monitor_cfgs, nodes)
51         # setup operations
52         if "operations" in self.scenario_cfg["options"]:
53             LOG.debug("start init operations...")
54             operation_cfgs = self.scenario_cfg["options"]["operations"]
55             self.operationMgr = baseoperation.OperationMgr()
56             self.operationMgr.init_operations(operation_cfgs, nodes)
57         # setup result checker
58         if "resultCheckers" in self.scenario_cfg["options"]:
59             LOG.debug("start init resultCheckers...")
60             result_check_cfgs = self.scenario_cfg["options"]["resultCheckers"]
61             self.resultCheckerMgr = baseresultchecker.ResultCheckerMgr()
62             self.resultCheckerMgr.init_ResultChecker(result_check_cfgs, nodes)
63
64     def createActionPlayer(self, type, key):
65         LOG.debug(
66             "the type of current action is %s, the key is %s", type, key)
67         if type == ActionType.ATTACKER:
68             return actionplayers.AttackerPlayer(self.attackerMgr[key])
69         if type == ActionType.MONITOR:
70             return actionplayers.MonitorPlayer(self.monitorMgr[key])
71         if type == ActionType.RESULTCHECKER:
72             return actionplayers.ResultCheckerPlayer(
73                 self.resultCheckerMgr[key])
74         if type == ActionType.OPERATION:
75             return actionplayers.OperationPlayer(self.operationMgr[key])
76         LOG.debug("something run when creatactionplayer")
77
78     def createActionRollbacker(self, type, key):
79         LOG.debug(
80             "the type of current action is %s, the key is %s", type, key)
81         if type == ActionType.ATTACKER:
82             return actionrollbackers.AttackerRollbacker(self.attackerMgr[key])
83         if type == ActionType.OPERATION:
84             return actionrollbackers.OperationRollbacker(
85                 self.operationMgr[key])
86         LOG.debug("no rollbacker created for %s", key)
87
88     def verify(self):
89         result = True
90         if hasattr(self, 'monitorMgr'):
91             result &= self.monitorMgr.verify_SLA()
92         if hasattr(self, 'resultCheckerMgr'):
93             result &= self.resultCheckerMgr.verify()
94         if result:
95             LOG.debug("monitors are passed")
96         return result
97
98     def stopMonitors(self):
99         if "monitors" in self.scenario_cfg["options"]:
100             self.monitorMgr.wait_monitors()
101
102     def knockoff(self):
103         LOG.debug("knock off ....")
104         while self.executionSteps:
105             singleStep = self.executionSteps.pop()
106             singleStep.rollback()