From 48b9e6264bd9d5430b115465d3811c376eec63ff Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Tue, 26 May 2020 13:15:56 +0200 Subject: [PATCH] Add test for build_images(). --- focker/focker.py | 1 + focker/jail.py | 9 +++++++- tests/test_compose.py | 48 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/focker/focker.py b/focker/focker.py index b69a6ea..f335316 100644 --- a/focker/focker.py +++ b/focker/focker.py @@ -139,6 +139,7 @@ def create_parser(): parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['remove', 'rem', 'rm', 'r']]) parser.set_defaults(func=command_jail_remove) parser.add_argument('reference', type=str) + parser.add_argument('--force', '-f', action='store_true') parser = ListForwarder([subparsers.add_parser(cmd) for cmd in ['exec', 'exe', 'e']]) parser.set_defaults(func=command_jail_exec) diff --git a/focker/jail.py b/focker/jail.py index 486c01a..0232341 100644 --- a/focker/jail.py +++ b/focker/jail.py @@ -235,7 +235,14 @@ def command_jail_stop(args): def command_jail_remove(args): - name, _ = zfs_find(args.reference, focker_type='jail') + try: + name, _ = zfs_find(args.reference, focker_type='jail') + except AmbiguousValueError: + raise + except ValueError: + if args.force: + return + raise path = zfs_mountpoint(name) jail_remove(path) diff --git a/tests/test_compose.py b/tests/test_compose.py index ab8c48b..0a50eba 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -3,7 +3,8 @@ from focker.compose import exec_hook, \ exec_postbuild, \ build_volumes, \ build_images, \ - setup_dependencies + setup_dependencies, \ + build_jails from tempfile import TemporaryDirectory import os import pytest @@ -200,3 +201,48 @@ def test_setup_dependencies(): del conf['test-setup-dependencies-B'] del conf['test-setup-dependencies-C'] conf.write('/etc/jail.conf') + + +def test_build_jails(): + backup_file('/etc/jail.conf') + conf = jailconf.load('/etc/jail.conf') + for k in list(conf.keys()): + if conf[k]['host.hostname'].strip('\'"') in ['test-build-jails-A', 'test-build-jails-B']: + del conf[k] + conf.write('/etc/jail.conf') + subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-A']) + subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-B']) + subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-focker-bootstrap']) + subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-focker-bootstrap']) + spec = { + 'test-build-jails-A': { + 'image': 'test-focker-bootstrap', + 'exec.start': 'test-exec-start', + 'exec.stop': 'test-exec-stop', + 'ip4.addr': 'test-ip4-addr', + 'interface': 'test-interface', + 'host.hostname': 'test-build-jails-A' + } + } + spec['test-build-jails-B'] = spec['test-build-jails-A'].copy() + spec['test-build-jails-B']['host.hostname'] = 'test-build-jails-B' + build_jails(spec) + conf = jailconf.load('/etc/jail.conf') + print(conf.values()) + blocks = list(filter(lambda a: a['host.hostname'].strip('"\'') in [ 'test-build-jails-A', + 'test-build-jails-B' ], conf.values())) + print(blocks) + assert len(blocks) == 2 + assert blocks[0]['host.hostname'] != blocks[1]['host.hostname'] + for b in blocks: + name, _ = zfs_find(b['host.hostname'].strip('\'"'), focker_type='jail') + mountpoint = zfs_mountpoint(name) + assert b['path'] == mountpoint + assert b['exec.start'].strip('\'"') == 'test-exec-start' + assert b['exec.stop'].strip('\'"') == 'test-exec-stop' + assert b['ip4.addr'].strip('\'"') == 'test-ip4-addr' + assert b['interface'].strip('\'"') == 'test-interface' + subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-A']) + subprocess.check_output(['focker', 'jail', 'remove', '--force', 'test-build-jails-B']) + subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap']) + conf.write('/etc/jail.conf')