|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING |
| 3 | +import socket |
| 4 | +import time |
| 5 | +import warnings |
| 6 | +from typing import TYPE_CHECKING, Any |
4 | 7 |
|
5 | | -import boto3 |
| 8 | +import docker |
6 | 9 | import pytest |
7 | 10 | import requests |
8 | | -from botocore import UNSIGNED |
9 | | -from botocore.client import Config |
10 | | -from moto.moto_server.threaded_moto_server import ThreadedMotoServer |
| 11 | +from minio import Minio |
| 12 | +from requests.exceptions import RequestException |
11 | 13 |
|
12 | 14 | from obstore.store import S3Store |
13 | 15 |
|
14 | 16 | if TYPE_CHECKING: |
15 | | - from obstore.store import S3Config |
| 17 | + from collections.abc import Generator |
16 | 18 |
|
17 | | -TEST_BUCKET_NAME = "test" |
| 19 | + from obstore.store import ClientConfig, S3Config |
| 20 | + |
| 21 | +TEST_BUCKET_NAME = "test-bucket" |
| 22 | + |
| 23 | + |
| 24 | +def find_available_port() -> int: |
| 25 | + """Find a free port on localhost. |
| 26 | +
|
| 27 | + Note that this is susceptible to race conditions. |
| 28 | + """ |
| 29 | + # https://stackoverflow.com/a/36331860 |
| 30 | + |
| 31 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 32 | + # Bind to a free port provided by the host. |
| 33 | + s.bind(("", 0)) |
| 34 | + |
| 35 | + # Return the port number assigned. |
| 36 | + return s.getsockname()[1] |
18 | 37 |
|
19 | 38 |
|
20 | | -# See docs here: https://docs.getmoto.org/en/latest/docs/server_mode.html |
21 | 39 | @pytest.fixture(scope="session") |
22 | | -def moto_server_uri(): |
23 | | - """Fixture to run a mocked AWS server for testing.""" |
24 | | - # Note: pass `port=0` to get a random free port. |
25 | | - server = ThreadedMotoServer(ip_address="localhost", port=0) |
26 | | - server.start() |
27 | | - if hasattr(server, "get_host_and_port"): |
28 | | - host, port = server.get_host_and_port() |
29 | | - else: |
30 | | - s = server._server |
31 | | - assert s is not None |
32 | | - # An AF_INET6 socket address has 4 components. |
33 | | - host, port = s.server_address[:2] |
34 | | - uri = f"http://{host}:{port}" |
35 | | - yield uri |
36 | | - server.stop() |
| 40 | +def minio_config() -> Generator[tuple[S3Config, ClientConfig], Any, None]: |
| 41 | + warnings.warn( |
| 42 | + "Creating Docker client...", |
| 43 | + UserWarning, |
| 44 | + stacklevel=1, |
| 45 | + ) |
| 46 | + docker_client = docker.from_env() |
| 47 | + warnings.warn( |
| 48 | + "Finished creating Docker client...", |
| 49 | + UserWarning, |
| 50 | + stacklevel=1, |
| 51 | + ) |
37 | 52 |
|
| 53 | + username = "minioadmin" |
| 54 | + password = "minioadmin" # noqa: S105 |
| 55 | + port = find_available_port() |
| 56 | + console_port = find_available_port() |
38 | 57 |
|
39 | | -@pytest.fixture |
40 | | -def s3(moto_server_uri: str): |
41 | | - client = boto3.client( |
42 | | - "s3", |
43 | | - config=Config(signature_version=UNSIGNED), |
44 | | - region_name="us-east-1", |
45 | | - endpoint_url=moto_server_uri, |
| 58 | + print(f"Using ports: {port=}, {console_port=}") # noqa: T201 |
| 59 | + print( # noqa: T201 |
| 60 | + f"Log on to MinIO console at http://localhost:{console_port} with " |
| 61 | + f"{username=} and {password=}", |
46 | 62 | ) |
47 | | - client.create_bucket(Bucket=TEST_BUCKET_NAME, ACL="public-read") |
48 | | - client.put_object(Bucket=TEST_BUCKET_NAME, Key="afile", Body=b"hello world") |
49 | | - yield moto_server_uri |
50 | | - objects = client.list_objects_v2(Bucket=TEST_BUCKET_NAME) |
51 | | - for name in objects.get("Contents", []): |
52 | | - key = name.get("Key") |
53 | | - assert key is not None |
54 | | - client.delete_object(Bucket=TEST_BUCKET_NAME, Key=key) |
55 | | - requests.post(f"{moto_server_uri}/moto-api/reset", timeout=30) |
56 | 63 |
|
| 64 | + warnings.warn( |
| 65 | + "Starting MinIO container...", |
| 66 | + UserWarning, |
| 67 | + stacklevel=1, |
| 68 | + ) |
| 69 | + minio_container = docker_client.containers.run( |
| 70 | + "quay.io/minio/minio", |
| 71 | + "server /data --console-address :9001", |
| 72 | + detach=True, |
| 73 | + ports={ |
| 74 | + "9000/tcp": port, |
| 75 | + "9001/tcp": console_port, |
| 76 | + }, |
| 77 | + environment={ |
| 78 | + "MINIO_ROOT_USER": username, |
| 79 | + "MINIO_ROOT_PASSWORD": password, |
| 80 | + }, |
| 81 | + ) |
| 82 | + warnings.warn( |
| 83 | + "Finished starting MinIO container...", |
| 84 | + UserWarning, |
| 85 | + stacklevel=1, |
| 86 | + ) |
57 | 87 |
|
58 | | -@pytest.fixture |
59 | | -def s3_store(s3: str): |
60 | | - return S3Store.from_url( |
61 | | - f"s3://{TEST_BUCKET_NAME}/", |
62 | | - endpoint=s3, |
63 | | - region="us-east-1", |
64 | | - skip_signature=True, |
65 | | - client_options={"allow_http": True}, |
| 88 | + # Wait for MinIO to be ready |
| 89 | + endpoint = f"http://localhost:{port}" |
| 90 | + wait_for_minio(endpoint, timeout=30) |
| 91 | + |
| 92 | + minio_client = Minio( |
| 93 | + f"localhost:{port}", |
| 94 | + access_key=username, |
| 95 | + secret_key=password, |
| 96 | + secure=False, |
66 | 97 | ) |
| 98 | + minio_client.make_bucket(TEST_BUCKET_NAME) |
| 99 | + |
| 100 | + s3_config: S3Config = { |
| 101 | + "bucket": TEST_BUCKET_NAME, |
| 102 | + "endpoint": endpoint, |
| 103 | + "access_key_id": username, |
| 104 | + "secret_access_key": password, |
| 105 | + "virtual_hosted_style_request": False, |
| 106 | + } |
| 107 | + client_options: ClientConfig = {"allow_http": True} |
| 108 | + |
| 109 | + yield (s3_config, client_options) |
| 110 | + |
| 111 | + minio_container.stop() |
| 112 | + minio_container.remove() |
67 | 113 |
|
68 | 114 |
|
69 | 115 | @pytest.fixture |
70 | | -def s3_store_config(s3: str) -> S3Config: |
71 | | - return { |
72 | | - "endpoint": s3, |
73 | | - "region": "us-east-1", |
74 | | - "skip_signature": True, |
75 | | - } |
| 116 | +def minio_bucket( |
| 117 | + minio_config: tuple[S3Config, ClientConfig], |
| 118 | +) -> Generator[tuple[S3Config, ClientConfig], Any, None]: |
| 119 | + yield minio_config |
| 120 | + |
| 121 | + # Remove all files from bucket |
| 122 | + store = S3Store(config=minio_config[0], client_options=minio_config[1]) |
| 123 | + objects = store.list().collect() |
| 124 | + paths = [obj["path"] for obj in objects] |
| 125 | + store.delete(paths) |
| 126 | + |
| 127 | + |
| 128 | +@pytest.fixture |
| 129 | +def minio_store(minio_bucket: tuple[S3Config, ClientConfig]) -> S3Store: |
| 130 | + """Create an S3Store configured for MinIO integration testing.""" |
| 131 | + return S3Store(config=minio_bucket[0], client_options=minio_bucket[1]) |
| 132 | + |
| 133 | + |
| 134 | +def wait_for_minio(endpoint: str, timeout: int): |
| 135 | + start_time = time.time() |
| 136 | + while time.time() - start_time < timeout: |
| 137 | + try: |
| 138 | + # MinIO health check endpoint |
| 139 | + response = requests.get(f"{endpoint}/minio/health/live", timeout=2) |
| 140 | + if response.status_code == 200: |
| 141 | + return |
| 142 | + except RequestException: |
| 143 | + pass |
| 144 | + time.sleep(0.5) |
| 145 | + |
| 146 | + exc_str = f"MinIO failed to start within {timeout} seconds" |
| 147 | + raise TimeoutError(exc_str) |
0 commit comments