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