Merge "Update setup.py to include plugin install/remove scripts and rename "script...
[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.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             exit_status, stdout, stderr = self.connection.execute(
56                 self.shell_cmd,
57                 stdin=open(self.verify_script, "r"))
58             LOG.debug("action script of the operation is: {0}"
59                       .format(self.verify_script))
60             LOG.debug("action parameter the of operation is: {0}"
61                       .format(self.shell_cmd))
62         else:
63             exit_status, stdout, stderr = self.connection.execute(
64                 "/bin/bash -s ",
65                 stdin=open(self.verify_script, "r"))
66             LOG.debug("action script of the operation is: {0}"
67                       .format(self.verify_script))
68
69         LOG.debug("exit_status ,stdout : {0} ,{1}".format(exit_status, stdout))
70         if exit_status == 0 and stdout:
71             self.actualResult = stdout
72             LOG.debug("verifying resultchecker: {0}".format(self.key))
73             LOG.debug("verifying resultchecker,expected: {0}"
74                       .format(self.expectedResult))
75             LOG.debug("verifying resultchecker,actual: {0}"
76                       .format(self.actualResult))
77             LOG.debug("verifying resultchecker,condition: {0}"
78                       .format(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: {0} Invalid condition"
97                     .format(self.key))
98         else:
99             self.success = False
100             LOG.debug(
101                 "error happened when resultchecker: {0} verifying the result"
102                 .format(self.key))
103             LOG.error(stderr)
104
105         LOG.debug(
106             "verifying resultchecker: {0},the result is : {1}"
107             .format(self.key, self.success))
108         return self.success