From 4324b79a47464936c5920bcda997574f4431d258 Mon Sep 17 00:00:00 2001 From: tatsuya4559 Date: Fri, 24 May 2024 16:17:51 +0900 Subject: [PATCH 01/10] add an option to remove exited container --- localstack_utils/container.py | 2 ++ localstack_utils/localstack.py | 5 +++++ localstack_utils/localstack_docker_configuration.py | 1 + 3 files changed, 8 insertions(+) diff --git a/localstack_utils/container.py b/localstack_utils/container.py index 9cc8535..60a5c4e 100644 --- a/localstack_utils/container.py +++ b/localstack_utils/container.py @@ -31,6 +31,7 @@ def create_localstack_container( environment_variables: dict = None, bind_ports: dict = None, pro: bool = False, + auto_remove: bool = False, ): environment_variables = environment_variables or {} environment_variables["GATEWAY_LISTEN"] = gateway_listen @@ -56,6 +57,7 @@ def create_localstack_container( image_name_or_default, ports=bind_ports, environment=environment_variables, + auto_remove=auto_remove, detach=True, ) diff --git a/localstack_utils/localstack.py b/localstack_utils/localstack.py index 311a57a..0422877 100644 --- a/localstack_utils/localstack.py +++ b/localstack_utils/localstack.py @@ -48,6 +48,7 @@ def startup(self, docker_configuration): docker_configuration.environment_variables, docker_configuration.port_mappings, docker_configuration.pro, + docker_configuration.auto_remove_container, ) self.setup_logger() @@ -77,6 +78,7 @@ def startup_localstack( env_variables=None, gateway_listen="", ignore_docker_errors=False, + auto_remove_container=False, ): global localstack_instance localstack_instance = Localstack.INSTANCE() @@ -101,6 +103,9 @@ def startup_localstack( if gateway_listen: config.gateway_listen = gateway_listen + if auto_remove_container: + config.auto_remove_container = auto_remove_container + localstack_instance.startup(config) diff --git a/localstack_utils/localstack_docker_configuration.py b/localstack_utils/localstack_docker_configuration.py index 0c0a0c3..5083c28 100644 --- a/localstack_utils/localstack_docker_configuration.py +++ b/localstack_utils/localstack_docker_configuration.py @@ -14,3 +14,4 @@ class LocalstackDockerConfiguration: use_dingle_docker_container = False ignore_docker_runerrors = False pro = False + auto_remove_container = False From 4d4d8d7ac96f21247fc70af010057addb3940920 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 14:18:30 -0500 Subject: [PATCH 02/10] update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3b4a6c5..2324d6d 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ class TestKinesis(unittest.TestCase): ``` ## Change Log +* 1.0.3: Add auto_remove config option * 1.0.2: LocalStack Pro image set as default * 1.0.1: Repository URL fixed * 1.0.0: Initial version From 7e1b5dfc810413ae0bfbc640592da35395033e8e Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 14:29:22 -0500 Subject: [PATCH 03/10] update version --- localstack_utils/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/localstack_utils/__init__.py b/localstack_utils/__init__.py index 5c4105c..976498a 100644 --- a/localstack_utils/__init__.py +++ b/localstack_utils/__init__.py @@ -1 +1 @@ -__version__ = "1.0.1" +__version__ = "1.0.3" From de9750c0a3874a1c14f947ad2e1736494c43d534 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 14:53:44 -0500 Subject: [PATCH 04/10] Add workflows for testing and releasing the utility --- .github/workflows/release.yml | 32 +++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 36 +++++++++++++++++++++++++++++++++++ .gitignore | 1 + tests/test_kinesis.py | 2 +- 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2f8a0fd --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,32 @@ +name: Release to PyPI + +on: + release: + types: [published] + workflow_dispatch: + +jobs: + release: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install build dependencies + run: | + python -m pip install --upgrade pip + pip install build twine + + - name: Build package + run: python -m build + + - name: Publish to PyPI + env: + TWINE_USERNAME: __token__ + TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} + run: twine upload dist/* diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..bb6e669 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,36 @@ +name: Tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -e ".[dev]" + + - name: Lint with ruff + run: ruff check . + + - name: Check formatting with black + run: black --check . + + - name: Run tests + run: pytest tests/ -v diff --git a/.gitignore b/.gitignore index 8cd0668..c956e4b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ localstack_utils.egg-info .ruff_cache dist +*.env diff --git a/tests/test_kinesis.py b/tests/test_kinesis.py index 06f2382..f41282e 100644 --- a/tests/test_kinesis.py +++ b/tests/test_kinesis.py @@ -6,7 +6,7 @@ class TestKinesis(unittest.TestCase): def setUp(self): - startup_localstack() + startup_localstack(image_name="localstack/localstack") def tearDown(self): stop_localstack() From 2bdcd1dfafc84ef89bb88253b7eb361e101b6087 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 14:59:34 -0500 Subject: [PATCH 05/10] add annotations for previous python versions --- localstack_utils/container.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/localstack_utils/container.py b/localstack_utils/container.py index 61ca26c..8580e35 100644 --- a/localstack_utils/container.py +++ b/localstack_utils/container.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import logging import re import docker From 59ed616521ce3cd41670aa86343af934b3defb03 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 15:12:29 -0500 Subject: [PATCH 06/10] fixes to tests and api --- localstack_utils/container.py | 2 +- localstack_utils/localstack.py | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/localstack_utils/container.py b/localstack_utils/container.py index 8580e35..3bd2890 100644 --- a/localstack_utils/container.py +++ b/localstack_utils/container.py @@ -71,4 +71,4 @@ def wait_for_ready(container, pattern): attempts += 1 if attempts >= MAX_LOG_COLLECTION_ATTEMPTS: - raise "Could not find token: " + pattern.toString() + "in logs" + raise RuntimeError(f"Could not find token: {pattern.pattern} in logs") diff --git a/localstack_utils/localstack.py b/localstack_utils/localstack.py index 0422877..dd4985d 100644 --- a/localstack_utils/localstack.py +++ b/localstack_utils/localstack.py @@ -1,5 +1,4 @@ import re -import sys import docker import logging from localstack_utils.container import Container @@ -41,14 +40,13 @@ def startup(self, docker_configuration): try: self.localstack_container = Container.create_localstack_container( - docker_configuration.pull_new_image, - docker_configuration.image_name, - docker_configuration.image_tag, - docker_configuration.gateway_listen, - docker_configuration.environment_variables, - docker_configuration.port_mappings, - docker_configuration.pro, - docker_configuration.auto_remove_container, + pull_new_image=docker_configuration.pull_new_image, + image_name=docker_configuration.image_name, + image_tag=docker_configuration.image_tag, + gateway_listen=docker_configuration.gateway_listen, + auto_remove=docker_configuration.auto_remove_container, + environment_variables=docker_configuration.environment_variables, + bind_ports=docker_configuration.port_mappings, ) self.setup_logger() @@ -57,10 +55,10 @@ def startup(self, docker_configuration): except docker.errors.APIError: if not docker_configuration.ignore_docker_runerrors: - raise "Unable to start docker" + raise RuntimeError("Unable to start docker") except Exception: - raise sys.exc_info() + raise def stop(self): self.localstack_container.stop() From 4032277bdf2e5b74cff10fb53c4f76eb63320f1f Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 15:14:18 -0500 Subject: [PATCH 07/10] fix boto region error --- tests/test_kinesis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_kinesis.py b/tests/test_kinesis.py index f41282e..89eaaef 100644 --- a/tests/test_kinesis.py +++ b/tests/test_kinesis.py @@ -16,6 +16,7 @@ def test_create_stream(self): kinesis = boto3.client( service_name="kinesis", aws_access_key_id="test", + region_name="us-east-1", aws_secret_access_key="test", endpoint_url="http://localhost:4566", ) From 3329709c6bcea5aabfe60111d5e7c2682b9d9f00 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Wed, 25 Feb 2026 15:22:32 -0500 Subject: [PATCH 08/10] upgrade lib version --- README.md | 1 + localstack_utils/__init__.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2324d6d..b3cbf1d 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ class TestKinesis(unittest.TestCase): ``` ## Change Log +* 1.0.4: Fixes to LocalStack and Container modules * 1.0.3: Add auto_remove config option * 1.0.2: LocalStack Pro image set as default * 1.0.1: Repository URL fixed diff --git a/localstack_utils/__init__.py b/localstack_utils/__init__.py index 976498a..92192ee 100644 --- a/localstack_utils/__init__.py +++ b/localstack_utils/__init__.py @@ -1 +1 @@ -__version__ = "1.0.3" +__version__ = "1.0.4" From 69334ec0cd6d72dcdf89dd1b63a44fbbe7000a5a Mon Sep 17 00:00:00 2001 From: Mark Heiges Date: Tue, 24 Mar 2026 12:51:23 -0400 Subject: [PATCH 09/10] configurable container name --- localstack_utils/container.py | 2 ++ localstack_utils/localstack.py | 5 +++++ localstack_utils/localstack_docker_configuration.py | 1 + 3 files changed, 8 insertions(+) diff --git a/localstack_utils/container.py b/localstack_utils/container.py index 3bd2890..b2a18c2 100644 --- a/localstack_utils/container.py +++ b/localstack_utils/container.py @@ -29,6 +29,7 @@ def create_localstack_container( pull_new_image: bool = False, image_name: str = LOCALSTACK_IMAGE_NAME, image_tag: str = LATEST_TAG, + container_name: str = DEFAULT_CONTAINER_ID, gateway_listen: str = "0.0.0.0:4566", auto_remove: bool = False, environment_variables: dict | None = None, @@ -52,6 +53,7 @@ def create_localstack_container( return DOCKER_CLIENT.containers.run( image_name, + name=container_name, ports=bind_ports, environment=environment_variables, auto_remove=auto_remove, diff --git a/localstack_utils/localstack.py b/localstack_utils/localstack.py index dd4985d..353aae4 100644 --- a/localstack_utils/localstack.py +++ b/localstack_utils/localstack.py @@ -43,6 +43,7 @@ def startup(self, docker_configuration): pull_new_image=docker_configuration.pull_new_image, image_name=docker_configuration.image_name, image_tag=docker_configuration.image_tag, + container_name=docker_configuration.container_name, gateway_listen=docker_configuration.gateway_listen, auto_remove=docker_configuration.auto_remove_container, environment_variables=docker_configuration.environment_variables, @@ -71,6 +72,7 @@ def setup_logger(self): def startup_localstack( image_name="", tag="", + container_name="", pro=False, ports=None, env_variables=None, @@ -89,6 +91,9 @@ def startup_localstack( if tag: config.image_tag = tag + if container_name: + config.container_name = container_name + if ports: config.port_mappings = ports diff --git a/localstack_utils/localstack_docker_configuration.py b/localstack_utils/localstack_docker_configuration.py index 5083c28..d1e332f 100644 --- a/localstack_utils/localstack_docker_configuration.py +++ b/localstack_utils/localstack_docker_configuration.py @@ -3,6 +3,7 @@ class LocalstackDockerConfiguration: randomize_ports = False image_name = None image_tag = None + container_name = None platform = None gateway_listen = "0.0.0.0:4566" From abe71eecdc301a4180f0e291c2b2d397c46491f9 Mon Sep 17 00:00:00 2001 From: Cristopher Pinzon Date: Thu, 30 Apr 2026 16:49:56 -0500 Subject: [PATCH 10/10] Release 1.0.5: configurable container name and auth token forwarding --- README.md | 1 + localstack_utils/__init__.py | 2 +- localstack_utils/container.py | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b3cbf1d..e8a9d85 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ class TestKinesis(unittest.TestCase): ``` ## Change Log +* 1.0.5: Add support for configurable container name and auth token forwarding * 1.0.4: Fixes to LocalStack and Container modules * 1.0.3: Add auto_remove config option * 1.0.2: LocalStack Pro image set as default diff --git a/localstack_utils/__init__.py b/localstack_utils/__init__.py index 92192ee..68cdeee 100644 --- a/localstack_utils/__init__.py +++ b/localstack_utils/__init__.py @@ -1 +1 @@ -__version__ = "1.0.4" +__version__ = "1.0.5" diff --git a/localstack_utils/container.py b/localstack_utils/container.py index b2a18c2..8d96bb7 100644 --- a/localstack_utils/container.py +++ b/localstack_utils/container.py @@ -1,6 +1,7 @@ from __future__ import annotations import logging +import os import re import docker from time import sleep @@ -38,6 +39,8 @@ def create_localstack_container( ): environment_variables = environment_variables or {} environment_variables["GATEWAY_LISTEN"] = gateway_listen + if "LOCALSTACK_AUTH_TOKEN" not in environment_variables and os.environ.get("LOCALSTACK_AUTH_TOKEN"): + environment_variables["LOCALSTACK_AUTH_TOKEN"] = os.environ["LOCALSTACK_AUTH_TOKEN"] image_exists = ( True if len(DOCKER_CLIENT.images.list(name=image_name)) else False