ff6017b8834d44e5ad6ceaf4b1ffeea7c1352efe
[yardstick.git] / yardstick / benchmark / scenarios / availability / result_checker / result_checker_general.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 logging
11
12
13 from yardstick.benchmark.scenarios.availability.result_checker \
14     .baseresultchecker import \
15     BaseResultChecker
16 from yardstick.benchmark.scenarios.availability import Condition
17 import yardstick.ssh as ssh
18 from yardstick.benchmark.scenarios.availability.util import buildshellparams
19
20 LOG = logging.getLogger(__name__)
21
22
23 class GeneralResultChecker(BaseResultChecker):
24     __result_checker__type__ = "general-result-checker"
25
26     def setup(self):
27         LOG.debug("config:%s context:%s", self._config, self._context)
28         host = self._context.get(self._config['host'], None)
29
30         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
31         self.connection.wait(timeout=600)
32         LOG.debug("ssh host success!")
33
34         self.key = self._config['key']
35         self.resultchecker_key = self._config['checker_key']
36         self.type = self._config['checker_type']
37         self.condition = self._config['condition']
38         self.expectedResult = self._config['expectedValue']
39         self.actualResult = object()
40
41         self.key = self._config['key']
42         if "parameter" in self._config:
43             parameter = self._config['parameter']
44             str = buildshellparams(parameter)
45             l = list(item for item in parameter.values())
46             self.shell_cmd = str.format(*l)
47
48         self.resultchecker_cfgs = BaseResultChecker.resultchecker_cfgs.get(
49             self.resultchecker_key)
50         self.verify_script = self.get_script_fullpath(
51             self.resultchecker_cfgs['verify_script'])
52
53     def verify(self):
54         if "parameter" in self._config:
55             with open(self.verify_script, "r") as stdin_file:
56                 exit_status, stdout, stderr = self.connection.execute(
57                     self.shell_cmd,
58                     stdin=stdin_file)
59             LOG.debug("action script of the operation is: %s",
60                       self.verify_script)
61             LOG.debug("action parameter the of operation is: %s",
62                       self.shell_cmd)
63         else:
64             with open(self.verify_script, "r") as stdin_file:
65                 exit_status, stdout, stderr = self.connection.execute(
66                     "/bin/bash -s ",
67                     stdin=stdin_file)
68             LOG.debug("action script of the operation is: %s",
69                       self.verify_script)
70
71         LOG.debug("exit_status ,stdout : %s ,%s", exit_status, stdout)
72         if exit_status == 0 and stdout:
73             self.actualResult = stdout
74             LOG.debug("verifying resultchecker: %s", self.key)
75             LOG.debug("verifying resultchecker,expected: %s",
76                       self.expectedResult)
77             LOG.debug("verifying resultchecker,actual: %s", self.actualResult)
78             LOG.debug("verifying resultchecker,condition: %s", self.condition)
79             if (type(self.expectedResult) is int):
80                 self.actualResult = int(self.actualResult)
81             if self.condition == Condition.EQUAL:
82                 self.success = self.actualResult == self.expectedResult
83             elif self.condition == Condition.GREATERTHAN:
84                 self.success = self.actualResult > self.expectedResult
85             elif self.condition == Condition.GREATERTHANEQUAL:
86                 self.success = self.actualResult >= self.expectedResult
87             elif self.condition == Condition.LESSTHANEQUAL:
88                 self.success = self.actualResult <= self.expectedResult
89             elif self.condition == Condition.LESSTHAN:
90                 self.success = self.actualResult < self.expectedResult
91             elif self.condition == Condition.IN:
92                 self.success = self.expectedResult in self.actualResult
93             else:
94                 self.success = False
95                 LOG.debug(
96                     "error happened when resultchecker: %s Invalid condition",
97                     self.key)
98         else:
99             self.success = False
100             LOG.debug(
101                 "error happened when resultchecker: %s verifying the result",
102                 self.key)
103             LOG.error(stderr)
104
105         LOG.debug(
106             "verifying resultchecker: %s,the result is : %s", self.key,
107             self.success)
108         return self.success