Repository navigation
Expand file tree
/
Copy pathtest_log.py
More file actions
156 lines (123 loc) · 5.43 KB
/
Copy pathtest_log.py
File metadata and controls
156 lines (123 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Unified tests for log (sync + async)."""
from __future__ import annotations
import asyncio
import logging
import threading
from contextlib import AbstractAsyncContextManager, AbstractContextManager
from typing import TYPE_CHECKING
from .._utils import maybe_await
from apify_client._models import ListOfBuilds, Run
from apify_client._resource_clients import RunClient, RunClientAsync
from apify_client.http_clients import HttpResponse
if TYPE_CHECKING:
import pytest
from _pytest.logging import LogCaptureFixture
from apify_client import ApifyClient, ApifyClientAsync
from apify_client.types import Timeout
# Use a simple, fast public actor for testing
HELLO_WORLD_ACTOR = 'apify/hello-world'
async def test_log_get_from_run(client: ApifyClient | ApifyClientAsync) -> None:
"""Test retrieving log from an Actor run."""
# Run hello-world actor
actor = client.actor(HELLO_WORLD_ACTOR)
run = await maybe_await(actor.call())
assert isinstance(run, Run)
# Get log as text
run_client = client.run(run.id)
log = await maybe_await(run_client.log().get())
assert log is not None
assert isinstance(log, str)
assert len(log) > 0
# Cleanup
await maybe_await(run_client.delete())
async def test_log_get_from_build(client: ApifyClient | ApifyClientAsync) -> None:
"""Test retrieving log from a build."""
# Get a build from hello-world actor
actor = client.actor(HELLO_WORLD_ACTOR)
builds_page = await maybe_await(actor.builds().list(limit=1))
assert isinstance(builds_page, ListOfBuilds)
assert builds_page.items
build_id = builds_page.items[0].id
# Get log from the build
build = client.build(build_id)
log = await maybe_await(build.log().get())
# Build log may be None or empty for some builds
if log is not None:
assert isinstance(log, str)
async def test_log_get_as_bytes(client: ApifyClient | ApifyClientAsync) -> None:
"""Test retrieving log as raw bytes."""
# Run hello-world actor
actor = client.actor(HELLO_WORLD_ACTOR)
run = await maybe_await(actor.call())
assert isinstance(run, Run)
# Get log as bytes
run_client = client.run(run.id)
log_bytes = await maybe_await(run_client.log().get_as_bytes())
assert log_bytes is not None
assert isinstance(log_bytes, bytes)
assert len(log_bytes) > 0
# Cleanup
await maybe_await(run_client.delete())
async def test_log_stream_from_run(client: ApifyClient | ApifyClientAsync, *, is_async: bool) -> None:
"""Test streaming a run's log via the stream() context manager."""
actor = client.actor(HELLO_WORLD_ACTOR)
run = await maybe_await(actor.call())
assert isinstance(run, Run)
run_client = client.run(run.id)
try:
log_client = run_client.log()
stream_ctx = log_client.stream()
if is_async:
assert isinstance(stream_ctx, AbstractAsyncContextManager)
async with stream_ctx as response:
assert isinstance(response, HttpResponse)
content = await response.aread()
assert isinstance(content, bytes)
assert len(content) > 0
else:
assert isinstance(stream_ctx, AbstractContextManager)
with stream_ctx as response:
assert isinstance(response, HttpResponse)
content = response.read()
assert isinstance(content, bytes)
assert len(content) > 0
finally:
await maybe_await(run_client.delete())
async def test_actor_call_returns_run_when_status_poll_fails(
client: ApifyClient | ApifyClientAsync,
caplog: LogCaptureFixture,
monkeypatch: pytest.MonkeyPatch,
*,
is_async: bool,
) -> None:
"""A failing status poll is reported to the logger and leaves `call` returning the run the platform finished."""
logger = logging.getLogger(f'test-status-poll-failure-{"async" if is_async else "sync"}')
# `call` polls the run status from a background task or thread and awaits the run itself on the main one, so
# failing only the background polls leaves the rest of `call` talking to the real API.
if is_async:
main_task = asyncio.current_task()
original_get_async = RunClientAsync.get
async def failing_get_async(self: RunClientAsync, *, timeout: Timeout = 'short') -> Run | None:
if asyncio.current_task() is main_task:
return await original_get_async(self, timeout=timeout)
raise RuntimeError('Simulated status poll failure')
monkeypatch.setattr(RunClientAsync, 'get', failing_get_async)
else:
original_get = RunClient.get
def failing_get(self: RunClient, *, timeout: Timeout = 'short') -> Run | None:
if threading.current_thread() is threading.main_thread():
return original_get(self, timeout=timeout)
raise RuntimeError('Simulated status poll failure')
monkeypatch.setattr(RunClient, 'get', failing_get)
with caplog.at_level(logging.DEBUG, logger=logger.name):
run = await maybe_await(client.actor(HELLO_WORLD_ACTOR).call(logger=logger))
assert isinstance(run, Run)
try:
assert run.status == 'SUCCEEDED'
assert any(
record.levelno == logging.ERROR
and record.message == 'Status message redirection stopped due to unexpected error:'
for record in caplog.records
)
finally:
await maybe_await(client.run(run.id).delete())