blob: bb2fcfb41b448f44897d60d82303748042306268 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050014import time
15
16from tempest.common.ssh import Client
Sean Dague86bd8422013-12-20 09:56:44 -050017from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018from tempest.exceptions import ServerUnreachable
Daryl Walleck98e66dd2012-06-21 04:58:39 -050019
Sean Dague86bd8422013-12-20 09:56:44 -050020CONF = config.CONF
21
Daryl Walleck6b9b2882012-04-08 21:43:39 -050022
23class RemoteClient():
24
Attila Fazekasc3a095b2013-08-17 09:15:44 +020025 # NOTE(afazekas): It should always get an address instead of server
Attila Fazekasa23f5002012-10-23 19:32:45 +020026 def __init__(self, server, username, password=None, pkey=None):
Sean Dague86bd8422013-12-20 09:56:44 -050027 ssh_timeout = CONF.compute.ssh_timeout
28 network = CONF.compute.network_for_ssh
29 ip_version = CONF.compute.ip_version_for_ssh
30 ssh_channel_timeout = CONF.compute.ssh_channel_timeout
Attila Fazekasa23f5002012-10-23 19:32:45 +020031 if isinstance(server, basestring):
32 ip_address = server
33 else:
34 addresses = server['addresses'][network]
35 for address in addresses:
36 if address['version'] == ip_version:
37 ip_address = address['addr']
38 break
39 else:
40 raise ServerUnreachable()
Attila Fazekasa23f5002012-10-23 19:32:45 +020041 self.ssh_client = Client(ip_address, username, password, ssh_timeout,
Chris Yeoh76916042013-02-27 16:25:25 +103042 pkey=pkey,
43 channel_timeout=ssh_channel_timeout)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050044
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010045 def validate_authentication(self):
46 """Validate ssh connection and authentication
47 This method raises an Exception when the validation fails.
48 """
49 self.ssh_client.test_connection_auth()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050050
51 def hostname_equals_servername(self, expected_hostname):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070052 # Get host name using command "hostname"
Daryl Walleck6b9b2882012-04-08 21:43:39 -050053 actual_hostname = self.ssh_client.exec_command("hostname").rstrip()
54 return expected_hostname == actual_hostname
55
56 def get_files(self, path):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070057 # Return a list of comma separated files
Daryl Walleck6b9b2882012-04-08 21:43:39 -050058 command = "ls -m " + path
59 return self.ssh_client.exec_command(command).rstrip('\n').split(', ')
60
61 def get_ram_size_in_mb(self):
62 output = self.ssh_client.exec_command('free -m | grep Mem')
63 if output:
64 return output.split()[1]
65
66 def get_number_of_vcpus(self):
67 command = 'cat /proc/cpuinfo | grep processor | wc -l'
68 output = self.ssh_client.exec_command(command)
69 return int(output)
Dan Smithc18d8c62012-07-02 08:09:26 -070070
71 def get_partitions(self):
72 # Return the contents of /proc/partitions
73 command = 'cat /proc/partitions'
74 output = self.ssh_client.exec_command(command)
75 return output
Daryl Walleck98e66dd2012-06-21 04:58:39 -050076
77 def get_boot_time(self):
Vincent Untz3c0b5b92014-01-18 10:56:00 +010078 cmd = 'cut -f1 -d. /proc/uptime'
79 boot_secs = self.ssh_client.exec_command(cmd)
80 boot_time = time.time() - int(boot_secs)
81 return time.localtime(boot_time)
Attila Fazekasa23f5002012-10-23 19:32:45 +020082
83 def write_to_console(self, message):
84 message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
85 # usually to /dev/ttyS0
86 cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
87 return self.ssh_client.exec_command(cmd)
Yair Fried5f670ab2013-12-09 09:26:51 +020088
89 def ping_host(self, host):
90 cmd = 'ping -c1 -w1 %s' % host
91 return self.ssh_client.exec_command(cmd)
Yair Fried4d7efa62013-11-17 17:12:29 +020092
93 def get_mac_address(self):
94 cmd = "/sbin/ifconfig | awk '/HWaddr/ {print $5}'"
95 return self.ssh_client.exec_command(cmd)