1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2026-07-03 20:05:36 +01:00
Files
supervisor/tests/test_validate.py
T
Stefan Agner ed91b18c4b tests: enable flake8-pytest-style (PT) ruff rules (#6857)
* tests: enable flake8-pytest-style (PT) ruff rules

Enable the `PT` ruff rule set and fix the resulting violations across the
test suite:

- PT006: pass parametrize argument names as tuples instead of a single
  comma-separated string.
- PT022: switch fixtures that have no teardown from `yield` to `return`
  so the lack of cleanup is obvious at a glance.
- PT011: add `match=` to broad `pytest.raises(ValueError)` blocks so the
  expected error is anchored to a specific message.
- PT012: hoist setup (patches, branching) out of `pytest.raises()`
  blocks so only the call that is expected to raise remains inside.
- PT013: replace `from pytest import X` with `import pytest` and access
  attributes via the module.
- PT015: replace `try/except` + `assert False` patterns with
  `pytest.raises(...)`.
- PT017: replace `assert` on exceptions inside `except` blocks with
  `pytest.raises(...) as exc_info` and assert on `exc_info.value`.

No behavioral changes to the tests; the full suite still passes.

* tests: address review feedback on PT ruff rule enablement

- Fix fixture return-type annotations after switching `yield` to `return`
  in tests/conftest.py: drop the `Generator[...]`/`AsyncGenerator[...]`
  wrapper for `dns_manager_service`, `supervisor_internet`, `websession`,
  and `mock_update_data` so the annotation matches what the fixture
  actually returns.
- Correct the return-type annotation of `fixture_ip6config_service` from
  `IP4ConfigService` to `IP6ConfigService`.
- Fix recurring "excepiton" typo in tests/utils/test_exception_helper.py.

* tests: verify backup cleanup on permission error

After `test_new_backup_permission_error` raises `BackupPermissionError`,
assert that no tarfile was left behind and `tmp_path` is empty. The
previous version only checked that the exception was raised, which
missed any regression where a partial tarfile would survive the failed
create.

* tests: rename DNS_GOOD_V6 to DNS_V6_UNSUPPORTED

The constant was named "good" but its tests assert that the URLs are
rejected by the DNS validator. The IPv6 URLs are well-formed but
currently rejected because IPv6 doesn't work with the Docker network
(see `dns_url` in supervisor/validate.py). Rename the constant and the
related test to make the intent obvious.
2026-05-20 22:17:54 +02:00

156 lines
4.7 KiB
Python

"""Test validators."""
import pytest
import voluptuous as vol
from supervisor import validate
from supervisor.validate import SCHEMA_SUPERVISOR_CONFIG
DNS_GOOD_V4 = [
"dns://10.0.0.1", # random local
"dns://254.254.254.254", # random high numbers
"DNS://1.1.1.1", # cloudflare
"dns://9.9.9.9", # quad-9
]
DNS_V6_UNSUPPORTED = [
"dns://2606:4700:4700::1111", # cloudflare
"DNS://2606:4700:4700::1001", # cloudflare
]
DNS_BAD = ["hello world", "https://foo.bar", "", "dns://example.com"]
IMAGE_NAME_GOOD = [
"ghcr.io/home-assistant/{machine}-homeassistant",
"ghcr.io/home-assistant/{arch}-homeassistant",
"homeassistant/{arch}-homeassistant",
"doocker.io/homeassistant/{arch}-homeassistant",
"ghcr.io/home-assistant/amd64-homeassistant",
"homeassistant/amd64-homeassistant",
"ttl.sh/homeassistant",
"myreg.local:8080/homeassistant",
"localhost/myimage",
"localhost:5000/myimage",
"127.0.0.1/myimage",
"127.0.0.1:5000/org/myimage",
"[::1]:5000/myimage",
"dockeruser/nice-app-1.2",
"ghcr.io/blakeblackshear/frigate",
]
IMAGE_NAME_BAD = [
"ghcr.io/home-assistant/homeassistant:123",
"ghcr.io/blakeblackshear/frigate:stable-rocm",
".ghcr.io/home-assistant/homeassistant",
"HOMEASSISTANT/homeassistant",
"homeassistant/HOMEASSISTANT",
"homeassistant/_homeassistant",
"homeassistant/-homeassistant",
"GHCR.IO/home-assistant/homeassistant",
]
def test_dns_url_v4_good():
"""Test the DNS validator with known-good IPv4 DNS URLs."""
for url in DNS_GOOD_V4:
assert validate.dns_url(url)
@pytest.mark.parametrize("url", DNS_V6_UNSUPPORTED)
def test_dns_url_v6_rejected(url: str):
"""Test the DNS validator rejects well-formed IPv6 DNS URLs.
IPv6 is currently not supported for DNS because it doesn't work with
the Docker network.
"""
with pytest.raises(vol.error.Invalid):
validate.dns_url(url)
def test_dns_server_list_v4():
"""Test a list with v4 addresses."""
assert validate.dns_server_list(DNS_GOOD_V4)
def test_dns_server_list_v6_rejected():
"""Test that lists of IPv6 DNS URLs are rejected."""
with pytest.raises(vol.error.Invalid):
assert validate.dns_server_list(DNS_V6_UNSUPPORTED)
def test_dns_server_list_combined():
"""Test a list with both v4 and v6 addresses."""
combined = DNS_GOOD_V4 + DNS_V6_UNSUPPORTED
# test the matches
with pytest.raises(vol.error.Invalid):
validate.dns_server_list(combined)
# test max_length is OK still
with pytest.raises(vol.error.Invalid):
validate.dns_server_list(combined)
# test that it fails when the list is too long
with pytest.raises(vol.error.Invalid):
validate.dns_server_list(combined + combined + combined + combined)
def test_dns_server_list_bad():
"""Test the bad list."""
# test the matches
with pytest.raises(vol.error.Invalid):
assert validate.dns_server_list(DNS_BAD)
def test_dns_server_list_bad_combined():
"""Test the bad list, combined with the good."""
combined = DNS_GOOD_V4 + DNS_V6_UNSUPPORTED + DNS_BAD
with pytest.raises(vol.error.Invalid):
# bad list
assert validate.dns_server_list(combined)
def test_image_name_good():
"""Test container image names validator with known-good image names."""
for image_name in IMAGE_NAME_GOOD:
assert validate.docker_image(image_name)
def test_image_name_bad():
"""Test container image names validator with known-bad image names."""
for image_name in IMAGE_NAME_BAD:
with pytest.raises(vol.error.Invalid):
assert validate.docker_image(image_name)
def test_version_complex():
"""Test version simple with good version."""
for version in (
"landingpage",
"dev",
"1c002dd",
"1.1.1",
"1.0",
"0.150.1",
"0.150.1b1",
"0.150.1.dev20200715",
"1",
"alpine-5.4",
1,
1.1,
):
assert validate.version_tag(version) == str(version)
assert validate.version_tag(None) is None
def test_supervisor_config_migration_addons_custom_list():
"""Test that old 'addons_custom_list' key is migrated to 'apps_custom_list'."""
result = SCHEMA_SUPERVISOR_CONFIG(
{"addons_custom_list": ["https://example.com/repo"]}
)
assert result["apps_custom_list"] == ["https://example.com/repo"]
assert "addons_custom_list" not in result
def test_supervisor_config_apps_custom_list_unchanged():
"""Test that new 'apps_custom_list' key passes through unchanged."""
result = SCHEMA_SUPERVISOR_CONFIG(
{"apps_custom_list": ["https://example.com/repo"]}
)
assert result["apps_custom_list"] == ["https://example.com/repo"]