Merge "Add a new runner to test end-to-end fast data path"
[yardstick.git] / yardstick / benchmark / scenarios / availability / operation / operation_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.operation.baseoperation \
13     import \
14     BaseOperation
15
16 import yardstick.ssh as ssh
17 from yardstick.benchmark.scenarios.availability.util import buildshellparams
18
19 LOG = logging.getLogger(__name__)
20
21
22 class GeneralOperaion(BaseOperation):
23
24     __operation__type__ = "general-operation"
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.operation_key = self._config['operation_key']
41
42         if "action_parameter" in self._config:
43             actionParameter = self._config['action_parameter']
44             str = buildshellparams(actionParameter)
45             l = list(item for item in actionParameter.values())
46             self.action_param = str.format(*l)
47
48         if "rollback_parameter" in self._config:
49             rollbackParameter = self._config['rollback_parameter']
50             str = buildshellparams(rollbackParameter)
51             l = list(item for item in rollbackParameter.values())
52             self.rollback_param = str.format(*l)
53
54         self.operation_cfgs = BaseOperation.operation_cfgs.get(
55             self.operation_key)
56         self.action_script = self.get_script_fullpath(
57             self.operation_cfgs['action_script'])
58         self.rollback_script = self.get_script_fullpath(
59             self.operation_cfgs['rollback_script'])
60
61     def run(self):
62         if "action_parameter" in self._config:
63             with open(self.action_script, "r") as stdin_file:
64                 exit_status, stdout, stderr = self.connection.execute(
65                     self.action_param,
66                     stdin=stdin_file)
67         else:
68             with open(self.action_script, "r") as stdin_file:
69                 exit_status, stdout, stderr = self.connection.execute(
70                     "/bin/sh -s ",
71                     stdin=stdin_file)
72
73         if exit_status == 0:
74             LOG.debug("success,the operation's output is: {0}".format(stdout))
75         else:
76             LOG.error(
77                 "the operation's error, stdout:%s, stderr:%s",
78                 stdout, stderr)
79
80     def rollback(self):
81         if "rollback_parameter" in self._config:
82             with open(self.rollback_script, "r") as stdin_file:
83                 exit_status, stdout, stderr = self.connection.execute(
84                     self.rollback_param,
85                     stdin=stdin_file)
86         else:
87             with open(self.rollback_script, "r") as stdin_file:
88                 exit_status, stdout, stderr = self.connection.execute(
89                     "/bin/sh -s ",
90                     stdin=stdin_file)