ddaf099693d1c79398871d5ec5dc48348f8b5497
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / baseattacker.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
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 pkg_resources
10 import yaml
11 import logging
12 import os
13
14 import yardstick.common.utils as utils
15
16 LOG = logging.getLogger(__name__)
17
18 attacker_conf_path = pkg_resources.resource_filename(
19     "yardstick.benchmark.scenarios.availability.attacker",
20     "attacker_conf.yaml")
21
22
23 class BaseAttacker(object):
24
25     attacker_cfgs = {}
26
27     def __init__(self, config, context):
28         if not BaseAttacker.attacker_cfgs:
29             with open(attacker_conf_path) as stream:
30                 BaseAttacker.attacker_cfgs = yaml.load(stream)
31
32         self._config = config
33         self._context = context
34         self.setup_done = False
35
36     @staticmethod
37     def get_attacker_cls(attacker_cfg):
38         '''return attacker instance of specified type'''
39         attacker_type = attacker_cfg['fault_type']
40         for attacker_cls in utils.itersubclasses(BaseAttacker):
41             if attacker_type == attacker_cls.__attacker_type__:
42                 return attacker_cls
43         raise RuntimeError("No such runner_type %s" % attacker_type)
44
45     def get_script_fullpath(self, path):
46         base_path = os.path.dirname(attacker_conf_path)
47         return os.path.join(base_path, path)