Skip to content

Commit

Permalink
Merge branch 'develop' into dependabot/pip/requirements/develop/cfn-l…
Browse files Browse the repository at this point in the history
…int-0.83.3
  • Loading branch information
hnnasit authored Nov 21, 2023
2 parents 1bca3f2 + 6726731 commit a2f6f46
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion samcli/commands/local/cli_common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from samcli.commands._utils.options import docker_click_options, parameter_override_click_option, template_click_option
from samcli.commands.local.cli_common.invoke_context import ContainersInitializationMode
from samcli.local.docker.container import DEFAULT_CONTAINER_HOST_INTERFACE


def get_application_dir():
Expand Down Expand Up @@ -59,7 +60,7 @@ def local_common_options(f):
),
click.option(
"--container-host-interface",
default="127.0.0.1",
default=DEFAULT_CONTAINER_HOST_INTERFACE,
show_default=True,
help="IP address of the host network interface that container ports should bind to. "
"Use 0.0.0.0 to bind to all interfaces.",
Expand Down
7 changes: 5 additions & 2 deletions samcli/local/docker/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
LOG = logging.getLogger(__name__)

CONTAINER_CONNECTION_TIMEOUT = float(os.environ.get("SAM_CLI_CONTAINER_CONNECTION_TIMEOUT", 20))
DEFAULT_CONTAINER_HOST_INTERFACE = "127.0.0.1"


class ContainerResponseException(Exception):
Expand Down Expand Up @@ -74,7 +75,7 @@ def __init__(
container_opts=None,
additional_volumes=None,
container_host="localhost",
container_host_interface="127.0.0.1",
container_host_interface=DEFAULT_CONTAINER_HOST_INTERFACE,
mount_with_write: bool = False,
host_tmp_dir: Optional[str] = None,
):
Expand Down Expand Up @@ -130,7 +131,9 @@ def __init__(
self._host_tmp_dir = host_tmp_dir

try:
self.rapid_port_host = find_free_port(start=self._start_port_range, end=self._end_port_range)
self.rapid_port_host = find_free_port(
network_interface=self._container_host_interface, start=self._start_port_range, end=self._end_port_range
)
except NoFreePortsError as ex:
raise ContainerNotStartableException(str(ex)) from ex

Expand Down
4 changes: 2 additions & 2 deletions samcli/local/docker/lambda_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from samcli.lib.utils.packagetype import IMAGE
from samcli.local.docker.lambda_debug_settings import LambdaDebugSettings

from .container import Container
from .container import DEFAULT_CONTAINER_HOST_INTERFACE, Container
from .lambda_image import LambdaImage, Runtime

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(
env_vars=None,
debug_options=None,
container_host=None,
container_host_interface=None,
container_host_interface=DEFAULT_CONTAINER_HOST_INTERFACE,
function_full_path=None,
):
"""
Expand Down
5 changes: 3 additions & 2 deletions samcli/local/docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def to_posix_path(code_path):
)


def find_free_port(start=5000, end=9000):
def find_free_port(network_interface: str, start: int = 5000, end: int = 9000) -> int:
"""
Utility function which scans through a port range in a randomized manner
and finds the first free port a socket can bind to.
Expand All @@ -61,8 +61,9 @@ def find_free_port(start=5000, end=9000):
port_range = [random.randrange(start, end) for _ in range(start, end)]
for port in port_range:
try:
LOG.debug("Checking free port on %s:%s", network_interface, port)
s = socket.socket()
s.bind(("", port))
s.bind((network_interface, port))
s.close()
return port
except OSError:
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/local/docker/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import os
from parameterized import parameterized
from unittest import TestCase
from unittest.mock import patch, Mock

Expand All @@ -29,11 +30,12 @@ def test_do_not_convert_posix_path(self, mock_os):


class TestFreePorts(TestCase):
@parameterized.expand([("0.0.0.0",), ("127.0.0.1",)])
@patch("samcli.local.docker.utils.socket")
@patch("samcli.local.docker.utils.random")
def test_free_port_first_attempt(self, mock_random, mock_socket):
def test_free_port_first_attempt(self, network_interface, mock_random, mock_socket):
mock_random.randrange = Mock(side_effect=[3093] * 1000)
port = find_free_port(start=3000, end=4000)
port = find_free_port(network_interface, start=3000, end=4000)
self.assertEqual(port, 3093)

@patch("samcli.local.docker.utils.socket")
Expand All @@ -43,7 +45,7 @@ def test_free_port_after_failed_attempts(self, mock_random, mock_socket_module):
mock_socket_object.bind = Mock(side_effect=[OSError, OSError, Mock()])
mock_socket_module.socket = Mock(return_value=mock_socket_object)
mock_random.randrange = Mock(side_effect=[3093, 3987, 3300, 3033] * 250)
port = find_free_port(start=3000, end=4000)
port = find_free_port("127.0.0.1", start=3000, end=4000)
self.assertEqual(port, 3300)

@patch("samcli.local.docker.utils.socket")
Expand All @@ -54,7 +56,7 @@ def test_no_free_port_after_failed_attempts(self, mock_random, mock_socket_modul
mock_socket_module.socket = Mock(return_value=mock_socket_object)
mock_random.randrange = Mock(side_effect=[1, 2, 3] * 3)
with self.assertRaises(NoFreePortsError):
find_free_port(start=1, end=4)
find_free_port("127.0.0.1", start=1, end=4)


class TestGetRapidName(TestCase):
Expand Down

0 comments on commit a2f6f46

Please sign in to comment.