Skip to content

Commit

Permalink
Fix get_log and test_get_log in api_connexion
Browse files Browse the repository at this point in the history
  • Loading branch information
jason810496 committed Dec 23, 2024
1 parent a6afa8b commit 0f19a8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 6 additions & 3 deletions airflow/api_connexion/endpoints/log_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ def get_log(
# return_type would be either the above two or None
logs: Any
if return_type == "application/json" or return_type is None: # default
logs, metadata = task_log_reader.read_log_chunks(ti, task_try_number, metadata)
logs = logs[0] if task_try_number is not None else logs
hosts, log_streams, metadata = task_log_reader.read_log_chunks(ti, task_try_number, metadata)
host = f"{hosts[0] or ''}\n"
logs = log_streams[0] if task_try_number is not None else log_streams
# we must have token here, so we can safely ignore it
token = URLSafeSerializer(key).dumps(metadata) # type: ignore[assignment]
return logs_schema.dump(LogResponseObject(continuation_token=token, content=logs))
return logs_schema.dump(
LogResponseObject(continuation_token=token, content=host + "\n".join(log for log in logs))
)
# text/plain. Stream
logs = task_log_reader.read_log_stream(ti, task_try_number, metadata)

Expand Down
14 changes: 7 additions & 7 deletions tests/api_connexion/endpoints/test_log_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ def test_should_respond_200_json(self, try_number):
)
expected_filename = f"{self.log_dir}/dag_id={self.DAG_ID}/run_id={self.RUN_ID}/task_id={self.TASK_ID}/attempt={try_number}.log"
log_content = "Log for testing." if try_number == 1 else "Log for testing 2."
assert "[('localhost'," in response.json["content"]
assert f"*** Found local files:\\n*** * {expected_filename}\\n" in response.json["content"]
assert f"{log_content}')]" in response.json["content"]
assert response.json["content"].startswith("localhost\n")
assert f"*** Found local files:\n*** * {expected_filename}\n" in response.json["content"]
assert log_content in response.json["content"]

info = serializer.loads(response.json["continuation_token"])
assert info == {"end_of_log": True, "log_pos": 16 if try_number == 1 else 18}
Expand Down Expand Up @@ -322,10 +322,10 @@ def test_get_logs_response_with_ti_equal_to_none(self, try_number):
@pytest.mark.parametrize("try_number", [1, 2])
def test_get_logs_with_metadata_as_download_large_file(self, try_number):
with mock.patch("airflow.utils.log.file_task_handler.FileTaskHandler.read") as read_mock:
first_return = ([[("", "1st line")]], [{}])
second_return = ([[("", "2nd line")]], [{"end_of_log": False}])
third_return = ([[("", "3rd line")]], [{"end_of_log": True}])
fourth_return = ([[("", "should never be read")]], [{"end_of_log": True}])
first_return = ([""], [iter(["1st line"])], [{}])
second_return = ([""], [iter(["2nd line"])], [{"end_of_log": False}])
third_return = ([""], [iter(["3rd line"])], [{"end_of_log": True}])
fourth_return = ([""], [iter(["should never be read"])], [{"end_of_log": True}])
read_mock.side_effect = [first_return, second_return, third_return, fourth_return]

response = self.client.get(
Expand Down

0 comments on commit 0f19a8b

Please sign in to comment.