-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathconftest.py
More file actions
65 lines (50 loc) · 1.87 KB
/
conftest.py
File metadata and controls
65 lines (50 loc) · 1.87 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
"""Shared fixtures for S3 storage driver tests."""
from __future__ import annotations
import socket
import urllib.request
from collections.abc import AsyncIterator, Iterator
import aioboto3
import pytest
from types_aiobotocore_s3.client import S3Client
from temporalio.contrib.aws.s3driver import S3StorageDriverClient
from temporalio.contrib.aws.s3driver.aioboto3 import new_aioboto3_client
BUCKET = "test-bucket"
CLIENT_REGION = "us-east-1"
def _find_free_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
@pytest.fixture(scope="session")
def moto_server_url() -> Iterator[str]:
"""Start a moto S3 server for the test session and yield its base URL."""
port = _find_free_port()
from moto.server import ThreadedMotoServer
server = ThreadedMotoServer(port=port)
server.start()
yield f"http://127.0.0.1:{port}"
server.stop()
@pytest.fixture
async def aioboto3_client(moto_server_url: str) -> AsyncIterator[S3Client]:
"""Yield an aioboto3 S3 client pointed at the moto server.
Resets all moto state before each test to guarantee isolation, then
pre-creates the standard test bucket.
"""
urllib.request.urlopen(
urllib.request.Request(
f"{moto_server_url}/moto-api/reset", method="POST", data=b""
)
)
session = aioboto3.Session()
async with session.client(
"s3",
region_name=CLIENT_REGION,
endpoint_url=moto_server_url,
aws_access_key_id="testing",
aws_secret_access_key="testing",
) as client:
await client.create_bucket(Bucket=BUCKET)
yield client
@pytest.fixture
def driver_client(aioboto3_client: S3Client) -> S3StorageDriverClient:
"""Wrap the aioboto3 S3 client in an S3StorageDriverClient adapter."""
return new_aioboto3_client(aioboto3_client)