diff --git a/komposer/types/cli.py b/komposer/types/cli.py index 92d2927..83d96e9 100644 --- a/komposer/types/cli.py +++ b/komposer/types/cli.py @@ -7,17 +7,12 @@ from komposer.types.base import ImmutableBaseModel from komposer.utils import load_yaml -RFC_1123_MAX_LENGTH = 63 +lowercase_kebak_re = re.compile(r"^[a-z0-9][a-z\-0-9]*[a-z0-9]$") -rfc_1123_re = re.compile(r"^[a-z][a-z\-0-9]*[a-z]$") - -def ensure_is_rfc_1123(string: str) -> None: - if not rfc_1123_re.match(string): - raise ValueError("Not a valid RFC-1123 string") - - if len(string) > RFC_1123_MAX_LENGTH: - raise ValueError("String is longer than 63 characters") +def ensure_lowercase_kebab(string: str) -> None: + if not lowercase_kebak_re.match(string): + raise ValueError("Not a lowercase kebab string") class DeploymentContext(ImmutableBaseModel): @@ -50,19 +45,19 @@ class Context(ImmutableBaseModel): @validator("project_name") def project_name_matches_kubernetes_name(cls, value: str) -> str: - ensure_is_rfc_1123(value) + ensure_lowercase_kebab(value) return value @validator("branch_name") def branch_name_matches_kubernetes_name(cls, value: str) -> str: - ensure_is_rfc_1123(value) + ensure_lowercase_kebab(value) return value @validator("repository_name") def repository_name_matches_kubernetes_name(cls, value: str) -> str: - ensure_is_rfc_1123(value) + ensure_lowercase_kebab(value) return value diff --git a/tests/core/base_test.py b/tests/core/base_test.py index dd0ea82..8efd814 100644 --- a/tests/core/base_test.py +++ b/tests/core/base_test.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Sequence +from typing import Optional, Sequence import pytest from pytest_mock import MockerFixture @@ -29,7 +29,7 @@ from tests.fixtures import make_context, make_labels -def make_minimal_docker_compose(ports: list[str] = None) -> docker_compose.DockerCompose: +def make_minimal_docker_compose(ports: Optional[list[str]] = None) -> docker_compose.DockerCompose: return docker_compose.DockerCompose( services={"my-service": docker_compose.Service(ports=ports or [])} ) diff --git a/tests/types/cli_test.py b/tests/types/cli_test.py index fa39f53..ef189b4 100644 --- a/tests/types/cli_test.py +++ b/tests/types/cli_test.py @@ -2,7 +2,7 @@ import pytest -from komposer.types.cli import ensure_is_rfc_1123 +from komposer.types.cli import ensure_lowercase_kebab from tests.fixtures import make_context @@ -45,37 +45,34 @@ def test_context_manifest_prefix( pytest.param("string", id="Simple string"), pytest.param("my-string", id="String with hyphens"), pytest.param("my-1-string", id="String with digits"), - pytest.param("a" * 63, id="Max length of the string"), + pytest.param("1-string", id="String with leading digits"), + pytest.param("string-1", id="String with trailing digits"), ], ) def test_ensure_is_kubernetes_name(value: str) -> None: """ GIVEN a string - AND the string is a valid RFC 1123 + AND the string is in lowercase kebab format WHEN calling ensure_is_kubernetes_name THEN no exception is raised """ - ensure_is_rfc_1123(value) + ensure_lowercase_kebab(value) @pytest.mark.parametrize( - "value, expected", + "value", [ - pytest.param("", "Not a valid RFC-1123 string", id="Empty string"), - pytest.param("1string", "Not a valid RFC-1123 string", id="String with leading digit"), - pytest.param("string1", "Not a valid RFC-1123 string", id="String with trailing digit"), - pytest.param( - "my/string", "Not a valid RFC-1123 string", id="String with unsupported symbol" - ), - pytest.param("a" * 64, "String is longer than 63 characters", id="String is too long"), + pytest.param("", id="Empty string"), + pytest.param("my/string", id="String with unsupported symbol /"), + pytest.param("my_string", id="String with unsupported symbol _"), ], ) -def test_ensure_is_kubernetes_name_fails_if_not_rfc_1123(value: str, expected: str) -> None: +def test_ensure_is_kubernetes_name_fails_if_not_lowercase_kebak(value: str) -> None: """ GIVEN a string - AND the string is not a valid RFC 1123 + AND the string is not in lowercase kebab format WHEN calling ensure_is_kubernetes_name THEN an exception is raised """ - with pytest.raises(ValueError, match=expected): - ensure_is_rfc_1123(value) + with pytest.raises(ValueError, match="Not a lowercase kebab string"): + ensure_lowercase_kebab(value)