Parse managed inspect array directly
Some checks are pending
CI / lint-and-test (push) Waiting to run

This commit is contained in:
fwazb 2026-07-25 10:30:24 -07:00
parent 6d066d90bf
commit 7af13e9929
2 changed files with 16 additions and 27 deletions

View file

@ -106,20 +106,20 @@ def command(argv, *, input_bytes=None, timeout=120, accepted=(0,)):
require(completed.returncode in accepted, "command_failed")
return completed
def strict_json(raw, reason, expected_type=dict):
def strict_json(raw, reason):
try:
value = json.loads(raw.decode("utf-8", "strict"))
except Exception:
raise RuntimeError(reason)
require(isinstance(value, expected_type), reason)
require(isinstance(value, dict), reason)
return value
def managed_posture():
value = strict_json(
command([*docker, "inspect", managed_name]).stdout,
"managed_inspect_invalid",
list,
)
raw = command([*docker, "inspect", managed_name]).stdout
try:
value = json.loads(raw.decode("utf-8", "strict"))
except Exception:
raise RuntimeError("managed_inspect_invalid")
require(isinstance(value, list) and len(value) == 1 and isinstance(value[0], dict), "managed_inspect_invalid")
item = value[0]
config = item.get("Config") or {}

View file

@ -1,6 +1,5 @@
from __future__ import annotations
import ast
import io
import json
import os
@ -268,25 +267,15 @@ def test_rendered_remote_contract_is_transient_private_and_stdin_only(tmp_path:
assert REQUEST_ID in cleanup[-1]
def test_remote_json_parser_accepts_the_docker_inspect_array() -> None:
parsed = ast.parse(operator.REMOTE_EXECUTOR_SOURCE)
strict_json = next(
node for node in parsed.body if isinstance(node, ast.FunctionDef) and node.name == "strict_json"
)
def require(condition: bool, reason: str) -> None:
if not condition:
raise RuntimeError(reason)
namespace: dict[str, Any] = {"json": json, "require": require}
exec(compile(ast.Module(body=[strict_json], type_ignores=[]), "<remote-strict-json>", "exec"), namespace)
decode = namespace["strict_json"]
assert decode(b'[{"Name":"/livingip-leoclean-nosend"}]', "invalid", list) == [
{"Name": "/livingip-leoclean-nosend"}
]
with pytest.raises(RuntimeError, match="invalid"):
decode(b'[{"Name":"/livingip-leoclean-nosend"}]', "invalid")
def test_remote_managed_inspect_parses_docker_array_directly() -> None:
rendered = operator.REMOTE_EXECUTOR_SOURCE
assert 'raw = command([*docker, "inspect", managed_name]).stdout' in rendered
assert 'value = json.loads(raw.decode("utf-8", "strict"))' in rendered
assert (
"require(isinstance(value, list) and len(value) == 1 and "
'isinstance(value[0], dict), "managed_inspect_invalid")'
) in rendered
assert 'strict_json(command([*docker, "inspect", managed_name])' not in rendered
def test_gcloud_environment_resolves_and_verifies_python_311_once(