Skip to content

Commit 1985c39

Browse files
committed
refactor wiremock extension to use utils base class as well
1 parent b48ccf4 commit 1985c39

File tree

7 files changed

+46
-227
lines changed

7 files changed

+46
-227
lines changed

.github/workflows/wiremock.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
pip install localstack terraform-local awscli-local[ver1]
4040
4141
make install
42+
make lint
4243
make dist
4344
localstack extensions -v install file://$(ls ./dist/localstack_wiremock-*.tar.gz)
4445

utils/localstack_extensions/utils/docker.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
from localstack.utils.docker_utils import DOCKER_CLIENT
1313
from localstack.extensions.api import Extension, http
1414
from localstack.http import Request
15-
from localstack.utils.container_utils.container_client import PortMappings
15+
from localstack.utils.container_utils.container_client import (
16+
PortMappings,
17+
SimpleVolumeBind,
18+
)
1619
from localstack.utils.net import get_addressable_container_host
1720
from localstack.utils.sync import retry
1821
from rolo import route
@@ -52,11 +55,17 @@ class ProxiedDockerContainerExtension(Extension):
5255
"""Optional command (and flags) to execute in the container."""
5356
env_vars: dict[str, str] | None
5457
"""Optional environment variables to pass to the container."""
58+
volumes: list[SimpleVolumeBind] | None
59+
"""Optional volumes to mount into the container."""
5560
health_check_fn: Callable[[], None] | None
5661
"""
5762
Optional custom health check function. If not provided, defaults to HTTP GET on main_port.
5863
The function should raise an exception if the health check fails.
5964
"""
65+
health_check_retries: int
66+
"""Number of times to retry the health check before giving up."""
67+
health_check_sleep: float
68+
"""Time in seconds to sleep between health check retries."""
6069

6170
request_to_port_router: Callable[[Request], int] | None
6271
"""Callable that returns the target port for a given request, for routing purposes"""
@@ -87,7 +96,10 @@ def __init__(
8796
path: str | None = None,
8897
command: list[str] | None = None,
8998
env_vars: dict[str, str] | None = None,
99+
volumes: list[SimpleVolumeBind] | None = None,
90100
health_check_fn: Callable[[], None] | None = None,
101+
health_check_retries: int = 60,
102+
health_check_sleep: float = 1.0,
91103
request_to_port_router: Callable[[Request], int] | None = None,
92104
http2_ports: list[int] | None = None,
93105
tcp_ports: list[int] | None = None,
@@ -101,7 +113,10 @@ def __init__(
101113
self.container_name = re.sub(r"\W", "-", f"ls-ext-{self.name}")
102114
self.command = command
103115
self.env_vars = env_vars
116+
self.volumes = volumes
104117
self.health_check_fn = health_check_fn
118+
self.health_check_retries = health_check_retries
119+
self.health_check_sleep = health_check_sleep
105120
self.request_to_port_router = request_to_port_router
106121
self.http2_ports = http2_ports
107122
self.tcp_ports = tcp_ports
@@ -193,6 +208,8 @@ def start_container(self) -> None:
193208
kwargs["command"] = self.command
194209
if self.env_vars:
195210
kwargs["env_vars"] = self.env_vars
211+
if self.volumes:
212+
kwargs["volumes"] = self.volumes
196213

197214
try:
198215
DOCKER_CLIENT.run_container(
@@ -213,7 +230,11 @@ def start_container(self) -> None:
213230
health_check = self.health_check_fn or self._default_health_check
214231

215232
try:
216-
retry(health_check, retries=60, sleep=1)
233+
retry(
234+
health_check,
235+
retries=self.health_check_retries,
236+
sleep=self.health_check_sleep,
237+
)
217238
except Exception as e:
218239
LOG.info("Failed to connect to container %s: %s", self.container_name, e)
219240
self._remove_container()

wiremock/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ entrypoints: venv # Generate plugin entrypoints for Python package
3434

3535
format: ## Run ruff to format the whole codebase
3636
$(VENV_RUN); python -m ruff format .; python -m ruff check --output-format=full --fix .
37+
38+
lint: ## Run ruff to lint the codebase
39+
$(VENV_RUN); python -m ruff check --output-format=full .
40+
3741
sample-oss: ## Deploy sample app (OSS mode)
3842
echo "Creating stubs in WireMock ..."
3943
bin/create-stubs.sh

wiremock/localstack_wiremock/extension.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import logging
22
import os
33
from pathlib import Path
4+
import requests
45

56
from localstack import config, constants
6-
from localstack_wiremock.utils.docker import ProxiedDockerContainerExtension
7+
from localstack.utils.net import get_addressable_container_host
8+
from localstack_extensions.utils.docker import ProxiedDockerContainerExtension
79

810

911
LOG = logging.getLogger(__name__)
@@ -71,15 +73,23 @@ def __init__(self):
7173
health_check_port = ADMIN_PORT if api_token else SERVICE_PORT
7274
self._is_runner_mode = bool(api_token)
7375

76+
def _health_check():
77+
"""Custom health check for WireMock."""
78+
container_host = get_addressable_container_host()
79+
health_url = (
80+
f"http://{container_host}:{health_check_port}{health_check_path}"
81+
)
82+
LOG.debug("Health check: %s", health_url)
83+
response = requests.get(health_url, timeout=5)
84+
assert response.ok
85+
7486
super().__init__(
7587
image_name=image_name,
7688
container_ports=container_ports,
77-
container_name=self.CONTAINER_NAME,
7889
host=self.HOST,
7990
env_vars=env_vars if env_vars else None,
8091
volumes=volumes,
81-
health_check_path=health_check_path,
82-
health_check_port=health_check_port,
92+
health_check_fn=_health_check,
8393
health_check_retries=health_check_retries,
8494
health_check_sleep=health_check_sleep,
8595
)

wiremock/localstack_wiremock/utils/__init__.py

Whitespace-only changes.

wiremock/localstack_wiremock/utils/docker.py

Lines changed: 0 additions & 220 deletions
This file was deleted.

wiremock/pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ authors = [
1414
keywords = ["LocalStack", "WireMock"]
1515
classifiers = []
1616
dependencies = [
17-
"priority"
17+
"priority",
18+
# TODO remove / replace prior to merge!
19+
# "localstack-extensions-utils",
20+
"localstack-extensions-utils @ git+https://github.com/localstack/localstack-extensions.git@extract-utils-package#subdirectory=utils"
1821
]
1922

2023
[project.urls]

0 commit comments

Comments
 (0)