diff --git a/docs/docs/server_mode.rst b/docs/docs/server_mode.rst index 4f5ec2798..168e1e630 100644 --- a/docs/docs/server_mode.rst +++ b/docs/docs/server_mode.rst @@ -119,7 +119,7 @@ It is possible to keep the state in between tests, using this environment variab .. code-block:: bash - MOTO_CALL_RESET_API=true + MOTO_CALL_RESET_API=false Dashboard diff --git a/moto/core/models.py b/moto/core/models.py index b69d9c824..370d6e3c0 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -388,7 +388,7 @@ MockAWS = BotocoreEventMockAWS class ServerModeMockAWS(BaseMockAWS): def __init__(self, *args, **kwargs): - self.port = settings.moto_server_port() + self.test_server_mode_endpoint = settings.test_server_mode_endpoint() super().__init__(*args, **kwargs) def reset(self): @@ -396,7 +396,7 @@ class ServerModeMockAWS(BaseMockAWS): if not call_reset_api or call_reset_api.lower() != "false": import requests - requests.post(f"http://localhost:{self.port}/moto-api/reset") + requests.post(f"{self.test_server_mode_endpoint}/moto-api/reset") def enable_patching(self, reset=True): if self.__class__.nested_count == 1 and reset: @@ -414,12 +414,12 @@ class ServerModeMockAWS(BaseMockAWS): config = Config(user_agent_extra="region/" + region) kwargs["config"] = config if "endpoint_url" not in kwargs: - kwargs["endpoint_url"] = f"http://localhost:{self.port}" + kwargs["endpoint_url"] = self.test_server_mode_endpoint return real_boto3_client(*args, **kwargs) def fake_boto3_resource(*args, **kwargs): if "endpoint_url" not in kwargs: - kwargs["endpoint_url"] = f"http://localhost:{self.port}" + kwargs["endpoint_url"] = self.test_server_mode_endpoint return real_boto3_resource(*args, **kwargs) self._client_patcher = patch("boto3.client", fake_boto3_client) diff --git a/moto/core/responses.py b/moto/core/responses.py index 6f3f9c3f2..8d3326d1f 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -141,13 +141,13 @@ class ActionAuthenticatorMixin(object): @staticmethod def set_initial_no_auth_action_count(initial_no_auth_action_count): - _port = settings.moto_server_port() + _test_server_mode_endpoint = settings.test_server_mode_endpoint() def decorator(function): def wrapper(*args, **kwargs): if settings.TEST_SERVER_MODE: response = requests.post( - f"http://localhost:{_port}/moto-api/reset-auth", + f"{_test_server_mode_endpoint}/moto-api/reset-auth", data=str(initial_no_auth_action_count).encode("utf-8"), ) original_initial_no_auth_action_count = response.json()[ @@ -165,7 +165,7 @@ class ActionAuthenticatorMixin(object): finally: if settings.TEST_SERVER_MODE: requests.post( - f"http://localhost:{_port}/moto-api/reset-auth", + f"{_test_server_mode_endpoint}/moto-api/reset-auth", data=str(original_initial_no_auth_action_count).encode( "utf-8" ), diff --git a/moto/settings.py b/moto/settings.py index c5d77591e..25a131de7 100644 --- a/moto/settings.py +++ b/moto/settings.py @@ -70,6 +70,12 @@ def moto_network_mode(): return os.environ.get("MOTO_DOCKER_NETWORK_MODE") +def test_server_mode_endpoint(): + return os.environ.get( + "TEST_SERVER_MODE_ENDPOINT", f"http://localhost:{moto_server_port()}" + ) + + def is_docker(): path = "/proc/self/cgroup" return (