Files
tracer-cloud--opensre/tests/masking/test_integration_with_k8s_fixture.py
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

88 lines
2.9 KiB
Python

"""Integration test: mask a realistic k8s alert payload and round-trip it.
Uses the repo's existing Datadog k8s alert fixture — no live k8s required.
Verifies that pod/namespace/cluster/host identifiers are all replaced by
placeholders, and that unmasking reproduces the original payload.
"""
from __future__ import annotations
import json
from pathlib import Path
from platform.masking.context import MaskingContext
from platform.masking.policy import ALL_KINDS, MaskingPolicy
FIXTURE = (
Path(__file__).parent.parent / "e2e" / "kubernetes" / "fixtures" / "datadog_k8s_alert.json"
)
def _load_fixture() -> dict:
return json.loads(FIXTURE.read_text(encoding="utf-8"))
def _enabled_ctx() -> MaskingContext:
return MaskingContext(
policy=MaskingPolicy.model_validate({"enabled": True, "kinds": ALL_KINDS})
)
def test_fixture_file_exists() -> None:
assert FIXTURE.exists(), f"expected fixture at {FIXTURE}"
def test_round_trip_reproduces_fixture_exactly() -> None:
original = _load_fixture()
ctx = _enabled_ctx()
masked = ctx.mask_value(original)
restored = ctx.unmask_value(masked)
assert restored == original
def test_namespace_value_is_masked() -> None:
original = _load_fixture()
ctx = _enabled_ctx()
masked = ctx.mask_value(original)
masked_text = json.dumps(masked)
# 'tracer-test' is the namespace value mentioned in the fixture's
# annotation summary ("namespace tracer-test"); after masking, the raw
# value should not appear on its own in contexts that the namespace
# detector recognises.
assert (
any("tracer-test" in original_value for original_value in ctx.placeholder_map.values())
or "tracer-test" not in masked_text
)
def test_masking_produces_at_least_one_placeholder() -> None:
ctx = _enabled_ctx()
ctx.mask_value(_load_fixture())
assert ctx.placeholder_map, "expected at least one placeholder from a realistic k8s alert"
def test_disabled_policy_leaves_fixture_unchanged() -> None:
original = _load_fixture()
disabled = MaskingContext(
policy=MaskingPolicy.model_validate({"enabled": False, "kinds": ALL_KINDS})
)
assert disabled.mask_value(original) == original
assert disabled.placeholder_map == {}
def test_extra_regex_policy_activates_new_kind_without_code_change() -> None:
"""Acceptance criterion #2: configuration without editing code."""
ctx = MaskingContext(
policy=MaskingPolicy.from_env(
{
"OPENSRE_MASK_ENABLED": "true",
"OPENSRE_MASK_KINDS": "", # use defaults
"OPENSRE_MASK_EXTRA_REGEX": '{"run_name": "([a-z]+-[a-z]+-[a-z]+)"}',
}
)
)
masked = ctx.mask("run_name=etl-transform-error completed")
# Custom pattern matched and produced a placeholder
assert any(p.startswith("<RUN_NAME_") for p in ctx.placeholder_map)
assert ctx.unmask(masked) == "run_name=etl-transform-error completed"