Skip to content

Commit

Permalink
fix: unit tests for add-host cli option
Browse files Browse the repository at this point in the history
With these fixes, unit tests are aligned to handle add-host cli option and extra_hosts dictionary for Docker container creation.
  • Loading branch information
Murat Yukselen committed Oct 13, 2023
1 parent d876cb0 commit c31bf76
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 2 deletions.
1 change: 1 addition & 0 deletions samcli/commands/local/cli_common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def local_common_options(f):
),
click.option(
"--add-host",
default=None,
multiple=True,
help="Utilize hostname to IP mapping via docker --add-host flag. Can be multiple."
),
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/invoke/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"shutdown",
"container_host",
"container_host_interface",
"add_host",
"invoke_image",
]

Expand Down
2 changes: 2 additions & 0 deletions samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def cli(
debug_function,
container_host,
container_host_interface,
add_host,
invoke_image,
hook_name,
skip_prepare_infra,
Expand Down Expand Up @@ -142,6 +143,7 @@ def cli(
debug_function,
container_host,
container_host_interface,
add_host,
invoke_image,
hook_name,
) # pragma: no cover
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/start_api/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"shutdown",
"container_host",
"container_host_interface",
"add_host",
"invoke_image",
]

Expand Down
2 changes: 2 additions & 0 deletions samcli/commands/local/start_lambda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def cli(
debug_function,
container_host,
container_host_interface,
add_host,
invoke_image,
hook_name,
skip_prepare_infra,
Expand Down Expand Up @@ -128,6 +129,7 @@ def cli(
debug_function,
container_host,
container_host_interface,
add_host,
invoke_image,
hook_name,
) # pragma: no cover
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/start_lambda/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"shutdown",
"container_host",
"container_host_interface",
"add_host",
"invoke_image",
]

Expand Down
4 changes: 2 additions & 2 deletions samcli/local/lambdafn/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def __init__(self, container_manager, image_builder, observer=None):

super().__init__(container_manager, image_builder)

def create(self, function_config, debug_context=None, container_host=None, container_host_interface=None):
def create(self, function_config, debug_context=None, container_host=None, container_host_interface=None, extra_hosts=None):
"""
Create a new Container for the passed function, then store it in a dictionary using the function name,
so it can be retrieved later and used in the other functions. Make sure to use the debug_context only
Expand Down Expand Up @@ -418,7 +418,7 @@ def create(self, function_config, debug_context=None, container_host=None, conta
self._observer.watch(function_config)
self._observer.start()

container = super().create(function_config, debug_context, container_host, container_host_interface)
container = super().create(function_config, debug_context, container_host, container_host_interface, extra_hosts)
self._function_configs[function_config.full_path] = function_config
self._containers[function_config.full_path] = container

Expand Down
92 changes: 92 additions & 0 deletions tests/unit/commands/local/cli_common/test_invoke_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ def test_must_create_runner(
aws_region="region",
container_host=None,
container_host_interface=None,
extra_hosts=None,
)

result = self.context.local_lambda_runner
Expand Down Expand Up @@ -708,6 +709,7 @@ def test_must_create_runner_using_warm_containers(
aws_region="region",
container_host=None,
container_host_interface=None,
extra_hosts=None,
)

result = self.context.local_lambda_runner
Expand Down Expand Up @@ -792,6 +794,95 @@ def test_must_create_runner_with_container_host_option(
aws_region="region",
container_host="abcdef",
container_host_interface="192.168.100.101",
extra_hosts=None,
)

result = self.context.local_lambda_runner
self.assertEqual(result, runner_mock)
# assert that lambda runner is created only one time, and the cached version used in the second call
self.assertEqual(LocalLambdaMock.call_count, 1)

@patch("samcli.local.lambdafn.runtime.LambdaFunctionObserver")
@patch("samcli.commands.local.cli_common.invoke_context.LambdaImage")
@patch("samcli.commands.local.cli_common.invoke_context.LayerDownloader")
@patch("samcli.commands.local.cli_common.invoke_context.LambdaRuntime")
@patch("samcli.commands.local.cli_common.invoke_context.LocalLambdaRunner")
@patch("samcli.commands.local.cli_common.invoke_context.SamFunctionProvider")
def test_must_create_runner_with_extra_hosts_option(
self,
SamFunctionProviderMock,
LocalLambdaMock,
LambdaRuntimeMock,
download_layers_mock,
lambda_image_patch,
LambdaFunctionObserver_patch,
):
runtime_mock = Mock()
LambdaRuntimeMock.return_value = runtime_mock

runner_mock = Mock()
LocalLambdaMock.return_value = runner_mock

download_mock = Mock()
download_layers_mock.return_value = download_mock

image_mock = Mock()
lambda_image_patch.return_value = image_mock

LambdaFunctionObserver_mock = Mock()
LambdaFunctionObserver_patch.return_value = LambdaFunctionObserver_mock

cwd = "cwd"
self.context = InvokeContext(
template_file="template_file",
function_identifier="id",
env_vars_file="env_vars_file",
docker_volume_basedir="volumedir",
docker_network="network",
log_file="log_file",
skip_pull_image=True,
force_image_build=True,
debug_ports=[1111],
debugger_path="path-to-debugger",
debug_args="args",
aws_profile="profile",
aws_region="region",
container_host="abcdef",
add_host=["prod-na.host:10.11.12.13", "gamma-na.host:10.22.23.24"],
)
self.context.get_cwd = Mock()
self.context.get_cwd.return_value = cwd

self.context._get_stacks = Mock()
self.context._get_stacks.return_value = [Mock()]
self.context._get_env_vars_value = Mock()
self.context._setup_log_file = Mock()
self.context._get_debug_context = Mock(return_value=None)

container_manager_mock = Mock()
container_manager_mock.is_docker_reachable = PropertyMock(return_value=True)
self.context._get_container_manager = Mock(return_value=container_manager_mock)

with self.context:
result = self.context.local_lambda_runner
self.assertEqual(result, runner_mock)

LambdaRuntimeMock.assert_called_with(container_manager_mock, image_mock)
lambda_image_patch.assert_called_once_with(download_mock, True, True, invoke_images=None)
LocalLambdaMock.assert_called_with(
local_runtime=runtime_mock,
function_provider=ANY,
cwd=cwd,
debug_context=None,
env_vars_values=ANY,
aws_profile="profile",
aws_region="region",
container_host="abcdef",
container_host_interface=None,
extra_hosts={
"prod-na.host":"10.11.12.13",
"gamma-na.host":"10.22.23.24",
}
)

result = self.context.local_lambda_runner
Expand Down Expand Up @@ -875,6 +966,7 @@ def test_must_create_runner_with_invoke_image_option(
aws_region="region",
container_host=None,
container_host_interface=None,
extra_hosts=None,
)

result = self.context.local_lambda_runner
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/commands/local/invoke/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def setUp(self):
self.profile = "profile"
self.container_host = "localhost"
self.container_host_interface = "127.0.0.1"
self.add_host = ("prod-na.host:10.11.12.13",),
self.invoke_image = ("amazon/aws-sam-cli-emulation-image-python3.9",)
self.hook_name = None

Expand Down Expand Up @@ -72,6 +73,7 @@ def call_cli(self):
shutdown=self.shutdown,
container_host=self.container_host,
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_image=self.invoke_image,
hook_name=self.hook_name,
)
Expand Down Expand Up @@ -108,6 +110,7 @@ def test_cli_must_setup_context_and_invoke(self, get_event_mock, InvokeContextMo
aws_profile=self.profile,
container_host=self.container_host,
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_images={None: "amazon/aws-sam-cli-emulation-image-python3.9"},
)

Expand Down Expand Up @@ -147,6 +150,7 @@ def test_cli_must_invoke_with_no_event(self, get_event_mock, InvokeContextMock):
aws_profile=self.profile,
container_host=self.container_host,
container_host_interface=self.container_host_interface,
add_host=self.add_host,
invoke_images={None: "amazon/aws-sam-cli-emulation-image-python3.9"},
)

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/commands/local/lib/test_local_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ def test_must_work(self, patched_validate_architecture_runtime):
stderr=stderr,
container_host=None,
container_host_interface=None,
extra_hosts=None,
)

@patch("samcli.commands.local.lib.local_lambda.validate_architecture_runtime")
Expand All @@ -594,6 +595,7 @@ def test_must_work_packagetype_ZIP(self, patched_validate_architecture_runtime):
stderr=stderr,
container_host=None,
container_host_interface=None,
extra_hosts=None,
)

@patch("samcli.commands.local.lib.local_lambda.validate_architecture_runtime")
Expand Down Expand Up @@ -677,6 +679,7 @@ def test_works_if_imageuri_and_Image_packagetype(self, patched_validate_architec
stderr=stderr,
container_host=None,
container_host_interface=None,
extra_hosts=None,
)

def test_must_raise_if_imageuri_not_found(self):
Expand Down Expand Up @@ -756,6 +759,7 @@ def test_must_work(self, patched_validate_architecture_runtime):
stderr=stderr,
container_host="localhost",
container_host_interface="127.0.0.1",
extra_hosts=None,
)


Expand Down
5 changes: 5 additions & 0 deletions tests/unit/commands/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ def test_local_invoke(self, do_cli_mock):
{"Key": "Value", "Key2": "Value2"},
"localhost",
"127.0.0.1",
(),
("image",),
None,
)
Expand Down Expand Up @@ -456,6 +457,7 @@ def test_local_start_api(self, do_cli_mock):
None,
"localhost",
"127.0.0.1",
(),
("image",),
None,
)
Expand Down Expand Up @@ -518,6 +520,7 @@ def test_local_start_lambda(self, do_cli_mock):
None,
"localhost",
"127.0.0.1",
(),
("image",),
None,
)
Expand Down Expand Up @@ -1128,6 +1131,7 @@ def test_override_with_cli_params(self, do_cli_mock):
None,
"localhost",
"127.0.0.1",
(),
("image",),
None,
)
Expand Down Expand Up @@ -1222,6 +1226,7 @@ def test_override_with_cli_params_and_envvars(self, do_cli_mock):
None,
"localhost",
"127.0.0.1",
(),
("image",),
None,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/local/lambdafn/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_must_create_lambda_container(self, LambdaContainerMock, LogMock):
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
)
# Run the container and get results
Expand Down Expand Up @@ -161,6 +162,7 @@ def test_must_log_if_template_has_runtime_version(self, LambdaContainerMock, Log
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
)
# Run the container and get results
Expand Down Expand Up @@ -337,6 +339,7 @@ def test_must_run_container_and_wait_for_result(self, LambdaContainerMock):
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
)

Expand Down Expand Up @@ -690,6 +693,7 @@ def test_must_run_container_then_wait_for_result_and_container_not_stopped(
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
)

Expand Down Expand Up @@ -791,6 +795,7 @@ def test_must_create_non_cached_container(self, LambdaContainerMock, LambdaFunct
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
)

Expand Down Expand Up @@ -837,6 +842,7 @@ def test_must_create_incase_function_config_changed(self, LambdaContainerMock, L
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
),
call(
Expand All @@ -854,6 +860,7 @@ def test_must_create_incase_function_config_changed(self, LambdaContainerMock, L
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
),
]
Expand Down Expand Up @@ -925,6 +932,7 @@ def test_must_ignore_debug_options_if_function_name_is_not_debug_function(
memory_mb=self.DEFAULT_MEMORY,
container_host=None,
container_host_interface=None,
extra_hosts=None,
function_full_path=self.full_path,
)
self.manager_mock.create.assert_called_with(container)
Expand Down

0 comments on commit c31bf76

Please sign in to comment.