Refactoring et mise en place des tests

This commit is contained in:
wpetit 2017-02-11 11:51:01 +01:00
parent 24dbf2fe47
commit e58ab60bd8
9 changed files with 137 additions and 63 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ packages
*.log *.log
*~ *~
__pycache__ __pycache__
*.pyc

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
test:
python3 -m unittest discover test "*_test.py"

161
package
View File

@ -6,8 +6,8 @@ sys.path.append(os.path.dirname(__file__) + '/lib')
import tamarin, system, rkt import tamarin, system, rkt
def configure_args_parser(): def create_args_parser():
'''Return a new configured ArgumentParser'''
profile_names = tamarin.get_available_profile_names() profile_names = tamarin.get_available_profile_names()
parser = argparse.ArgumentParser(description="Generate packages for various GNU/Linux distributions") parser = argparse.ArgumentParser(description="Generate packages for various GNU/Linux distributions")
@ -21,70 +21,36 @@ def configure_args_parser():
return parser return parser
if __name__ == "__main__": def download_and_extract_rkt(dest_dir, verbose=True):
'''Download and extract rkt to the given destination directory'''
parser = configure_args_parser()
args = parser.parse_args()
# Verify project directory
project_dir = os.path.abspath(args.project_directory)
output_dir = os.path.abspath(args.output)
# Load build profile
profile = tamarin.load_profile(args.profile)
workspace = tamarin.get_workspace_dir()
workspace_tmp = tamarin.get_workspace_subdir('tmp')
local_rkt_dir = tamarin.get_workspace_subdir('rkt')
if not system.which('rkt', local_rkt_dir):
# Download and extract rkt
rkt_archive_path = tamarin.download_rkt() rkt_archive_path = tamarin.download_rkt()
system.extract_tar(rkt_archive_path, workspace_tmp) system.extract_tar(rkt_archive_path, workspace_tmp)
rkt_archive_dir = tamarin.get_rkt_achive_dest_dir() rkt_archive_dir = tamarin.get_rkt_achive_dest_dir()
shutil.rmtree(local_rkt_dir, ignore_errors=True) shutil.rmtree(local_rkt_dir, ignore_errors=True)
os.rename(rkt_archive_dir, local_rkt_dir) os.rename(rkt_archive_dir, dest_dir)
local_acbuild_dir = tamarin.get_workspace_subdir('acbuild') def download_and_extract_acbuild(dest_dir, verbose=True):
if not system.which('acbuild', local_acbuild_dir): '''Download and extract acbuild to the given destination directory'''
# Download and extract acbuild
acbuild_archive_path = tamarin.download_acbuild() acbuild_archive_path = tamarin.download_acbuild()
system.extract_tar(acbuild_archive_path, workspace_tmp) system.extract_tar(acbuild_archive_path, workspace_tmp)
acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir() acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir()
shutil.rmtree(local_acbuild_dir, ignore_errors=True) shutil.rmtree(local_acbuild_dir, ignore_errors=True)
os.rename(acbuild_archive_dir, local_acbuild_dir) os.rename(acbuild_archive_dir, dest_dir)
pid = os.getpid() def get_cached_image_path(profile):
build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid)) '''Compute and return the path for an hypothetic cached image for the given profile'''
rkt_store = tamarin.get_workspace_subdir('store')
rkt_flags = ["--dir={:s}".format(rkt_store)]
base_image = profile['profile']['default_image']
# If the base image is Docker-based, preload it and get its name from the store
if base_image.startswith('docker://'):
rkt.run([
"fetch",
"--insecure-options=image",
base_image
] + rkt_flags)
aci_file = os.path.join(os.sep, build_workspace, 'image.aci')
acbuild_flags = ["--modify", aci_file, "--work-path", build_workspace]
# Use cached image base on base_image and containerbuild hooks
containerbuild_hooks = profile['containerbuild']['hooks'] containerbuild_hooks = profile['containerbuild']['hooks']
hasher = hashlib.sha1() hasher = hashlib.sha1()
hasher.update(base_image.encode()) hasher.update(base_image.encode())
hasher.update(containerbuild_hooks.encode()) hasher.update(containerbuild_hooks.encode())
image_hash = hasher.hexdigest() image_hash = hasher.hexdigest()
cache_dir = tamarin.get_workspace_subdir('cache') cache_dir = tamarin.get_workspace_subdir('cache')
cached_image_file = os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12])); return os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12]));
def build_image(build_workspace, aci_file, base_image, profile):
acbuild_flags = ["--modify", aci_file, "--work-path", build_workspace]
if not args.rebuild and os.path.exists(cached_image_file):
# Copy cached image
shutil.copyfile(cached_image_file, aci_file)
else:
# Find and export base image from rkt' store # Find and export base image from rkt' store
name_pattern = base_image.split('/')[-1] + '$' name_pattern = base_image.split('/')[-1] + '$'
image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags) image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
@ -98,14 +64,86 @@ if __name__ == "__main__":
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"])
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"])
# Execute containerbuild hooks # Configure "containerbuild" hooks environment
cb_hooks_env = os.environ.copy() hooks_env = os.environ.copy()
cb_hooks_env["PATH"] = os.environ['PATH'] + ':' + local_acbuild_dir hooks_env["PATH"] = os.environ['PATH'] + ':' + tamarin.get_workspace_subdir('acbuild')
cb_hooks_env["TAMARIN_ACBUILD"] = " ".join([system.which('acbuild', local_acbuild_dir)]+acbuild_flags) hooks_env["TAMARIN_ACBUILD"] = " ".join([system.which('acbuild', local_acbuild_dir)]+acbuild_flags)
cb_hooks_env["TAMARIN_ACBUILD_ENGINE"] = "chroot" if not system.which('systemctl') else "systemd-nspawn" hooks_env["TAMARIN_ACBUILD_ENGINE"] = "chroot" if not system.which('systemctl') else "systemd-nspawn"
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=cb_hooks_env) # Run hooks
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env)
return aci_file
def cleanup(build_workspace, rkt_flags):
# Nettoyage des conteneurs
rkt.run([
"gc",
"--grace-period=0"
] + rkt_flags, as_root=True)
# Nettoyage des images obsolètes du store
rkt.run([
"image",
"gc"
] + rkt_flags, as_root=True)
# Suppression de l'espace de travail de build
shutil.rmtree(build_workspace, ignore_errors=True)
def validate_args(args):
'''TODO'''
if __name__ == "__main__":
parser = create_args_parser()
args = parser.parse_args()
validate_args(args)
# Verify project directory
project_dir = os.path.abspath(args.project_directory)
output_dir = os.path.abspath(args.output)
# Load build profile
profile = tamarin.load_profile(args.profile)
workspace = tamarin.get_workspace_dir()
workspace_tmp = tamarin.get_workspace_subdir('tmp')
local_rkt_dir = tamarin.get_workspace_subdir('rkt')
if not system.which('rkt', local_rkt_dir):
download_and_extract_rkt(local_rkt_dir)
local_acbuild_dir = tamarin.get_workspace_subdir('acbuild')
if not system.which('acbuild', local_acbuild_dir):
download_and_extract_acbuild(local_acbuild_dir)
pid = os.getpid()
build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid))
rkt_store = tamarin.get_workspace_subdir('store')
rkt_flags = ["--dir={:s}".format(rkt_store)]
base_image = profile['profile']['default_image']
# If the base image is Docker-based, download it
if base_image.startswith('docker://'):
rkt.run([
"fetch",
"--insecure-options=image",
base_image
] + rkt_flags)
aci_file = os.path.join(os.sep, build_workspace, 'image.aci')
cached_image_file = get_cached_image_path(profile)
if not args.rebuild and os.path.exists(cached_image_file):
# Copy cached image
shutil.copyfile(cached_image_file, aci_file)
else:
build_image(build_workspace, aci_file, base_image, profile)
# Cache image # Cache image
shutil.copyfile(aci_file, cached_image_file) shutil.copyfile(aci_file, cached_image_file)
@ -128,15 +166,4 @@ if __name__ == "__main__":
], as_root=True) ], as_root=True)
# Cleanup # Cleanup
cleanup(build_workspace, rkt_flags)
rkt.run([
"gc",
"--grace-period=0"
] + rkt_flags, as_root=True)
rkt.run([
"image",
"gc"
] + rkt_flags, as_root=True)
shutil.rmtree(build_workspace, ignore_errors=True)

View File

@ -0,0 +1,5 @@
dummy-project (0.0.1) unstable; urgency=low
* Dev release
-- William Petit <wpetit@cadoles.com> Fri, 16 Oct 2015 15:57:03 +0200

View File

@ -0,0 +1 @@
9

View File

@ -0,0 +1,13 @@
Source: dummy-project
Section: unknown
Priority: optional
Maintainer: William Petit <wpetit@cadoles.com>
Build-Depends: debhelper (>= 8.0.0)
Standards-Version: 3.9.4
Homepage:
Vcs-Git: https://forge.cadoles.com/wpetit/tamarin.git
Vcs-Browser: https://forge.cadoles.com/wpetit/tamarin
Package: dummy-project
Architecture: any
Description: Projet test pour la construction de paquets via Tamarin

View File

@ -0,0 +1,8 @@
#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
%:
dh $@

View File

@ -0,0 +1 @@
3.0 (quilt)

16
test/package_test.py Normal file
View File

@ -0,0 +1,16 @@
import unittest, os
from importlib.machinery import SourceFileLoader
package = SourceFileLoader('package', os.path.dirname(__file__) + '/../package').load_module()
class TestPackage(unittest.TestCase):
def test_args_parser(self):
parser = package.create_args_parser()
def test_download_and_extract_rkt(self):
if __name__ == '__main__':
unittest.main()