From b5c9c8eae37ddcac4151257cf7c405b155f9d8c5 Mon Sep 17 00:00:00 2001 From: Stanislaw Adaszewski Date: Wed, 27 May 2020 10:13:45 +0200 Subject: [PATCH] Add test_build(). --- tests/test_image.py | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index e83198d..283c940 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -1,12 +1,14 @@ import pytest from focker.image import validate_spec, \ - build_squeeze + build_squeeze, \ + build import subprocess from tempfile import TemporaryDirectory import focker.image import os from focker.zfs import zfs_find, \ - zfs_mountpoint + zfs_mountpoint, \ + zfs_exists_snapshot_sha256 from focker.misc import focker_unlock @@ -57,3 +59,35 @@ def test_build_squeeze(monkeypatch): 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) + + +def test_build(monkeypatch): + focker_unlock() + 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') + + counter = 0 + def count_calls(*args, **kwargs): + nonlocal counter + counter += 1 + return zfs_exists_snapshot_sha256(*args, **kwargs) + monkeypatch.setattr(focker.image, 'zfs_exists_snapshot_sha256', count_calls) + + with TemporaryDirectory() as d: + args = lambda: 0 + args.focker_dir = d + name, _ = build(spec, args) + + assert counter == 2 + 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)