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