Skip to content

Commit

Permalink
Validate for lower kebab format instead of RFC-1123 (#9)
Browse files Browse the repository at this point in the history
* Check if branch, project and repository names are lower kebak instead of RFC-1123

* Fixed linting
  • Loading branch information
expobrain authored Jul 8, 2022
1 parent 38e532d commit 5f9811c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
19 changes: 7 additions & 12 deletions komposer/types/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/core/base_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Sequence
from typing import Optional, Sequence

import pytest
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -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 [])}
)
Expand Down
29 changes: 13 additions & 16 deletions tests/types/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)

0 comments on commit 5f9811c

Please sign in to comment.