Bugfix: Local Openstack Operation in HA test frameworks
[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 from yardstick.benchmark.scenarios.availability.result_checker \
13     .baseresultchecker import \
14     BaseResultChecker
15 from yardstick.benchmark.scenarios.availability import Condition
16 import yardstick.ssh as ssh
17 from yardstick.benchmark.scenarios.availability.util \
18     import buildshellparams, execute_shell_command
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.get('host', None), None)
29
30         self.connection = None
31         if host:
32             self.connection = ssh.SSH.from_node(
33                 host, defaults={"user": "root"})
34             self.connection.wait(timeout=600)
35             LOG.debug("ssh host success!")
36
37         self.key = self._config['key']
38         self.resultchecker_key = self._config['checker_key']
39         self.type = self._config['checker_type']
40         self.condition = self._config['condition']
41         self.expectedResult = self._config['expectedValue']
42         self.actualResult = object()
43
44         self.key = self._config['key']
45         if "parameter" in self._config:
46             parameter = self._config['parameter']
47             str = buildshellparams(
48                 parameter, True if self.connection else False)
49             l = list(item for item in parameter.values())
50             self.shell_cmd = str.format(*l)
51
52         self.resultchecker_cfgs = BaseResultChecker.resultchecker_cfgs.get(
53             self.resultchecker_key)
54         self.verify_script = self.get_script_fullpath(
55             self.resultchecker_cfgs['verify_script'])
56
57     def verify(self):
58         if "parameter" in self._config:
59             if self.connection:
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             else:
65                 exit_status, stdout = \
66                     execute_shell_command(
67                         "/bin/bash {0} {1}".format(
68                             self.verify_script,
69                             self.rollback_param))
70
71             LOG.debug("action script of the operation is: %s",
72                       self.verify_script)
73             LOG.debug("action parameter the of operation is: %s",
74                       self.shell_cmd)
75         else:
76             if self.connection:
77                 with open(self.verify_script, "r") as stdin_file:
78                     exit_status, stdout, stderr = self.connection.execute(
79                         "/bin/bash -s ",
80                         stdin=stdin_file)
81             else:
82                 exit_status, stdout = execute_shell_command(
83                     "/bin/bash {0}".format(self.verify_script))
84
85             LOG.debug("action script of the operation is: %s",
86                       self.verify_script)
87
88         LOG.debug("exit_status ,stdout : %s ,%s", exit_status, stdout)
89         if exit_status == 0 and stdout:
90             self.actualResult = stdout
91             LOG.debug("verifying resultchecker: %s", self.key)
92             LOG.debug("verifying resultchecker,expected: %s",
93                       self.expectedResult)
94             LOG.debug("verifying resultchecker,actual: %s", self.actualResult)
95             LOG.debug("verifying resultchecker,condition: %s", self.condition)
96             if (type(self.expectedResult) is int):
97                 self.actualResult = int(self.actualResult)
98             if self.condition == Condition.EQUAL:
99                 self.success = self.actualResult == self.expectedResult
100             elif self.condition == Condition.GREATERTHAN:
101                 self.success = self.actualResult > self.expectedResult
102             elif self.condition == Condition.GREATERTHANEQUAL:
103                 self.success = self.actualResult >= self.expectedResult
104             elif self.condition == Condition.LESSTHANEQUAL:
105                 self.success = self.actualResult <= self.expectedResult
106             elif self.condition == Condition.LESSTHAN:
107                 self.success = self.actualResult < self.expectedResult
108             elif self.condition == Condition.IN:
109                 self.success = self.expectedResult in self.actualResult
110             else:
111                 self.success = False
112                 LOG.debug(
113                     "error happened when resultchecker: %s Invalid condition",
114                     self.key)
115         else:
116             self.success = False
117             LOG.debug(
118                 "error happened when resultchecker: %s verifying the result",
119                 self.key)
120             LOG.error(stderr)
121
122         LOG.debug(
123             "verifying resultchecker: %s,the result is : %s", self.key,
124             self.success)
125         return self.success