aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/result_checker/baseresultchecker.py
blob: ce34d8be0ac0e82f1c79f1e55cd24ad2bb343057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
##############################################################################
# Copyright (c) 2016 Juan Qiu and others
# juan_ qiu@tongji.edu.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import absolute_import
import pkg_resources
import yaml
import logging
import os

import yardstick.common.utils as utils

LOG = logging.getLogger(__name__)

resultchecker_conf_path = pkg_resources.resource_filename(
    "yardstick.benchmark.scenarios.availability",
    "result_checker_conf.yaml")


class ResultCheckerMgr(object):

    def __init__(self):
        self._result_checker_list = []

    def init_ResultChecker(self, resultchecker_cfgs, context):
        LOG.debug("resultcheckerMgr confg: %s", resultchecker_cfgs)

        for cfg in resultchecker_cfgs:
            resultchecker_type = cfg['checker_type']
            resultchecker_cls = BaseResultChecker.get_resultchecker_cls(
                resultchecker_type)
            resultchecker_ins = resultchecker_cls(cfg, context)
            resultchecker_ins.key = cfg['key']
            resultchecker_ins.setup()
            self._result_checker_list.append(resultchecker_ins)

    def __getitem__(self, item):
        for obj in self._result_checker_list:
            if(obj.key == item):
                return obj
        raise KeyError("No such result checker instance of key - %s" % item)

    def verify(self):
        result = True
        for obj in self._result_checker_list:
            result &= obj.success
        return result


class BaseResultChecker(object):

    resultchecker_cfgs = {}

    def __init__(self, config, context):
        if not BaseResultChecker.resultchecker_cfgs:
            with open(resultchecker_conf_path) as stream:
                BaseResultChecker.resultchecker_cfgs = yaml.safe_load(stream)
        self.actualResult = object()
        self.expectedResult = object()
        self.success = False

        self._config = config
        self._context = context
        self.setup_done = False

    @staticmethod
    def get_resultchecker_cls(type):
        """return resultchecker instance of specified type"""
        resultchecker_type = type
        for checker_cls in utils.itersubclasses(BaseResultChecker):
            if resultchecker_type == checker_cls.__result_checker__type__:
                return checker_cls
        raise RuntimeError("No such runner_type %s" % resultchecker_type)

    def get_script_fullpath(self, path):
        base_path = os.path.dirname(resultchecker_conf_path)
        return os.path.join(base_path, path)

    def setup(self):
        pass

    def verify(self):
        if(self.actualResult == self.expectedResult):
            self.success = True
        return self.success