Ejemplo n.º 1
0
def test_build_images():
    subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
    subprocess.check_output(['focker', 'bootstrap', '--empty', '--tags', 'test-focker-bootstrap'])
    subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
    with TemporaryDirectory() as d:
        with open(os.path.join(d, 'Fockerfile'), 'w') as f:
            yaml.dump({
                'base': 'test-focker-bootstrap',
                'steps': [
                    { 'copy': [
                        [ '/bin/sh', '/bin/sh', { 'chmod': 0o777 } ],
                        [ '/lib/libedit.so.7', '/lib/libedit.so.7' ],
                        [ '/lib/libncursesw.so.8', '/lib/libncursesw.so.8' ],
                        [ '/lib/libc.so.7', '/lib/libc.so.7' ],
                        [ '/usr/bin/touch', '/usr/bin/touch', { 'chmod': 0o777 } ],
                        [ '/libexec/ld-elf.so.1', '/libexec/ld-elf.so.1', { 'chmod': 0o555 } ]
                    ] },
                    { 'run': 'touch /test-build-images' }
                ]
            }, f)
        args = lambda: 0
        args.squeeze = False
        build_images({
            'test-build-images': '.'
        }, d, args)
    focker_unlock()
    name, _ = zfs_find('test-build-images', focker_type='image')
    assert os.path.exists(os.path.join(zfs_mountpoint(name), 'test-build-images'))
    subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-build-images'])
    subprocess.check_output(['focker', 'image', 'prune'])
    subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
Ejemplo n.º 2
0
def test_build_squeeze(monkeypatch):
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base'
    ])
    subprocess.check_output(
        ['focker', 'bootstrap', '--empty', '-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)
Ejemplo n.º 3
0
def test_command_image_build():
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R',
        'test-command-image-build-base'
    ])
    subprocess.check_output([
        'focker', 'bootstrap', '--empty', '-t', 'test-command-image-build-base'
    ])

    with TemporaryDirectory() as d:
        args = lambda: 0
        with open(os.path.join(d, 'Fockerfile'), 'w') as f:
            yaml.dump(
                dict(base='test-command-image-build-base',
                     steps=[
                         dict(copy=['/etc/localtime', '/etc/localtime']),
                         dict(copy=['/etc/hosts', '/etc/hosts'])
                     ]), f)
        args.focker_dir = d
        args.squeeze = False
        args.tags = ['test-command-image-build']
        command_image_build(args)
        focker_unlock()

    name, _ = zfs_find('test-command-image-build', focker_type='image')
    mountpoint = zfs_mountpoint(name)
    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-command-image-build-base'])
    assert not os.path.exists(mountpoint)
Ejemplo n.º 4
0
def test_exec_hook_07():
    os.chdir('/')
    spec = 'flock --nonblock /var/lock/focker.lock -c ls'
    focker_lock()
    assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
    with TemporaryDirectory() as d:
        exec_hook(spec, d, 'test-exec-hook')
    assert fcntl.flock(focker_lock.fd, fcntl.LOCK_EX | fcntl.LOCK_NB) != 0
    focker_unlock()
Ejemplo n.º 5
0
def test_jail_oneshot_01():
    with tempfile.TemporaryDirectory() as d:
        jail_oneshot('test-jail', [
            '/bin/sh', '-c',
            'echo test-jail-oneshot-01 $FOO $BAR >/mnt/test.txt'
        ], {
            'FOO': '1',
            'BAR': '2'
        }, {d: '/mnt'})
        focker_unlock()
        assert os.path.exists(os.path.join(d, 'test.txt'))
        with open(os.path.join(d, 'test.txt'), 'r') as f:
            assert f.read() == 'test-jail-oneshot-01 1 2\n'
Ejemplo n.º 6
0
def test_command_image_prune():
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R',
        'test-command-image-prune'
    ])
    subprocess.check_output(
        ['focker', 'bootstrap', '--empty', '-t', 'test-command-image-prune'])
    name, sha256 = zfs_find('test-command-image-prune', focker_type='image')
    mountpoint = zfs_mountpoint(name)
    subprocess.check_output(
        ['focker', 'image', 'untag', 'test-command-image-prune'])
    args = lambda: 0
    command_image_prune(args)
    with pytest.raises(ValueError):
        zfs_find('test-command-image-prune', focker_type='image')
    with pytest.raises(ValueError):
        zfs_find(sha256, focker_type='image')
    assert not os.path.exists(mountpoint)
Ejemplo n.º 7
0
def test_command_image_remove():
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R',
        'test-command-image-remove'
    ])
    subprocess.check_output(
        ['focker', 'bootstrap', '--empty', '-t', 'test-command-image-remove'])
    name, sha256 = zfs_find('test-command-image-remove', focker_type='image')
    mountpoint = zfs_mountpoint(name)
    args = lambda: 0
    args.reference = 'test-command-image-remove'
    args.force = False
    args.remove_dependents = False
    command_image_remove(args)
    with pytest.raises(ValueError):
        zfs_find('test-command-image-remove', focker_type='image')
    with pytest.raises(ValueError):
        zfs_find(sha256, focker_type='image')
    assert not os.path.exists(mountpoint)
    assert not zfs_exists(name)
Ejemplo n.º 8
0
def test_bootstrap_02():
    subprocess.check_output(['focker', 'image', 'remove', '--force', 'test-focker-bootstrap'])
    args = lambda: 0
    args.no_image = False
    args.empty = True
    args.unfinalized = False
    args.non_interactive = False
    args.create_interface = False
    args.full_auto = False
    args.add_pf_rule = False
    args.tags = ['test-focker-bootstrap']
    command_bootstrap(args)
    focker_unlock()
    name, sha256 = zfs_find('test-focker-bootstrap', focker_type='image')
    basename = os.path.basename(name)
    assert 7 <= len(basename) <= 64
    assert re.search('[a-f]', basename[:7])
    assert len(sha256) == 64
    assert basename == sha256[:len(basename)]
    assert zfs_exists_snapshot_sha256(sha256)
    assert zfs_parse_output(['zfs', 'get', '-H', 'rdonly', name])[0][2] == 'on'
    subprocess.check_output(['zfs', 'destroy', '-r', '-f', name])
Ejemplo n.º 9
0
def test_command_image_list(monkeypatch):
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R', 'test-command-image-list'
    ])
    subprocess.check_output([
        'focker', 'bootstrap', '--empty', '-t', 'test-command-image-list',
        'test-command-image-list-1', 'test-command-image-list-2'
    ])
    name, sha256 = zfs_find('test-command-image-list', focker_type='image')
    args = lambda: 0
    args.tagged_only = True
    args.full_sha256 = True
    lst = None
    headers = None

    def fake_tabulate(*args, **kwargs):
        nonlocal lst
        nonlocal headers
        lst = args[0]
        headers = kwargs['headers']

    monkeypatch.setattr(focker.image, 'tabulate', fake_tabulate)
    command_image_list(args)
    assert lst is not None
    assert headers == ['Tags', 'Size', 'SHA256', 'Base']
    assert len(lst) >= 3
    match = list(
        filter(
            lambda a: sorted(a[0].split(' ')) == [
                'test-command-image-list', 'test-command-image-list-1',
                'test-command-image-list-2'
            ], lst))
    assert len(match) == 1
    match = match[0]
    assert match[2] == sha256
    assert match[3] == '-'
    subprocess.check_output(
        ['focker', 'image', 'remove', 'test-command-image-list'])
Ejemplo n.º 10
0
def test_build(monkeypatch):
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R', 'test-build-squeeze-base'
    ])
    subprocess.check_output(
        ['focker', 'bootstrap', '--empty', '-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)
Ejemplo n.º 11
0
def _test_parallel_build(squeeze=False):
    focker_unlock()

    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R', 'test-parallel-build-01'
    ])
    subprocess.check_output(
        ['focker', 'bootstrap', '--empty', '-t', 'test-parallel-build-01'])

    _, base_sha256 = zfs_find('test-parallel-build-01',
                              focker_type='image',
                              zfs_type='snapshot')

    spec = dict(base='test-parallel-build-01',
                steps=[dict(copy=['/etc/localtime', '/etc/localtime'])])

    args = lambda: 0
    args.focker_dir = os.path.abspath(os.curdir)
    step = CopyStep(spec['steps'][0]['copy'])
    st_sha256 = step.hash(base_sha256, args)

    pool = zfs_poolname()
    # sha256 = random_sha256_hexdigest()
    name = find_prefix(pool + '/focker/images/', st_sha256)
    print('Creating:', name)
    zfs_run(['zfs', 'create', '-o', 'focker:sha256=' + st_sha256, name])

    with pytest.raises(RuntimeError) as excinfo:
        if squeeze:
            build_squeeze(spec, args)
        else:
            build(spec, args)

    assert excinfo.value.args[
        0] == 'A build with the same SHA256 is in progress'

    zfs_run(['zfs', 'destroy', name])
    zfs_run(['focker', 'image', 'remove', '-R', 'test-parallel-build-01'])
Ejemplo n.º 12
0
def test_command_image_untag():
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R',
        'test-command-image-untag'
    ])
    subprocess.check_output([
        'focker', 'bootstrap', '--empty', '-t', 'test-command-image-untag',
        'test-command-image-untag-1', 'test-command-image-untag-2'
    ])
    name, sha256 = zfs_find('test-command-image-untag', focker_type='image')
    args = lambda: 0
    args.tags = ['test-command-image-untag-1', 'test-command-image-untag-2']
    command_image_untag(args)
    tags = zfs_parse_output(['zfs', 'get', '-H', 'focker:tags', name])
    tags = tags[0][2].split(',')
    assert tags == ['test-command-image-untag']
    with pytest.raises(ValueError):
        zfs_find('test-command-image-untag-1', focker_type='image')
    with pytest.raises(ValueError):
        zfs_find('test-command-image-untag-2', focker_type='image')
    subprocess.check_output(
        ['focker', 'image', 'remove', 'test-command-image-untag'])
Ejemplo n.º 13
0
def test_command_image_tag():
    focker_unlock()
    subprocess.check_output([
        'focker', 'image', 'remove', '--force', '-R', 'test-command-image-tag'
    ])
    subprocess.check_output(
        ['focker', 'bootstrap', '--empty', '-t', 'test-command-image-tag'])
    name_1, sha256_1 = zfs_find('test-command-image-tag', focker_type='image')
    args = lambda: 0
    args.reference = sha256_1
    args.tags = ['test-a', 'test-b', 'test-c']
    command_image_tag(args)
    for t in args.tags:
        name_2, sha256_2 = zfs_find(t, focker_type='image')
        assert name_2 == name_1
        assert sha256_2 == sha256_1
    subprocess.check_output(
        ['focker', 'image', 'remove', '-R', 'test-command-image-tag'])
    for t in args.tags:
        with pytest.raises(ValueError):
            zfs_find(t, focker_type='image')
    with pytest.raises(ValueError):
        zfs_find('test-command-image-tag', focker_type='image')