Commit 6286d820 authored by Tristan Van Berkom's avatar Tristan Van Berkom
Browse files

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.
parent 4ca37dcd
Loading
Loading
Loading
Loading
Loading
+46 −7
Original line number Original line Diff line number Diff line
@@ -17,15 +17,23 @@
#
#
#  Authors:
#  Authors:
#        Tristan Maat <tristan.maat@codethink.co.uk>
#        Tristan Maat <tristan.maat@codethink.co.uk>

#
import os
import os
import shutil
import shutil

import tempfile
import pytest
import pytest

from buildstream._platform.platform import Platform
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):
def pytest_addoption(parser):
    parser.addoption('--integration', action='store_true', default=False,
    parser.addoption('--integration', action='store_true', default=False,
                     help='Run integration tests')
                     help='Run integration tests')
@@ -36,26 +44,57 @@ def pytest_runtest_setup(item):
        pytest.skip('skipping integration test')
        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')
@pytest.fixture(scope='session')
def integration_cache(request):
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.
    # default if that is not set.
    if 'INTEGRATION_CACHE' in os.environ:
    if 'INTEGRATION_CACHE' in os.environ:
        cache_dir = os.environ['INTEGRATION_CACHE']
        cache_dir = os.environ['INTEGRATION_CACHE']
    else:
    else:
        cache_dir = os.path.abspath('./integration-cache')
        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
    # Clean up the artifacts after each test run - we only want to
    # cache sources
    # cache sources between runs
    try:
    try:
        shutil.rmtree(os.path.join(cache_dir, 'artifacts'))
        shutil.rmtree(cache.artifacts)
    except FileNotFoundError:
    except FileNotFoundError:
        pass
        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():
def clean_platform_cache():
    Platform._instance = None
    Platform._instance = None


+2 −2
Original line number Original line Diff line number Diff line
@@ -94,7 +94,7 @@ def test_deterministic_source_umask(cli, tmpdir, datafiles, kind, integration_ca
                return f.read()
                return f.read()
        finally:
        finally:
            os.umask(old_umask)
            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,
            cli.remove_artifact_from_cache(project, element_name,
                                           cache_dir=cache_dir)
                                           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:
            with open(os.path.join(checkoutdir, 'ls-l'), 'r') as f:
                return f.read()
                return f.read()
        finally:
        finally:
            cache_dir = os.path.join(integration_cache, 'artifacts')
            cache_dir = integration_cache.artifacts
            cli.remove_artifact_from_cache(project, element_name,
            cli.remove_artifact_from_cache(project, element_name,
                                           cache_dir=cache_dir)
                                           cache_dir=cache_dir)


+2 −2
Original line number Original line Diff line number Diff line
@@ -525,8 +525,8 @@ def cli_integration(tmpdir, integration_cache):
    # We want to cache sources for integration tests more permanently,
    # We want to cache sources for integration tests more permanently,
    # to avoid downloading the huge base-sdk repeatedly
    # to avoid downloading the huge base-sdk repeatedly
    fixture.configure({
    fixture.configure({
        'sourcedir': os.path.join(integration_cache, 'sources'),
        'sourcedir': integration_cache.sources,
        'artifactdir': os.path.join(integration_cache, 'artifacts')
        'artifactdir': integration_cache.artifacts
    })
    })


    return fixture
    return fixture