From 27e80ebd62fffa20f0828d9d1ef30095cd787ed1 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Wed, 27 May 2020 09:38:20 +0200 Subject: [PATCH] Add test_build_squeeze. --- tests/test_image.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/test_image.py diff --git a/tests/test_image.py b/tests/test_image.py new file mode 100644 index 0000000..fd88de6 --- /dev/null +++ b/tests/test_image.py @@ -0,0 +1,58 @@ +import pytest +from focker.image import validate_spec, \ + build_squeeze +import subprocess +from tempfile import TemporaryDirectory +import focker.image +import os +from focker.zfs import zfs_find, \ + zfs_mountpoint +from focker.misc import focker_unlock + + +def test_validate_spec_01(): + spec = { 'base': 'base', 'steps': 'steps' } + validate_spec(spec) + + +def test_validate_spec_02(): + spec = { 'steps': 'steps' } + with pytest.raises(ValueError): + validate_spec(spec) + + +def test_validate_spec_03(): + spec = { 'base': 'base' } + with pytest.raises(ValueError): + validate_spec(spec) + + +def test_validate_spec_04(): + spec = {} + with pytest.raises(ValueError): + validate_spec(spec) + + +def test_build_squeeze(monkeypatch): + subprocess.check_output(['focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base']) + subprocess.check_output(['focker', 'bootstrap', '--dry-run', '-t', 'test-build-squeeze-base']) + spec = dict(base='test-build-squeeze-base', steps=[ + dict(copy=['/etc/localtime', '/etc/localtime']), + dict(copy=['/etc/hosts', '/etc/hosts']) + ]) + _, base_sha256 = zfs_find('test-build-squeeze-base', focker_type='image') + def fail(sha256, *args, **kwargs): + if sha256 != base_sha256: + raise RuntimeError('No pre-existing layers expected apart from base') + monkeypatch.setattr(focker.image, 'zfs_snapshot_by_sha256', fail) + with TemporaryDirectory() as d: + args = lambda: 0 + args.focker_dir = d + name, _ = build_squeeze(spec, args) + focker_unlock() + mountpoint = zfs_mountpoint(name.split('@')[0]) + print('name:', name, 'mountpoint:', mountpoint) + assert os.path.exists(os.path.join(mountpoint, 'etc/localtime')) + assert os.path.exists(os.path.join(mountpoint, 'etc/hosts')) + subprocess.check_output(['focker', 'image', 'remove', '-R', 'test-build-squeeze-base']) + assert not os.path.exists(mountpoint)