From 8f3a2b9bee5db48ef8ef2675c047c96d08f96dfe Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Wed, 12 Aug 2020 10:51:04 +0200 Subject: [PATCH] AWS Lambda: Safeties in Lambda Handler parsing --- instana/__init__.py | 4 ++-- tests/platforms/test_lambda.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/instana/__init__.py b/instana/__init__.py index 860f1aea..11353224 100644 --- a/instana/__init__.py +++ b/instana/__init__.py @@ -61,8 +61,8 @@ def get_lambda_handler_or_default(): if handler: parts = handler.split(".") - handler_function = parts.pop() - handler_module = ".".join(parts) + handler_function = parts.pop().strip() + handler_module = ".".join(parts).strip() except Exception: pass diff --git a/tests/platforms/test_lambda.py b/tests/platforms/test_lambda.py index 9fb1d25e..4aa68ce2 100644 --- a/tests/platforms/test_lambda.py +++ b/tests/platforms/test_lambda.py @@ -128,6 +128,26 @@ def test_get_handler(self): self.assertEqual("tests", handler_module) self.assertEqual("lambda_handler", handler_function) + def test_get_handler_with_multi_subpackages(self): + os.environ["LAMBDA_HANDLER"] = "tests.one.two.three.lambda_handler" + handler_module, handler_function = get_lambda_handler_or_default() + + self.assertEqual("tests.one.two.three", handler_module) + self.assertEqual("lambda_handler", handler_function) + + def test_get_handler_with_space_in_it(self): + os.environ["LAMBDA_HANDLER"] = " tests.another_module.lambda_handler" + handler_module, handler_function = get_lambda_handler_or_default() + + self.assertEqual("tests.another_module", handler_module) + self.assertEqual("lambda_handler", handler_function) + + os.environ["LAMBDA_HANDLER"] = "tests.another_module.lambda_handler " + handler_module, handler_function = get_lambda_handler_or_default() + + self.assertEqual("tests.another_module", handler_module) + self.assertEqual("lambda_handler", handler_function) + def test_agent_extra_http_headers(self): os.environ['INSTANA_EXTRA_HTTP_HEADERS'] = "X-Test-Header;X-Another-Header;X-And-Another-Header" self.create_agent_and_setup_tracer() @@ -590,4 +610,4 @@ def test_agent_default_log_level(self): def test_agent_custom_log_level(self): os.environ['INSTANA_LOG_LEVEL'] = "eRror" self.create_agent_and_setup_tracer() - assert self.agent.options.log_level == logging.ERROR \ No newline at end of file + assert self.agent.options.log_level == logging.ERROR