1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
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
15 import yardstick.common.utils as utils
17 LOG = logging.getLogger(__name__)
19 attacker_conf_path = pkg_resources.resource_filename(
20 "yardstick.benchmark.scenarios.availability",
24 class AttackerMgr(object):
27 self._attacker_list = []
29 def init_attackers(self, attacker_cfgs, context):
30 LOG.debug("attackerMgr confg: %s", attacker_cfgs)
32 for cfg in attacker_cfgs:
33 attacker_cls = BaseAttacker.get_attacker_cls(cfg)
34 attacker_ins = attacker_cls(cfg, context)
35 attacker_ins.key = cfg['key']
37 self._attacker_list.append(attacker_ins)
39 def __getitem__(self, item):
40 for obj in self._attacker_list:
45 for _instance in self._attacker_list:
49 class BaseAttacker(object):
53 def __init__(self, config, context):
54 if not BaseAttacker.attacker_cfgs:
55 with open(attacker_conf_path) as stream:
56 BaseAttacker.attacker_cfgs = yaml.load(stream)
59 self._context = context
60 self.setup_done = False
63 def get_attacker_cls(attacker_cfg):
64 '''return attacker instance of specified type'''
65 attacker_type = attacker_cfg['fault_type']
66 for attacker_cls in utils.itersubclasses(BaseAttacker):
67 if attacker_type == attacker_cls.__attacker_type__:
69 raise RuntimeError("No such runner_type %s" % attacker_type)
71 def get_script_fullpath(self, path):
72 base_path = os.path.dirname(attacker_conf_path)
73 return os.path.join(base_path, path)