9
9
from subprocess import Popen
10
10
from typing import Any , Iterator , Mapping
11
11
12
+ import moto
12
13
import pytest
13
14
import requests
14
15
20
21
logger = logging .getLogger (__name__ )
21
22
22
23
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 ]:
24
25
"""
25
26
This originally comes from the tests in aioboto3 that starts up a moto server to test interactions
26
27
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]:
30
31
raise ValueError (
31
32
"Could not find a path to moto_server, is it installed in the virtualenvironment?"
32
33
)
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
+
34
40
# For debugging
35
41
# args = f"moto_svr_path service_name -H host -p port 2>&1 | tee -a /tmp/moto.log" # noqa: ERA001
36
42
logger .info (f"Starting moto server: { args } " )
@@ -91,20 +97,36 @@ def moto_services() -> Iterator[Mapping[str, str]]:
91
97
Map of mocked services with moto where the key is the service name and the value is the moto url to that service
92
98
"""
93
99
processes = []
94
- services : dict [str , str ] = {}
100
+ service_to_url : dict [str , str ] = {}
101
+
102
+ services_to_start = ("s3" ,)
95
103
"""
96
104
97
105
1. Add a new entry to the 'extras' section in pyproject.toml for types-aiobotocore, moto and boto3-stubs like 'dynamodb' or 'ec2'
98
106
2. Add to this tuple the service that you want to mock, the same as the extra you added
99
107
"""
100
- for service in ("s3" ,):
108
+
109
+ def _start_moto (service_name : str | None = None ) -> str :
101
110
host = "localhost"
102
111
port = get_free_tcp_port ()
103
112
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
106
128
107
- yield services
129
+ yield service_to_url
108
130
109
131
for process in processes :
110
132
try :
0 commit comments