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