-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsession.py
More file actions
458 lines (392 loc) · 16.7 KB
/
session.py
File metadata and controls
458 lines (392 loc) · 16.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""Run and interact with a Localstack container."""
import logging
import os
import re
import string
import threading
import time
from copy import copy
from keyword import kwlist
from docker.types import Mount
from pytest_localstack import (
constants,
container,
exceptions,
plugin,
service_checks,
utils,
)
logger = logging.getLogger(__name__)
class RunningSession:
"""Connects to an already running localstack server"""
def __init__(
self,
hostname,
services=None,
region_name=None,
use_ssl=False,
localstack_version="latest",
**kwargs,
):
self.kwargs = kwargs
self.use_ssl = use_ssl
self.region_name = region_name
self._hostname = hostname
self.localstack_version = localstack_version
if self.localstack_version != "latest" and utils.get_version_tuple(
localstack_version
) < utils.get_version_tuple("0.11"):
self.service_ports = constants.LEGACY_SERVICE_PORTS
else:
self.service_ports = constants.SERVICE_PORTS
plugin.manager.hook.contribute_to_session(session=self)
# If no region was provided, use what botocore defaulted to.
if not region_name:
self.region_name = (
self.botocore.session().get_config_variable("region")
or constants.DEFAULT_AWS_REGION
)
if services is None:
self.services = copy(self.service_ports)
elif isinstance(services, (list, tuple, set)):
self.services = {}
for service_name in services:
try:
port = self.service_ports[service_name]
except KeyError:
raise exceptions.ServiceError("unknown service " + service_name)
self.services[service_name] = port
elif isinstance(services, dict):
self.services = {}
for service_name, port in services.items():
if service_name not in self.service_ports:
raise exceptions.ServiceError("unknown service " + service_name)
if port is None:
port = self.service_ports[service_name]
self.services[service_name] = port
else:
raise TypeError("unsupported services type: %r" % (services,))
@property
def hostname(self):
"""Return hostname of Localstack."""
return self._hostname
@property
def service_aliases(self):
"""Return a full list of possible names supported."""
services = set(self.services)
result = set()
for alias, service_name in constants.SERVICE_ALIASES.items():
if service_name in services:
result.add(service_name)
result.add(alias)
return result
def start(self, timeout=60):
"""Starts Localstack if needed."""
plugin.manager.hook.session_starting(session=self)
self._check_services(timeout)
plugin.manager.hook.session_started(session=self)
def _check_services(self, timeout, initial_retry_delay=0.01, max_delay=1):
"""Check that all Localstack services are running and accessible.
Does exponential backoff up to `max_delay`.
Args:
timeout (float): Number of seconds to wait for services to
be available.
initial_retry_delay (float, optional): Initial retry delay value
in seconds. Will be multiplied by `2^n` for each retry.
Default: 0.01
max_delay (float, optional): Max time in seconds to wait between
checking service availability. Default: 1
Returns:
None
Raises:
pytest_localstack.exceptions.TimeoutError: If not all services
started before `timeout` was reached.
"""
services = set(self.services)
num_retries = 0
start_time = time.time()
while services and (time.time() - start_time) < timeout:
for service_name in list(
services
): # list() because set may change during iteration
try:
service_checks.SERVICE_CHECKS[service_name](self)
services.discard(service_name)
except exceptions.ServiceError as e:
if (time.time() - start_time) >= timeout:
raise exceptions.TimeoutError(
f"Localstack service not started: {service_name}"
) from e
if services:
delay = (2**num_retries) * initial_retry_delay
if delay > max_delay:
delay = max_delay
time.sleep(delay)
num_retries += 1
def stop(self, timeout=10):
"""Stops Localstack."""
plugin.manager.hook.session_stopping(session=self)
plugin.manager.hook.session_stopped(session=self)
def __enter__(
self,
start_timeout=constants.DEFAULT_CONTAINER_START_TIMEOUT,
stop_timeout=constants.DEFAULT_CONTAINER_STOP_TIMEOUT,
):
self.__stop_timeout = stop_timeout
self.start(timeout=start_timeout)
return self
def __exit__(self, exc_type, exc, tb):
timeout = getattr(
self, "__stop_timeout", constants.DEFAULT_CONTAINER_STOP_TIMEOUT
)
self.stop(timeout=timeout)
def map_port(self, port):
"""Return host port based on Localstack port."""
return port
def service_hostname(self, service_name):
"""Get hostname and port for an AWS service."""
service_name = constants.SERVICE_ALIASES.get(service_name, service_name)
if service_name not in self.services:
raise exceptions.ServiceError(
f"{self!r} does not have {service_name} enabled"
)
port = self.map_port(self.services[service_name])
return "%s:%i" % (self.hostname, port)
def endpoint_url(self, service_name):
"""Get the URL for a service endpoint."""
url = ("https" if self.use_ssl else "http") + "://"
url += self.service_hostname(service_name)
return url
class LocalstackSession(RunningSession):
"""Run a localstack Docker container.
This class can start and stop a Localstack container, as well as capture
its logs.
Can be used as a context manager:
>>> import docker
>>> client = docker.from_env()
>>> with LocalstackSession(client) as session:
... s3 = session.boto3.resource('s3')
Args:
docker_client: A docker-py Client object that will be used
to talk to Docker.
services (list|dict, optional): One of
- A list of AWS service names to start in the
Localstack container.
- A dict of service names to the port they should run on.
Defaults to all services. Setting this
can reduce container startup time and therefore test time.
region_name (str, optional): Region name to assume.
Each Localstack container acts like a single AWS region.
Defaults to 'us-east-1'.
kinesis_error_probability (float, optional): Decimal value between
0.0 (default) and 1.0 to randomly inject
ProvisionedThroughputExceededException errors
into Kinesis API responses.
dynamodb_error_probability (float, optional): Decimal value
between 0.0 (default) and 1.0 to randomly inject
ProvisionedThroughputExceededException errors into
DynamoDB API responses.
container_log_level (int, optional): The logging level to use
for Localstack container logs. Defaults to :attr:`logging.DEBUG`.
localstack_version (str, optional): The version of the Localstack
image to use. Defaults to `latest`.
auto_remove (bool, optional): If True, delete the Localstack
container when it stops.
container_name (str, optional): The name for the Localstack
container. Defaults to a randomly generated id.
use_ssl (bool, optional): If True use SSL to connect to Localstack.
Default is False.
**kwargs: Additional kwargs will be stored in a `kwargs` attribute
in case test resource factories want to access them.
"""
image_name = "localstack/localstack"
def __init__(
self,
docker_client,
services=None,
region_name=None,
kinesis_error_probability=0.0,
dynamodb_error_probability=0.0,
container_log_level=logging.DEBUG,
localstack_version="latest",
auto_remove=True,
pull_image=True,
container_name=None,
use_ssl=False,
hostname=None,
privileged=False,
mount_docker_socket=False,
docker_socket="/var/run/docker.sock",
additional_env=None,
**kwargs,
):
self._container = None
self._container_lock = threading.RLock()
self._factory_cache = {}
self.docker_client = docker_client
self.region_name = region_name
self.kinesis_error_probability = kinesis_error_probability
self.dynamodb_error_probability = dynamodb_error_probability
self.auto_remove = bool(auto_remove)
self.pull_image = bool(pull_image)
self.docker_socket = docker_socket
self.mount_docker_socket = bool(mount_docker_socket)
self.additional_env = (additional_env or {}).copy()
self.privileged = bool(privileged)
super(LocalstackSession, self).__init__(
hostname=hostname if hostname else default_hostname(),
services=services,
region_name=region_name,
use_ssl=use_ssl,
localstack_version=localstack_version,
**kwargs,
)
self.container_log_level = container_log_level
self.localstack_version = localstack_version
self.container_name = container_name or generate_container_name()
def start(self, timeout=60):
"""Start the Localstack container.
Args:
timeout (float, optional): Wait at most this many seconds
for the Localstack services to start. Default is 1 minute.
Raises:
pytest_localstack.exceptions.TimeoutError:
If *timeout* was reached before all Localstack
services were available.
docker.errors.APIError: If the Docker daemon returns an error.
"""
with self._container_lock:
if self._container is not None:
raise exceptions.ContainerAlreadyStartedError(self)
logger.debug("Starting Localstack container %s", self.container_name)
logger.debug("%r running starting hooks", self)
plugin.manager.hook.session_starting(session=self)
image_name = self.image_name + ":" + self.localstack_version
if self.pull_image:
logger.debug("Pulling docker image %r", image_name)
self.docker_client.images.pull(image_name)
start_time = time.time()
services = ",".join("%s:%s" % pair for pair in self.services.items())
kinesis_error_probability = "%f" % self.kinesis_error_probability
dynamodb_error_probability = "%f" % self.dynamodb_error_probability
use_ssl = str(self.use_ssl).lower()
mounts = []
if self.mount_docker_socket and self.docker_socket:
# required for lambda v2 but optional because some
# environments don't allow mounting the docker socket
# or not requiring it (e.g. when not using lambda)
mount = Mount(
target="/var/run/docker.sock",
source=self.docker_socket,
type="bind",
)
mounts.append(mount)
if self.region_name:
# Default region is deprecated, but some tests
# may still rely on it or in older container versions
self.additional_env["DEFAULT_REGION"] = self.region_name
self._container = self.docker_client.containers.run(
image_name,
name=self.container_name,
detach=True,
auto_remove=self.auto_remove,
environment={
"SERVICES": services,
"KINESIS_ERROR_PROBABILITY": kinesis_error_probability,
"DYNAMODB_ERROR_PROBABILITY": dynamodb_error_probability,
"USE_SSL": use_ssl,
**self.additional_env,
},
privileged=self.privileged,
ports={port: None for port in self.services.values()},
mounts=mounts,
)
logger.debug(
"Started Localstack container %s (id: %s)",
self.container_name,
self._container.short_id,
)
# Tail container logs
container_logger = logger.getChild(
"containers.%s" % self._container.short_id
)
self._stdout_tailer = container.DockerLogTailer(
self._container,
container_logger.getChild("stdout"),
self.container_log_level,
stdout=True,
stderr=False,
)
self._stdout_tailer.start()
self._stderr_tailer = container.DockerLogTailer(
self._container,
container_logger.getChild("stderr"),
self.container_log_level,
stdout=False,
stderr=True,
)
self._stderr_tailer.start()
try:
timeout_remaining = timeout - (time.time() - start_time)
if timeout_remaining <= 0:
raise exceptions.TimeoutError("Container took too long to start.")
self._check_services(timeout_remaining)
logger.debug("%r running started hooks", self)
plugin.manager.hook.session_started(session=self)
logger.debug("%r finished started hooks", self)
except exceptions.TimeoutError:
if self._container is not None:
self.stop(0.1)
raise
def stop(self, timeout=10):
"""Stop the Localstack container.
Args:
timeout (float, optional): Timeout in seconds to wait for the
container to stop before sending a SIGKILL. Default: 10
Raises:
docker.errors.APIError: If the Docker daemon returns an error.
"""
with self._container_lock:
if self._container is not None:
logger.debug("Stopping %r", self)
logger.debug("Running stopping hooks for %r", self)
plugin.manager.hook.session_stopping(session=self)
logger.debug("Finished stopping hooks for %r", self)
self._container.stop(timeout=10)
self._container = None
self._stdout_tailer = None
self._stderr_tailer = None
logger.debug("Stopped %r", self)
logger.debug("Running stopped hooks for %r", self)
plugin.manager.hook.session_stopped(session=self)
logger.debug("Finished stopped hooks for %r", self)
def __del__(self):
"""Stop container on garbage collection."""
self.stop(0.1)
def map_port(self, port):
"""Return host port based on Localstack container port."""
with self._container_lock:
if self._container is None:
raise exceptions.ContainerNotStartedError(self)
result = self.docker_client.api.port(self._container.id, int(port))
if not result:
return None
return int(result[0]["HostPort"])
def generate_container_name():
"""Generate a random name for a Localstack container."""
valid_chars = set(string.ascii_letters)
chars = []
while len(chars) < 6:
new_chars = [chr(c) for c in os.urandom(6 - len(chars))]
chars += [c for c in new_chars if c in valid_chars]
return "pytest-localstack-" + "".join(chars)
def default_hostname():
"""Return the default hostname for Localstack, based on the DOCKER_HOST env var."""
hostname = constants.LOCALHOST
docker_host = os.environ.get("DOCKER_HOST", "").strip()
match = re.match(r"^([^:]+)://(?:([^:]+?)(?::(\d+))?)?$", docker_host)
if match is not None:
protocol = match.group(1)
if "unix" not in protocol and "npipe" not in protocol and match.group(2):
hostname = match.group(2)
return hostname