Exemplo n.º 1
0
def test_yamls_load(tmpdir):
    """
    Verify yamls module can load yaml file properly.
    """
    p = tmpdir.mkdir("yamls").join("test_load.yml")
    p.write("""---
network:
    ip: 0.0.0.0
    mask: 255.255.255.255

intkey: 1
strkey: Value2
""")

    res = yamls.load(p.strpath)

    assert isinstance(res, dict)
    assert 'network' in res
    assert 'ip' in res['network']
    assert res['network']['ip'] == '0.0.0.0'
    assert 'mask' in res['network']
    assert res['network']['mask'] == '255.255.255.255'
    assert 'intkey' in res
    assert res['intkey'] == 1
    assert 'strkey' in res
    assert res['strkey'] == "Value2"
Exemplo n.º 2
0
def test_yamls_random(tmpdir, random_arg):
    """
    Verifies the random constructor for yaml files.
    """
    p = tmpdir.mkdir("yamls").join("test_yamls_random.yml")
    p.write("""---
random_string: !random {}
    """.format(random_arg))

    res = yamls.load(p.strpath)
    assert 'random_string' in res
    assert len(res['random_string']) == random_arg
Exemplo n.º 3
0
def test_placeholder_validator(our_cwd_setup):

    injector = 'placeholder_injector.yml'
    overwriter = 'placeholder_overwriter.yml'

    # Checks that 'IRPlaceholderException' is raised if value isn't been
    # overwritten
    settings = yamls.load(os.path.join(test_utils.TESTS_CWD, injector))
    assert isinstance(settings['place']['holder']['validator'],
                      yamls.Placeholder)
    with pytest.raises(exceptions.IRPlaceholderException) as exc:
        yaml.safe_dump(settings, default_flow_style=False)
    assert "Mandatory value is missing." in str(exc.value.message)

    # Checks that exceptions haven't been raised after overwriting the
    # placeholder
    overwriter_dict = yamls.load(os.path.join(test_utils.TESTS_CWD,
                                              overwriter))
    core_utils.dict_merge(settings, overwriter_dict)
    assert settings['place']['holder']['validator'] == \
        "'!placeholder' has been overwritten"
    yaml.safe_dump(settings, default_flow_style=False)
Exemplo n.º 4
0
def test_placeholder_double_validator(our_cwd_setup):
    injector = 'placeholder_double_injector.yml'

    # Checks that 'IRPlaceholderException' is raised if value isn't been
    # overwritten
    settings = yamls.load(os.path.join(test_utils.TESTS_CWD, injector))

    assert isinstance(settings['place']['holder']['validator1'],
                      yamls.Placeholder)
    assert isinstance(settings['place']['holder']['validator2'],
                      yamls.Placeholder)
    with pytest.raises(exceptions.IRPlaceholderException) as exc:
        yaml.safe_dump(settings, default_flow_style=False)
    assert "Mandatory value is missing." in str(exc.value.message)
Exemplo n.º 5
0
def load_settings_files(settings_files):
    """
    Loads and merges settings (YAML) files into a new dictionary object.

    :param settings_files: List of strings representing paths to YAML files.
    :return: The newly created Dictionary object containing the merging
    results of all the settings files.
    """
    settings_dict = {}

    for settings_file in settings_files:
        loaded_dict = yamls.load(settings_file, True)
        dict_merge(
            settings_dict,
            loaded_dict,
            conflict_resolver=ConflictResolver.unique_append_list_resolver)

    return settings_dict
Exemplo n.º 6
0
def load_yaml(filename, *search_paths):
    """Find YAML file. search default path first.
    :param filename: path to file
    :param search_paths: the list of paths to search for a file.
    :returns: dict. loaded YAML file.
    """
    path = None
    searched_files = []
    files_to_search = map(
        lambda search_path: os.path.join(search_path, filename), search_paths)

    for filename in files_to_search:
        searched_files.append(os.path.abspath(filename))
        if os.path.exists(filename):
            path = os.path.abspath(filename)
            break

    if path is not None:
        LOG.debug("Loading YAML file: %s" % path)
        return yamls.load(path)
    else:
        raise exceptions.IRFileNotFoundException(file_path=searched_files)
Exemplo n.º 7
0
def merge_extra_vars(settings, extra_vars):
    """
    Merging 'extra-vars' into 'settings'

    :param settings: Dictionary to merge extra-vars into
    :param extra_vars: List of extra-vars
    """
    for extra_var in extra_vars or []:
        if extra_var.startswith('@'):
            if not len(extra_var[1:]):
                raise exceptions.IRExtraVarsException(extra_var)
            settings_file = normalize_file(extra_var[1:])
            dict_merge(
                settings,
                yamls.load(settings_file),
                conflict_resolver=ConflictResolver.unique_append_list_resolver)

        else:
            if '=' not in extra_var:
                raise exceptions.IRExtraVarsException(extra_var)
            key, value = extra_var.split("=")
            dict_insert(settings, value, *key.split("."))
Exemplo n.º 8
0
def test_unsupported_yaml_constructor(our_cwd_setup):
    tester_file = 'IRYAMLConstructorError.yml'
    with pytest.raises(exceptions.IRYAMLConstructorError):
        yamls.load(os.path.join(test_utils.TESTS_CWD, tester_file))