def test_must_start_service(self, make_routing_list_mock, log_routes_mock, make_static_dir_mock, SamApiProviderMock, ApiGwServiceMock): routing_list = [1, 2, 3] # something static_dir_path = "/foo/bar" make_routing_list_mock.return_value = routing_list make_static_dir_mock.return_value = static_dir_path SamApiProviderMock.return_value = self.api_provider_mock ApiGwServiceMock.return_value = self.apigw_service # Now start the service local_service = LocalApiService(self.lambda_invoke_context_mock, self.port, self.host, self.static_dir) local_service.start() # Make sure the right methods are called SamApiProviderMock.assert_called_with(self.template, cwd=self.cwd) make_routing_list_mock.assert_called_with(self.api_provider_mock) log_routes_mock.assert_called_with(self.api_provider_mock, self.host, self.port) make_static_dir_mock.assert_called_with(self.cwd, self.static_dir) ApiGwServiceMock.assert_called_with(routing_list=routing_list, lambda_runner=self.lambda_runner_mock, static_dir=static_dir_path, port=self.port, host=self.host, stderr=self.stderr_mock) self.apigw_service.create.assert_called_with() self.apigw_service.run.assert_called_with()
def do_cli(ctx, host, port, static_dir, template, env_vars, debug_port, debug_args, # pylint: disable=R0914 debugger_path, docker_volume_basedir, docker_network, log_file, skip_pull_image, profile, region): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ LOG.debug("local start-api command is called") # Pass all inputs to setup necessary context to invoke function locally. # Handler exception raised by the processor for invalid args and print errors try: with InvokeContext(template_file=template, function_identifier=None, # Don't scope to one particular function env_vars_file=env_vars, docker_volume_basedir=docker_volume_basedir, docker_network=docker_network, log_file=log_file, skip_pull_image=skip_pull_image, aws_profile=profile, debug_port=debug_port, debug_args=debug_args, debugger_path=debugger_path, aws_region=region) as invoke_context: service = LocalApiService(lambda_invoke_context=invoke_context, port=port, host=host, static_dir=static_dir) service.start() except NoApisDefined: raise UserException("Template does not have any APIs connected to Lambda functions") except InvalidSamDocumentException as ex: raise UserException(str(ex))
def test_must_raise_if_route_not_available( self, extract_api, log_routes_mock, make_static_dir_mock, SamApiProviderMock, ApiGwServiceMock ): routing_list = [] # Empty api = Api() extract_api.return_value = api SamApiProviderMock.extract_api.return_value = api SamApiProviderMock.return_value = self.api_provider_mock ApiGwServiceMock.return_value = self.apigw_service # Now start the service local_service = LocalApiService(self.lambda_invoke_context_mock, self.port, self.host, self.static_dir) local_service.api_provider.api.routes = routing_list with self.assertRaises(NoApisDefined): local_service.start()
def do_cli( # pylint: disable=R0914 ctx, host, port, static_dir, template, env_vars, debug_port, debug_args, debugger_path, container_env_vars, docker_volume_basedir, docker_network, log_file, layer_cache_basedir, skip_pull_image, force_image_build, parameter_overrides, warm_containers, debug_function, ): """ Implementation of the ``cli`` method, just separated out for unit testing purposes """ from samcli.commands.local.cli_common.invoke_context import InvokeContext from samcli.commands.local.lib.exceptions import NoApisDefined from samcli.lib.providers.exceptions import InvalidLayerReference from samcli.commands.exceptions import UserException from samcli.commands.local.lib.local_api_service import LocalApiService from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError from samcli.local.docker.lambda_debug_settings import DebuggingNotSupported LOG.debug("local start-api command is called") # Pass all inputs to setup necessary context to invoke function locally. # Handler exception raised by the processor for invalid args and print errors try: with InvokeContext( template_file=template, function_identifier= None, # Don't scope to one particular function env_vars_file=env_vars, docker_volume_basedir=docker_volume_basedir, docker_network=docker_network, log_file=log_file, skip_pull_image=skip_pull_image, debug_ports=debug_port, debug_args=debug_args, debugger_path=debugger_path, container_env_vars_file=container_env_vars, parameter_overrides=parameter_overrides, layer_cache_basedir=layer_cache_basedir, force_image_build=force_image_build, aws_region=ctx.region, aws_profile=ctx.profile, warm_container_initialization_mode=warm_containers, debug_function=debug_function, ) as invoke_context: service = LocalApiService(lambda_invoke_context=invoke_context, port=port, host=host, static_dir=static_dir) service.start() except NoApisDefined as ex: raise UserException( "Template does not have any APIs connected to Lambda functions", wrapped_from=ex.__class__.__name__) from ex except ( InvalidSamDocumentException, OverridesNotWellDefinedError, InvalidLayerReference, InvalidIntermediateImageError, DebuggingNotSupported, ) as ex: raise UserException(str(ex), wrapped_from=ex.__class__.__name__) from ex except ContainerNotStartableException as ex: raise UserException(str(ex), wrapped_from=ex.__class__.__name__) from ex