Skip to content

Commit

Permalink
Validate the received version as an integer
Browse files Browse the repository at this point in the history
This is was is expected by ubuntu-image in the revision field.

Signed-off-by: Paul Mars <[email protected]>
  • Loading branch information
upils committed Jul 31, 2024
1 parent e7755b0 commit dbe2575
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 22 deletions.
4 changes: 2 additions & 2 deletions imagecraft/image_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ImageDefinition(BaseModel):

name: str
display_name: str
revision: str
revision: int
class_: str = Field(alias="class")
architecture: str
series: str
Expand All @@ -141,7 +141,7 @@ class Config:
def __init__( # noqa: PLR0913
self,
series: str,
revision: str,
revision: int,
architecture: str,
pocket: str,
kernel: str | None,
Expand Down
11 changes: 11 additions & 0 deletions imagecraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ def _validate_package_repositories(
validate_package_repositories(repositories)
return repositories

@validator("version") # pyright: ignore[reportUntypedFunctionDecorator]
@classmethod
def _validate_version(cls, version: str) -> str:
try:
int(version)
except ValueError as e:
raise CraftValidationError(
"Invalid version",
details="value is not an integer.",
) from e
return version

@validator("platforms", pre=True) # pyright: ignore[reportUntypedFunctionDecorator]
@classmethod
Expand Down
2 changes: 1 addition & 1 deletion imagecraft/plugins/ubuntu_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_build_commands(self) -> list[str]:
if branch:
source_branch = branch

version = self._part_info.project_info.get_project_var("version", raw_read=True)
version = int(self._part_info.project_info.get_project_var("version", raw_read=True))

main_repo = get_main_package_repository(self._part_info.project_info.package_repositories)

Expand Down
2 changes: 1 addition & 1 deletion imagecraft/ubuntu_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

def ubuntu_image_cmds_build_rootfs( # noqa: PLR0913
series: str,
version: str,
version: int,
arch: str,
pocket: str,
sources: list[str],
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def default_project(extra_project_params):

return Project(
name=ProjectName("default"),
version=VersionStr("1.0"),
version=VersionStr("1"),
summary=SummaryStr("default project"),
description="default project",
base="[email protected]",
Expand Down
24 changes: 19 additions & 5 deletions tests/unit/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

IMAGECRAFT_YAML_GENERIC = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand Down Expand Up @@ -79,7 +79,7 @@

IMAGECRAFT_YAML_SIMPLE_PLATFORM = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand All @@ -94,7 +94,7 @@

IMAGECRAFT_YAML_MINIMAL_PLATFORM = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand All @@ -107,7 +107,7 @@

IMAGECRAFT_YAML_NO_BUILD_FOR_PLATFORM = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand All @@ -121,7 +121,7 @@

IMAGECRAFT_YAML_SIMPLE_PACKAGE_REPO = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand Down Expand Up @@ -288,3 +288,17 @@ def reload_project_package_repositories(mock_package_repositories, raises):
mock_package_repositories,
ProjectValidationError,
)


def test_project_version_invalid(yaml_loaded_data):
def reload_project_version(mock_version):
yaml_loaded_data["version"] = mock_version
with pytest.raises(CraftValidationError) as err:
Project.unmarshal(yaml_loaded_data)

return str(err.value)

mock_version = ["24.04"]
assert "value is not an integer" in reload_project_version(
mock_version,
)
2 changes: 1 addition & 1 deletion tests/unit/plugins/test_ubuntu_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _ubuntu_seed_plugin(spec, tmp_path):
plugin_properties=plugin_properties,
)
project_vars = {
"version": "22.04",
"version": "1",
}
package_repositories = [
PackageRepositoryApt.unmarshal(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/services/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_lifecycle_args(
platform="amd64",
base="[email protected]",
project_name="default",
project_vars={"version": "1.0"},
project_vars={"version": "1"},
package_repositories=None,
)

Expand Down Expand Up @@ -73,7 +73,7 @@ def test_lifecycle_args_no_platform(
platform=None,
base="[email protected]",
project_name="default",
project_vars={"version": "1.0"},
project_vars={"version": "1"},
package_repositories=None,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

IMAGECRAFT_YAML = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand Down Expand Up @@ -59,7 +59,7 @@

IMAGECRAFT_YAML_NO_GADGET = """
name: ubuntu-server-amd64
version: "22.04"
version: 1
series: jammy
platforms:
amd64:
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/test_ubuntu_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(
ImageDefinition(
series="mantic",
revision="22.04",
revision=1,
architecture="amd64",
pocket="release",
kernel="linux-image-generic",
Expand Down Expand Up @@ -69,7 +69,7 @@
),
"""name: craft-driver
display-name: Craft Driver
revision: '22.04'
revision: 1
class: preinstalled
architecture: amd64
series: mantic
Expand Down Expand Up @@ -114,7 +114,7 @@
(
ImageDefinition(
series="mantic",
revision="22.04",
revision=2,
architecture="amd64",
pocket="proposed",
kernel="linux-image-generic",
Expand All @@ -129,7 +129,7 @@
),
"""name: craft-driver
display-name: Craft Driver
revision: '22.04'
revision: 2
class: preinstalled
architecture: amd64
series: mantic
Expand Down Expand Up @@ -158,7 +158,7 @@
(
ImageDefinition(
series="mantic",
revision="22.04",
revision=1,
architecture="amd64",
pocket="proposed",
kernel=None,
Expand All @@ -173,7 +173,7 @@
),
"""name: craft-driver
display-name: Craft Driver
revision: '22.04'
revision: 1
class: preinstalled
architecture: amd64
series: mantic
Expand All @@ -200,7 +200,7 @@ def test_ubuntu_image_cmds_build_rootfs(mocker):

assert ubuntu_image_cmds_build_rootfs(
series="mantic",
version="22.04",
version=1,
arch="amd64",
pocket="proposed",
sources=["source1", "source2"],
Expand Down

0 comments on commit dbe2575

Please sign in to comment.