Exemple #1
0
def test_hash_changes_when_content_changes(tmpdir):
    target = tmpdir.join('compiled_output.crumhorn').strpath
    compile_folder(source=_raw_configuration, output=target)
    original = machineconfiguration.load_configuration(target)

    new_config_path = tmpdir.join('changed')
    shutil.copytree(_raw_configuration, new_config_path.strpath)
    new_unchanged_output_path = tmpdir.join('unchanged.crumhorn').strpath
    compile_folder(new_config_path.strpath, new_unchanged_output_path)
    new_unchanged = machineconfiguration.load_configuration(new_unchanged_output_path)
    assert original.config_hash == new_unchanged.config_hash

    with new_config_path.join('horn.toml').open('a') as f:
        f.writelines(['\n\t', '\t\n'])  # any change to the file
    new_changed_output_path = tmpdir.join('changed.crumhorn').strpath
    compile_folder(new_config_path.strpath, new_changed_output_path)
    new_changed = machineconfiguration.load_configuration(new_changed_output_path)
    assert original.config_hash != new_changed.config_hash
Exemple #2
0
def compile_folder(source, output, base_package=None, machinespec_repository=None):
    source = path.abspath(source)
    output = path.abspath(output)

    configuration = toml.load(path.join(source, 'horn.toml'))
    # sort to ensure stable hashing for unchanged content
    local_files = sorted(_files_from_configuration(configuration) + ['horn.toml'])
    if base_package is None:
        parent = configuration['horn'].get('parent', None)
        if parent is not None:
            base_package = machinespec_repository.find_machine_spec(parent)

    with tarfile.open(output, mode='w:gz') as dest:
        lock_hash = hashlib.sha512()

        for f in local_files:
            f_path = path.join(source, f)
            try:
                with open(f_path, 'rb') as opened:
                    lock_hash.update(opened.read())
            except IOError:
                raise MissingDataFileError(f)

            file_to_add = path.abspath(f_path)
            dest_location = path.relpath(file_to_add, source)
            dest.add(file_to_add, arcname=dest_location)

        if base_package is not None:
            with TemporaryDirectory() as tempdir:
                with tarfile.open(base_package, mode='r:gz') as base:
                    base.extractall(path=tempdir)
                    with open(path.join(tempdir, 'horn.toml'), 'rb') as base_hash:
                        lock_hash.update(base_hash.read())
                dest.add(tempdir, arcname='base')

        with NamedTemporaryFile(mode='wb') as lock_file:
            lock = base64.urlsafe_b64encode(lock_hash.digest())
            lock_file.file.write(lock)
            lock_file.file.close()
            dest.add(lock_file.name, arcname='lock')

    # Check the resultant machinespec can load
    machineconfiguration.load_configuration(output)
Exemple #3
0
def test_hash_changes_when_content_changes(tmpdir):
    target = tmpdir.join('compiled_output.crumhorn').strpath
    compile_folder(source=_raw_configuration, output=target)
    original = machineconfiguration.load_configuration(target)

    new_config_path = tmpdir.join('changed')
    shutil.copytree(_raw_configuration, new_config_path.strpath)
    new_unchanged_output_path = tmpdir.join('unchanged.crumhorn').strpath
    compile_folder(new_config_path.strpath, new_unchanged_output_path)
    new_unchanged = machineconfiguration.load_configuration(
        new_unchanged_output_path)
    assert original.config_hash == new_unchanged.config_hash

    with new_config_path.join('horn.toml').open('a') as f:
        f.writelines(['\n\t', '\t\n'])  # any change to the file
    new_changed_output_path = tmpdir.join('changed.crumhorn').strpath
    compile_folder(new_config_path.strpath, new_changed_output_path)
    new_changed = machineconfiguration.load_configuration(
        new_changed_output_path)
    assert original.config_hash != new_changed.config_hash
def loading_as_package(folder_to_package):
    with tempfile.NamedTemporaryFile(mode='w') as package_file:
        package_file.close()
        with tarfile.open(name=package_file.name, mode='w:gz') as package:
            for dirpath, dirnames, fnames in os.walk(folder_to_package):
                for f in fnames:
                    abspath = path.join(dirpath, f)
                    package.add(abspath, arcname=path.relpath(abspath, folder_to_package))

        result = machineconiguration.load_configuration(package_file.name)
    return result
def loading_as_package(folder_to_package):
    with tempfile.NamedTemporaryFile(mode='w') as package_file:
        package_file.close()
        with tarfile.open(name=package_file.name, mode='w:gz') as package:
            for dirpath, dirnames, fnames in os.walk(folder_to_package):
                for f in fnames:
                    abspath = path.join(dirpath, f)
                    package.add(abspath,
                                arcname=path.relpath(abspath,
                                                     folder_to_package))

        result = machineconiguration.load_configuration(package_file.name)
    return result
Exemple #6
0
def test_base_package_is_bundled(tmpdir):
    child_package_source = tmpdir.join('child_package').strpath
    shutil.copytree(_raw_configuration, child_package_source)
    with fileinput.input(path.join(child_package_source, 'horn.toml'), inplace=True) as child_config:
        for line in child_config:
            if line.startswith('name'):
                print('name = "childname"')
            elif line.startswith('base_image'):
                continue
            else:
                print(line)

    parent_machine_spec = tmpdir.join('parent.crumspec').strpath
    compile_folder(_raw_configuration, parent_machine_spec)

    child_machine_spec = tmpdir.join('child.crumspec').strpath
    compile_folder(child_package_source, child_machine_spec, base_package=parent_machine_spec)

    result = machineconfiguration.load_configuration(child_machine_spec)
    assert result.name == 'childname'
    assert result.base_image_configuration.name == 'raw_configuration'
Exemple #7
0
def test_base_package_is_bundled(tmpdir):
    child_package_source = tmpdir.join('child_package').strpath
    shutil.copytree(_raw_configuration, child_package_source)
    with fileinput.input(path.join(child_package_source, 'horn.toml'),
                         inplace=True) as child_config:
        for line in child_config:
            if line.startswith('name'):
                print('name = "childname"')
            elif line.startswith('base_image'):
                continue
            else:
                print(line)

    parent_machine_spec = tmpdir.join('parent.crumspec').strpath
    compile_folder(_raw_configuration, parent_machine_spec)

    child_machine_spec = tmpdir.join('child.crumspec').strpath
    compile_folder(child_package_source,
                   child_machine_spec,
                   base_package=parent_machine_spec)

    result = machineconfiguration.load_configuration(child_machine_spec)
    assert result.name == 'childname'
    assert result.base_image_configuration.name == 'raw_configuration'
Exemple #8
0
def create_droplet(manager, config_path):
    return manager.first_boot(
        machine_configuration=machineconfiguration.load_configuration(
            config_path),
        size_slug='512mb')
Exemple #9
0
def create_droplet(manager, config_path):
    return manager.first_boot(
        machine_configuration=machineconfiguration.load_configuration(config_path),
        size_slug='512mb')
Exemple #10
0
def test_compilation_results_in_file_that_can_be_read_by_configuration_parser(
        tmpdir):
    target = tmpdir.join('compiled_output.crumhorn').strpath
    compile_folder(source=_raw_configuration, output=target)
    assert machineconfiguration.load_configuration(target) is not None
Exemple #11
0
def test_compilation_results_in_file_that_can_be_read_by_configuration_parser(tmpdir):
    target = tmpdir.join('compiled_output.crumhorn').strpath
    compile_folder(source=_raw_configuration, output=target)
    assert machineconfiguration.load_configuration(target) is not None
Exemple #12
0
def launch_droplet(manager, config_path):
    return manager.launch(
        machine_configuration=machineconfiguration.load_configuration(
            config_path),
        size_slug='512mb')
Exemple #13
0
def launch_droplet(manager, config_path):
    return manager.launch(
        machine_configuration=machineconfiguration.load_configuration(config_path),
        size_slug='512mb')