This is an automated email from the ASF dual-hosted git repository.

kaxil pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new ff4c44289b6 Name the failed task in K8s test assertions (#77144)
ff4c44289b6 is described below

commit ff4c44289b650fcf3048bb968ea99c857d4010ac
Author: Tzu-ping Chung <[email protected]>
AuthorDate: Wed Nov 18 09:59:47 2026 +0800

    Name the failed task in K8s test assertions (#77144)
    
    A Kubernetes test that failed in monitor_task or
    ensure_dag_expected_state only reported "'failed' != 'success'",
    without saying which task or Dag run was affected.
    
    The assertions now name the Dag, task and run. When a Dag run ends in
    the wrong state, its task instances are also printed with their
    state, map index and try number, failed ones first, so the failing
    task can be spotted without digging through the cluster.
---
 .../tests/kubernetes_tests/test_base.py            | 48 +++++++++++++++++++---
 1 file changed, 42 insertions(+), 6 deletions(-)

diff --git a/kubernetes-tests/tests/kubernetes_tests/test_base.py 
b/kubernetes-tests/tests/kubernetes_tests/test_base.py
index 9a7a32dd078..42a07e05485 100644
--- a/kubernetes-tests/tests/kubernetes_tests/test_base.py
+++ b/kubernetes-tests/tests/kubernetes_tests/test_base.py
@@ -238,9 +238,37 @@ class BaseK8STest:
                 tries += 1
             except requests.exceptions.ConnectionError as e:
                 check_call(["echo", f"api call failed. trying again. error 
{e}"])
-        if state != expected_final_state:
-            print(f"The expected state is wrong {state} != 
{expected_final_state} (expected)!")
-        assert state == expected_final_state
+        outcome = f"ended {state!r}" if state else "was never seen (API 
returned 404 or timed out)"
+        assert state == expected_final_state, (
+            f"Task {dag_id}.{task_id} in run {dag_run_id} {outcome}, expected 
{expected_final_state!r}"
+        )
+
+    def _print_task_instance_states(self, host, dag_id, dag_run_id):
+        """Print every task instance of a Dag run with its state, failed ones 
first.
+
+        This is only diagnostic output for a failing test, so it never raises.
+        """
+        try:
+            get_string = 
f"http://{host}/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances";
+            result = self.session.get(get_string, params={"limit": 100})
+            if result.status_code != 200:
+                print(
+                    f"Could not list task instances of run {dag_run_id}: 
{result.status_code} {result.text}"
+                )
+                return
+            result_json = result.json()
+            task_instances = result_json["task_instances"]
+            failed_states = ("failed", "upstream_failed")
+            task_instances = sorted(task_instances, key=lambda ti: ti["state"] 
not in failed_states)
+            print(f"Task instances of {dag_id} run {dag_run_id}:")
+            for ti in task_instances:
+                map_index = f"[{ti['map_index']}]" if ti["map_index"] >= 0 
else ""
+                print(f"  {ti['task_id']}{map_index}: {ti['state']} (try 
{ti['try_number']})")
+            total = result_json.get("total_entries")
+            if total is not None and total > len(task_instances):
+                print(f"  ... and {total - len(task_instances)} more")
+        except Exception as e:
+            print(f"Could not list task instances of run {dag_run_id}: {e!r}")
 
     def wait_until_task_in_executor(self, host, dag_run_id, dag_id, task_id, 
timeout=60):
         """Poll until the task instance has been handed to the executor.
@@ -291,6 +319,7 @@ class BaseK8STest:
     def ensure_dag_expected_state(self, host, logical_date, dag_id, 
expected_final_state, timeout):
         tries = 0
         state = ""
+        dag_run_id = None
         max_tries = max(int(timeout / 5), 1)
         # Wait some time for the operator to complete
         while tries < max_tries:
@@ -303,9 +332,11 @@ class BaseK8STest:
             result_json = result.json()
             print(f"Received: {result}")
             state = None
+            dag_run_id = None
             for dag_run in result_json["dag_runs"]:
                 if dag_run["logical_date"] == logical_date:
                     state = dag_run["state"]
+                    dag_run_id = dag_run["dag_run_id"]
             check_call(["echo", f"Attempt {tries}: Current state of dag is 
{state}"])
             print(f"Attempt {tries}: Current state of dag is {state}")
 
@@ -317,9 +348,14 @@ class BaseK8STest:
             self._describe_resources("airflow")
             self._describe_resources("default")
             tries += 1
-        assert state == expected_final_state
-
-        # Maybe check if we can retrieve the logs, but then we need to extend 
the API
+        if dag_run_id is None:
+            raise AssertionError(f"No run of Dag {dag_id} with logical date 
{logical_date} was found")
+        if state != expected_final_state:
+            self._print_task_instance_states(host, dag_id, dag_run_id)
+        assert state == expected_final_state, (
+            f"Dag {dag_id} run {dag_run_id} (logical date {logical_date}) 
ended {state!r}, "
+            f"expected {expected_final_state!r}"
+        )
 
     def start_dag(self, dag_id, host):
         patch_string = f"http://{host}/dags/{dag_id}";

Reply via email to