Merge "Check VM's console log before trying to SSH to it."

This commit is contained in:
Zuul 2020-11-17 03:50:00 +00:00 committed by Gerrit Code Review
commit 7a48433b7a
3 changed files with 54 additions and 1 deletions

View File

@ -64,7 +64,7 @@ def is_scheduler_filter_enabled(filter_name):
def create_test_server(clients, validatable=False, validation_resources=None,
tenant_network=None, wait_until=None,
volume_backed=False, name=None, flavor=None,
image_id=None, **kwargs):
image_id=None, wait_for_sshable=True, **kwargs):
"""Common wrapper utility returning a test server.
This method is a common wrapper returning a test server that can be
@ -100,6 +100,8 @@ def create_test_server(clients, validatable=False, validation_resources=None,
CONF.compute.flavor_ref will be used instead.
:param image_id: ID of the image to be used to provision the server. If not
defined, CONF.compute.image_ref will be used instead.
:param wait_for_sshable: Check server's console log and wait until it will
be ready to login.
:returns: a tuple
"""
@ -270,6 +272,10 @@ def create_test_server(clients, validatable=False, validation_resources=None,
LOG.exception('Server %s failed to delete in time',
server['id'])
if (validatable and CONF.compute_feature_enabled.console_output and
wait_for_sshable):
waiters.wait_for_guest_os_boot(clients.servers_client, server['id'])
return body, servers

View File

@ -437,3 +437,20 @@ def wait_for_interface_detach(client, server_id, port_id):
'the required time (%s s)' % (port_id, server_id,
client.build_timeout))
raise lib_exc.TimeoutException(message)
def wait_for_guest_os_boot(client, server_id):
start_time = int(time.time())
while True:
console_output = client.get_console_output(server_id)['output']
for line in console_output.split('\n'):
if 'login:' in line.lower():
return
if int(time.time()) - start_time >= client.build_timeout:
LOG.info("Guest OS on server %s probably isn't ready or its "
"console log can't be parsed properly. If guest OS "
"isn't ready, that may cause problems with SSH to "
"the server.",
server_id)
return
time.sleep(client.build_interval)

View File

@ -131,6 +131,36 @@ class TestInterfaceWaiters(base.TestCase):
mock.call('server_id')])
sleep.assert_called_once_with(client.build_interval)
def test_wait_for_guest_os_boot(self):
get_console_output = mock.Mock(
side_effect=[
{'output': 'os not ready yet\n'},
{'output': 'login:\n'}
])
client = self.mock_client(get_console_output=get_console_output)
self.patch('time.time', return_value=0.)
sleep = self.patch('time.sleep')
with mock.patch.object(waiters.LOG, "info") as log_info:
waiters.wait_for_guest_os_boot(client, 'server_id')
get_console_output.assert_has_calls([
mock.call('server_id'), mock.call('server_id')])
sleep.assert_called_once_with(client.build_interval)
log_info.assert_not_called()
def test_wait_for_guest_os_boot_timeout(self):
get_console_output = mock.Mock(
return_value={'output': 'os not ready yet\n'})
client = self.mock_client(get_console_output=get_console_output)
self.patch('time.time', side_effect=[0., client.build_timeout + 1.])
self.patch('time.sleep')
with mock.patch.object(waiters.LOG, "info") as log_info:
waiters.wait_for_guest_os_boot(client, 'server_id')
log_info.assert_called_once()
class TestVolumeWaiters(base.TestCase):
vol_migrating_src_host = {