Add server param in manager.get_remote_client

Methods in RemoteClient have debug_ssh decorator, which can
get server console output on failure.

This is to:
1. Add server param in manager.get_remote_client
2. Add print error info in RemoteClient
3. Remove _log_console_output from manager.get_remote_client.
   The 3rd will be slightly different from the original code,
   because now if no server is provided, we will no longer print
   the console output for all servers.
   As to this, I think to print console output for all servers
   may not be so necessary, besides, if we really want to, we
   can add this logic in debug_ssh decorator.

Change-Id: I9314490a5ab71f14636ddf3cfcbe61158fb1069d
This commit is contained in:
zhufl 2017-05-25 13:55:24 +08:00
parent fcefb26f2a
commit f52c759e5c
6 changed files with 54 additions and 49 deletions

View File

@ -28,29 +28,37 @@ def debug_ssh(function):
def wrapper(self, *args, **kwargs):
try:
return function(self, *args, **kwargs)
except tempest.lib.exceptions.SSHTimeout:
try:
original_exception = sys.exc_info()
caller = test_utils.find_test_caller() or "not found"
if self.server:
msg = 'Caller: %s. Timeout trying to ssh to server %s'
LOG.debug(msg, caller, self.server)
if self.console_output_enabled and self.servers_client:
try:
msg = 'Console log for server %s: %s'
console_log = (
self.servers_client.get_console_output(
self.server['id'])['output'])
LOG.debug(msg, self.server['id'], console_log)
except Exception:
msg = 'Could not get console_log for server %s'
LOG.debug(msg, self.server['id'])
# re-raise the original ssh timeout exception
six.reraise(*original_exception)
finally:
# Delete the traceback to avoid circular references
_, _, trace = original_exception
del trace
except Exception as e:
caller = test_utils.find_test_caller() or "not found"
if not isinstance(e, tempest.lib.exceptions.SSHTimeout):
message = ('Initializing SSH connection to %(ip)s failed. '
'Error: %(error)s' % {'ip': self.ip_address,
'error': e})
message = '(%s) %s' % (caller, message)
LOG.error(message)
raise
else:
try:
original_exception = sys.exc_info()
if self.server:
msg = 'Caller: %s. Timeout trying to ssh to server %s'
LOG.debug(msg, caller, self.server)
if self.console_output_enabled and self.servers_client:
try:
msg = 'Console log for server %s: %s'
console_log = (
self.servers_client.get_console_output(
self.server['id'])['output'])
LOG.debug(msg, self.server['id'], console_log)
except Exception:
msg = 'Could not get console_log for server %s'
LOG.debug(msg, self.server['id'])
# re-raise the original ssh timeout exception
six.reraise(*original_exception)
finally:
# Delete the traceback to avoid circular references
_, _, trace = original_exception
del trace
return wrapper
@ -78,6 +86,7 @@ class RemoteClient(object):
"""
self.server = server
self.servers_client = servers_client
self.ip_address = ip_address
self.console_output_enabled = console_output_enabled
self.ssh_shell_prologue = ssh_shell_prologue
self.ping_count = ping_count

View File

@ -313,13 +313,15 @@ class ScenarioTest(tempest.test.BaseTestCase):
return secgroup
def get_remote_client(self, ip_address, username=None, private_key=None):
def get_remote_client(self, ip_address, username=None, private_key=None,
server=None):
"""Get a SSH client to a remote server
@param ip_address the server floating or fixed IP address to use
for ssh validation
@param username name of the Linux account on the remote server
@param private_key the SSH private key to use
@param server: server dict, used for debugging purposes
@return a RemoteClient object
"""
@ -334,22 +336,10 @@ class ScenarioTest(tempest.test.BaseTestCase):
else:
password = CONF.validation.image_ssh_password
private_key = None
linux_client = remote_client.RemoteClient(ip_address, username,
pkey=private_key,
password=password)
try:
linux_client.validate_authentication()
except Exception as e:
message = ('Initializing SSH connection to %(ip)s failed. '
'Error: %(error)s' % {'ip': ip_address,
'error': e})
caller = test_utils.find_test_caller()
if caller:
message = '(%s) %s' % (caller, message)
LOG.exception(message)
self._log_console_output()
raise
linux_client = remote_client.RemoteClient(
ip_address, username, pkey=private_key, password=password,
server=server, servers_client=self.servers_client)
linux_client.validate_authentication()
return linux_client
def _image_create(self, name, fmt, path,

View File

@ -139,14 +139,16 @@ class TestMinimumBasicScenario(manager.ScenarioTest):
# check that we can SSH to the server before reboot
self.linux_client = self.get_remote_client(
floating_ip['ip'], private_key=keypair['private_key'])
floating_ip['ip'], private_key=keypair['private_key'],
server=server)
self.nova_reboot(server)
# check that we can SSH to the server after reboot
# (both connections are part of the scenario)
self.linux_client = self.get_remote_client(
floating_ip['ip'], private_key=keypair['private_key'])
floating_ip['ip'], private_key=keypair['private_key'],
server=server)
self.check_disks()

View File

@ -238,7 +238,7 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
ip_address = old_floating_ip['floating_ip_address']
private_key = self._get_server_key(server)
ssh_client = self.get_remote_client(
ip_address, private_key=private_key)
ip_address, private_key=private_key, server=server)
old_nic_list = self._get_server_nics(ssh_client)
# get a port from a list of one item
port_list = self.admin_manager.ports_client.list_ports(
@ -346,7 +346,8 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
ip_address = floating_ip['floating_ip_address']
private_key = self._get_server_key(self.floating_ip_tuple.server)
ssh_source = self.get_remote_client(
ip_address, private_key=private_key)
ip_address, private_key=private_key,
server=self.floating_ip_tuple.server)
for remote_ip in address_list:
self.check_remote_connectivity(ssh_source, remote_ip,
@ -573,7 +574,7 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
ip_address = floating_ip['floating_ip_address']
private_key = self._get_server_key(server)
ssh_client = self.get_remote_client(
ip_address, private_key=private_key)
ip_address, private_key=private_key, server=server)
dns_servers = [initial_dns_server]
servers = ssh_client.get_dns_servers()
@ -639,7 +640,8 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
private_key = self._get_server_key(server2)
ssh_client = self.get_remote_client(server2_fip['floating_ip_address'],
private_key=private_key)
private_key=private_key,
server=server2)
self.check_public_network_connectivity(
should_connect=True, msg="before updating "
@ -828,7 +830,8 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
spoof_port = new_ports[0]
private_key = self._get_server_key(server)
ssh_client = self.get_remote_client(fip['floating_ip_address'],
private_key=private_key)
private_key=private_key,
server=server)
spoof_nic = ssh_client.get_nic_name_by_mac(spoof_port["mac_address"])
peer = self._create_server(self.new_net)
peer_address = peer['addresses'][self.new_net['name']][0]['addr']

View File

@ -129,7 +129,7 @@ class TestGettingAddress(manager.NetworkScenarioTest):
ips = self.define_server_ips(srv=srv)
ssh = self.get_remote_client(
ip_address=fip['floating_ip_address'],
username=username)
username=username, server=srv)
return ssh, ips, srv["id"]
def turn_nic6_on(self, ssh, sid, network_id):

View File

@ -56,7 +56,8 @@ class TestServerBasicOps(manager.ScenarioTest):
self.ssh_client = self.get_remote_client(
ip_address=self.fip,
username=self.ssh_user,
private_key=keypair['private_key'])
private_key=keypair['private_key'],
server=self.instance)
def verify_metadata(self):
if self.run_ssh and CONF.compute_feature_enabled.metadata_service: