From 7af13e9929d190db22fc8283a18686aee874ade0 Mon Sep 17 00:00:00 2001 From: fwazb Date: Sat, 25 Jul 2026 10:30:24 -0700 Subject: [PATCH] Parse managed inspect array directly --- ops/run_gcp_leoclean_nosend_prompt_ingress.py | 14 ++++----- ...test_gcp_leoclean_nosend_prompt_ingress.py | 29 ++++++------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/ops/run_gcp_leoclean_nosend_prompt_ingress.py b/ops/run_gcp_leoclean_nosend_prompt_ingress.py index a34a29c..5950e39 100644 --- a/ops/run_gcp_leoclean_nosend_prompt_ingress.py +++ b/ops/run_gcp_leoclean_nosend_prompt_ingress.py @@ -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 {} diff --git a/tests/test_gcp_leoclean_nosend_prompt_ingress.py b/tests/test_gcp_leoclean_nosend_prompt_ingress.py index 9810a26..3731a18 100644 --- a/tests/test_gcp_leoclean_nosend_prompt_ingress.py +++ b/tests/test_gcp_leoclean_nosend_prompt_ingress.py @@ -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=[]), "", "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(