Skip to content

test(gke): give the mocked Job the string fields kubernetes 37 requires - #7448

Closed
sushant-me wants to merge 3 commits into
google:mainfrom
sushant-me:fix-gke-tests-kubernetes-37
Closed

sushant-me wants to merge 3 commits into
google:mainfrom
sushant-me:fix-gke-tests-kubernetes-37

Conversation

@sushant-me

Copy link
Copy Markdown
Contributor

Fixes #7443.

The problem

Since kubernetes 37.0.0, the mock_k8s_clients fixture hands back the default MagicMock for create_namespaced_job. GkeCodeExecutor._add_owner_reference copies four fields from that Job into a V1OwnerReference:

owner_reference = k8s.client.V1OwnerReference(
    api_version=owner_job.api_version,
    kind=owner_job.kind,
    name=owner_job.metadata.name,
    uid=owner_job.metadata.uid,
)

An unconfigured MagicMock returns a MagicMock for each of those, and kubernetes 37 validates the model with pydantic:

ValidationError: 4 validation errors for V1OwnerReference
  api_version  Input should be a valid string [input_type=MagicMock]

execute_code catches the exception and returns "An unexpected executor error occurred", so five assertions failed and the Unit Tests jobs have been red on main for Python 3.11-3.14 since 51a20e24.

The change

Only the test fixture. It now returns a V1Job with the string fields a real cluster returns:

mock_batch_v1.create_namespaced_job.return_value = client.V1Job(
    api_version="batch/v1",
    kind="Job",
    metadata=client.V1ObjectMeta(name="test-job-name", uid="test-job-uid"),
)

No executor code changes: against a real cluster create_namespaced_job already returns a V1Job with string fields, so the production path was never affected. The failure was the mock being less realistic than the API it stands in for.

Verification

pip install "kubernetes==37.0.0"
pytest tests/unittests/code_executors/test_gke_code_executor.py
  18 passed        (was: 5 failed, 13 passed)

That matches the expected behaviour in the report — all 18 passing as they do on 36.0.3.

I confirmed the failure without the change first (ValidationError ... input_type=MagicMock), so the fix is measured against the real regression rather than assumed.

The A2A executor put str(exception) into the peer-visible failed-task response.
A remote caller that can reach the endpoint therefore receives server-side detail:
absolute filesystem paths, module locations, configuration-key names and upstream
error text - none of which the peer needs, all of which is useful reconnaissance.

The throwable is already logged in full. It is now logged under a short opaque
correlation id, and the peer gets only:

    Agent execution failed. (error_id: <12 hex chars>)

Ports the behaviour of adk-java a3df463. ADK_DEBUG_ERRORS=1 restores the old
peer-visible text, for local debugging only and off by default.

Verified: ./pytest tests/unittests/a2a/executor/test_a2a_agent_executor.py
          32 passed (30 pre-existing + 2 new)

Fixes google#7445
…acy declaration

build_function_declaration drops the ignored parameters (tool_context) from the
signature rebuilt for the legacy JSON-schema-disabled path, but copied the
original __annotations__ mapping wholesale. typing.get_type_hints() then
resolved annotations for parameters that were no longer present, so a context
parameter annotated with a TYPE_CHECKING-only import raised:

    NameError: name 'ToolContext' is not defined

Only annotations for parameters that survive into the new signature are copied
now. This is the legacy-path counterpart of 2ecb3fb, which fixed the same
symptom on the JSON-schema path.

Verified:
  ADK_DISABLE_JSON_SCHEMA_FOR_FUNC_DECL=1 python repro.py   -> builds, city only
  pytest test_function_tool_with_import_annotations.py test_function_tool.py
    86 passed (85 pre-existing + 1 new)
kubernetes 37.0.0 validates model fields with pydantic. The mock_k8s_clients
fixture never configured create_namespaced_job's return value, so it handed back
the default MagicMock. GkeCodeExecutor._add_owner_reference copies that Job's
api_version, kind, metadata.name and metadata.uid into a V1OwnerReference, and
those MagicMock values no longer validate as strings:

    ValidationError: 4 validation errors for V1OwnerReference
      api_version  Input should be a valid string [input_type=MagicMock]

execute_code caught the error and returned 'An unexpected executor error
occurred', so five assertions failed and the Unit Tests jobs went red on main
for Python 3.11-3.14.

The fixture now returns a V1Job carrying the string fields a real cluster
returns, which is what the production path sees. No executor code changes.

Verified with kubernetes 37.0.0:
  pytest tests/unittests/code_executors/test_gke_code_executor.py
  18 passed (0 failed; previously 5 failed)

Fixes google#7443
@sushant-me

Copy link
Copy Markdown
Contributor Author

Closing as superseded — current main already carries this fix in tests/unittests/code_executors/test_gke_code_executor.py:

mock_batch_v1.create_namespaced_job.return_value = client.V1Job(
    api_version="batch/v1",
    kind="Job",
    metadata=client.V1ObjectMeta(name="test-job", uid="test-job-uid"),
)

That is the same change this PR made — giving the mocked Job the string fields kubernetes 37's pydantic validation requires — with a different name. There is nothing left to merge.

This PR was part of a four-commit stack (#7446 → #7447 → #7448 → #7450), which is why its diff included the a2a and tools changes. I am untangling that in #7446; the other three commits are all redundant now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GKE code executor unit tests fail with kubernetes 37.0.0, turning main CI red

1 participant