Sean Dague | 556add5 | 2013-07-19 14:28:44 -0400 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 13 | import re |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 14 | import sys |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 15 | import time |
| 16 | |
Masayuki Igawa | 134d9f7 | 2017-02-10 18:05:26 +0900 | [diff] [blame] | 17 | import netaddr |
| 18 | import six |
| 19 | |
David Kranz | 968f1b3 | 2015-06-18 16:58:18 -0400 | [diff] [blame] | 20 | from oslo_log import log as logging |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 21 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 22 | from tempest import config |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 23 | from tempest.lib.common import ssh |
Jordan Pittier | 9e227c5 | 2016-02-09 14:35:18 +0100 | [diff] [blame] | 24 | from tempest.lib.common.utils import test_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 25 | import tempest.lib.exceptions |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 26 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 27 | CONF = config.CONF |
| 28 | |
David Kranz | 968f1b3 | 2015-06-18 16:58:18 -0400 | [diff] [blame] | 29 | LOG = logging.getLogger(__name__) |
| 30 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 31 | |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 32 | def debug_ssh(function): |
| 33 | """Decorator to generate extra debug info in case off SSH failure""" |
| 34 | def wrapper(self, *args, **kwargs): |
| 35 | try: |
| 36 | return function(self, *args, **kwargs) |
| 37 | except tempest.lib.exceptions.SSHTimeout: |
| 38 | try: |
| 39 | original_exception = sys.exc_info() |
Jordan Pittier | 9e227c5 | 2016-02-09 14:35:18 +0100 | [diff] [blame] | 40 | caller = test_utils.find_test_caller() or "not found" |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 41 | if self.server: |
| 42 | msg = 'Caller: %s. Timeout trying to ssh to server %s' |
| 43 | LOG.debug(msg, caller, self.server) |
| 44 | if self.log_console and self.servers_client: |
| 45 | try: |
| 46 | msg = 'Console log for server %s: %s' |
| 47 | console_log = ( |
| 48 | self.servers_client.get_console_output( |
| 49 | self.server['id'])['output']) |
| 50 | LOG.debug(msg, self.server['id'], console_log) |
| 51 | except Exception: |
| 52 | msg = 'Could not get console_log for server %s' |
| 53 | LOG.debug(msg, self.server['id']) |
| 54 | # re-raise the original ssh timeout exception |
| 55 | six.reraise(*original_exception) |
| 56 | finally: |
| 57 | # Delete the traceback to avoid circular references |
| 58 | _, _, trace = original_exception |
| 59 | del trace |
| 60 | return wrapper |
| 61 | |
| 62 | |
Joe Gordon | 28788b4 | 2015-02-25 12:42:37 -0800 | [diff] [blame] | 63 | class RemoteClient(object): |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 64 | |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 65 | def __init__(self, ip_address, username, password=None, pkey=None, |
| 66 | server=None, servers_client=None): |
| 67 | """Executes commands in a VM over ssh |
| 68 | |
| 69 | :param ip_address: IP address to ssh to |
| 70 | :param username: ssh username |
| 71 | :param password: ssh password (optional) |
| 72 | :param pkey: ssh public key (optional) |
| 73 | :param server: server dict, used for debugging purposes |
| 74 | :param servers_client: servers client, used for debugging purposes |
| 75 | """ |
| 76 | self.server = server |
| 77 | self.servers_client = servers_client |
Ken'ichi Ohmichi | 191e1ca | 2017-03-01 11:52:34 -0800 | [diff] [blame] | 78 | |
nithya-ganesan | 67da287 | 2015-02-08 23:13:48 +0000 | [diff] [blame] | 79 | ssh_timeout = CONF.validation.ssh_timeout |
nithya-ganesan | 67da287 | 2015-02-08 23:13:48 +0000 | [diff] [blame] | 80 | connect_timeout = CONF.validation.connect_timeout |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 81 | self.log_console = CONF.compute_feature_enabled.console_output |
Ken'ichi Ohmichi | 191e1ca | 2017-03-01 11:52:34 -0800 | [diff] [blame] | 82 | self.ssh_shell_prologue = CONF.validation.ssh_shell_prologue |
Ken'ichi Ohmichi | 191e1ca | 2017-03-01 11:52:34 -0800 | [diff] [blame] | 83 | self.ping_count = CONF.validation.ping_count |
| 84 | self.ping_size = CONF.validation.ping_size |
Sean Dague | 20e9861 | 2016-01-06 14:33:28 -0500 | [diff] [blame] | 85 | |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 86 | self.ssh_client = ssh.Client(ip_address, username, password, |
| 87 | ssh_timeout, pkey=pkey, |
nithya-ganesan | 67da287 | 2015-02-08 23:13:48 +0000 | [diff] [blame] | 88 | channel_timeout=connect_timeout) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 89 | |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 90 | @debug_ssh |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 91 | def exec_command(self, cmd): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 92 | # Shell options below add more clearness on failures, |
| 93 | # path is extended for some non-cirros guest oses (centos7) |
Ken'ichi Ohmichi | 191e1ca | 2017-03-01 11:52:34 -0800 | [diff] [blame] | 94 | cmd = self.ssh_shell_prologue + " " + cmd |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 95 | LOG.debug("Remote command: %s", cmd) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 96 | return self.ssh_client.exec_command(cmd) |
| 97 | |
Andrea Frittoli (andreaf) | 5f5e4fc | 2016-04-29 16:00:17 -0500 | [diff] [blame] | 98 | @debug_ssh |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 99 | def validate_authentication(self): |
| 100 | """Validate ssh connection and authentication |
Ken'ichi Ohmichi | cb67d2d | 2015-11-19 08:23:22 +0000 | [diff] [blame] | 101 | |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 102 | This method raises an Exception when the validation fails. |
| 103 | """ |
| 104 | self.ssh_client.test_connection_auth() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 105 | |
Evgeny Antyshev | 4894a91 | 2016-11-21 12:17:18 +0000 | [diff] [blame] | 106 | def get_disks(self): |
| 107 | # Select root disk devices as shown by lsblk |
| 108 | command = 'lsblk -lb --nodeps' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 109 | output = self.exec_command(command) |
Evgeny Antyshev | 4894a91 | 2016-11-21 12:17:18 +0000 | [diff] [blame] | 110 | selected = [] |
| 111 | pos = None |
| 112 | for l in output.splitlines(): |
| 113 | if pos is None and l.find("TYPE") > 0: |
| 114 | pos = l.find("TYPE") |
| 115 | # Show header line too |
| 116 | selected.append(l) |
| 117 | # lsblk lists disk type in a column right-aligned with TYPE |
| 118 | elif pos > 0 and l[pos:pos + 4] == "disk": |
| 119 | selected.append(l) |
| 120 | |
| 121 | return "\n".join(selected) |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 122 | |
| 123 | def get_boot_time(self): |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame] | 124 | cmd = 'cut -f1 -d. /proc/uptime' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 125 | boot_secs = self.exec_command(cmd) |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame] | 126 | boot_time = time.time() - int(boot_secs) |
| 127 | return time.localtime(boot_time) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 128 | |
| 129 | def write_to_console(self, message): |
| 130 | message = re.sub("([$\\`])", "\\\\\\\\\\1", message) |
| 131 | # usually to /dev/ttyS0 |
| 132 | cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 133 | return self.exec_command(cmd) |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 134 | |
Ken'ichi Ohmichi | 191e1ca | 2017-03-01 11:52:34 -0800 | [diff] [blame] | 135 | def ping_host(self, host, count=None, size=None, nic=None): |
| 136 | if count is None: |
| 137 | count = self.ping_count |
| 138 | if size is None: |
| 139 | size = self.ping_size |
| 140 | |
Kirill Shileev | 1411357 | 2014-11-21 16:58:02 +0300 | [diff] [blame] | 141 | addr = netaddr.IPAddress(host) |
| 142 | cmd = 'ping6' if addr.version == 6 else 'ping' |
Yair Fried | bc46f59 | 2015-11-18 16:29:34 +0200 | [diff] [blame] | 143 | if nic: |
| 144 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
Richard Winters | f87059b | 2015-02-17 11:46:54 -0500 | [diff] [blame] | 145 | cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 146 | return self.exec_command(cmd) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 147 | |
Yair Fried | bc46f59 | 2015-11-18 16:29:34 +0200 | [diff] [blame] | 148 | def set_mac_address(self, nic, address): |
| 149 | self.set_nic_state(nic=nic, state="down") |
| 150 | cmd = "sudo ip link set dev {0} address {1}".format(nic, address) |
| 151 | self.exec_command(cmd) |
| 152 | self.set_nic_state(nic=nic, state="up") |
| 153 | |
| 154 | def get_mac_address(self, nic=""): |
| 155 | show_nic = "show {nic} ".format(nic=nic) if nic else "" |
| 156 | cmd = "ip addr %s| awk '/ether/ {print $2}'" % show_nic |
| 157 | return self.exec_command(cmd).strip().lower() |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 158 | |
Evgeny Antyshev | 9b77ae5 | 2016-02-16 09:48:57 +0000 | [diff] [blame] | 159 | def get_nic_name_by_mac(self, address): |
| 160 | cmd = "ip -o link | awk '/%s/ {print $2}'" % address |
| 161 | nic = self.exec_command(cmd) |
| 162 | return nic.strip().strip(":").lower() |
| 163 | |
| 164 | def get_nic_name_by_ip(self, address): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 165 | cmd = "ip -o addr | awk '/%s/ {print $2}'" % address |
Daniel Mellado | 9e3e106 | 2015-08-06 18:07:05 +0200 | [diff] [blame] | 166 | nic = self.exec_command(cmd) |
| 167 | return nic.strip().strip(":").lower() |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 168 | |
Ken'ichi Ohmichi | cc1264c | 2017-03-01 12:27:28 -0800 | [diff] [blame] | 169 | def assign_static_ip(self, nic, addr, network_mask_bits=28): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 170 | cmd = "sudo ip addr add {ip}/{mask} dev {nic}".format( |
Ken'ichi Ohmichi | cc1264c | 2017-03-01 12:27:28 -0800 | [diff] [blame] | 171 | ip=addr, mask=network_mask_bits, nic=nic) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 172 | return self.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 173 | |
Yair Fried | bc46f59 | 2015-11-18 16:29:34 +0200 | [diff] [blame] | 174 | def set_nic_state(self, nic, state="up"): |
| 175 | cmd = "sudo ip link set {nic} {state}".format(nic=nic, state=state) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 176 | return self.exec_command(cmd) |
Elena Ezhova | 4a27b46 | 2014-04-09 15:25:46 +0400 | [diff] [blame] | 177 | |
| 178 | def get_pids(self, pr_name): |
| 179 | # Get pid(s) of a process/program |
| 180 | cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name |
| 181 | return self.exec_command(cmd).split('\n') |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 182 | |
| 183 | def get_dns_servers(self): |
| 184 | cmd = 'cat /etc/resolv.conf' |
| 185 | resolve_file = self.exec_command(cmd).strip().split('\n') |
| 186 | entries = (l.split() for l in resolve_file) |
| 187 | dns_servers = [l[1] for l in entries |
| 188 | if len(l) and l[0] == 'nameserver'] |
| 189 | return dns_servers |
| 190 | |
| 191 | def send_signal(self, pid, signum): |
| 192 | cmd = 'sudo /bin/kill -{sig} {pid}'.format(pid=pid, sig=signum) |
| 193 | return self.exec_command(cmd) |
| 194 | |
| 195 | def _renew_lease_udhcpc(self, fixed_ip=None): |
| 196 | """Renews DHCP lease via udhcpc client. """ |
| 197 | file_path = '/var/run/udhcpc.' |
Evgeny Antyshev | 9b77ae5 | 2016-02-16 09:48:57 +0000 | [diff] [blame] | 198 | nic_name = self.get_nic_name_by_ip(fixed_ip) |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 199 | pid = self.exec_command('cat {path}{nic}.pid'. |
| 200 | format(path=file_path, nic=nic_name)) |
| 201 | pid = pid.strip() |
| 202 | self.send_signal(pid, 'USR1') |
| 203 | |
| 204 | def _renew_lease_dhclient(self, fixed_ip=None): |
| 205 | """Renews DHCP lease via dhclient client. """ |
Itzik Brown | ffb1402 | 2015-03-23 17:03:55 +0200 | [diff] [blame] | 206 | cmd = "sudo /sbin/dhclient -r && sudo /sbin/dhclient" |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 207 | self.exec_command(cmd) |
| 208 | |
Ken'ichi Ohmichi | 4e33785 | 2017-03-01 12:04:23 -0800 | [diff] [blame] | 209 | def renew_lease(self, fixed_ip=None, dhcp_client='udhcpc'): |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 210 | """Wrapper method for renewing DHCP lease via given client |
| 211 | |
| 212 | Supporting: |
| 213 | * udhcpc |
| 214 | * dhclient |
| 215 | """ |
| 216 | # TODO(yfried): add support for dhcpcd |
Takashi NATSUME | 6d5a2b4 | 2015-09-08 11:27:49 +0900 | [diff] [blame] | 217 | supported_clients = ['udhcpc', 'dhclient'] |
Takashi NATSUME | 6d5a2b4 | 2015-09-08 11:27:49 +0900 | [diff] [blame] | 218 | if dhcp_client not in supported_clients: |
Matthew Treinish | 4217a70 | 2016-10-07 17:27:11 -0400 | [diff] [blame] | 219 | raise tempest.lib.exceptions.InvalidConfiguration( |
| 220 | '%s DHCP client unsupported' % dhcp_client) |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 221 | if dhcp_client == 'udhcpc' and not fixed_ip: |
| 222 | raise ValueError("need to set 'fixed_ip' for udhcpc client") |
Joe Gordon | 28788b4 | 2015-02-25 12:42:37 -0800 | [diff] [blame] | 223 | return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip) |
Alexander Gubanov | abd154c | 2015-09-23 23:24:06 +0300 | [diff] [blame] | 224 | |
| 225 | def mount(self, dev_name, mount_path='/mnt'): |
| 226 | cmd_mount = 'sudo mount /dev/%s %s' % (dev_name, mount_path) |
| 227 | self.exec_command(cmd_mount) |
| 228 | |
| 229 | def umount(self, mount_path='/mnt'): |
| 230 | self.exec_command('sudo umount %s' % mount_path) |
| 231 | |
| 232 | def make_fs(self, dev_name, fs='ext4'): |
| 233 | cmd_mkfs = 'sudo /usr/sbin/mke2fs -t %s /dev/%s' % (fs, dev_name) |
Sean Dague | 57c6655 | 2016-02-08 08:51:13 -0500 | [diff] [blame] | 234 | try: |
| 235 | self.exec_command(cmd_mkfs) |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 236 | except tempest.lib.exceptions.SSHExecCommandFailed: |
Sean Dague | 57c6655 | 2016-02-08 08:51:13 -0500 | [diff] [blame] | 237 | LOG.error("Couldn't mke2fs") |
| 238 | cmd_why = 'sudo ls -lR /dev' |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 239 | LOG.info("Contents of /dev: %s", self.exec_command(cmd_why)) |
Sean Dague | 57c6655 | 2016-02-08 08:51:13 -0500 | [diff] [blame] | 240 | raise |