|
| 1 | +"""Tests that run against Temporal Cloud.""" |
| 2 | + |
| 3 | +import multiprocessing |
| 4 | +import os |
| 5 | +from collections.abc import AsyncGenerator, Iterator |
| 6 | + |
| 7 | +import pytest |
| 8 | +import pytest_asyncio |
| 9 | + |
| 10 | +from temporalio.api.cloud.cloudservice.v1 import GetNamespaceRequest |
| 11 | +from temporalio.client import Client, CloudOperationsClient |
| 12 | +from temporalio.service import TLSConfig |
| 13 | +from temporalio.testing import WorkflowEnvironment |
| 14 | +from temporalio.worker import SharedStateManager |
| 15 | +from tests.helpers.worker import ExternalPythonWorker, ExternalWorker |
| 16 | + |
| 17 | +# Skip entire module unless explicitly enabled |
| 18 | +pytestmark = pytest.mark.skipif( |
| 19 | + "TEMPORAL_IS_CLOUD_TESTS" not in os.environ, |
| 20 | + reason="Cloud tests not enabled", |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest_asyncio.fixture(scope="module") # type: ignore[reportUntypedFunctionDecorator] |
| 25 | +async def env() -> AsyncGenerator[WorkflowEnvironment, None]: |
| 26 | + tls_config: bool | TLSConfig = True |
| 27 | + client_cert = os.environ.get("TEMPORAL_CLIENT_CERT") |
| 28 | + client_key = os.environ.get("TEMPORAL_CLIENT_KEY") |
| 29 | + if client_cert and client_key: |
| 30 | + tls_config = TLSConfig( |
| 31 | + client_cert=client_cert.encode(), |
| 32 | + client_private_key=client_key.encode(), |
| 33 | + ) |
| 34 | + client = await Client.connect( |
| 35 | + os.environ["TEMPORAL_CLIENT_CLOUD_TARGET"], |
| 36 | + namespace=os.environ["TEMPORAL_CLIENT_CLOUD_NAMESPACE"], |
| 37 | + api_key=os.environ.get("TEMPORAL_CLIENT_CLOUD_API_KEY"), |
| 38 | + tls=tls_config, |
| 39 | + ) |
| 40 | + env = WorkflowEnvironment.from_client(client) |
| 41 | + yield env |
| 42 | + await env.shutdown() |
| 43 | + |
| 44 | + |
| 45 | +@pytest_asyncio.fixture # type: ignore[reportUntypedFunctionDecorator] |
| 46 | +async def client(env: WorkflowEnvironment) -> Client: |
| 47 | + return env.client |
| 48 | + |
| 49 | + |
| 50 | +@pytest_asyncio.fixture(scope="module") # type: ignore[reportUntypedFunctionDecorator] |
| 51 | +async def worker( |
| 52 | + env: WorkflowEnvironment, |
| 53 | +) -> AsyncGenerator[ExternalWorker, None]: |
| 54 | + w = ExternalPythonWorker(env) |
| 55 | + yield w |
| 56 | + await w.close() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture(scope="module") |
| 60 | +def shared_state_manager() -> Iterator[SharedStateManager]: |
| 61 | + mp_mgr = multiprocessing.Manager() |
| 62 | + mgr = SharedStateManager.create_from_multiprocessing(mp_mgr) |
| 63 | + try: |
| 64 | + yield mgr |
| 65 | + finally: |
| 66 | + mp_mgr.shutdown() |
| 67 | + |
| 68 | + |
| 69 | +# --- Cloud-specific tests --- |
| 70 | + |
| 71 | + |
| 72 | +async def test_cloud_client_simple(): |
| 73 | + client = await CloudOperationsClient.connect( |
| 74 | + api_key=os.environ["TEMPORAL_CLIENT_CLOUD_API_KEY"], |
| 75 | + version=os.environ["TEMPORAL_CLIENT_CLOUD_API_VERSION"], |
| 76 | + ) |
| 77 | + result = await client.cloud_service.get_namespace( |
| 78 | + GetNamespaceRequest(namespace=os.environ["TEMPORAL_CLIENT_CLOUD_NAMESPACE"]) |
| 79 | + ) |
| 80 | + assert os.environ["TEMPORAL_CLIENT_CLOUD_NAMESPACE"] == result.namespace.namespace |
| 81 | + |
| 82 | + |
| 83 | +# --- Delegated tests --- |
| 84 | +# Import test functions to re-run them against cloud fixtures. |
| 85 | + |
| 86 | +from tests.worker.test_activity import ( # noqa: E402 |
| 87 | + test_activity_info, # pyright: ignore[reportUnusedImport] # noqa: F401 |
| 88 | +) |
0 commit comments