Repository: apache/airflow
File: providers/cncf/kubernetes/tests/unit/cncf/kubernetes/operators/test_spark_kubernetes.py (Python)

Code before:
        Verifies that find_spark_job selects the most recently created Spark driver pod
        when multiple candidate driver pods are present and status does not disambiguate.
        """

        task_name = "test_find_spark_job_picks_latest_pod"
        job_spec = yaml.safe_load(data_file("spark/application_template.yaml").read_text())

        mock_create_job_name.return_value = task_name
        op = SparkKubernetesOperator(
            template_spec=job_spec,
            kubernetes_conn_id="kubernetes_default_kube_config",
            task_id=task_name,
            get_logs=True,
            reattach_on_restart=True,
        )

        context = create_context(op)

        # Latest pod should be selected.
        pending_pod = mock.MagicMock()
        pending_pod.metadata.creation_timestamp = timezone.datetime(2025, 1, 3, tzinfo=timezone.utc)
        pending_pod.metadata.name = "spark-driver"
        pending_pod.metadata.labels = {"try_number": "1"}
        pending_pod.status.phase = "Pending"

        # Terminating pod should not be selected.
        terminating_pod = mock.MagicMock()
        terminating_pod.metadata.creation_timestamp = timezone.datetime(2025, 1, 1, tzinfo=timezone.utc)
        terminating_pod.metadata.deletion_timestamp = timezone.datetime(2025, 1, 2, tzinfo=timezone.utc)
        terminating_pod.metadata.name = "spark-driver"
        terminating_pod.metadata.labels = {"try_number": "1"}
        terminating_pod.status.phase = "Running"

        mock_get_kube_client.list_namespaced_pod.return_value.items = [
            terminating_pod,
            pending_pod,
        ]

        returned_pod = op.find_spark_job(context)

        assert returned_pod is pending_pod

    def test_find_spark_job_tiebreaks_by_name(
        self,
        mock_is_in_cluster,
        mock_parent_execute,
        mock_create_namespaced_crd,
        mock_get_namespaced_custom_object_status,
        mock_cleanup,
        mock_create_job_name,
        mock_get_kube_client,

Review comment (suggestion):
Should be:

` # Older pod should not be selected.`

Code after:
        mock_create_job_name,
        mock_get_kube_client,
        mock_create_pod,
        mock_await_pod_completion,
        mock_fetch_requested_container_logs,
        data_file,
    ):
        """
        Verifies that find_spark_job selects the most recently created Spark driver pod
        when multiple candidate driver pods are present and status does not disambiguate.
        """

        task_name = "test_find_spark_job_picks_latest_pod"
        job_spec = yaml.safe_load(data_file("spark/application_template.yaml").read_text())

        mock_create_job_name.return_value = task_name
        op = SparkKubernetesOperator(
            template_spec=job_spec,
            kubernetes_conn_id="kubernetes_default_kube_config",
            task_id=task_name,
            get_logs=True,
            reattach_on_restart=True,
        )

        context = create_context(op)

        # Latest pod should be selected.
        new_pod = mock.MagicMock()
        new_pod.metadata.creation_timestamp = timezone.datetime(2025, 1, 3, tzinfo=timezone.utc)
        new_pod.metadata.name = "spark-driver"
        new_pod.metadata.labels = {"try_number": "1"}
        new_pod.status.phase = "Pending"

        # Older pod should not be selected.
        old_pod = mock.MagicMock()
        old_pod.metadata.creation_timestamp = timezone.datetime(2025, 1, 1, tzinfo=timezone.utc)
        old_pod.metadata.name = "spark-driver"
        old_pod.metadata.labels = {"try_number": "1"}
        old_pod.status.phase = "Running"

        mock_get_kube_client.list_namespaced_pod.return_value.items = [
            old_pod,
            new_pod,
        ]

        returned_pod = op.find_spark_job(context)

        assert returned_pod is new_pod

    def test_find_spark_job_tiebreaks_by_name(
        self,