blob: 95b68335051608f0843e24b1589e1cca2f3ca94b [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
llg821243b20502014-02-22 10:32:49 +080014import six
Matthew Treinisha83a16e2012-12-07 13:44:02 -050015import time
16
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090017from tempest.common import ssh
Sean Dague86bd8422013-12-20 09:56:44 -050018from tempest import config
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090019from tempest import exceptions
Daryl Walleck98e66dd2012-06-21 04:58:39 -050020
Sean Dague86bd8422013-12-20 09:56:44 -050021CONF = config.CONF
22
Daryl Walleck6b9b2882012-04-08 21:43:39 -050023
24class RemoteClient():
25
Attila Fazekasc3a095b2013-08-17 09:15:44 +020026 # NOTE(afazekas): It should always get an address instead of server
Attila Fazekasa23f5002012-10-23 19:32:45 +020027 def __init__(self, server, username, password=None, pkey=None):
Sean Dague86bd8422013-12-20 09:56:44 -050028 ssh_timeout = CONF.compute.ssh_timeout
29 network = CONF.compute.network_for_ssh
30 ip_version = CONF.compute.ip_version_for_ssh
31 ssh_channel_timeout = CONF.compute.ssh_channel_timeout
llg821243b20502014-02-22 10:32:49 +080032 if isinstance(server, six.string_types):
Attila Fazekasa23f5002012-10-23 19:32:45 +020033 ip_address = server
34 else:
35 addresses = server['addresses'][network]
36 for address in addresses:
37 if address['version'] == ip_version:
38 ip_address = address['addr']
39 break
40 else:
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090041 raise exceptions.ServerUnreachable()
42 self.ssh_client = ssh.Client(ip_address, username, password,
43 ssh_timeout, pkey=pkey,
44 channel_timeout=ssh_channel_timeout)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050045
Elena Ezhova91db24e2014-02-28 20:47:10 +040046 def exec_command(self, cmd):
47 return self.ssh_client.exec_command(cmd)
48
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010049 def validate_authentication(self):
50 """Validate ssh connection and authentication
51 This method raises an Exception when the validation fails.
52 """
53 self.ssh_client.test_connection_auth()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050054
55 def hostname_equals_servername(self, expected_hostname):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070056 # Get host name using command "hostname"
Elena Ezhova91db24e2014-02-28 20:47:10 +040057 actual_hostname = self.exec_command("hostname").rstrip()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050058 return expected_hostname == actual_hostname
59
60 def get_files(self, path):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070061 # Return a list of comma separated files
Daryl Walleck6b9b2882012-04-08 21:43:39 -050062 command = "ls -m " + path
Elena Ezhova91db24e2014-02-28 20:47:10 +040063 return self.exec_command(command).rstrip('\n').split(', ')
Daryl Walleck6b9b2882012-04-08 21:43:39 -050064
65 def get_ram_size_in_mb(self):
Elena Ezhova91db24e2014-02-28 20:47:10 +040066 output = self.exec_command('free -m | grep Mem')
Daryl Walleck6b9b2882012-04-08 21:43:39 -050067 if output:
68 return output.split()[1]
69
70 def get_number_of_vcpus(self):
71 command = 'cat /proc/cpuinfo | grep processor | wc -l'
Elena Ezhova91db24e2014-02-28 20:47:10 +040072 output = self.exec_command(command)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050073 return int(output)
Dan Smithc18d8c62012-07-02 08:09:26 -070074
75 def get_partitions(self):
76 # Return the contents of /proc/partitions
77 command = 'cat /proc/partitions'
Elena Ezhova91db24e2014-02-28 20:47:10 +040078 output = self.exec_command(command)
Dan Smithc18d8c62012-07-02 08:09:26 -070079 return output
Daryl Walleck98e66dd2012-06-21 04:58:39 -050080
81 def get_boot_time(self):
Vincent Untz3c0b5b92014-01-18 10:56:00 +010082 cmd = 'cut -f1 -d. /proc/uptime'
Elena Ezhova91db24e2014-02-28 20:47:10 +040083 boot_secs = self.exec_command(cmd)
Vincent Untz3c0b5b92014-01-18 10:56:00 +010084 boot_time = time.time() - int(boot_secs)
85 return time.localtime(boot_time)
Attila Fazekasa23f5002012-10-23 19:32:45 +020086
87 def write_to_console(self, message):
88 message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
89 # usually to /dev/ttyS0
90 cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
Elena Ezhova91db24e2014-02-28 20:47:10 +040091 return self.exec_command(cmd)
Yair Fried5f670ab2013-12-09 09:26:51 +020092
93 def ping_host(self, host):
94 cmd = 'ping -c1 -w1 %s' % host
Elena Ezhova91db24e2014-02-28 20:47:10 +040095 return self.exec_command(cmd)
Yair Fried4d7efa62013-11-17 17:12:29 +020096
97 def get_mac_address(self):
98 cmd = "/sbin/ifconfig | awk '/HWaddr/ {print $5}'"
Elena Ezhova91db24e2014-02-28 20:47:10 +040099 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200100
101 def get_ip_list(self):
102 cmd = "/bin/ip address"
Elena Ezhova91db24e2014-02-28 20:47:10 +0400103 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200104
105 def assign_static_ip(self, nic, addr):
106 cmd = "sudo /bin/ip addr add {ip}/{mask} dev {nic}".format(
107 ip=addr, mask=CONF.network.tenant_network_mask_bits,
108 nic=nic
109 )
Elena Ezhova91db24e2014-02-28 20:47:10 +0400110 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200111
112 def turn_nic_on(self, nic):
113 cmd = "sudo /bin/ip link set {nic} up".format(nic=nic)
Elena Ezhova91db24e2014-02-28 20:47:10 +0400114 return self.exec_command(cmd)
Elena Ezhova4a27b462014-04-09 15:25:46 +0400115
116 def get_pids(self, pr_name):
117 # Get pid(s) of a process/program
118 cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name
119 return self.exec_command(cmd).split('\n')