diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-03 22:20:48 -0400 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-09-04 04:04:11 -0400 |
commit | 15db6d56ec6617b96f0ec8966345fccd2a557617 (patch) | |
tree | 647c6c4736430d9a8130fc0a3bce77ef03992dd4 | |
parent | 00bc9d3abae0abe731ab3f57a450800c6a27df00 (diff) | |
download | buildstream-15db6d56ec6617b96f0ec8966345fccd2a557617.tar.gz |
tests/testutils/repo module: New module for Source support scaffolding
Currently comes with a Repo() abstract class and a git implementation
-rw-r--r-- | tests/testutils/repo/__init__.py | 29 | ||||
-rw-r--r-- | tests/testutils/repo/bzr.py | 42 | ||||
-rw-r--r-- | tests/testutils/repo/git.py | 39 | ||||
-rw-r--r-- | tests/testutils/repo/ostree.py | 38 | ||||
-rw-r--r-- | tests/testutils/repo/repo.py | 70 | ||||
-rw-r--r-- | tests/testutils/repo/tar.py | 40 |
6 files changed, 258 insertions, 0 deletions
diff --git a/tests/testutils/repo/__init__.py b/tests/testutils/repo/__init__.py new file mode 100644 index 000000000..4651c8519 --- /dev/null +++ b/tests/testutils/repo/__init__.py @@ -0,0 +1,29 @@ +import pytest +from .git import Git +from .bzr import Bzr +from .ostree import OSTree +from .tar import Tar + +available_repos = { + 'git': Git, + 'bzr': Bzr, + 'ostree': OSTree, + 'tar': Tar +} + + +# create_repo() +# +# Convenience for creating a Repo +# +# Args: +# kind (str): The kind of repo to create (a source plugin basename) +# directory (str): The path where the repo will keep a cache +# +def create_repo(kind, directory): + try: + constructor = available_repos[kind] + except KeyError as e: + raise AssertionError("Unsupported repo kind {}".format(kind)) from e + + return constructor(directory) diff --git a/tests/testutils/repo/bzr.py b/tests/testutils/repo/bzr.py new file mode 100644 index 000000000..848cff85f --- /dev/null +++ b/tests/testutils/repo/bzr.py @@ -0,0 +1,42 @@ +import os +import shutil +import subprocess +from .repo import Repo + +BZR_ENV = { + "BZR_EMAIL": "Testy McTesterson <testy.mctesterson@example.com>" +} + + +class Bzr(Repo): + + def create(self, directory): + branch_dir = os.path.join(self.repo, 'trunk') + + subprocess.call(['bzr', 'init-repo', self.repo], env=BZR_ENV) + subprocess.call(['bzr', 'init', branch_dir], env=BZR_ENV) + self.copy_directory(directory, branch_dir) + subprocess.call(['bzr', 'add', '.'], env=BZR_ENV, cwd=branch_dir) + subprocess.call(['bzr', 'commit', '--message="Initial commit"'], + env=BZR_ENV, cwd=branch_dir) + + return self.latest_commit() + + def source_config(self, ref=None): + config = { + 'kind': 'bzr', + 'url': 'file://' + self.repo, + 'track': 'trunk' + } + if ref is not None: + config['ref'] = ref + + return config + + def latest_commit(self): + output = subprocess.check_output([ + 'bzr', 'version-info', + '--custom', '--template={revno}', + os.path.join(self.repo, 'trunk') + ], env=BZR_ENV) + return output.decode('UTF-8').strip() diff --git a/tests/testutils/repo/git.py b/tests/testutils/repo/git.py new file mode 100644 index 000000000..9ceada301 --- /dev/null +++ b/tests/testutils/repo/git.py @@ -0,0 +1,39 @@ +import shutil +import subprocess +from .repo import Repo + +GIT_ENV = { + 'GIT_AUTHOR_DATE': '1320966000 +0200', + 'GIT_AUTHOR_NAME': 'tomjon', + 'GIT_AUTHOR_EMAIL': 'tom@jon.com', + 'GIT_COMMITTER_DATE': '1320966000 +0200', + 'GIT_COMMITTER_NAME': 'tomjon', + 'GIT_COMMITTER_EMAIL': 'tom@jon.com' +} + + +class Git(Repo): + + def create(self, directory): + self.copy_directory(directory, self.repo) + subprocess.call(['git', 'init', '.'], env=GIT_ENV, cwd=self.repo) + subprocess.call(['git', 'add', '.'], env=GIT_ENV, cwd=self.repo) + subprocess.call(['git', 'commit', '-m', 'Initial commit'], env=GIT_ENV, cwd=self.repo) + return self.latest_commit() + + def source_config(self, ref=None): + config = { + 'kind': 'git', + 'url': 'file://' + self.repo, + 'track': 'master' + } + if ref is not None: + config['ref'] = ref + + return config + + def latest_commit(self): + output = subprocess.check_output([ + 'git', 'rev-parse', 'master' + ], env=GIT_ENV, cwd=self.repo) + return output.decode('UTF-8').strip() diff --git a/tests/testutils/repo/ostree.py b/tests/testutils/repo/ostree.py new file mode 100644 index 000000000..2b7546645 --- /dev/null +++ b/tests/testutils/repo/ostree.py @@ -0,0 +1,38 @@ +import subprocess +from .repo import Repo + + +class OSTree(Repo): + + def create(self, directory): + subprocess.call(['ostree', 'init', + '--repo', self.repo, + '--mode', 'archive-z2']) + subprocess.call(['ostree', 'commit', + '--repo', self.repo, + '--branch', 'master', + '--subject', 'Initial commit', + directory]) + + latest = self.latest_commit() + + return latest + + def source_config(self, ref=None): + config = { + 'kind': 'ostree', + 'url': 'file://' + self.repo, + 'track': 'master' + } + if ref is not None: + config['ref'] = ref + + return config + + def latest_commit(self): + output = subprocess.check_output([ + 'ostree', 'rev-parse', + '--repo', self.repo, + 'master' + ]) + return output.decode('UTF-8').strip() diff --git a/tests/testutils/repo/repo.py b/tests/testutils/repo/repo.py new file mode 100644 index 000000000..03d616a90 --- /dev/null +++ b/tests/testutils/repo/repo.py @@ -0,0 +1,70 @@ +import os +import shutil + + +# Repo() +# +# Abstract class providing scaffolding for +# generating data to be used with various sources +# +# Args: +# directory (str): The base temp directory for the test +# +class Repo(): + + def __init__(self, directory): + + # The working directory for the repo object + # + self.directory = os.path.abspath(directory) + + # The directory the actual repo will be stored in + self.repo = os.path.join(self.directory, 'repo') + + os.makedirs(self.repo) + + # create(): + # + # Create a repository in self.directory and add the initial content + # + # Args: + # directory: A directory with content to commit + # + # Returns: + # (smth): A new ref corresponding to this commit, which can + # be passed as the ref in the Repo.source_config() API. + # + def create(self, directory): + pass + + # source_config() + # + # Args: + # ref (smth): An optional abstract ref object, usually a string. + # + # Returns: + # (dict): A configuration which can be serialized as a + # source when generating an element file on the fly + # + def source_config(self, ref=None): + pass + + # copy_directory(): + # + # Copies the content of src to the directory dest + # + # Like shutil.copytree(), except dest is expected + # to exist. + # + # Args: + # src (str): The source directory + # dest (str): The destination directory + # + def copy_directory(self, src, dest): + for filename in os.listdir(src): + src_path = os.path.join(src, filename) + dest_path = os.path.join(dest, filename) + if os.path.isdir(src_path): + shutil.copytree(src_path, dest_path) + else: + shutil.copy2(src_path, dest_path) diff --git a/tests/testutils/repo/tar.py b/tests/testutils/repo/tar.py new file mode 100644 index 000000000..430ebe427 --- /dev/null +++ b/tests/testutils/repo/tar.py @@ -0,0 +1,40 @@ +import os +import tarfile +import hashlib + +from .repo import Repo + + +class Tar(Repo): + + def create(self, directory): + tarball = os.path.join(self.repo, 'file.tar.gz') + + old_dir = os.getcwd() + os.chdir(directory) + with tarfile.open(tarball, "w:gz") as tar: + tar.add(".") + os.chdir(old_dir) + + return sha256sum(tarball) + + def source_config(self, ref=None): + tarball = os.path.join(self.repo, 'file.tar.gz') + config = { + 'kind': 'tar', + 'url': 'file://' + tarball, + 'track': 'master', + 'directory': '' + } + if ref is not None: + config['ref'] = ref + + return config + + +def sha256sum(filename): + h = hashlib.sha256() + with open(filename, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + h.update(chunk) + return h.hexdigest() |