def test_error_message(self): event, context = create_event(), LambdaContextMock() logger = CumulusLogger() logger.setMetadata(event, context) try: 1 / 0 except ZeroDivisionError as ex: msg = logger.createMessage("test exc_info", exc_info=False) self.assertIn("test exc_info", msg["message"]) self.assertNotIn("ZeroDivisionError", msg["message"]) logger.error("test exc_info", exc_info=False) msg = logger.createMessage( "test formatted {} exc_info ", "bar", exc_info=True) self.assertIn("test formatted bar exc_info", msg["message"]) self.assertIn("ZeroDivisionError", msg["message"]) logger.warn("test formatted {} exc_info ", "bar", exc_info=True) msg = logger.createMessage( "test exc_info", exc_info=sys.exc_info()) self.assertIn("test exc_info", msg["message"]) self.assertIn("ZeroDivisionError", msg["message"]) logger.fatal("test exc_info", exc_info=sys.exc_info()) msg = logger.createMessage("test exc_info", exc_info=ex) self.assertIn("test exc_info", msg["message"]) self.assertIn("ZeroDivisionError", msg["message"]) logger.trace("test exc_info", exc_info=ex)
def test_formatted_message(self): event, context = create_event(), LambdaContextMock() logger = CumulusLogger() logger.setMetadata(event, context) msg = logger.createMessage("test formatted {} {}", "foo", "bar") self.assertEqual(msg["message"], "test formatted foo bar") logger.debug("test formatted {} {}", "foo", "bar")
def test_simple_handler_without_context(self): def handler_fn(event, context): return event test_event = create_event() response = run_cumulus_task(handler_fn, test_event) self.assertIsNotNone(response) self.assertEqual(response['cumulus_meta']['task'], 'Example') self.assertEqual(response['payload']['input']['anykey'], 'anyvalue')
def test_workflow_error(self): def workflow_error_fn(event, context): raise Exception('SomeWorkflowError') test_event = create_event() context = LambdaContextMock() response = run_cumulus_task(workflow_error_fn, test_event, context) self.assertIsNone(response['payload']) self.assertEqual(response['exception'], 'SomeWorkflowError')
def test_logger_name_loglevel(self): event, context = create_event(), LambdaContextMock() logger = CumulusLogger('logger_test', logging.INFO) logger.setMetadata(event, context) self.assertTrue(logger.logger.getEffectiveLevel() == logging.INFO) logger.debug("test logging level debug") logger.info("test logging level info") logger.warning("test logging level warning")
def test_simple_handler(self): def handler_fn(event, context): return event test_event = create_event() context = LambdaContextMock() response = run_cumulus_task(handler_fn, test_event, context) self.assertIsNotNone(response) self.assertEqual(response['cumulus_meta']['task'], 'Example') # payload includes the entire event self.assertEqual(response['payload']['input']['anykey'], 'anyvalue')
def test_other_error(self): def other_error_fn(event, context): raise Exception('SomeError') test_event = create_event() context = LambdaContextMock() try: run_cumulus_task(other_error_fn, test_event, context) except Exception as exception: name = exception.args[0] self.assertEqual(name, 'SomeError')
def test_message_adapter_disabled(self): def disabled_adapter_handler_fn(event, context): return {"message": "hello"} os.environ['CUMULUS_MESSAGE_ADAPTER_DISABLED'] = 'true' test_event = create_event() context = LambdaContextMock() response = run_cumulus_task(disabled_adapter_handler_fn, test_event, context) self.assertEqual(response["message"], "hello")
def test_task_function_with_additional_arguments(self): taskargs = {"taskArgOne": "one", "taskArgTwo": "two"} def handler_fn(event, context, taskArgOne, taskArgTwo): self.assertEqual(taskArgOne, taskargs['taskArgOne']) self.assertEqual(taskArgTwo, taskargs['taskArgTwo']) return event test_event = create_event() response = run_cumulus_task(handler_fn, test_event, **taskargs) self.assertIsNotNone(response) self.assertEqual(response['cumulus_meta']['task'], 'Example') self.assertEqual(response['payload']['input']['anykey'], 'anyvalue')
def test_simple_message(self): event, context = create_event(), LambdaContextMock() logger = CumulusLogger() logger.setMetadata(event, context) msg = logger.createMessage("test simple") self.assertEqual(msg["sender"], context.function_name) self.assertEqual(msg["version"], context.function_version) self.assertEqual( msg["executions"], event["cumulus_meta"]["execution_name"]) self.assertEqual( msg["asyncOperationId"], event["cumulus_meta"]["asyncOperationId"]) self.assertEqual( msg["granules"], json.dumps([granule["granuleId"] for granule in event["meta"]["input_granules"]])) self.assertEqual( msg["parentArn"], event["cumulus_meta"]["parentExecutionArn"]) self.assertEqual(msg["stackName"], event["meta"]["stack"]) self.assertEqual(msg["message"], "test simple") logger.info("test simple")