moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1052276734
##########
tests/providers/cncf/kubernetes/utils/test_pod_manager.py:
##########
@@ -403,3 +421,77 @@ def test_container_is_running(remote_pod, result):
an object `e` such that `e.status.container_statuses` is None, and so on.
This test
verifies the expected behavior."""
assert container_is_running(remote_pod, "base") is result
+
+
+class TestPodLogsConsumer:
+ @pytest.mark.parametrize(
+ "chunks, expected_logs",
+ [
+ ([b"message"], [b"message"]),
+ ([b"message1\nmessage2"], [b"message1\n", b"message2"]),
+ ([b"message1\n", b"message2"], [b"message1\n", b"message2"]),
+ ([b"first_part", b"_second_part"], [b"first_part_second_part"]),
+ ([b""], [b""]),
+ ],
+ )
+ def test_chunks(self, chunks, expected_logs):
+ with mock.patch.object(PodLogsConsumer, "logs_available") as
logs_available:
+ logs_available.return_value = True
+ consumer = PodLogsConsumer(
+
response=mock.MagicMock(stream=mock.MagicMock(return_value=chunks)),
+ pod=mock.MagicMock(),
+
pod_manager=mock.MagicMock(container_is_running=mock.MagicMock(return_value=True)),
+ container_name="base",
+ )
+ assert list(consumer) == expected_logs
+
+ def test_container_is_not_running(self):
+ with mock.patch.object(PodLogsConsumer, "logs_available") as
logs_available:
+ logs_available.return_value = False
+ consumer = PodLogsConsumer(
+
response=mock.MagicMock(stream=mock.MagicMock(return_value=[b"message1",
b"message2"])),
+ pod=mock.MagicMock(),
+
pod_manager=mock.MagicMock(container_is_running=mock.MagicMock(return_value=False)),
+ container_name="base",
+ )
+ assert list(consumer) == []
+
+ @pytest.mark.parametrize(
+ "container_run, termination_time, now_time, timeout,
expected_logs_available",
+ [
+ (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0,
1, 0, 0), 120, True),
+ (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0,
2, 0, 0), 120, False),
+ (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0,
5, 0, 0), 120, False),
+ (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0,
1, 0, 0), 120, True),
+ (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0,
2, 0, 0), 120, True),
+ (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0,
5, 0, 0), 120, True),
+ ],
+ )
+
@mock.patch("airflow.providers.cncf.kubernetes.utils.pod_manager.container_is_running")
+
@mock.patch("airflow.providers.cncf.kubernetes.utils.pod_manager.get_container_status")
+ def test_logs_available(
+ self,
+ mock_get_container_status,
+ mock_container_is_running,
+ container_run,
+ termination_time,
+ now_time,
+ timeout,
+ expected_logs_available,
+ ):
+ mock_container_is_running.return_value = container_run
+ mock_get_container_status.return_value = mock.MagicMock(
+
state=mock.MagicMock(terminated=mock.MagicMock(finished_at=termination_time))
+ )
+ with mock.patch(
+ "airflow.providers.cncf.kubernetes.utils.pod_manager.datetime",
+ mock.MagicMock(now=mock.MagicMock(return_value=now_time),
fromtimestamp=datetime.fromtimestamp),
+ ):
Review Comment:
Thank you for pointing out the
[time-machine](https://github.com/adamchainz/time-machine) - it solves lots of
problems.
Speaking about the
[V1ContainerStateTerminated.finished_at](https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ContainerStateTerminated.md),
experiments show that it retrieves datetime in UTC. I also added timezone
specifications to the unit test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]