blob: 2e233c5eb011e40e0f2a6d2f094a56ea39592556 [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
Kirill Shileev14113572014-11-21 16:58:02 +030013import netaddr
Attila Fazekasa23f5002012-10-23 19:32:45 +020014import re
Matthew Treinisha83a16e2012-12-07 13:44:02 -050015import time
16
David Kranz968f1b32015-06-18 16:58:18 -040017from oslo_log import log as logging
Andrey Pavlov64723762015-04-29 06:24:58 +030018from tempest_lib.common import ssh
Matthew Treinish96e9e882014-06-09 18:37:19 -040019
Sean Dague86bd8422013-12-20 09:56:44 -050020from tempest import config
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090021from tempest import exceptions
Daryl Walleck98e66dd2012-06-21 04:58:39 -050022
Sean Dague86bd8422013-12-20 09:56:44 -050023CONF = config.CONF
24
David Kranz968f1b32015-06-18 16:58:18 -040025LOG = logging.getLogger(__name__)
26
Daryl Walleck6b9b2882012-04-08 21:43:39 -050027
Joe Gordon28788b42015-02-25 12:42:37 -080028class RemoteClient(object):
Daryl Walleck6b9b2882012-04-08 21:43:39 -050029
Sean Dague20e98612016-01-06 14:33:28 -050030 def __init__(self, ip_address, username, password=None, pkey=None):
nithya-ganesan67da2872015-02-08 23:13:48 +000031 ssh_timeout = CONF.validation.ssh_timeout
nithya-ganesan67da2872015-02-08 23:13:48 +000032 connect_timeout = CONF.validation.connect_timeout
Sean Dague20e98612016-01-06 14:33:28 -050033
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090034 self.ssh_client = ssh.Client(ip_address, username, password,
35 ssh_timeout, pkey=pkey,
nithya-ganesan67da2872015-02-08 23:13:48 +000036 channel_timeout=connect_timeout)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050037
Elena Ezhova91db24e2014-02-28 20:47:10 +040038 def exec_command(self, cmd):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +000039 # Shell options below add more clearness on failures,
40 # path is extended for some non-cirros guest oses (centos7)
lanoux283273b2015-12-04 03:01:54 -080041 cmd = CONF.validation.ssh_shell_prologue + " " + cmd
David Kranz968f1b32015-06-18 16:58:18 -040042 LOG.debug("Remote command: %s" % cmd)
Elena Ezhova91db24e2014-02-28 20:47:10 +040043 return self.ssh_client.exec_command(cmd)
44
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010045 def validate_authentication(self):
46 """Validate ssh connection and authentication
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +000047
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010048 This method raises an Exception when the validation fails.
49 """
50 self.ssh_client.test_connection_auth()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050051
52 def hostname_equals_servername(self, expected_hostname):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070053 # Get host name using command "hostname"
Elena Ezhova91db24e2014-02-28 20:47:10 +040054 actual_hostname = self.exec_command("hostname").rstrip()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050055 return expected_hostname == actual_hostname
56
Daryl Walleck6b9b2882012-04-08 21:43:39 -050057 def get_ram_size_in_mb(self):
Elena Ezhova91db24e2014-02-28 20:47:10 +040058 output = self.exec_command('free -m | grep Mem')
Daryl Walleck6b9b2882012-04-08 21:43:39 -050059 if output:
60 return output.split()[1]
61
62 def get_number_of_vcpus(self):
Mathieu Gagné2020d6e2015-12-10 19:52:12 -050063 output = self.exec_command('grep -c ^processor /proc/cpuinfo')
Daryl Walleck6b9b2882012-04-08 21:43:39 -050064 return int(output)
Dan Smithc18d8c62012-07-02 08:09:26 -070065
66 def get_partitions(self):
67 # Return the contents of /proc/partitions
68 command = 'cat /proc/partitions'
Elena Ezhova91db24e2014-02-28 20:47:10 +040069 output = self.exec_command(command)
Dan Smithc18d8c62012-07-02 08:09:26 -070070 return output
Daryl Walleck98e66dd2012-06-21 04:58:39 -050071
72 def get_boot_time(self):
Vincent Untz3c0b5b92014-01-18 10:56:00 +010073 cmd = 'cut -f1 -d. /proc/uptime'
Elena Ezhova91db24e2014-02-28 20:47:10 +040074 boot_secs = self.exec_command(cmd)
Vincent Untz3c0b5b92014-01-18 10:56:00 +010075 boot_time = time.time() - int(boot_secs)
76 return time.localtime(boot_time)
Attila Fazekasa23f5002012-10-23 19:32:45 +020077
78 def write_to_console(self, message):
79 message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
80 # usually to /dev/ttyS0
81 cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
Elena Ezhova91db24e2014-02-28 20:47:10 +040082 return self.exec_command(cmd)
Yair Fried5f670ab2013-12-09 09:26:51 +020083
lanoux283273b2015-12-04 03:01:54 -080084 def ping_host(self, host, count=CONF.validation.ping_count,
85 size=CONF.validation.ping_size, nic=None):
Kirill Shileev14113572014-11-21 16:58:02 +030086 addr = netaddr.IPAddress(host)
87 cmd = 'ping6' if addr.version == 6 else 'ping'
Yair Friedbc46f592015-11-18 16:29:34 +020088 if nic:
89 cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic)
Richard Wintersf87059b2015-02-17 11:46:54 -050090 cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host)
Elena Ezhova91db24e2014-02-28 20:47:10 +040091 return self.exec_command(cmd)
Yair Fried4d7efa62013-11-17 17:12:29 +020092
Yair Friedbc46f592015-11-18 16:29:34 +020093 def set_mac_address(self, nic, address):
94 self.set_nic_state(nic=nic, state="down")
95 cmd = "sudo ip link set dev {0} address {1}".format(nic, address)
96 self.exec_command(cmd)
97 self.set_nic_state(nic=nic, state="up")
98
99 def get_mac_address(self, nic=""):
100 show_nic = "show {nic} ".format(nic=nic) if nic else ""
101 cmd = "ip addr %s| awk '/ether/ {print $2}'" % show_nic
102 return self.exec_command(cmd).strip().lower()
Yair Fried3097dc12014-01-26 08:46:43 +0200103
Yair Fried413bf2d2014-11-19 17:07:11 +0200104 def get_nic_name(self, address):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000105 cmd = "ip -o addr | awk '/%s/ {print $2}'" % address
Daniel Mellado9e3e1062015-08-06 18:07:05 +0200106 nic = self.exec_command(cmd)
107 return nic.strip().strip(":").lower()
Yair Fried413bf2d2014-11-19 17:07:11 +0200108
Yair Fried3097dc12014-01-26 08:46:43 +0200109 def get_ip_list(self):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000110 cmd = "ip address"
Elena Ezhova91db24e2014-02-28 20:47:10 +0400111 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200112
113 def assign_static_ip(self, nic, addr):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000114 cmd = "sudo ip addr add {ip}/{mask} dev {nic}".format(
Yair Fried3097dc12014-01-26 08:46:43 +0200115 ip=addr, mask=CONF.network.tenant_network_mask_bits,
116 nic=nic
117 )
Elena Ezhova91db24e2014-02-28 20:47:10 +0400118 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200119
Yair Friedbc46f592015-11-18 16:29:34 +0200120 def set_nic_state(self, nic, state="up"):
121 cmd = "sudo ip link set {nic} {state}".format(nic=nic, state=state)
Elena Ezhova91db24e2014-02-28 20:47:10 +0400122 return self.exec_command(cmd)
Elena Ezhova4a27b462014-04-09 15:25:46 +0400123
124 def get_pids(self, pr_name):
125 # Get pid(s) of a process/program
126 cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name
127 return self.exec_command(cmd).split('\n')
Yair Fried413bf2d2014-11-19 17:07:11 +0200128
129 def get_dns_servers(self):
130 cmd = 'cat /etc/resolv.conf'
131 resolve_file = self.exec_command(cmd).strip().split('\n')
132 entries = (l.split() for l in resolve_file)
133 dns_servers = [l[1] for l in entries
134 if len(l) and l[0] == 'nameserver']
135 return dns_servers
136
137 def send_signal(self, pid, signum):
138 cmd = 'sudo /bin/kill -{sig} {pid}'.format(pid=pid, sig=signum)
139 return self.exec_command(cmd)
140
141 def _renew_lease_udhcpc(self, fixed_ip=None):
142 """Renews DHCP lease via udhcpc client. """
143 file_path = '/var/run/udhcpc.'
144 nic_name = self.get_nic_name(fixed_ip)
Yair Fried413bf2d2014-11-19 17:07:11 +0200145 pid = self.exec_command('cat {path}{nic}.pid'.
146 format(path=file_path, nic=nic_name))
147 pid = pid.strip()
148 self.send_signal(pid, 'USR1')
149
150 def _renew_lease_dhclient(self, fixed_ip=None):
151 """Renews DHCP lease via dhclient client. """
Itzik Brownffb14022015-03-23 17:03:55 +0200152 cmd = "sudo /sbin/dhclient -r && sudo /sbin/dhclient"
Yair Fried413bf2d2014-11-19 17:07:11 +0200153 self.exec_command(cmd)
154
155 def renew_lease(self, fixed_ip=None):
156 """Wrapper method for renewing DHCP lease via given client
157
158 Supporting:
159 * udhcpc
160 * dhclient
161 """
162 # TODO(yfried): add support for dhcpcd
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +0900163 supported_clients = ['udhcpc', 'dhclient']
Yair Fried413bf2d2014-11-19 17:07:11 +0200164 dhcp_client = CONF.scenario.dhcp_client
Takashi NATSUME6d5a2b42015-09-08 11:27:49 +0900165 if dhcp_client not in supported_clients:
Yair Fried413bf2d2014-11-19 17:07:11 +0200166 raise exceptions.InvalidConfiguration('%s DHCP client unsupported'
167 % dhcp_client)
168 if dhcp_client == 'udhcpc' and not fixed_ip:
169 raise ValueError("need to set 'fixed_ip' for udhcpc client")
Joe Gordon28788b42015-02-25 12:42:37 -0800170 return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip)
Alexander Gubanovabd154c2015-09-23 23:24:06 +0300171
172 def mount(self, dev_name, mount_path='/mnt'):
173 cmd_mount = 'sudo mount /dev/%s %s' % (dev_name, mount_path)
174 self.exec_command(cmd_mount)
175
176 def umount(self, mount_path='/mnt'):
177 self.exec_command('sudo umount %s' % mount_path)
178
179 def make_fs(self, dev_name, fs='ext4'):
180 cmd_mkfs = 'sudo /usr/sbin/mke2fs -t %s /dev/%s' % (fs, dev_name)
181 self.exec_command(cmd_mkfs)