Skip to content
Open
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
15 changes: 12 additions & 3 deletions rockcraft/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,25 @@ class ExecCheck(_BaseCheck):
exec: ExecCheckOptions


def _get_check_tag(check: Mapping[str, Any]) -> str:
def _get_check_tag(check: Mapping[str, Any] | _BaseCheck) -> str:
if isinstance(check, HttpCheck):
return "http"
if isinstance(check, TcpCheck):
return "tcp"
if isinstance(check, ExecCheck):
return "exec"
if not isinstance(check, Mapping):
raise CraftValidationError(f"Unknown check type for {check!r}.")

tags = ("http", "tcp", "exec")
check_types = check.keys() & tags
check_types = [tag for tag in tags if tag in check]
match len(check_types):
case 0:
raise CraftValidationError(
f"Must specify exactly one of {', '.join(tags)} for each check."
)
case 1:
return check_types.pop()
return check_types[0]
case _:
raise CraftValidationError(
f"Multiple check types specified ({', '.join(sorted(check_types))}). "
Expand Down
34 changes: 33 additions & 1 deletion tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import datetime
import os
import subprocess
import warnings
from pathlib import Path
from typing import cast
from typing import Any, cast

import pydantic
import pytest
Expand Down Expand Up @@ -859,6 +860,37 @@ def test_provider_base(base, expected_base):
assert actual_base == expected_base


def test_project_marshal_exec_check_emits_no_pydantic_warning():
yaml_data = {
"name": "gubernator",
"version": "3.0.0",
"summary": "High-performance, distributed rate-limiting service",
"description": "example",
"base": "ubuntu@24.04",
"platforms": {"amd64": None},
"parts": {"foo": {"plugin": "nil"}},
"checks": {
"online": {
"override": "replace",
"period": "3s",
"exec": {"command": "/bin/healthcheck"},
}
},
}

project = Project.unmarshal(yaml_data)

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
dumped = project.marshal()

checks = cast(dict[str, Any], dumped["checks"])
assert checks["online"]["exec"]["command"] == "/bin/healthcheck"
assert not any(
"PydanticSerializationUnexpectedValue" in str(w.message) for w in caught
)


def test_provider_base_error():
with pytest.raises(ValueError, match="Unknown base 'unknown'"):
Project._providers_base("unknown") # pylint: disable=protected-access
Loading