Esempio n. 1
0
def get_provider():
    """Get the configured or appropriate provider for the host OS.

    If platform is not Linux, use Multipass.

    If platform is Linux:
    (1) use provider specified with CHARMCRAFT_PROVIDER if running
        in developer mode,
    (2) use provider specified with snap configuration if running
        as snap,
    (3) default to platform default (LXD on Linux).

    :return: Provider instance.
    """
    provider = None

    if is_charmcraft_running_in_developer_mode():
        provider = os.getenv("CHARMCRAFT_PROVIDER")

    if provider is None and is_charmcraft_running_from_snap():
        snap_config = get_snap_configuration()
        provider = snap_config.provider if snap_config else None

    if provider is None:
        provider = _get_platform_default_provider()

    if provider == "lxd":
        return LXDProvider()
    elif provider == "multipass":
        return MultipassProvider()

    raise CraftError(f"Unsupported provider specified {provider!r}.")
Esempio n. 2
0
def test_get_snap_configuration_empty(mock_snap_config):
    def fake_get(key: str):
        raise snaphelpers._conf.UnknownConfigKey(key=key)

    mock_snap_config.return_value.get.side_effect = fake_get

    snap_config = get_snap_configuration()

    assert snap_config == CharmcraftSnapConfiguration()
Esempio n. 3
0
def test_get_snap_configuration_valid_providers(mock_snap_config, provider):
    def fake_get(key: str):
        if key == "provider":
            return provider
        raise snaphelpers._conf.UnknownConfigKey(key=key)

    mock_snap_config.return_value.get.side_effect = fake_get

    snap_config = get_snap_configuration()

    assert snap_config == CharmcraftSnapConfiguration(provider=provider)
Esempio n. 4
0
def test_get_snap_configuration_invalid_providers(mock_snap_config, provider):
    def fake_get(key: str):
        if key == "provider":
            return provider
        raise snaphelpers._conf.UnknownConfigKey(key=key)

    mock_snap_config.return_value.get.side_effect = fake_get

    snap_config = get_snap_configuration()

    assert snap_config == CharmcraftSnapConfiguration(provider=provider)
    assert snap_config.provider == provider

    with pytest.raises(ValueError, match=re.escape(f"provider {provider!r} is not supported")):
        validate_snap_configuration(snap_config)