Add intermediate variables for attacker,monitor,result_checker
[yardstick.git] / yardstick / benchmark / scenarios / availability / result_checker / baseresultchecker.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 from __future__ import absolute_import
10 import pkg_resources
11 import logging
12 import os
13
14 import yardstick.common.utils as utils
15 from yardstick.common.yaml_loader import yaml_load
16
17 LOG = logging.getLogger(__name__)
18
19 resultchecker_conf_path = pkg_resources.resource_filename(
20     "yardstick.benchmark.scenarios.availability",
21     "result_checker_conf.yaml")
22
23
24 class ResultCheckerMgr(object):
25
26     def __init__(self):
27         self._result_checker_list = []
28
29     def init_ResultChecker(self, resultchecker_cfgs, context):
30         LOG.debug("resultcheckerMgr confg: %s", resultchecker_cfgs)
31
32         for cfg in resultchecker_cfgs:
33             resultchecker_type = cfg['checker_type']
34             resultchecker_cls = BaseResultChecker.get_resultchecker_cls(
35                 resultchecker_type)
36             resultchecker_ins = resultchecker_cls(cfg, context)
37             resultchecker_ins.key = cfg['key']
38             resultchecker_ins.setup()
39             self._result_checker_list.append(resultchecker_ins)
40
41     def __getitem__(self, item):
42         for obj in self._result_checker_list:
43             if(obj.key == item):
44                 return obj
45         raise KeyError("No such result checker instance of key - %s" % item)
46
47     def verify(self):
48         result = True
49         for obj in self._result_checker_list:
50             result &= obj.success
51         return result
52
53
54 class BaseResultChecker(object):
55
56     resultchecker_cfgs = {}
57
58     def __init__(self, config, context):
59         if not BaseResultChecker.resultchecker_cfgs:
60             with open(resultchecker_conf_path) as stream:
61                 BaseResultChecker.resultchecker_cfgs = yaml_load(stream)
62         self.actualResult = object()
63         self.expectedResult = object()
64         self.success = False
65
66         self._config = config
67         self._context = context
68         self.setup_done = False
69         self.intermediate_variables = {}
70
71     @staticmethod
72     def get_resultchecker_cls(type):
73         """return resultchecker instance of specified type"""
74         resultchecker_type = type
75         for checker_cls in utils.itersubclasses(BaseResultChecker):
76             if resultchecker_type == checker_cls.__result_checker__type__:
77                 return checker_cls
78         raise RuntimeError("No such runner_type %s" % resultchecker_type)
79
80     def get_script_fullpath(self, path):
81         base_path = os.path.dirname(resultchecker_conf_path)
82         return os.path.join(base_path, path)
83
84     def setup(self):
85         pass
86
87     def verify(self):
88         if(self.actualResult == self.expectedResult):
89             self.success = True
90         return self.success