def test_valid_configurator(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
    """Confirm a valid ansible.cfg is parsed using configurator.

    :param ee_enabled: Indicate if EE support is enabled
    :param tmp_path: The path to a test temporary directory
    :param monkeypatch: The monkeypatch fixture
    """
    cfg_path = tmp_path / "ansible.cfg"
    with cfg_path.open(mode="w") as fh:
        fh.write(ANSIBLE_CFG_VALID)
    monkeypatch.chdir(tmp_path)
    application_configuration = deepcopy(NavigatorConfiguration)
    application_configuration.internals.initializing = True
    configurator = Configurator(
        params=["--ee", str(ee_enabled)],
        application_configuration=application_configuration,
    )
    configurator.configure()

    assert application_configuration.internals.ansible_configuration.contents == {
        "defaults": {"cow_selection": "milk", "inventory": "inventory.yml"},
    }
    assert application_configuration.internals.ansible_configuration.path == cfg_path
    assert (
        application_configuration.internals.ansible_configuration.text
        == ANSIBLE_CFG_VALID.splitlines()
    )
def test_settings_file_entry(sample_settings, settings_file_dict):
    """Ensure the settings file entry is properly constructed.

    :param sample_settings: A sample application configuration (settings)
    :param settings_file_dict: The expected settings as a dictionary
    """
    configurator = Configurator(params=[], application_configuration=sample_settings)
    configurator._post_process()  # pylint: disable=protected-access
    presentable = to_presentable(sample_settings)
    # pylint: disable=not-an-iterable # https://github.com/PyCQA/pylint/issues/2296
    assert all(isinstance(p, PresentableSettingsEntry) for p in presentable)
    assert asdict(presentable[0]) == settings_file_dict
Beispiel #3
0
def test_apply_before_initial_saved():
    """Ensure the apply_previous_cli_entries cant' be used before initial"""
    with pytest.raises(ValueError, match="enabled prior to"):
        Configurator(
            params=None,
            application_configuration=NavigatorConfiguration,
            apply_previous_cli_entries=C.ALL,
        ).configure()
Beispiel #4
0
def test_mutual_exclusivity_for_configuration_init():
    """Ensure the configuration cannot be intited with both
    apply_previous_cli_entries and intitial"""
    with pytest.raises(ValueError, match="cannot be used with"):
        Configurator(
            params=None,
            application_configuration=None,
            initial=True,
            apply_previous_cli_entries=C.ALL,
        )
Beispiel #5
0
def test_apply_before_initial_saved():
    """Ensure the apply_previous_cli_entries can't be used before initial"""
    with pytest.raises(ValueError, match="enabled prior to"):
        application_configuration = deepcopy(NavigatorConfiguration)
        application_configuration.internals.initializing = False
        Configurator(
            params=None,
            application_configuration=application_configuration,
            apply_previous_cli_entries=C.ALL,
        ).configure()
Beispiel #6
0
def test_mutual_exclusivity_for_configuration_init():
    """Ensure the configuration cannot be initiated with both
    apply_previous_cli_entries and initial"""
    with pytest.raises(ValueError, match="cannot be used while initializing"):
        application_configuration = deepcopy(NavigatorConfiguration)
        application_configuration.internals.initializing = True
        Configurator(
            params=None,
            application_configuration=application_configuration,
            apply_previous_cli_entries=C.ALL,
        )
def test_settings_entry(sample_settings, settings_file_dict):
    """Ensure a settings entry is properly constructed.

    :param sample_settings: A sample application configuration (settings)
    :param settings_file_dict: The expected settings as a dictionary
    """
    entry = SettingsEntry(
        name="se_1",
        choices=["choice_1", "choice_2"],
        cli_parameters=CliParameters(short="-se1"),
        short_description="description",
        value=SettingsEntryValue(
            current="current",
            default="default",
            source=Constants.ENVIRONMENT_VARIABLE,
        ),
        version_added="v0.0",
    )
    sample_settings.entries = [entry]
    configurator = Configurator(params=[], application_configuration=sample_settings)
    configurator._post_process()  # pylint: disable=protected-access
    presentable = to_presentable(sample_settings)
    # pylint: disable=not-an-iterable # https://github.com/PyCQA/pylint/issues/2296
    assert all(isinstance(p, PresentableSettingsEntry) for p in presentable)
    assert asdict(presentable[0]) == settings_file_dict
    entry_dict = {
        "choices": ["choice_1", "choice_2"],
        "current_settings_file": "/test/path",
        "current_value": "current",
        "default_value": "default",
        "default": False,
        "description": "description",
        "env_var": "APP_SE_1",
        "name": "Se 1",
        "settings_file_sample": {"app": {"se-1": "<------"}},
        "source": "Environment variable",
        "subcommands": ["subcommand_1"],
        "cli_parameters": {"long": "--se-1", "short": "-se1"},
        "version_added": "v0.0",
    }
    assert asdict(presentable[1]) == entry_dict
def test_defaults(schema_dict: SettingsSchemaType):
    """Check the settings file used as a sample against the schema.

    :param schema_dict: The json schema as a dictionary
    """
    settings = deepcopy(NavigatorConfiguration)
    settings.internals.initializing = True
    Configurator(params=[], application_configuration=settings).configure()

    effective = to_effective(settings)
    errors = validate(schema=schema_dict, data=effective)
    assert not errors
Beispiel #9
0
def test_defaults():
    """Check the settings file used as a sample against the schema."""
    settings = deepcopy(NavigatorConfiguration)
    settings.internals.initializing = True
    Configurator(params=[], application_configuration=settings).configure()

    sources = to_sources(settings)
    for path, source in sources.items():
        assert source in [C.AUTO.value, C.DEFAULT_CFG.value, C.NOT_SET.value, C.NONE.value], (
            path,
            source,
        )
def test_invalid_configurator(ee_enabled, tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
    """Confirm a invalid ansible.cfg raises errors using configurator.

    :param ee_enabled: Indicate if EE support is enabled
    :param tmp_path: The path to a test temporary directory
    :param monkeypatch: The monkeypatch fixture
    """
    cfg_path = tmp_path / "ansible.cfg"
    with cfg_path.open(mode="w") as fh:
        fh.write(ANSIBLE_CFG_INVALID)
    monkeypatch.chdir(tmp_path)
    application_configuration = deepcopy(NavigatorConfiguration)
    application_configuration.internals.initializing = True
    configurator = Configurator(
        params=["--ee", str(ee_enabled)],
        application_configuration=application_configuration,
    )
    _messages, exit_messages = configurator.configure()

    assert application_configuration.internals.ansible_configuration.contents is Constants.NONE
    assert application_configuration.internals.ansible_configuration.path is Constants.NONE
    assert application_configuration.internals.ansible_configuration.text is Constants.NONE
    assert "12345" in exit_messages[3].message
Beispiel #11
0
def test_from_ansible_cfg(ee_enabled, tmp_path: Path,
                          monkeypatch: pytest.MonkeyPatch):
    """Confirm inventory is used from a valid ansible.cfg.

    :param ee_enabled: Indicate if EE support is enabled
    :param tmp_path: The path to a test temporary directory
    :param monkeypatch: The monkeypatch fixture
    """
    cfg_path = tmp_path / "ansible.cfg"
    with cfg_path.open(mode="w") as fh:
        fh.write(ANSIBLE_CFG_VALID)
    monkeypatch.chdir(tmp_path)
    application_configuration = deepcopy(NavigatorConfiguration)
    application_configuration.internals.initializing = True
    configurator = Configurator(
        params=["--ee", str(ee_enabled)],
        application_configuration=application_configuration,
    )
    configurator.configure()
    entry = application_configuration.entry("inventory")
    assert entry.value.source is Constants.ANSIBLE_CFG
    assert entry.value.current == [
        str(tmp_path / "inventory.yml"), "/tmp/inventory.yaml"
    ]