Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: fix test_ssh_import_id.py (SC-272) #954

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/integration_tests/modules/test_ssh_import_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pytest

from tests.integration_tests.util import retry

USER_DATA = """\
#cloud-config
Expand All @@ -26,6 +27,11 @@
class TestSshImportId:

@pytest.mark.user_data(USER_DATA)
# Retry is needed here because ssh import id is one of the last modules
# run, and it fires off a web request, then continues with the rest of
# cloud-init. It is possible cloud-init's status is "done" before the
# id's have been fully imported.
@retry(tries=30, delay=1)
def test_ssh_import_id(self, client):
ssh_output = client.read_from_file(
"/home/ubuntu/.ssh/authorized_keys")
Expand Down
30 changes: 30 additions & 0 deletions tests/integration_tests/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import logging
import multiprocessing
import os
Expand Down Expand Up @@ -64,3 +65,32 @@ def get_test_rsa_keypair(key_name: str = 'test1') -> key_pair:
with private_key_path.open() as private_file:
private_key = private_file.read()
return key_pair(public_key, private_key)


def retry(*, tries: int = 30, delay: int = 1):
"""Decorator for retries.

Retry a function until code no longer raises an exception or
max tries is reached.

Example:
@retry(tries=5, delay=1)
def try_something_that_may_not_be_ready():
...
"""
def _retry(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
last_error = None
for _ in range(tries):
try:
func(*args, **kwargs)
break
except Exception as e:
last_error = e
time.sleep(delay)
else:
if last_error:
raise last_error
return wrapper
return _retry