From 089408751cdbd7950f58616f9d85ff9dfd9aa3c7 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Thu, 14 May 2020 09:27:44 -0400 Subject: cloud_tests: emit dots on Travis while fetching images (#347) This ensures that Travis will not kill our tests if fetching images is taking a long time. In implementation terms, this introduces a context manager which will spin up a multiprocessing.Process in the background and print a dot to stdout every 10 seconds. The process is terminated when the context manager exits. This also drop the use of travis_wait, which was being used to work around this issue. --- tests/cloud_tests/platforms/__init__.py | 4 +++- tests/cloud_tests/util.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/cloud_tests/platforms/__init__.py b/tests/cloud_tests/platforms/__init__.py index 6a410b84..e506baa0 100644 --- a/tests/cloud_tests/platforms/__init__.py +++ b/tests/cloud_tests/platforms/__init__.py @@ -6,6 +6,7 @@ from .ec2 import platform as ec2 from .lxd import platform as lxd from .nocloudkvm import platform as nocloudkvm from .azurecloud import platform as azurecloud +from ..util import emit_dots_on_travis PLATFORMS = { 'ec2': ec2.EC2Platform, @@ -17,7 +18,8 @@ PLATFORMS = { def get_image(platform, config): """Get image from platform object using os_name.""" - return platform.get_image(config) + with emit_dots_on_travis(): + return platform.get_image(config) def get_instance(snapshot, *args, **kwargs): diff --git a/tests/cloud_tests/util.py b/tests/cloud_tests/util.py index 06f7d865..e65771b1 100644 --- a/tests/cloud_tests/util.py +++ b/tests/cloud_tests/util.py @@ -5,6 +5,7 @@ import base64 import copy import glob +import multiprocessing import os import random import shlex @@ -12,7 +13,9 @@ import shutil import string import subprocess import tempfile +import time import yaml +from contextlib import contextmanager from cloudinit import util as c_util from tests.cloud_tests import LOG @@ -118,6 +121,36 @@ def current_verbosity(): return max(min(3 - int(LOG.level / 10), 2), 0) +@contextmanager +def emit_dots_on_travis(): + """ + A context manager that emits a dot every 10 seconds if running on Travis. + + Travis will kill jobs that don't emit output for a certain amount of time. + This context manager spins up a background process which will emit a dot to + stdout every 10 seconds to avoid being killed. + + It should be wrapped selectively around operations that are known to take a + long time. + """ + if os.environ.get('TRAVIS') != "true": + # If we aren't on Travis, don't do anything. + yield + return + + def emit_dots(): + while True: + print(".") + time.sleep(10) + + dot_process = multiprocessing.Process(target=emit_dots) + dot_process.start() + try: + yield + finally: + dot_process.terminate() + + def is_writable_dir(path): """Make sure dir is writable. -- cgit v1.2.3