Core Architecture & Invariant CI / validate (pull_request) Failing after 1m14s
Harden unit test assertions and coverage across core control plane modules based on mutation testing findings. - tests/unit/test_appctl_cli.py: preserve PYTHONPATH in subprocess environment; add coverage for empty workload lists, core service display, rich metadata, SSL status formatting (_format_ssl_info), and resolve JSON output. - tests/unit/test_appctl_auth.py: assert specific ErrorCode on permission denials; verify operator denial without explicit target on mutating operations. - tests/unit/test_appctl_audit.py: verify journal serialization and entry ordering. - tests/unit/test_appctl_contracts.py: verify sanitize_validation_error strips secret token values and exact error details. - tests/unit/test_appctl_router.py: assert RouterValidationError on whitespace domains and verify domain canonicalization. - tests/unit/test_core_manifest.py: anchor empty URL regex validation; verify rejection of root repository URLs (https://git.example.com/). - tests/unit/test_provenance.py: verify Class B workload image-only status and default timeout enforcement in resolve_upstream_provenance(). Rationale: Mutation analysis identified weak substring assertions and missing branch coverage across CLI output formatting and security boundaries. Hardening these assertions kills surviving mutants and prevents behavioral regressions.
343 lines
9.1 KiB
Python
343 lines
9.1 KiB
Python
"""
|
|
Unit test suite for scripts/core_manifest.py (Schema v1.0 & Parser Library).
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
# # Add scripts directory to path
|
|
# CORE_ROOT = Path(__file__).resolve().parent.parent.parent
|
|
# sys.path.insert(0, str(CORE_ROOT / "scripts"))
|
|
from core_manifest import (
|
|
Manifest,
|
|
ManifestValidationError,
|
|
RefType,
|
|
TransportScheme,
|
|
classify_ref,
|
|
parse_manifest,
|
|
validate_transport,
|
|
)
|
|
|
|
|
|
def test_validate_transport_https():
|
|
|
|
scheme, url = validate_transport(
|
|
"https://git.example.com/operator/homelab-snappymail.git"
|
|
)
|
|
assert scheme == TransportScheme.HTTPS
|
|
assert url == "https://git.example.com/operator/homelab-snappymail.git"
|
|
|
|
|
|
def test_validate_transport_ssh():
|
|
scheme, url = validate_transport(
|
|
"ssh://git@git.example.com:2223/operator/homelab-docs.git"
|
|
)
|
|
assert scheme == TransportScheme.SSH
|
|
assert url == "ssh://git@git.example.com:2223/operator/homelab-docs.git"
|
|
|
|
|
|
def test_validate_transport_git_scp():
|
|
scheme, url = validate_transport("git@github.com:operator/custom-workload.git")
|
|
assert scheme == TransportScheme.GIT_SCP
|
|
assert url == "git@github.com:operator/custom-workload.git"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"insecure_url",
|
|
[
|
|
"http://insecure.example.com/repo.git",
|
|
"http://github.com/repo.git",
|
|
],
|
|
)
|
|
def test_validate_transport_rejects_http(insecure_url: str):
|
|
with pytest.raises(
|
|
ManifestValidationError, match="Insecure HTTP source URL rejected"
|
|
):
|
|
validate_transport(insecure_url)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"empty_or_invalid_url",
|
|
[
|
|
"",
|
|
" ",
|
|
None,
|
|
],
|
|
)
|
|
def test_validate_transport_rejects_empty_or_non_string(empty_or_invalid_url):
|
|
with pytest.raises(ManifestValidationError, match=r"^Source URL cannot be empty\.$"):
|
|
validate_transport(empty_or_invalid_url) # type: ignore[arg-type]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("root_path_url", "scheme"),
|
|
[
|
|
("https://git.example.com/", "HTTPS"),
|
|
("ssh://git.example.com/", "SSH"),
|
|
],
|
|
)
|
|
def test_validate_transport_rejects_root_repository_path(root_path_url: str, scheme: str):
|
|
with pytest.raises(
|
|
ManifestValidationError,
|
|
match=rf"^Malformed {scheme} source URL \(missing repository path\)",
|
|
):
|
|
validate_transport(root_path_url)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"traversal_url",
|
|
[
|
|
"../../etc/shadow",
|
|
"/home/operator/secret",
|
|
"~/vault",
|
|
r"repo\windows\payload",
|
|
r"..\traversal",
|
|
],
|
|
)
|
|
def test_validate_transport_rejects_paths_and_traversal(traversal_url: str):
|
|
with pytest.raises(
|
|
ManifestValidationError,
|
|
match="Prohibited filesystem path or traversal in source URL",
|
|
):
|
|
validate_transport(traversal_url)
|
|
|
|
|
|
def test_validate_transport_rejects_file_scheme():
|
|
with pytest.raises(
|
|
ManifestValidationError,
|
|
match="Invalid source.url scheme: 'file:///etc/passwd'",
|
|
):
|
|
validate_transport("file:///etc/passwd")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"empty_ref",
|
|
[
|
|
"",
|
|
" ",
|
|
None,
|
|
12345,
|
|
],
|
|
)
|
|
def test_classify_ref_empty_or_invalid_defaults_to_main(empty_ref):
|
|
assert classify_ref(empty_ref) == (RefType.BRANCH, "main") # type: ignore[arg-type]
|
|
|
|
|
|
def test_classify_ref_branches():
|
|
assert classify_ref("main") == (RefType.BRANCH, "main")
|
|
assert classify_ref("refs/heads/develop") == (RefType.BRANCH, "develop")
|
|
assert classify_ref("feat/manifest-engine") == (
|
|
RefType.BRANCH,
|
|
"feat/manifest-engine",
|
|
)
|
|
assert classify_ref("release/v1.0") == (RefType.BRANCH, "release/v1.0")
|
|
assert classify_ref("custom-adhoc-branch") == (RefType.BRANCH, "custom-adhoc-branch")
|
|
|
|
|
|
|
|
def test_classify_ref_tags():
|
|
assert classify_ref("v1.0.0") == (RefType.TAG, "v1.0.0")
|
|
assert classify_ref("refs/tags/2.4.1") == (RefType.TAG, "2.4.1")
|
|
assert classify_ref("v0.1.0-alpha") == (RefType.TAG, "v0.1.0-alpha")
|
|
|
|
|
|
def test_classify_ref_commit_sha():
|
|
sha = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
|
assert classify_ref(sha) == (RefType.COMMIT, sha)
|
|
|
|
|
|
def test_parse_manifest_class_a_source_backed():
|
|
raw_yaml = """
|
|
schemaVersion: "1.0"
|
|
name: "bitetrack"
|
|
aliases:
|
|
- "bt"
|
|
domain: "bitetrack.example.com"
|
|
description: "Nutrition and macro tracking application"
|
|
visible: true
|
|
auth: true
|
|
networks:
|
|
- proxy-net
|
|
env:
|
|
PORT: 8080
|
|
source:
|
|
type: git
|
|
url: "https://git.example.com/operator/bitetrack.git"
|
|
ref: "v1.2.0"
|
|
deployment:
|
|
strategy: compose
|
|
branch: main
|
|
actions:
|
|
- git_pull
|
|
- compose_build
|
|
- compose_up
|
|
homepage:
|
|
title: "BiteTrack"
|
|
group: "Productivity"
|
|
icon: "food.png"
|
|
weight: 25
|
|
"""
|
|
manifest = parse_manifest(raw_yaml)
|
|
assert manifest.name == "bitetrack"
|
|
assert manifest.schema_version == "1.0"
|
|
assert manifest.aliases == ["bt"]
|
|
assert manifest.is_class_a is True
|
|
assert manifest.is_class_b is False
|
|
assert manifest.is_internal is False
|
|
assert manifest.source is not None
|
|
assert manifest.source.url == "https://git.example.com/operator/bitetrack.git"
|
|
assert manifest.source.ref == "v1.2.0"
|
|
assert manifest.source.ref_type == RefType.TAG
|
|
assert manifest.source.transport == TransportScheme.HTTPS
|
|
assert manifest.deployment.actions == ["git_pull", "compose_build", "compose_up"]
|
|
assert manifest.homepage is not None
|
|
assert manifest.homepage.weight == 25
|
|
|
|
|
|
def test_parse_manifest_class_b_image_only():
|
|
raw_yaml = """
|
|
name: "snappymail"
|
|
domain: "mail.example.com"
|
|
description: "Webmail Client"
|
|
deployment:
|
|
actions:
|
|
- compose_up
|
|
"""
|
|
manifest = parse_manifest(raw_yaml)
|
|
assert manifest.name == "snappymail"
|
|
assert manifest.is_class_a is False
|
|
assert manifest.is_class_b is True
|
|
assert manifest.source is None
|
|
assert manifest.deployment.actions == ["compose_up"]
|
|
|
|
|
|
def test_parse_manifest_rejects_missing_name():
|
|
with pytest.raises(
|
|
ManifestValidationError, match="missing mandatory string field 'name'"
|
|
):
|
|
parse_manifest("domain: test.example.com\n")
|
|
|
|
|
|
def test_parse_manifest_rejects_invalid_name():
|
|
with pytest.raises(ManifestValidationError, match="Invalid workload name"):
|
|
parse_manifest("name: 'invalid/name'\n")
|
|
|
|
|
|
def test_parse_manifest_rejects_prohibited_deployment_actions():
|
|
raw_yaml = """
|
|
name: "app"
|
|
deployment:
|
|
actions:
|
|
- compose_up
|
|
- rm_rf_root
|
|
"""
|
|
with pytest.raises(
|
|
ManifestValidationError, match="Prohibited deployment action 'rm_rf_root'"
|
|
):
|
|
parse_manifest(raw_yaml)
|
|
|
|
|
|
def test_parse_manifest_rejects_prohibited_strategy():
|
|
raw_yaml = """
|
|
name: "app"
|
|
deployment:
|
|
strategy: "swarm"
|
|
"""
|
|
with pytest.raises(
|
|
ManifestValidationError, match="Unsupported deployment strategy 'swarm'"
|
|
):
|
|
parse_manifest(raw_yaml)
|
|
|
|
|
|
def test_parse_manifest_from_path(tmp_path: Path):
|
|
manifest_file = tmp_path / "app.yaml"
|
|
manifest_file.write_text("name: file-app\ndomain: internal\n", encoding="utf-8")
|
|
|
|
manifest = parse_manifest(manifest_file)
|
|
assert manifest.name == "file-app"
|
|
assert manifest.is_internal is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"invalid_key",
|
|
[
|
|
"123_BAD",
|
|
"INVALID-KEY",
|
|
"FOO;BAR",
|
|
"KEY WITH SPACE",
|
|
"PATH",
|
|
"LD_PRELOAD",
|
|
"IFS",
|
|
"CORE_DIR",
|
|
"SITES_DIR",
|
|
],
|
|
)
|
|
def test_parse_manifest_rejects_invalid_or_reserved_env_keys(invalid_key: str):
|
|
raw_yaml = f"""
|
|
name: "app"
|
|
env:
|
|
"{invalid_key}": "some_value"
|
|
"""
|
|
with pytest.raises(
|
|
ManifestValidationError, match="Invalid or reserved environment variable key"
|
|
):
|
|
parse_manifest(raw_yaml)
|
|
|
|
|
|
def test_parse_manifest_accepts_valid_env_keys():
|
|
raw_yaml = """
|
|
name: "app"
|
|
env:
|
|
PROJECT_PATH: "/mnt/data"
|
|
CUSTOM_PORT_1: "8080"
|
|
_INTERNAL_FLAG: "true"
|
|
"""
|
|
manifest = parse_manifest(raw_yaml)
|
|
assert manifest.env["PROJECT_PATH"] == "/mnt/data"
|
|
assert manifest.env["CUSTOM_PORT_1"] == "8080"
|
|
assert manifest.env["_INTERNAL_FLAG"] == "true"
|
|
|
|
|
|
def test_parse_manifest_rejects_extra_fields():
|
|
"""Ensure extra='forbid' rejects unrecognized top-level manifest keys."""
|
|
raw_yaml = """
|
|
name: "app"
|
|
unrecognized_field: "should_fail"
|
|
"""
|
|
with pytest.raises(
|
|
ManifestValidationError, match="unrecognized or forbidden field"
|
|
):
|
|
parse_manifest(raw_yaml)
|
|
|
|
|
|
def test_parse_manifest_rejects_extra_fields_in_nested_blocks():
|
|
"""Ensure extra='forbid' rejects unrecognized keys in nested configuration blocks."""
|
|
raw_yaml = """
|
|
name: "app"
|
|
deployment:
|
|
strategy: compose
|
|
unknown_deploy_option: "forbidden"
|
|
"""
|
|
with pytest.raises(ManifestValidationError):
|
|
parse_manifest(raw_yaml)
|
|
|
|
|
|
def test_manifest_pydantic_serialization():
|
|
"""Ensure Manifest operates as a Pydantic v2 model with correct serialization."""
|
|
from pydantic import BaseModel
|
|
|
|
raw_yaml = """
|
|
name: "serialized-app"
|
|
domain: "app.example.test"
|
|
description: "Pydantic serialization test"
|
|
"""
|
|
manifest = parse_manifest(raw_yaml)
|
|
assert isinstance(manifest, Manifest)
|
|
assert isinstance(manifest, BaseModel)
|
|
|
|
dumped = manifest.model_dump(by_alias=True)
|
|
assert dumped["name"] == "serialized-app"
|
|
assert dumped["schemaVersion"] == "1.0"
|
|
assert manifest.to_dict()["name"] == "serialized-app"
|