Merge "Bugfixed:run command: "yardstick-img-modify" fail!"
[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             with open(self.verify_script, "r") as stdin_file:
57                 exit_status, stdout, stderr = self.connection.execute(
58                     self.shell_cmd,
59                     stdin=stdin_file)
60             LOG.debug("action script of the operation is: {0}"
61                       .format(self.verify_script))
62             LOG.debug("action parameter the of operation is: {0}"
63                       .format(self.shell_cmd))
64         else:
65             with open(self.verify_script, "r") as stdin_file:
66                 exit_status, stdout, stderr = self.connection.execute(
67                     "/bin/bash -s ",
68                     stdin=stdin_file)
69             LOG.debug("action script of the operation is: {0}"
70                       .format(self.verify_script))
71
72         LOG.debug("exit_status ,stdout : %s ,%s", exit_status, stdout)
73         if exit_status == 0 and stdout:
74             self.actualResult = stdout
75             LOG.debug("verifying resultchecker: {0}".format(self.key))
76             LOG.debug("verifying resultchecker,expected: {0}"
77                       .format(self.expectedResult))
78             LOG.debug("verifying resultchecker,actual: {0}"
79                       .format(self.actualResult))
80             LOG.debug("verifying resultchecker,condition: {0}"
81                       .format(self.condition))
82             if (type(self.expectedResult) is int):
83                 self.actualResult = int(self.actualResult)
84             if self.condition == Condition.EQUAL:
85                 self.success = self.actualResult == self.expectedResult
86             elif self.condition == Condition.GREATERTHAN:
87                 self.success = self.actualResult > self.expectedResult
88             elif self.condition == Condition.GREATERTHANEQUAL:
89                 self.success = self.actualResult >= self.expectedResult
90             elif self.condition == Condition.LESSTHANEQUAL:
91                 self.success = self.actualResult <= self.expectedResult
92             elif self.condition == Condition.LESSTHAN:
93                 self.success = self.actualResult < self.expectedResult
94             elif self.condition == Condition.IN:
95                 self.success = self.expectedResult in self.actualResult
96             else:
97                 self.success = False
98                 LOG.debug(
99                     "error happened when resultchecker: {0} Invalid condition"
100                     .format(self.key))
101         else:
102             self.success = False
103             LOG.debug(
104                 "error happened when resultchecker: {0} verifying the result"
105                 .format(self.key))
106             LOG.error(stderr)
107
108         LOG.debug(
109             "verifying resultchecker: %s,the result is : %s", self.key,
110             self.success)
111         return self.success