Skip to content

Commit ef2eafd

Browse files
Support Moto 5+ (#15)
See getmoto/moto#7198 (comment), can no longer pass the service name to the moto process
1 parent 0c16eda commit ef2eafd

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test:
44

55
.PHONY: rufflintfix
66
rufflintfix:
7-
poetry run ruff check . --fix
7+
poetry run ruff check . --fix --unsafe-fixes
88

99
.PHONY: rufflintcheck
1010
rufflintcheck:

pytest_aioboto3/moto_fixtures.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from subprocess import Popen
1010
from typing import Any, Iterator, Mapping
1111

12+
import moto
1213
import pytest
1314
import requests
1415

@@ -20,7 +21,7 @@
2021
logger = logging.getLogger(__name__)
2122

2223

23-
def start_moto_server(service_name: str, host: str, port: int) -> Popen[Any]:
24+
def start_moto_server(service_name: str | None, host: str, port: int) -> Popen[Any]:
2425
"""
2526
This originally comes from the tests in aioboto3 that starts up a moto server to test interactions
2627
at https://github.com/terrycain/aioboto3/blob/92a7a9b8a32615ab6a9ea51ef360475ede94bb1f/tests/mock_server.py
@@ -30,7 +31,12 @@ def start_moto_server(service_name: str, host: str, port: int) -> Popen[Any]:
3031
raise ValueError(
3132
"Could not find a path to moto_server, is it installed in the virtualenvironment?"
3233
)
33-
args = [moto_svr_path, service_name, "-H", host, "-p", str(port)]
34+
args = [moto_svr_path]
35+
if service_name:
36+
args.append(service_name)
37+
38+
args += ["-H", host, "-p", str(port)]
39+
3440
# For debugging
3541
# args = f"moto_svr_path service_name -H host -p port 2>&1 | tee -a /tmp/moto.log" # noqa: ERA001
3642
logger.info(f"Starting moto server: {args}")
@@ -91,20 +97,36 @@ def moto_services() -> Iterator[Mapping[str, str]]:
9197
Map of mocked services with moto where the key is the service name and the value is the moto url to that service
9298
"""
9399
processes = []
94-
services: dict[str, str] = {}
100+
service_to_url: dict[str, str] = {}
101+
102+
services_to_start = ("s3",)
95103
"""
96104
97105
1. Add a new entry to the 'extras' section in pyproject.toml for types-aiobotocore, moto and boto3-stubs like 'dynamodb' or 'ec2'
98106
2. Add to this tuple the service that you want to mock, the same as the extra you added
99107
"""
100-
for service in ("s3",):
108+
109+
def _start_moto(service_name: str | None = None) -> str:
101110
host = "localhost"
102111
port = get_free_tcp_port()
103112
url = f"http://{host}:{port}"
104-
processes.append(start_moto_server(service, host, port))
105-
services[service] = url
113+
processes.append(start_moto_server(service_name, host, port))
114+
return url
115+
116+
if int(moto.__version__.split(".")[0]) >= 5:
117+
# Only a single instance is necessary for moto5, in moto4 you start an instance for each service
118+
# more information see https://github.com/getmoto/moto/issues/7198#issue-2069113863
119+
120+
url = _start_moto()
121+
for service in services_to_start:
122+
service_to_url[service] = url
123+
124+
else:
125+
for service in ("s3",):
126+
url = _start_moto(service_name=service)
127+
service_to_url[service] = url
106128

107-
yield services
129+
yield service_to_url
108130

109131
for process in processes:
110132
try:

0 commit comments

Comments
 (0)