koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 1 | import abc |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 2 | import time |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 3 | import socket |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 4 | import random |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 5 | import os.path |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 6 | import logging |
koder aka kdanilov | ea22c3d | 2015-04-21 03:42:22 +0300 | [diff] [blame] | 7 | import datetime |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 8 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 9 | from paramiko import SSHException |
| 10 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 11 | from wally.utils import ssize_to_b, open_for_append_or_create, sec_to_str |
| 12 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 13 | from wally.ssh_utils import (copy_paths, run_over_ssh, |
| 14 | save_to_remote, ssh_mkdir, |
| 15 | # delete_file, |
| 16 | connect, read_from_remote, Local) |
| 17 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 18 | from . import postgres |
| 19 | from .io import agent as io_agent |
| 20 | from .io import formatter as io_formatter |
| 21 | from .io.results_loader import parse_output |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 22 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 23 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 24 | logger = logging.getLogger("wally") |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 25 | |
| 26 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 27 | class IPerfTest(object): |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 28 | def __init__(self, on_result_cb, test_uuid, node, log_directory=None): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 29 | self.on_result_cb = on_result_cb |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 30 | self.log_directory = log_directory |
| 31 | self.node = node |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 32 | self.test_uuid = test_uuid |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 33 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 34 | def pre_run(self): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 35 | pass |
| 36 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 37 | def cleanup(self): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 38 | pass |
| 39 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 40 | @abc.abstractmethod |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 41 | def run(self, barrier): |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 42 | pass |
| 43 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 44 | @classmethod |
| 45 | def format_for_console(cls, data): |
| 46 | msg = "{0}.format_for_console".format(cls.__name__) |
| 47 | raise NotImplementedError(msg) |
| 48 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 49 | def run_over_ssh(self, cmd, **kwargs): |
| 50 | return run_over_ssh(self.node.connection, cmd, |
| 51 | node=self.node.get_conn_id(), **kwargs) |
| 52 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 53 | |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 54 | class TwoScriptTest(IPerfTest): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 55 | remote_tmp_dir = '/tmp' |
| 56 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 57 | def __init__(self, opts, *dt, **mp): |
| 58 | IPerfTest.__init__(self, *dt, **mp) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 59 | self.opts = opts |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 60 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 61 | if 'run_script' in self.opts: |
| 62 | self.run_script = self.opts['run_script'] |
| 63 | self.prepare_script = self.opts['prepare_script'] |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 64 | |
| 65 | def get_remote_for_script(self, script): |
| 66 | return os.path.join(self.tmp_dir, script.rpartition('/')[2]) |
| 67 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 68 | def copy_script(self, src): |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 69 | remote_path = self.get_remote_for_script(src) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 70 | copy_paths(self.node.connection, {src: remote_path}) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 71 | return remote_path |
| 72 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 73 | def pre_run(self): |
| 74 | remote_script = self.copy_script(self.node.connection, |
| 75 | self.pre_run_script) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 76 | cmd = remote_script |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 77 | self.run_over_ssh(cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 78 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 79 | def run(self, barrier): |
| 80 | remote_script = self.copy_script(self.node.connection, self.run_script) |
Yulia Portnova | 886a256 | 2015-04-07 11:16:13 +0300 | [diff] [blame] | 81 | cmd_opts = ' '.join(["%s %s" % (key, val) for key, val |
| 82 | in self.opts.items()]) |
| 83 | cmd = remote_script + ' ' + cmd_opts |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 84 | out_err = self.run_over_ssh(cmd) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 85 | self.on_result(out_err, cmd) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 86 | |
| 87 | def parse_results(self, out): |
| 88 | for line in out.split("\n"): |
| 89 | key, separator, value = line.partition(":") |
| 90 | if key and value: |
| 91 | self.on_result_cb((key, float(value))) |
| 92 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 93 | def on_result(self, out_err, cmd): |
| 94 | try: |
| 95 | self.parse_results(out_err) |
| 96 | except Exception as exc: |
| 97 | msg_templ = "Error during postprocessing results: {0!r}. {1}" |
| 98 | raise RuntimeError(msg_templ.format(exc.message, out_err)) |
Yulia Portnova | 7ddfa73 | 2015-02-24 17:32:58 +0200 | [diff] [blame] | 99 | |
| 100 | |
| 101 | class PgBenchTest(TwoScriptTest): |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 102 | root = os.path.dirname(postgres.__file__) |
| 103 | prepare_script = os.path.join(root, "prepare.sh") |
| 104 | run_script = os.path.join(root, "run.sh") |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 105 | |
| 106 | |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 107 | class IOPerfTest(IPerfTest): |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 108 | io_py_remote_templ = "/tmp/test/{0}/io/disk_test_agent.py" |
| 109 | log_fl_templ = "/tmp/test/{0}/io/disk_test_agent_log.txt" |
| 110 | pid_file_templ = "/tmp/test/{0}/io/disk_test_agent_pid_file" |
| 111 | task_file_templ = "/tmp/test/{0}/io/io_task.cfg" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 112 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 113 | def __init__(self, test_options, *dt, **mp): |
| 114 | IPerfTest.__init__(self, *dt, **mp) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 115 | self.options = test_options |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 116 | self.config_fname = test_options['cfg'] |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 117 | self.alive_check_interval = test_options.get('alive_check_interval') |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 118 | self.config_params = test_options.get('params', {}) |
| 119 | self.tool = test_options.get('tool', 'fio') |
| 120 | self.raw_cfg = open(self.config_fname).read() |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 121 | self.configs = list(io_agent.parse_all_in_1(self.raw_cfg, |
| 122 | self.config_params)) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 123 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 124 | cmd_log = os.path.join(self.log_directory, "task_compiled.cfg") |
| 125 | raw_res = os.path.join(self.log_directory, "raw_results.txt") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 126 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 127 | self.io_py_remote = self.io_py_remote_templ.format(self.test_uuid) |
| 128 | self.log_fl = self.log_fl_templ.format(self.test_uuid) |
| 129 | self.pid_file = self.pid_file_templ.format(self.test_uuid) |
| 130 | self.task_file = self.task_file_templ.format(self.test_uuid) |
| 131 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 132 | fio_command_file = open_for_append_or_create(cmd_log) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 133 | |
| 134 | cfg_s_it = io_agent.compile_all_in_1(self.raw_cfg, self.config_params) |
| 135 | splitter = "\n\n" + "-" * 60 + "\n\n" |
| 136 | fio_command_file.write(splitter.join(cfg_s_it)) |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 137 | self.fio_raw_results_file = open_for_append_or_create(raw_res) |
| 138 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 139 | def cleanup(self): |
| 140 | # delete_file(conn, self.io_py_remote) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 141 | # Need to remove tempo files, used for testing |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 142 | pass |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 143 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 144 | def pre_run(self): |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 145 | with self.node.connection.open_sftp() as sftp: |
| 146 | ssh_mkdir(sftp, |
| 147 | os.path.dirname(self.io_py_remote), |
| 148 | intermediate=True) |
| 149 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 150 | try: |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 151 | self.run_over_ssh('which fio') |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 152 | except OSError: |
| 153 | # TODO: install fio, if not installed |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 154 | for i in range(3): |
| 155 | try: |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 156 | self.run_over_ssh("sudo apt-get -y install fio") |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 157 | break |
| 158 | except OSError as err: |
| 159 | time.sleep(3) |
| 160 | else: |
| 161 | raise OSError("Can't install fio - " + err.message) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 162 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 163 | local_fname = os.path.splitext(io_agent.__file__)[0] + ".py" |
| 164 | |
| 165 | self.files_to_copy = { |
| 166 | local_fname: self.io_py_remote, |
| 167 | } |
| 168 | |
| 169 | copy_paths(self.node.connection, self.files_to_copy) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 170 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 171 | if self.options.get('prefill_files', True): |
| 172 | files = {} |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 173 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 174 | for section in self.configs: |
| 175 | sz = ssize_to_b(section.vals['size']) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 176 | msz = sz / (1024 ** 2) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 177 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 178 | if sz % (1024 ** 2) != 0: |
| 179 | msz += 1 |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 180 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 181 | fname = section.vals['filename'] |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 182 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 183 | # if already has other test with the same file name |
| 184 | # take largest size |
| 185 | files[fname] = max(files.get(fname, 0), msz) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 186 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 187 | cmd_templ = "dd oflag=direct " + \ |
| 188 | "if=/dev/zero of={0} bs={1} count={2}" |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 189 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 190 | if self.options.get("use_sudo", True): |
| 191 | cmd_templ = "sudo " + cmd_templ |
| 192 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 193 | ssize = 0 |
| 194 | stime = time.time() |
| 195 | |
| 196 | for fname, curr_sz in files.items(): |
| 197 | cmd = cmd_templ.format(fname, 1024 ** 2, curr_sz) |
| 198 | ssize += curr_sz |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 199 | self.run_over_ssh(cmd, timeout=curr_sz) |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 200 | |
| 201 | ddtime = time.time() - stime |
| 202 | if ddtime > 1E-3: |
| 203 | fill_bw = int(ssize / ddtime) |
| 204 | mess = "Initiall dd fill bw is {0} MiBps for this vm" |
| 205 | logger.info(mess.format(fill_bw)) |
| 206 | else: |
| 207 | logger.warning("Test files prefill disabled") |
koder aka kdanilov | 6e2ae79 | 2015-03-04 18:02:24 -0800 | [diff] [blame] | 208 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 209 | def run(self, barrier): |
| 210 | cmd_templ = "screen -S {screen_name} -d -m " + \ |
| 211 | "env python2 {0} -p {pid_file} -o {results_file} " + \ |
| 212 | "--type {1} {2} --json {3}" |
| 213 | |
| 214 | if self.options.get("use_sudo", True): |
| 215 | cmd_templ = "sudo " + cmd_templ |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 216 | |
| 217 | params = " ".join("{0}={1}".format(k, v) |
| 218 | for k, v in self.config_params.items()) |
| 219 | |
| 220 | if "" != params: |
| 221 | params = "--params " + params |
| 222 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 223 | with self.node.connection.open_sftp() as sftp: |
| 224 | save_to_remote(sftp, |
| 225 | self.task_file, self.raw_cfg) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 226 | |
| 227 | screen_name = self.test_uuid |
| 228 | cmd = cmd_templ.format(self.io_py_remote, |
| 229 | self.tool, |
| 230 | params, |
| 231 | self.task_file, |
| 232 | pid_file=self.pid_file, |
| 233 | results_file=self.log_fl, |
| 234 | screen_name=screen_name) |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 235 | logger.debug("Waiting on barrier") |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 236 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 237 | exec_time = io_agent.calculate_execution_time(self.configs) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 238 | exec_time_str = sec_to_str(exec_time) |
| 239 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 240 | try: |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 241 | timeout = int(exec_time * 2 + 300) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 242 | if barrier.wait(): |
koder aka kdanilov | ea22c3d | 2015-04-21 03:42:22 +0300 | [diff] [blame] | 243 | templ = "Test should takes about {0}." + \ |
| 244 | " Should finish at {1}," + \ |
| 245 | " will wait at most till {2}" |
| 246 | now_dt = datetime.datetime.now() |
| 247 | end_dt = now_dt + datetime.timedelta(0, exec_time) |
| 248 | wait_till = now_dt + datetime.timedelta(0, timeout) |
| 249 | |
| 250 | logger.info(templ.format(exec_time_str, |
| 251 | end_dt.strftime("%H:%M:%S"), |
| 252 | wait_till.strftime("%H:%M:%S"))) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 253 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 254 | self.run_over_ssh(cmd) |
| 255 | logger.debug("Test started in screen {0}".format(screen_name)) |
| 256 | |
| 257 | end_of_wait_time = timeout + time.time() |
| 258 | |
| 259 | # time_till_check = random.randint(30, 90) |
| 260 | time_till_check = 1 |
| 261 | |
| 262 | pid = None |
| 263 | no_pid_file = True |
| 264 | tcp_conn_timeout = 30 |
| 265 | pid_get_timeout = 30 + time.time() |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 266 | connection_ok = True |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 267 | |
| 268 | # TODO: add monitoring socket |
| 269 | if self.node.connection is not Local: |
| 270 | self.node.connection.close() |
| 271 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 272 | conn_id = self.node.get_conn_id() |
| 273 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 274 | while end_of_wait_time > time.time(): |
| 275 | conn = None |
| 276 | time.sleep(time_till_check) |
| 277 | |
| 278 | try: |
| 279 | if self.node.connection is not Local: |
| 280 | conn = connect(self.node.conn_url, |
| 281 | conn_timeout=tcp_conn_timeout) |
| 282 | else: |
| 283 | conn = self.node.connection |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 284 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 285 | try: |
| 286 | with conn.open_sftp() as sftp: |
| 287 | pid = read_from_remote(sftp, self.pid_file) |
| 288 | no_pid_file = False |
| 289 | except (NameError, IOError): |
| 290 | no_pid_file = True |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 291 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 292 | sftp.close() |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 293 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 294 | if conn is not Local: |
| 295 | conn.close() |
| 296 | |
| 297 | if no_pid_file: |
| 298 | if pid is None: |
| 299 | if time.time() > pid_get_timeout: |
| 300 | msg = ("On node {0} pid file doesn't " + |
| 301 | "appears in time") |
| 302 | logger.error(msg.format(conn_id)) |
| 303 | raise RuntimeError("Start timeout") |
| 304 | else: |
| 305 | # execution finished |
| 306 | break |
| 307 | if not connection_ok: |
| 308 | msg = "Connection with {0} is restored" |
| 309 | logger.debug(msg.format(conn_id)) |
| 310 | connection_ok = True |
| 311 | |
| 312 | except (socket.error, SSHException) as exc: |
| 313 | if connection_ok: |
| 314 | connection_ok = False |
| 315 | msg = "Lost connection with " + conn_id |
| 316 | msg += ". Error: " + exc.message |
| 317 | logger.debug(msg) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 318 | |
| 319 | logger.debug("Done") |
| 320 | |
| 321 | if self.node.connection is not Local: |
| 322 | timeout = tcp_conn_timeout * 3 |
| 323 | self.node.connection = connect(self.node.conn_url, |
| 324 | conn_timeout=timeout) |
| 325 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame^] | 326 | with self.node.connection.open_sftp() as sftp: |
| 327 | # try to reboot and then connect |
| 328 | out_err = read_from_remote(, |
| 329 | self.log_fl) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 330 | finally: |
| 331 | barrier.exit() |
| 332 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 333 | self.on_result(out_err, cmd) |
| 334 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 335 | def on_result(self, out_err, cmd): |
| 336 | try: |
| 337 | for data in parse_output(out_err): |
| 338 | self.on_result_cb(data) |
| 339 | except Exception as exc: |
| 340 | msg_templ = "Error during postprocessing results: {0!r}" |
| 341 | raise RuntimeError(msg_templ.format(exc.message)) |
| 342 | |
| 343 | def merge_results(self, results): |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 344 | if len(results) == 0: |
| 345 | return None |
| 346 | |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 347 | merged_result = results[0] |
| 348 | merged_data = merged_result['res'] |
| 349 | expected_keys = set(merged_data.keys()) |
koder aka kdanilov | 4e9f3ed | 2015-04-14 11:26:12 +0300 | [diff] [blame] | 350 | mergable_fields = ['bw', 'clat', 'iops', 'lat', 'slat'] |
koder aka kdanilov | 66839a9 | 2015-04-11 13:22:31 +0300 | [diff] [blame] | 351 | |
| 352 | for res in results[1:]: |
| 353 | assert res['__meta__'] == merged_result['__meta__'] |
| 354 | |
| 355 | data = res['res'] |
| 356 | diff = set(data.keys()).symmetric_difference(expected_keys) |
| 357 | |
| 358 | msg = "Difference: {0}".format(",".join(diff)) |
| 359 | assert len(diff) == 0, msg |
| 360 | |
| 361 | for testname, test_data in data.items(): |
| 362 | res_test_data = merged_data[testname] |
| 363 | |
| 364 | diff = set(test_data.keys()).symmetric_difference( |
| 365 | res_test_data.keys()) |
| 366 | |
| 367 | msg = "Difference: {0}".format(",".join(diff)) |
| 368 | assert len(diff) == 0, msg |
| 369 | |
| 370 | for k, v in test_data.items(): |
| 371 | if k in mergable_fields: |
| 372 | res_test_data[k].extend(v) |
| 373 | else: |
| 374 | msg = "{0!r} != {1!r}".format(res_test_data[k], v) |
| 375 | assert res_test_data[k] == v, msg |
| 376 | |
| 377 | return merged_result |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 378 | |
| 379 | @classmethod |
| 380 | def format_for_console(cls, data): |
| 381 | return io_formatter.format_results_for_console(data) |