Add support for Python 3
[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         ip = host.get("ip", None)
30         user = host.get("user", "root")
31         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
32         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
33
34         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
35                                   port=ssh_port)
36         self.connection.wait(timeout=600)
37         LOG.debug("ssh host success!")
38
39         self.key = self._config['key']
40         self.resultchecker_key = self._config['checker_key']
41         self.type = self._config['checker_type']
42         self.condition = self._config['condition']
43         self.expectedResult = self._config['expectedValue']
44         self.actualResult = object()
45
46         self.key = self._config['key']
47         if "parameter" in self._config:
48             parameter = self._config['parameter']
49             str = buildshellparams(parameter)
50             l = list(item for item in parameter.values())
51             self.shell_cmd = str.format(*l)
52
53         self.resultchecker_cfgs = BaseResultChecker.resultchecker_cfgs.get(
54             self.resultchecker_key)
55         self.verify_script = self.get_script_fullpath(
56             self.resultchecker_cfgs['verify_script'])
57
58     def verify(self):
59         if "parameter" in self._config:
60             with open(self.verify_script, "r") as stdin_file:
61                 exit_status, stdout, stderr = self.connection.execute(
62                     self.shell_cmd,
63                     stdin=stdin_file)
64             LOG.debug("action script of the operation is: {0}"
65                       .format(self.verify_script))
66             LOG.debug("action parameter the of operation is: {0}"
67                       .format(self.shell_cmd))
68         else:
69             with open(self.verify_script, "r") as stdin_file:
70                 exit_status, stdout, stderr = self.connection.execute(
71                     "/bin/bash -s ",
72                     stdin=stdin_file)
73             LOG.debug("action script of the operation is: {0}"
74                       .format(self.verify_script))
75
76         LOG.debug("exit_status ,stdout : %s ,%s", exit_status, stdout)
77         if exit_status == 0 and stdout:
78             self.actualResult = stdout
79             LOG.debug("verifying resultchecker: {0}".format(self.key))
80             LOG.debug("verifying resultchecker,expected: {0}"
81                       .format(self.expectedResult))
82             LOG.debug("verifying resultchecker,actual: {0}"
83                       .format(self.actualResult))
84             LOG.debug("verifying resultchecker,condition: {0}"
85                       .format(self.condition))
86             if (type(self.expectedResult) is int):
87                 self.actualResult = int(self.actualResult)
88             if self.condition == Condition.EQUAL:
89                 self.success = self.actualResult == self.expectedResult
90             elif self.condition == Condition.GREATERTHAN:
91                 self.success = self.actualResult > self.expectedResult
92             elif self.condition == Condition.GREATERTHANEQUAL:
93                 self.success = self.actualResult >= self.expectedResult
94             elif self.condition == Condition.LESSTHANEQUAL:
95                 self.success = self.actualResult <= self.expectedResult
96             elif self.condition == Condition.LESSTHAN:
97                 self.success = self.actualResult < self.expectedResult
98             elif self.condition == Condition.IN:
99                 self.success = self.expectedResult in self.actualResult
100             else:
101                 self.success = False
102                 LOG.debug(
103                     "error happened when resultchecker: {0} Invalid condition"
104                     .format(self.key))
105         else:
106             self.success = False
107             LOG.debug(
108                 "error happened when resultchecker: {0} verifying the result"
109                 .format(self.key))
110             LOG.error(stderr)
111
112         LOG.debug(
113             "verifying resultchecker: %s,the result is : %s", self.key,
114             self.success)
115         return self.success