From 6286d8209a3db7fcf2b490a55044fa4c2be0c007 Mon Sep 17 00:00:00 2001 From: Tristan Van Berkom Date: Wed, 16 Jan 2019 10:42:06 -0500 Subject: conftest.py: Use different artifact directory for integration tests To ensure we can run integration tests in parallel, use a tempdir for the artifact cache of each separate integration test run. This makes it possible to run multiple full test runs including integration tests in parallel under detox. --- conftest.py | 53 ++++++++++++++++++++++++++++----- tests/integration/source-determinism.py | 4 +-- tests/testutils/runcli.py | 4 +-- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/conftest.py b/conftest.py index b0b1e280e..6e6e3b08f 100755 --- a/conftest.py +++ b/conftest.py @@ -17,15 +17,23 @@ # # Authors: # Tristan Maat - +# import os import shutil - +import tempfile import pytest - from buildstream._platform.platform import Platform +# +# This file is loaded by pytest, we use it to add a custom +# `--integration` option to our test suite, and to install +# a session scope fixture. +# + +################################################# +# Implement pytest option # +################################################# def pytest_addoption(parser): parser.addoption('--integration', action='store_true', default=False, help='Run integration tests') @@ -36,26 +44,57 @@ def pytest_runtest_setup(item): pytest.skip('skipping integration test') +################################################# +# integration_cache fixture # +################################################# +# +# This is yielded by the `integration_cache` fixture +# +class IntegrationCache(): + + def __init__(self, cache): + cache = os.path.abspath(cache) + + # Use the same sources every time + self.sources = os.path.join(cache, 'sources') + + # Create a temp directory for the duration of the test for + # the artifacts directory + try: + self.artifacts = tempfile.mkdtemp(dir=cache, prefix='artifacts-') + except OSError as e: + raise AssertionError("Unable to create test directory !") from e + + @pytest.fixture(scope='session') def integration_cache(request): - # Set the tempdir to the INTEGRATION_CACHE variable, or the + # Set the cache dir to the INTEGRATION_CACHE variable, or the # default if that is not set. if 'INTEGRATION_CACHE' in os.environ: cache_dir = os.environ['INTEGRATION_CACHE'] else: cache_dir = os.path.abspath('./integration-cache') - yield cache_dir + cache = IntegrationCache(cache_dir) + + yield cache # Clean up the artifacts after each test run - we only want to - # cache sources + # cache sources between runs try: - shutil.rmtree(os.path.join(cache_dir, 'artifacts')) + shutil.rmtree(cache.artifacts) except FileNotFoundError: pass +################################################# +# Automatically reset the platform # +################################################# +# +# This might need some refactor, maybe buildstream +# needs to cleanup more gracefully and we could remove this. +# def clean_platform_cache(): Platform._instance = None diff --git a/tests/integration/source-determinism.py b/tests/integration/source-determinism.py index fa12593df..a14f3e5b0 100644 --- a/tests/integration/source-determinism.py +++ b/tests/integration/source-determinism.py @@ -94,7 +94,7 @@ def test_deterministic_source_umask(cli, tmpdir, datafiles, kind, integration_ca return f.read() finally: os.umask(old_umask) - cache_dir = os.path.join(integration_cache, 'artifacts') + cache_dir = integration_cache.artifacts cli.remove_artifact_from_cache(project, element_name, cache_dir=cache_dir) @@ -156,7 +156,7 @@ def test_deterministic_source_local(cli, tmpdir, datafiles, integration_cache): with open(os.path.join(checkoutdir, 'ls-l'), 'r') as f: return f.read() finally: - cache_dir = os.path.join(integration_cache, 'artifacts') + cache_dir = integration_cache.artifacts cli.remove_artifact_from_cache(project, element_name, cache_dir=cache_dir) diff --git a/tests/testutils/runcli.py b/tests/testutils/runcli.py index 0c8e962c2..770f6289a 100644 --- a/tests/testutils/runcli.py +++ b/tests/testutils/runcli.py @@ -525,8 +525,8 @@ def cli_integration(tmpdir, integration_cache): # We want to cache sources for integration tests more permanently, # to avoid downloading the huge base-sdk repeatedly fixture.configure({ - 'sourcedir': os.path.join(integration_cache, 'sources'), - 'artifactdir': os.path.join(integration_cache, 'artifacts') + 'sourcedir': integration_cache.sources, + 'artifactdir': integration_cache.artifacts }) return fixture -- cgit v1.2.1