blob: ba81f8ae27b8b3eafcf08bada2d68ab8464d2148 [file] [log] [blame]
Sean Dague556add52013-07-19 14:28:44 -04001# 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 Fazekasa23f5002012-10-23 19:32:45 +020013import re
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -050014import sys
Matthew Treinisha83a16e2012-12-07 13:44:02 -050015import time
16
Masayuki Igawa134d9f72017-02-10 18:05:26 +090017import netaddr
18import six
19
David Kranz968f1b32015-06-18 16:58:18 -040020from oslo_log import log as logging
Matthew Treinish96e9e882014-06-09 18:37:19 -040021
Sean Dague86bd8422013-12-20 09:56:44 -050022from tempest import config
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050023from tempest.lib.common import ssh
Jordan Pittier9e227c52016-02-09 14:35:18 +010024from tempest.lib.common.utils import test_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050025import tempest.lib.exceptions
Daryl Walleck98e66dd2012-06-21 04:58:39 -050026
Sean Dague86bd8422013-12-20 09:56:44 -050027CONF = config.CONF
28
David Kranz968f1b32015-06-18 16:58:18 -040029LOG = logging.getLogger(__name__)
30
Daryl Walleck6b9b2882012-04-08 21:43:39 -050031
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -050032def 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 Pittier9e227c52016-02-09 14:35:18 +010040 caller = test_utils.find_test_caller() or "not found"
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -050041 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 Gordon28788b42015-02-25 12:42:37 -080063class RemoteClient(object):
Daryl Walleck6b9b2882012-04-08 21:43:39 -050064
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -050065 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 Ohmichi191e1ca2017-03-01 11:52:34 -080078
nithya-ganesan67da2872015-02-08 23:13:48 +000079 ssh_timeout = CONF.validation.ssh_timeout
nithya-ganesan67da2872015-02-08 23:13:48 +000080 connect_timeout = CONF.validation.connect_timeout
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -050081 self.log_console = CONF.compute_feature_enabled.console_output
Ken'ichi Ohmichi191e1ca2017-03-01 11:52:34 -080082 self.ssh_shell_prologue = CONF.validation.ssh_shell_prologue
83 self.project_network_mask_bits = CONF.network.project_network_mask_bits
84 self.dhcp_client = CONF.scenario.dhcp_client
85 self.ping_count = CONF.validation.ping_count
86 self.ping_size = CONF.validation.ping_size
Sean Dague20e98612016-01-06 14:33:28 -050087
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090088 self.ssh_client = ssh.Client(ip_address, username, password,
89 ssh_timeout, pkey=pkey,
nithya-ganesan67da2872015-02-08 23:13:48 +000090 channel_timeout=connect_timeout)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050091
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -050092 @debug_ssh
Elena Ezhova91db24e2014-02-28 20:47:10 +040093 def exec_command(self, cmd):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +000094 # Shell options below add more clearness on failures,
95 # path is extended for some non-cirros guest oses (centos7)
Ken'ichi Ohmichi191e1ca2017-03-01 11:52:34 -080096 cmd = self.ssh_shell_prologue + " " + cmd
Jordan Pittier525ec712016-12-07 17:51:26 +010097 LOG.debug("Remote command: %s", cmd)
Elena Ezhova91db24e2014-02-28 20:47:10 +040098 return self.ssh_client.exec_command(cmd)
99
Andrea Frittoli (andreaf)5f5e4fc2016-04-29 16:00:17 -0500100 @debug_ssh
Attila Fazekasad7ef7d2013-11-20 10:12:53 +0100101 def validate_authentication(self):
102 """Validate ssh connection and authentication
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +0000103
Attila Fazekasad7ef7d2013-11-20 10:12:53 +0100104 This method raises an Exception when the validation fails.
105 """
106 self.ssh_client.test_connection_auth()
Daryl Walleck6b9b2882012-04-08 21:43:39 -0500107
Evgeny Antyshev4894a912016-11-21 12:17:18 +0000108 def get_disks(self):
109 # Select root disk devices as shown by lsblk
110 command = 'lsblk -lb --nodeps'
Elena Ezhova91db24e2014-02-28 20:47:10 +0400111 output = self.exec_command(command)
Evgeny Antyshev4894a912016-11-21 12:17:18 +0000112 selected = []
113 pos = None
114 for l in output.splitlines():
115 if pos is None and l.find("TYPE") > 0:
116 pos = l.find("TYPE")
117 # Show header line too
118 selected.append(l)
119 # lsblk lists disk type in a column right-aligned with TYPE
120 elif pos > 0 and l[pos:pos + 4] == "disk":
121 selected.append(l)
122
123 return "\n".join(selected)
Daryl Walleck98e66dd2012-06-21 04:58:39 -0500124
125 def get_boot_time(self):
Vincent Untz3c0b5b92014-01-18 10:56:00 +0100126 cmd = 'cut -f1 -d. /proc/uptime'
Elena Ezhova91db24e2014-02-28 20:47:10 +0400127 boot_secs = self.exec_command(cmd)
Vincent Untz3c0b5b92014-01-18 10:56:00 +0100128 boot_time = time.time() - int(boot_secs)
129 return time.localtime(boot_time)
Attila Fazekasa23f5002012-10-23 19:32:45 +0200130
131 def write_to_console(self, message):
132 message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
133 # usually to /dev/ttyS0
134 cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
Elena Ezhova91db24e2014-02-28 20:47:10 +0400135 return self.exec_command(cmd)
Yair Fried5f670ab2013-12-09 09:26:51 +0200136
Ken'ichi Ohmichi191e1ca2017-03-01 11:52:34 -0800137 def ping_host(self, host, count=None, size=None, nic=None):
138 if count is None:
139 count = self.ping_count
140 if size is None:
141 size = self.ping_size
142
Kirill Shileev14113572014-11-21 16:58:02 +0300143 addr = netaddr.IPAddress(host)
144 cmd = 'ping6' if addr.version == 6 else 'ping'
Yair Friedbc46f592015-11-18 16:29:34 +0200145 if nic:
146 cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic)
Richard Wintersf87059b2015-02-17 11:46:54 -0500147 cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host)
Elena Ezhova91db24e2014-02-28 20:47:10 +0400148 return self.exec_command(cmd)
Yair Fried4d7efa62013-11-17 17:12:29 +0200149
Yair Friedbc46f592015-11-18 16:29:34 +0200150 def set_mac_address(self, nic, address):
151 self.set_nic_state(nic=nic, state="down")
152 cmd = "sudo ip link set dev {0} address {1}".format(nic, address)
153 self.exec_command(cmd)
154 self.set_nic_state(nic=nic, state="up")
155
156 def get_mac_address(self, nic=""):
157 show_nic = "show {nic} ".format(nic=nic) if nic else ""
158 cmd = "ip addr %s| awk '/ether/ {print $2}'" % show_nic
159 return self.exec_command(cmd).strip().lower()
Yair Fried3097dc12014-01-26 08:46:43 +0200160
Evgeny Antyshev9b77ae52016-02-16 09:48:57 +0000161 def get_nic_name_by_mac(self, address):
162 cmd = "ip -o link | awk '/%s/ {print $2}'" % address
163 nic = self.exec_command(cmd)
164 return nic.strip().strip(":").lower()
165
166 def get_nic_name_by_ip(self, address):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000167 cmd = "ip -o addr | awk '/%s/ {print $2}'" % address
Daniel Mellado9e3e1062015-08-06 18:07:05 +0200168 nic = self.exec_command(cmd)
169 return nic.strip().strip(":").lower()
Yair Fried413bf2d2014-11-19 17:07:11 +0200170
Yair Fried3097dc12014-01-26 08:46:43 +0200171 def get_ip_list(self):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000172 cmd = "ip address"
Elena Ezhova91db24e2014-02-28 20:47:10 +0400173 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200174
175 def assign_static_ip(self, nic, addr):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000176 cmd = "sudo ip addr add {ip}/{mask} dev {nic}".format(
Ken'ichi Ohmichi191e1ca2017-03-01 11:52:34 -0800177 ip=addr, mask=self.project_network_mask_bits,
Yair Fried3097dc12014-01-26 08:46:43 +0200178 nic=nic
179 )
Elena Ezhova91db24e2014-02-28 20:47:10 +0400180 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200181
Yair Friedbc46f592015-11-18 16:29:34 +0200182 def set_nic_state(self, nic, state="up"):
183 cmd = "sudo ip link set {nic} {state}".format(nic=nic, state=state)
Elena Ezhova91db24e2014-02-28 20:47:10 +0400184 return self.exec_command(cmd)
Elena Ezhova4a27b462014-04-09 15:25:46 +0400185
186 def get_pids(self, pr_name):
187 # Get pid(s) of a process/program
188 cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name
189 return self.exec_command(cmd).split('\n')
Yair Fried413bf2d2014-11-19 17:07:11 +0200190
191 def get_dns_servers(self):
192 cmd = 'cat /etc/resolv.conf'
193 resolve_file = self.exec_command(cmd).strip().split('\n')
194 entries = (l.split() for l in resolve_file)
195 dns_servers = [l[1] for l in entries
196 if len(l) and l[0] == 'nameserver']
197 return dns_servers
198
199 def send_signal(self, pid, signum):
200 cmd = 'sudo /bin/kill -{sig} {pid}'.format(pid=pid, sig=signum)
201 return self.exec_command(cmd)
202
203 def _renew_lease_udhcpc(self, fixed_ip=None):
204 """Renews DHCP lease via udhcpc client. """
205 file_path = '/var/run/udhcpc.'
Evgeny Antyshev9b77ae52016-02-16 09:48:57 +0000206 nic_name = self.get_nic_name_by_ip(fixed_ip)
Yair Fried413bf2d2014-11-19 17:07:11 +0200207 pid = self.exec_command('cat {path}{nic}.pid'.
208 format(path=file_path, nic=nic_name))
209 pid = pid.strip()
210 self.send_signal(pid, 'USR1')
211
212 def _renew_lease_dhclient(self, fixed_ip=None):
213 """Renews DHCP lease via dhclient client. """
Itzik Brownffb14022015-03-23 17:03:55 +0200214 cmd = "sudo /sbin/dhclient -r && sudo /sbin/dhclient"
Yair Fried413bf2d2014-11-19 17:07:11 +0200215 self.exec_command(cmd)
216
217 def renew_lease(self, fixed_ip=None):
218 """Wrapper method for renewing DHCP lease via given client
219
220 Supporting:
221 * udhcpc
222 * dhclient
223 """
224 # TODO(yfried): add support for dhcpcd
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +0900225 supported_clients = ['udhcpc', 'dhclient']
Ken'ichi Ohmichi191e1ca2017-03-01 11:52:34 -0800226 dhcp_client = self.dhcp_client
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +0900227 if dhcp_client not in supported_clients:
Matthew Treinish4217a702016-10-07 17:27:11 -0400228 raise tempest.lib.exceptions.InvalidConfiguration(
229 '%s DHCP client unsupported' % dhcp_client)
Yair Fried413bf2d2014-11-19 17:07:11 +0200230 if dhcp_client == 'udhcpc' and not fixed_ip:
231 raise ValueError("need to set 'fixed_ip' for udhcpc client")
Joe Gordon28788b42015-02-25 12:42:37 -0800232 return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip)
Alexander Gubanovabd154c2015-09-23 23:24:06 +0300233
234 def mount(self, dev_name, mount_path='/mnt'):
235 cmd_mount = 'sudo mount /dev/%s %s' % (dev_name, mount_path)
236 self.exec_command(cmd_mount)
237
238 def umount(self, mount_path='/mnt'):
239 self.exec_command('sudo umount %s' % mount_path)
240
241 def make_fs(self, dev_name, fs='ext4'):
242 cmd_mkfs = 'sudo /usr/sbin/mke2fs -t %s /dev/%s' % (fs, dev_name)
Sean Dague57c66552016-02-08 08:51:13 -0500243 try:
244 self.exec_command(cmd_mkfs)
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -0500245 except tempest.lib.exceptions.SSHExecCommandFailed:
Sean Dague57c66552016-02-08 08:51:13 -0500246 LOG.error("Couldn't mke2fs")
247 cmd_why = 'sudo ls -lR /dev'
Jordan Pittier525ec712016-12-07 17:51:26 +0100248 LOG.info("Contents of /dev: %s", self.exec_command(cmd_why))
Sean Dague57c66552016-02-08 08:51:13 -0500249 raise