def setUp(self): self.runtime = "nodejs4.3" self.handler = "handler" self.code_dir = "codedir" self.env_var = {"var": "value"} self.memory_mb = 1024 self.debug_options = DebugContext(debug_args="a=b c=d e=f", debug_port=1235)
def test_no_additional_volumes_when_debuggr_path_is_none(self, runtime): expected = {} debug_options = DebugContext(debug_ports=[1234]) result = LambdaContainer._get_additional_volumes(runtime, debug_options) self.assertEqual(result, expected)
def test_additional_volumes_returns_volume_with_debugger_path_is_set(self): expected = {'/somepath': {"bind": "/tmp/lambci_debug_files", "mode": "ro"}} debug_options = DebugContext(debug_port=1234, debugger_path='/somepath') result = LambdaContainer._get_additional_volumes(debug_options) self.assertEquals(result, expected)
def test_must_map_same_port_on_host_and_container(self): debug_options = DebugContext(debug_port=12345) expected = {debug_options.debug_port: debug_options.debug_port} result = LambdaContainer._get_exposed_ports(debug_options) self.assertEquals(expected, result)
def test_must_map_multiple_ports_on_host_and_container(self): debug_options = DebugContext(debug_ports=[12345, 67890]) expected = {port: port for port in debug_options.debug_ports} result = LambdaContainer._get_exposed_ports(debug_options) self.assertEqual(expected, result)
def test_go_runtime_returns_additional_options(self, runtime): expected = {"security_opt": ["seccomp:unconfined"], "cap_add": ["SYS_PTRACE"]} debug_options = DebugContext(debug_port=1235) result = LambdaContainer._get_additional_options(runtime, debug_options) self.assertEquals(result, expected)
def test_delve_api_version_can_be_read_from_debug_args( self, version, debug_args): debug_options = DebugContext(debug_ports=[1235], debug_args=debug_args) _, env_vars = LambdaContainer._get_debug_settings( Runtime.go1x.value, debug_options) self.assertEqual(env_vars.get("_AWS_LAMBDA_GO_DELVE_API_VERSION"), version)
def _get_debug_context( debug_ports: Optional[Tuple[int]], debug_args: Optional[str], debugger_path: Optional[str], container_env_vars: Optional[Dict[str, str]], debug_function: Optional[str] = None, ) -> DebugContext: """ Creates a DebugContext if the InvokeContext is in a debugging mode Parameters ---------- debug_ports tuple(int) Ports to bind the debugger to debug_args str Additional arguments passed to the debugger debugger_path str Path to the directory of the debugger to mount on Docker container_env_vars dict Dictionary containing debugging based environmental variables. debug_function str The Lambda function logicalId that will have the debugging options enabled in case of warm containers option is enabled Returns ------- samcli.commands.local.lib.debug_context.DebugContext Object representing the DebugContext Raises ------ samcli.commands.local.cli_common.user_exceptions.DebugContext When the debugger_path is not valid """ if debug_ports and debugger_path: try: debugger = Path(debugger_path).resolve(strict=True) except OSError as error: if error.errno == errno.ENOENT: raise DebugContextException( "'{}' could not be found.".format( debugger_path)) from error raise error if not debugger.is_dir(): raise DebugContextException( "'{}' should be a directory with the debugger in it.". format(debugger_path)) debugger_path = str(debugger) return DebugContext( debug_ports=debug_ports, debug_args=debug_args, debugger_path=debugger_path, debug_function=debug_function, container_env_vars=container_env_vars, )
def test_additional_volumes_returns_volume_with_debugger_path_is_set(self, runtime): expected = { "/somepath": {"bind": "/tmp/lambci_debug_files", "mode": "ro"}, } debug_options = DebugContext(debug_ports=[1234], debugger_path="/somepath") result = LambdaContainer._get_additional_volumes(runtime, debug_options) print(result) self.assertEqual(result, expected)
def test_must_return_lambci_image_without_debug(self): debug_options = DebugContext() expected = "lambci/lambda:foo" image_builder = Mock() image_builder.build.return_value = expected self.assertEqual(LambdaContainer._get_image(image_builder, "foo", [], debug_options), expected) image_builder.build.assert_called_with("foo", [], False)
def test_must_return_lambci_image_with_debug(self): debug_options = DebugContext(debug_ports=[1235], debugger_path="a", debug_args="a=b c=d e=f") expected = "lambci/lambda:foo" image_builder = Mock() image_builder.build.return_value = expected self.assertEqual(LambdaContainer._get_image(image_builder, "foo", [], debug_options), expected) image_builder.build.assert_called_with("foo", [], True)
def setUp(self): self.runtime = "nodejs12.x" self.handler = "handler" self.code_dir = "codedir" self.image_config = None self.imageuri = None self.packagetype = ZIP self.env_var = {"var": "value"} self.memory_mb = 1024 self.debug_options = DebugContext( debug_args="a=b c=d e=f", debug_ports=[1235], container_env_vars={"debug_var": "debug_value"} )
def setUp(self): random.seed() self.runtime = "nodejs4.3" self.expected_docker_image = self.IMAGE_NAME self.handler = "index.handler" self.debug_port = _rand_port() self.debug_context = DebugContext(debug_port=self.debug_port, debugger_path=None, debug_args=None) self.code_dir = nodejs_lambda(self.HELLO_WORLD_CODE) self.network_prefix = "sam_cli_test_network" self.docker_client = docker.from_env()
def _get_debug_context(debug_port, debug_args, debugger_path): """ Creates a DebugContext if the InvokeContext is in a debugging mode Parameters ---------- debug_port int Port to bind the debugger to debug_args str Additional arguments passed to the debugger debugger_path str Path to the directory of the debugger to mount on Docker Returns ------- samcli.commands.local.lib.debug_context.DebugContext Object representing the DebugContext Raises ------ samcli.commands.local.cli_common.user_exceptions.DebugContext When the debugger_path is not valid """ if debug_port and debugger_path: try: debugger = Path(debugger_path).resolve(strict=True) except OSError as error: if error.errno == errno.ENOENT: raise DebugContextException( "'{}' could not be found.".format(debugger_path)) else: raise error # We turn off pylint here due to https://github.com/PyCQA/pylint/issues/1660 if not debugger.is_dir(): # pylint: disable=no-member raise DebugContextException( "'{}' should be a directory with the debugger in it.". format(debugger_path)) debugger_path = str(debugger) return DebugContext(debug_port=debug_port, debug_args=debug_args, debugger_path=debugger_path)
def _get_debug_context(debug_ports, debug_args, debugger_path): """ Creates a DebugContext if the InvokeContext is in a debugging mode Parameters ---------- debug_ports tuple(int) Ports to bind the debugger to debug_args str Additional arguments passed to the debugger debugger_path str Path to the directory of the debugger to mount on Docker Returns ------- samcli.commands.local.lib.debug_context.DebugContext Object representing the DebugContext Raises ------ samcli.commands.local.cli_common.user_exceptions.DebugContext When the debugger_path is not valid """ if debug_ports and debugger_path: try: debugger = Path(debugger_path).resolve(strict=True) except OSError as error: if error.errno == errno.ENOENT: raise DebugContextException( "'{}' could not be found.".format(debugger_path)) raise error if not debugger.is_dir(): raise DebugContextException( "'{}' should be a directory with the debugger in it.". format(debugger_path)) debugger_path = str(debugger) return DebugContext(debug_ports=debug_ports, debug_args=debug_args, debugger_path=debugger_path)
def test_must_return_lambci_image_without_debug(self): debug_options = DebugContext() expected = "lambci/lambda:foo" image_builder = Mock() image_builder.build.return_value = expected self.assertEqual( LambdaContainer._get_image( lambda_image=image_builder, runtime="foo", packagetype=ZIP, image=None, layers=[], debug_options=debug_options, ), expected, ) image_builder.build.assert_called_with("foo", ZIP, None, [], False)
def test_must_return_lambci_image_with_debug(self): debug_options = DebugContext(debug_ports=[1235], debugger_path="a", debug_args="a=b c=d e=f") expected = "lambci/lambda:foo" image_builder = Mock() image_builder.build.return_value = expected self.assertEqual( LambdaContainer._get_image( lambda_image=image_builder, runtime="foo", packagetype=ZIP, image=None, layers=[], debug_options=debug_options, ), expected, ) image_builder.build.assert_called_with("foo", ZIP, None, [], True)
def test_no_additional_options_when_debug_options_is_none(self): debug_options = DebugContext(debug_ports=None) result = LambdaContainer._get_additional_options( "runtime", debug_options) self.assertIsNone(result)
def test_must_provide_entrypoint_even_without_debug_args(self, runtime): debug_options = DebugContext(debug_ports=[1235], debug_args=None) result = LambdaContainer._get_entry_point(runtime, debug_options) self.assertIsNotNone(result)
def setUp(self): self.debug_ports = [1235] self.debug_args = "a=b c=d e=f" self.debug_options = DebugContext(debug_ports=[1235], debug_args="a=b c=d e=f")
def test_none_ports_specified(self): debug_options = DebugContext(debug_ports=None) result = LambdaContainer._get_exposed_ports(debug_options) self.assertEqual(None, result)
def test_empty_ports_list(self): debug_options = DebugContext(debug_ports=[]) result = LambdaContainer._get_exposed_ports(debug_options) self.assertEqual(None, result)
def test_bool_truthy(self, port, debug_path, debug_ars): debug_context = DebugContext(port, debug_path, debug_ars) self.assertTrue(debug_context.__bool__()) self.assertTrue(os.environ["PYTHONUNBUFFERED"], "1")
def test_nonzero_falsy(self, port, debug_path, debug_ars): debug_context = DebugContext(port, debug_path, debug_ars) self.assertFalse(debug_context.__nonzero__())
def test_bool_falsy(self, port, debug_path, debug_ars): debug_context = DebugContext(port, debug_path, debug_ars) self.assertFalse(debug_context.__bool__()) self.assertFalse(False, "PYTHONUNBUFFERED" in os.environ.keys())
def test_bool_truthy(self, port, debug_path, debug_ars): debug_context = DebugContext(port, debug_path, debug_ars) self.assertTrue(debug_context.__bool__())
def test_nonzero_thruthy(self, port, debug_path, debug_ars): debug_context = DebugContext(port, debug_path, debug_ars) self.assertTrue(debug_context.__nonzero__())
def test_default_value_returned_for_non_go_runtimes(self, runtime): debug_options = DebugContext(debug_ports=[1235]) result = LambdaContainer._get_additional_options( runtime, debug_options) self.assertEqual(result, {})
def test_no_additional_volumes_when_debuggr_path_is_none(self): debug_options = DebugContext(debug_ports=[1234]) result = LambdaContainer._get_additional_volumes(debug_options) self.assertIsNone(result)
def test_init(self): context = DebugContext("port", "debuggerpath", "debug_args") self.assertEqual(context.debug_ports, "port") self.assertEqual(context.debugger_path, "debuggerpath") self.assertEqual(context.debug_args, "debug_args")