diff --git a/tests/_utils.py b/tests/_utils.py index 52cd8031..81411595 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -2,17 +2,23 @@ import asyncio import inspect +import os import secrets import string import time -from collections.abc import AsyncIterator, Iterator +from collections.abc import AsyncGenerator, AsyncIterator, Iterator +from contextlib import asynccontextmanager, contextmanager from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, overload +from datetime import timedelta +from typing import TYPE_CHECKING, Any, Literal, Protocol, TypeVar, cast, overload import pytest +from apify_client import ApifyClient, ApifyClientAsync +from apify_client._consts import DEFAULT_API_URL + if TYPE_CHECKING: - from collections.abc import Awaitable, Callable + from collections.abc import Awaitable, Callable, Generator from apify_client.http_clients import HttpClient, HttpClientAsync @@ -23,6 +29,8 @@ T = TypeVar('T') +ClientType = Literal['sync', 'async'] + class _HasId(Protocol): """Items returned by collection `iterate()` endpoints all expose `.id`.""" @@ -65,6 +73,69 @@ class HttpClientClasses: async_: type[HttpClientAsync] +def get_api_url() -> str: + """Return the API URL for integration tests, from `API_URL_ENV_VAR` or the default.""" + return os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + + +@asynccontextmanager +async def open_apify_client( + token: str, client_type: ClientType, http_client_classes: HttpClientClasses +) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: + """Build a sync or async client with the given token and HTTP client implementation, closing it on exit.""" + api_url = get_api_url() + if client_type == 'sync': + http_client = http_client_classes.sync() + try: + yield ApifyClient.with_custom_http_client( + token, + api_url=api_url, + http_client=http_client, + ) + finally: + http_client.close() + return + + http_client_async = http_client_classes.async_() + try: + yield ApifyClientAsync.with_custom_http_client( + token, + api_url=api_url, + http_client=http_client_async, + ) + finally: + await http_client_async.aclose() + + +@contextmanager +def create_built_actor( + client: ApifyClient, name: str, source_files: list[dict[str, Any]], **create_kwargs: Any +) -> Generator[str]: + """Create an Actor from source files and build it, yield its ID, and delete it on exit.""" + actor = client.actors().create( + name=name, + versions=[ + { + 'versionNumber': '0.0', + 'sourceType': 'SOURCE_FILES', + 'buildTag': 'latest', + 'sourceFiles': source_files, + } + ], + **create_kwargs, + ) + actor_client = client.actor(actor.id) + + try: + build = actor_client.build(version_number='0.0') + finished_build = client.build(build.id).wait_for_finish(wait_duration=timedelta(minutes=5)) + assert finished_build is not None + assert finished_build.status == 'SUCCEEDED' + yield actor.id + finally: + actor_client.delete() + + def get_crypto_random_object_id(length: int = 17) -> str: """Generate a cryptographically secure random object ID.""" chars = 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789' diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index b57a8647..df1fd48c 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -7,16 +7,17 @@ import pytest from .._utils import ( - API_URL_ENV_VAR, TOKEN_ENV_VAR, TOKEN_ENV_VAR_2, + ClientType, DatasetFixture, HttpClientClasses, KvsFixture, + get_api_url, get_crypto_random_object_id, + open_apify_client, ) from apify_client import ApifyClient, ApifyClientAsync -from apify_client._consts import DEFAULT_API_URL from apify_client._utils.crypto import create_hmac_signature, create_storage_content_signature from apify_client.http_clients import ( Httpx2HttpClient, @@ -50,7 +51,7 @@ def api_token_2() -> str: @pytest.fixture(scope='session') def test_dataset_of_another_user(api_token_2: str) -> Generator[DatasetFixture]: """Dataset owned by secondary user for testing cross-user access restrictions.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() client = ApifyClient(api_token_2, api_url=api_url) # Create dataset with test data @@ -79,7 +80,7 @@ def test_dataset_of_another_user(api_token_2: str) -> Generator[DatasetFixture]: @pytest.fixture(scope='session') def test_kvs_of_another_user(api_token_2: str) -> Generator[KvsFixture]: """Key-value store owned by secondary user for testing cross-user access restrictions.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() client = ApifyClient(api_token_2, api_url=api_url) # Create key-value store with test data @@ -120,39 +121,23 @@ def http_client_classes(request: pytest.FixtureRequest) -> HttpClientClasses: @pytest.fixture(params=['sync', 'async']) -def client_type(request: pytest.FixtureRequest) -> str: +def client_type(request: pytest.FixtureRequest) -> ClientType: """Parametrize tests to run with both sync and async clients.""" return request.param @pytest.fixture async def client( - client_type: str, + client_type: ClientType, api_token: str, http_client_classes: HttpClientClasses, ) -> AsyncGenerator[ApifyClient | ApifyClientAsync]: """Return each sync/async and HTTP client implementation combination.""" - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL - if client_type == 'sync': - http_client = http_client_classes.sync() - yield ApifyClient.with_custom_http_client( - api_token, - api_url=api_url, - http_client=http_client, - ) - http_client.close() - return - - http_client_async = http_client_classes.async_() - yield ApifyClientAsync.with_custom_http_client( - api_token, - api_url=api_url, - http_client=http_client_async, - ) - await http_client_async.aclose() + async with open_apify_client(api_token, client_type, http_client_classes) as client: + yield client @pytest.fixture -def is_async(client_type: str) -> bool: +def is_async(client_type: ClientType) -> bool: """True if current test is using async client.""" return client_type == 'async' diff --git a/tests/integration/test_actor_server.py b/tests/integration/test_actor_server.py index 895eac0d..5ee765b8 100644 --- a/tests/integration/test_actor_server.py +++ b/tests/integration/test_actor_server.py @@ -3,15 +3,13 @@ from __future__ import annotations import json -import os from datetime import timedelta from typing import TYPE_CHECKING import pytest -from .._utils import API_URL_ENV_VAR, get_random_resource_name, maybe_await +from .._utils import create_built_actor, get_api_url, get_random_resource_name, maybe_await from apify_client import ApifyClient -from apify_client._consts import DEFAULT_API_URL if TYPE_CHECKING: from collections.abc import Generator @@ -41,7 +39,7 @@ """ DOCKERFILE = """ -FROM node:22-alpine +FROM node:26-alpine COPY main.js ./ CMD ["node", "main.js"] """ @@ -50,7 +48,7 @@ @pytest.fixture(scope='module') def server_actor_id(api_token: str) -> Generator[str]: """Create and build a Server Actor running an echo server, and delete it after the module's tests.""" - client = ApifyClient(api_token, api_url=os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL) + client = ApifyClient(api_token, api_url=get_api_url()) actor_name = get_random_resource_name('server') actor_json = { 'actorSpecification': 1, @@ -59,34 +57,20 @@ def server_actor_id(api_token: str) -> Generator[str]: 'usesStandbyMode': True, 'webServerSchema': OPENAPI_SPEC, } - actor = client.actors().create( - name=actor_name, - versions=[ - { - 'versionNumber': '0.0', - 'sourceType': 'SOURCE_FILES', - 'buildTag': 'latest', - 'sourceFiles': [ - {'name': 'Dockerfile', 'format': 'TEXT', 'content': DOCKERFILE}, - {'name': 'main.js', 'format': 'TEXT', 'content': ECHO_SERVER_SOURCE}, - {'name': '.actor/actor.json', 'format': 'TEXT', 'content': json.dumps(actor_json)}, - ], - } - ], + source_files = [ + {'name': 'Dockerfile', 'format': 'TEXT', 'content': DOCKERFILE}, + {'name': 'main.js', 'format': 'TEXT', 'content': ECHO_SERVER_SOURCE}, + {'name': '.actor/actor.json', 'format': 'TEXT', 'content': json.dumps(actor_json)}, + ] + with create_built_actor( + client, + actor_name, + source_files, server_is_enabled=True, server_memory_mbytes=256, server_idle_timeout=timedelta(minutes=1), - ) - actor_client = client.actor(actor.id) - - try: - build = actor_client.build(version_number='0.0') - finished_build = client.build(build.id).wait_for_finish(wait_duration=timedelta(minutes=5)) - assert finished_build is not None - assert finished_build.status == 'SUCCEEDED' - yield actor.id - finally: - actor_client.delete() + ) as actor_id: + yield actor_id async def test_actor_server(client: ApifyClient | ApifyClientAsync, server_actor_id: str) -> None: diff --git a/tests/integration/test_apify_client.py b/tests/integration/test_apify_client.py index 6935f88c..d6478c08 100644 --- a/tests/integration/test_apify_client.py +++ b/tests/integration/test_apify_client.py @@ -2,11 +2,8 @@ from __future__ import annotations -import os - -from .._utils import API_URL_ENV_VAR, maybe_await +from .._utils import ClientType, get_api_url, maybe_await from apify_client import ApifyClient, ApifyClientAsync -from apify_client._consts import DEFAULT_API_URL from apify_client._models import UserPrivateInfo, UserPublicInfo from apify_client.http_clients import ImpitHttpClient, ImpitHttpClientAsync @@ -19,13 +16,13 @@ async def test_apify_client(client: ApifyClient | ApifyClientAsync) -> None: assert me.username is not None -async def test_plain_constructor_reaches_the_api(client_type: str, api_token: str) -> None: +async def test_plain_constructor_reaches_the_api(client_type: ClientType, api_token: str) -> None: """The plain constructor - the form users write - reaches the API with the HTTP client it builds itself. The rest of the suite injects a transport through `with_custom_http_client` to run against every built-in implementation, which leaves the default construction path untested against the live API. """ - api_url = os.getenv(API_URL_ENV_VAR) or DEFAULT_API_URL + api_url = get_api_url() if client_type == 'sync': sync_client = ApifyClient(api_token, api_url=api_url) diff --git a/tests/integration/test_run.py b/tests/integration/test_run.py index c5e0a46d..fee1ed77 100644 --- a/tests/integration/test_run.py +++ b/tests/integration/test_run.py @@ -2,11 +2,23 @@ from __future__ import annotations -from collections.abc import AsyncIterator, Iterator +from collections.abc import AsyncIterator, Generator, Iterator from datetime import UTC, datetime, timedelta from typing import TYPE_CHECKING -from .._utils import get_random_resource_name, maybe_await, poll_until_condition +import pytest + +from .._utils import ( + ClientType, + HttpClientClasses, + create_built_actor, + get_api_url, + get_random_resource_name, + maybe_await, + open_apify_client, + poll_until_condition, +) +from apify_client import ApifyClient from apify_client._models import ( ActorResource, Build, @@ -20,7 +32,7 @@ from apify_client.errors import ApifyApiError if TYPE_CHECKING: - from apify_client import ApifyClient, ApifyClientAsync + from apify_client import ApifyClientAsync HELLO_WORLD_ACTOR = 'apify/hello-world' @@ -30,7 +42,7 @@ { 'name': 'Dockerfile', 'format': 'TEXT', - 'content': 'FROM apify/actor-node:22\nCOPY . ./\nCMD ["node", "main.mjs"]\n', + 'content': 'FROM apify/actor-node:26\nCOPY . ./\nCMD ["node", "main.mjs"]\n', }, { 'name': 'main.mjs', @@ -53,6 +65,27 @@ """Source of an Actor that pushes `LIVE_ITEM_COUNT` items tagged with its run ID to its dataset, one per second.""" +METAMORPH_SOURCE_FILES = [ + { + 'name': 'Dockerfile', + 'format': 'TEXT', + 'content': 'FROM apify/actor-node:26\nCOPY . ./\nCMD ["node", "main.js"]\n', + }, + { + 'name': 'main.js', + 'format': 'TEXT', + 'content': """ +const { Actor } = require('apify'); +Actor.main(async () => { + await Actor.setValue('TOKEN', process.env.APIFY_TOKEN, { contentType: 'text/plain' }); + await new Promise((resolve) => setTimeout(resolve, 60000)); +}); +""", + }, +] +"""Source of an Actor that stores its run token under `TOKEN` in its key-value store, then waits for 60 seconds.""" + + async def test_run_collection_list_multiple_statuses(client: ApifyClient | ApifyClientAsync) -> None: """Test listing runs with multiple statuses.""" created_runs = list[Run]() @@ -316,48 +349,64 @@ async def test_run_runs_client(client: ApifyClient | ApifyClientAsync) -> None: assert first_run.act_id is not None -async def test_run_metamorph(client: ApifyClient | ApifyClientAsync) -> None: - """Test metamorphing a run into another Actor.""" - # Start an actor that will run long enough to metamorph. We use hello-world and try to metamorph it into itself - actor = client.actor(HELLO_WORLD_ACTOR) - run = await maybe_await(actor.start()) - assert isinstance(run, Run) - assert run.id is not None +@pytest.fixture(scope='module') +def metamorph_actor_id(api_token: str) -> Generator[str]: + """Create and build an Actor that stores its run token, and delete it after the module's tests.""" + client = ApifyClient(api_token, api_url=get_api_url()) + with create_built_actor(client, get_random_resource_name('actor'), METAMORPH_SOURCE_FILES) as actor_id: + yield actor_id + +async def test_run_metamorph( + client: ApifyClient | ApifyClientAsync, + client_type: ClientType, + http_client_classes: HttpClientClasses, + metamorph_actor_id: str, +) -> None: + """Test metamorphing a run into another Actor with the run's own token.""" + run = await maybe_await( + client.actor(metamorph_actor_id).start(memory_mbytes=256, run_timeout=timedelta(seconds=300)) + ) + assert isinstance(run, Run) run_client = client.run(run.id) try: - # Wait until the run starts (leaves READY), with exponential backoff: container startup time varies widely - async def get_run() -> Run | None: - return await maybe_await(run_client.get()) - - await poll_until_condition( - get_run, lambda run: isinstance(run, Run) and run.status != 'READY', backoff_factor=2 - ) - - # Metamorph the run into the same actor (allowed) with new input - # Note: hello-world may finish before we can metamorph, so we handle that case - try: + # A metamorph needs the run's own token, which the Actor stores in its key-value store. + async def get_run_token() -> str | None: + record = await maybe_await(client.key_value_store(run.default_key_value_store_id).get_record('TOKEN')) + if record is not None: + return record['value'] + current_run = await maybe_await(run_client.get()) + assert isinstance(current_run, Run) + assert current_run.status in ('READY', 'RUNNING'), f'The run ended as {current_run.status} without a token.' + return None + + # A fixed short interval, so the token is read well within the 60 seconds the Actor waits after storing it. + run_token = await poll_until_condition(get_run_token, timeout=120, poll_interval=2) + assert isinstance(run_token, str) + assert run_token + + async with open_apify_client(run_token, client_type, http_client_classes) as run_token_client: metamorphed_run = await maybe_await( - run_client.metamorph( + run_token_client.run(run.id).metamorph( target_actor_id=HELLO_WORLD_ACTOR, run_input={'message': 'Hello from metamorph!'}, ) ) - assert isinstance(metamorphed_run, Run) - assert metamorphed_run.id == run.id # Same run ID - - # Wait for the metamorphed run to finish - final_run = await maybe_await(run_client.wait_for_finish()) - assert isinstance(final_run, Run) - except ApifyApiError as exc: - # If the actor finished before we could metamorph, that's OK - the test still verified the API call - if 'already finished' not in str(exc): - raise + assert isinstance(metamorphed_run, Run) + assert metamorphed_run.id == run.id + # Hello-world replaces the waiting Actor, so the run finishes on its own. + final_run = await maybe_await(run_client.wait_for_finish()) + assert isinstance(final_run, Run) + assert final_run.status == 'SUCCEEDED' + assert final_run.metamorphs finally: + current_run = await maybe_await(run_client.get()) + if isinstance(current_run, Run) and current_run.status in ('READY', 'RUNNING'): + await maybe_await(run_client.abort()) await maybe_await(run_client.wait_for_finish()) - await maybe_await(run_client.delete()) + await maybe_await(run_client.delete()) async def test_run_reboot(client: ApifyClient | ApifyClientAsync) -> None: