Fix RemoteClient usage in scenario tests

Some scenario tests use the following patter to work with RemoteClient:

   client = self.get_remote_client(ip,
                                   private_key=keypair.private_key)
   return client.ssh_client

ssh_client is directly accessed because exec_command method
is not directly available in RemoteClient

In order to preserve encapsulation, RemoteClient should be improved to
expose that method.

Change-Id: I054762d769d002b5fb45c871d85a8ca8ff294695
Closes-Bug: #1285539
This commit is contained in:
Elena Ezhova 2014-02-28 20:47:10 +04:00
parent ff956a6669
commit 91db24e002
4 changed files with 18 additions and 18 deletions

View File

@ -43,6 +43,9 @@ class RemoteClient():
ssh_timeout, pkey=pkey,
channel_timeout=ssh_channel_timeout)
def exec_command(self, cmd):
return self.ssh_client.exec_command(cmd)
def validate_authentication(self):
"""Validate ssh connection and authentication
This method raises an Exception when the validation fails.
@ -51,33 +54,33 @@ class RemoteClient():
def hostname_equals_servername(self, expected_hostname):
# Get host name using command "hostname"
actual_hostname = self.ssh_client.exec_command("hostname").rstrip()
actual_hostname = self.exec_command("hostname").rstrip()
return expected_hostname == actual_hostname
def get_files(self, path):
# Return a list of comma separated files
command = "ls -m " + path
return self.ssh_client.exec_command(command).rstrip('\n').split(', ')
return self.exec_command(command).rstrip('\n').split(', ')
def get_ram_size_in_mb(self):
output = self.ssh_client.exec_command('free -m | grep Mem')
output = self.exec_command('free -m | grep Mem')
if output:
return output.split()[1]
def get_number_of_vcpus(self):
command = 'cat /proc/cpuinfo | grep processor | wc -l'
output = self.ssh_client.exec_command(command)
output = self.exec_command(command)
return int(output)
def get_partitions(self):
# Return the contents of /proc/partitions
command = 'cat /proc/partitions'
output = self.ssh_client.exec_command(command)
output = self.exec_command(command)
return output
def get_boot_time(self):
cmd = 'cut -f1 -d. /proc/uptime'
boot_secs = self.ssh_client.exec_command(cmd)
boot_secs = self.exec_command(cmd)
boot_time = time.time() - int(boot_secs)
return time.localtime(boot_time)
@ -85,27 +88,27 @@ class RemoteClient():
message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
# usually to /dev/ttyS0
cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
return self.ssh_client.exec_command(cmd)
return self.exec_command(cmd)
def ping_host(self, host):
cmd = 'ping -c1 -w1 %s' % host
return self.ssh_client.exec_command(cmd)
return self.exec_command(cmd)
def get_mac_address(self):
cmd = "/sbin/ifconfig | awk '/HWaddr/ {print $5}'"
return self.ssh_client.exec_command(cmd)
return self.exec_command(cmd)
def get_ip_list(self):
cmd = "/bin/ip address"
return self.ssh_client.exec_command(cmd)
return self.exec_command(cmd)
def assign_static_ip(self, nic, addr):
cmd = "sudo /bin/ip addr add {ip}/{mask} dev {nic}".format(
ip=addr, mask=CONF.network.tenant_network_mask_bits,
nic=nic
)
return self.ssh_client.exec_command(cmd)
return self.exec_command(cmd)
def turn_nic_on(self, nic):
cmd = "sudo /bin/ip link set {nic} up".format(nic=nic)
return self.ssh_client.exec_command(cmd)
return self.exec_command(cmd)

View File

@ -45,11 +45,10 @@ class TestSnapshotPattern(manager.OfficialClientTest):
def _ssh_to_server(self, server_or_ip):
try:
linux_client = self.get_remote_client(server_or_ip)
return self.get_remote_client(server_or_ip)
except Exception:
LOG.exception()
self._log_console_output()
return linux_client.ssh_client
def _write_timestamp(self, server_or_ip):
ssh_client = self._ssh_to_server(server_or_ip)

View File

@ -72,8 +72,7 @@ class TestStampPattern(manager.OfficialClientTest):
server.add_floating_ip(floating_ip)
def _ssh_to_server(self, server_or_ip):
linux_client = self.get_remote_client(server_or_ip)
return linux_client.ssh_client
return self.get_remote_client(server_or_ip)
def _create_volume_snapshot(self, volume):
snapshot_name = data_utils.rand_name('scenario-snapshot-')

View File

@ -101,14 +101,13 @@ class TestVolumeBootPattern(manager.OfficialClientTest):
ip = server.networks[network_name_for_ssh][0]
try:
client = self.get_remote_client(
return self.get_remote_client(
ip,
private_key=keypair.private_key)
except Exception:
LOG.exception('ssh to server failed')
self._log_console_output()
raise
return client.ssh_client
def _get_content(self, ssh_client):
return ssh_client.exec_command('cat /tmp/text')