Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bases: apply stricter typing to base names. #263

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions craft_providers/bases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

# Backward compatible, will be removed in 2.0
import sys
from enum import Enum
from typing import Dict, Tuple, Type
from typing import Dict, NamedTuple, Tuple, Type, Union

from craft_providers.errors import BaseCompatibilityError, BaseConfigurationError

Expand All @@ -32,39 +31,52 @@

sys.modules["craft_providers.bases.buildd"] = buildd

BaseAlias = Union[ubuntu.BuilddBaseAlias, centos.CentOSBaseAlias]

__all__ = [
"ubuntu",
"centos",
"BaseAlias",
"BaseName",
"BuilddBase",
"BuilddBaseAlias",
"BaseCompatibilityError",
"BaseConfigurationError",
]

BASE_NAME_TO_BASE_ALIAS: Dict[Tuple[str, str], Enum] = {
("ubuntu", "16.04"): ubuntu.BuilddBaseAlias.XENIAL,
("ubuntu", "18.04"): ubuntu.BuilddBaseAlias.BIONIC,
("ubuntu", "20.04"): ubuntu.BuilddBaseAlias.FOCAL,
("ubuntu", "22.04"): ubuntu.BuilddBaseAlias.JAMMY,
("ubuntu", "22.10"): ubuntu.BuilddBaseAlias.KINETIC,
("ubuntu", "23.04"): ubuntu.BuilddBaseAlias.LUNAR,
("ubuntu", "devel"): ubuntu.BuilddBaseAlias.DEVEL,
("centos", "7"): centos.CentOSBaseAlias.SEVEN,

class BaseName(NamedTuple):
"""A base image, by distribution and version."""

name: str
version: str


BASE_NAME_TO_BASE_ALIAS: Dict[BaseName, BaseAlias] = {
BaseName("ubuntu", "16.04"): ubuntu.BuilddBaseAlias.XENIAL,
BaseName("ubuntu", "18.04"): ubuntu.BuilddBaseAlias.BIONIC,
BaseName("ubuntu", "20.04"): ubuntu.BuilddBaseAlias.FOCAL,
BaseName("ubuntu", "22.04"): ubuntu.BuilddBaseAlias.JAMMY,
BaseName("ubuntu", "22.10"): ubuntu.BuilddBaseAlias.KINETIC,
BaseName("ubuntu", "23.04"): ubuntu.BuilddBaseAlias.LUNAR,
BaseName("ubuntu", "devel"): ubuntu.BuilddBaseAlias.DEVEL,
BaseName("centos", "7"): centos.CentOSBaseAlias.SEVEN,
}


def get_base_alias(
base_name: Tuple[str, str],
) -> Enum:
) -> BaseAlias:
"""Return a Base alias from a base (name, version) tuple."""
base_name = BaseName(*base_name)
if base_name in BASE_NAME_TO_BASE_ALIAS:
return BASE_NAME_TO_BASE_ALIAS[base_name]

raise BaseConfigurationError(f"Base alias not found for {base_name}")


def get_base_from_alias(
alias: Enum,
alias: BaseAlias,
) -> Type[Base]:
"""Return a Base class from a known base alias."""
if isinstance(alias, ubuntu.BuilddBaseAlias):
Expand Down
4 changes: 2 additions & 2 deletions craft_providers/lxd/remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from typing import Dict, Union

from craft_providers import Base
from craft_providers.bases import centos, get_base_alias, ubuntu
from craft_providers.bases import BaseName, centos, get_base_alias, ubuntu

from .errors import LXDError
from .lxc import LXC
Expand Down Expand Up @@ -180,7 +180,7 @@ def get_remote_image(provider_base: Union[Base, str]) -> RemoteImage:
"""
# temporary backward compatibility before 2.0
if isinstance(provider_base, str):
alias = get_base_alias(("ubuntu", provider_base))
alias = get_base_alias(BaseName("ubuntu", provider_base))
provider_base = ubuntu.BuilddBase(alias=alias) # type: ignore

image = _PROVIDER_BASE_TO_LXD_REMOTE_IMAGE.get(provider_base.alias)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/bases/test_get_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_get_base_alias_does_not_exist():
get_base_alias(("ubuntu", "8.04"))

assert exc_info.value == BaseConfigurationError(
brief="Base alias not found for ('ubuntu', '8.04')"
brief="Base alias not found for BaseName(name='ubuntu', version='8.04')"
)


Expand Down
4 changes: 3 additions & 1 deletion tests/unit/lxd/test_remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ def test_get_image_remote_deprecated_error():
with pytest.raises(BaseConfigurationError) as raised:
lxd.remotes.get_remote_image("8.04")

assert "Base alias not found for ('ubuntu', '8.04')" == str(raised.value)
assert "Base alias not found for BaseName(name='ubuntu', version='8.04')" == str(
raised.value
)


def test_configure_buildd_image_remote(
Expand Down