diff options
Diffstat (limited to 'tests/testutils/artifactshare.py')
-rw-r--r-- | tests/testutils/artifactshare.py | 98 |
1 files changed, 61 insertions, 37 deletions
diff --git a/tests/testutils/artifactshare.py b/tests/testutils/artifactshare.py index 8664c69d0..6b9117b48 100644 --- a/tests/testutils/artifactshare.py +++ b/tests/testutils/artifactshare.py @@ -2,10 +2,18 @@ import string import pytest import subprocess import os +import shutil +import signal -from buildstream import _yaml +from contextlib import contextmanager +from multiprocessing import Process, Queue +import pytest_cov -from .site import HAVE_OSTREE_CLI +from buildstream import _yaml +from buildstream._artifactcache.cascache import CASCache +from buildstream._artifactcache.casserver import create_server +from buildstream._context import Context +from buildstream._exceptions import ArtifactError # ArtifactShare() @@ -20,11 +28,6 @@ class ArtifactShare(): def __init__(self, directory): - # We need the ostree CLI for tests which use this - # - if not HAVE_OSTREE_CLI: - pytest.skip("ostree cli is not available") - # The working directory for the artifact share (in case it # needs to do something outside of it's backend's storage folder). # @@ -35,34 +38,42 @@ class ArtifactShare(): # Unless this gets more complicated, just use this directly # in tests as a remote artifact push/pull configuration # - self.repo = os.path.join(self.directory, 'repo') + self.repodir = os.path.join(self.directory, 'repo') - os.makedirs(self.repo) + os.makedirs(self.repodir) - self.init() - self.update_summary() + context = Context() + context.artifactdir = self.repodir - # init(): - # - # Initializes the artifact share - # - # Returns: - # (smth): A new ref corresponding to this commit, which can - # be passed as the ref in the Repo.source_config() API. - # - def init(self): - subprocess.call(['ostree', 'init', - '--repo', self.repo, - '--mode', 'archive-z2']) + self.cas = CASCache(context) + + q = Queue() + + self.process = Process(target=self.run, args=(q,)) + self.process.start() - # update_summary(): + # Retrieve port from server subprocess + port = q.get() + + self.repo = 'http://localhost:{}'.format(port) + + # run(): # - # Ensure that the summary is up to date + # Run the artifact server. # - def update_summary(self): - subprocess.call(['ostree', 'summary', - '--update', - '--repo', self.repo]) + def run(self, q): + pytest_cov.embed.cleanup_on_sigterm() + + server = create_server(self.repodir, enable_push=True) + port = server.add_insecure_port('localhost:0') + + server.start() + + # Send port to parent + q.put(port) + + # Sleep until termination by signal + signal.pause() # has_artifact(): # @@ -77,8 +88,8 @@ class ArtifactShare(): # (bool): True if the artifact exists in the share, otherwise false. def has_artifact(self, project_name, element_name, cache_key): - # NOTE: This should be kept in line with our ostree - # based artifact cache code, the below is the + # NOTE: This should be kept in line with our + # artifact cache code, the below is the # same algo for creating an artifact reference # @@ -93,18 +104,31 @@ class ArtifactShare(): ]) artifact_key = '{0}/{1}/{2}'.format(project_name, element_name, cache_key) - if not subprocess.call(['ostree', 'rev-parse', - '--repo', self.repo, - artifact_key]): + try: + tree = self.cas.resolve_ref(artifact_key) return True + except ArtifactError: + return False - return False + # close(): + # + # Remove the artifact share. + # + def close(self): + self.process.terminate() + self.process.join() + + shutil.rmtree(self.directory) # create_artifact_share() # # Create an ArtifactShare for use in a test case # +@contextmanager def create_artifact_share(directory): - - return ArtifactShare(directory) + share = ArtifactShare(directory) + try: + yield share + finally: + share.close() |