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_empty_event_and_context(self):
     event, context = {}, {}
     logger = CumulusLogger()
     logger.setMetadata(event, context)
     msg = logger.createMessage("empty event and context")
     self.assertEqual(set(msg.keys()),
                      {"version", "sender", "message", "timestamp"})
 def test_parameter_configured_message(self):
     event, context = create_parameter_event(), LambdaContextMock()
     logger = CumulusLogger()
     logger.setMetadata(event, context)
     msg = logger.createMessage("test parameter event")
     self.assertEqual(msg["sender"], context.function_name)
     self.assertEqual(msg["version"], context.function_version)
     self.assertEqual(
         msg["executions"],
         event["cma"]["event"]["cumulus_meta"]["execution_name"])
     self.assertEqual(
         msg["asyncOperationId"],
         event["cma"]["event"]["cumulus_meta"]["asyncOperationId"])
     self.assertEqual(
         msg["granules"],
         json.dumps([granule["granuleId"]
                     for granule in event["cma"]["event"]["payload"]["granules"]]))
     self.assertEqual(
         msg["parentArn"],
         event["cma"]["event"]["cumulus_meta"]["parentExecutionArn"])
     self.assertEqual(
         msg["stackName"],
         event["cma"]["event"]["meta"]["stack"])
     self.assertEqual(msg["message"], "test parameter event")
     logger.info("test parameter configured message")
 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")
Exemplo n.º 6
0
def run_cumulus_task(task_function,
                     cumulus_message,
                     context=None,
                     schemas=None,
                     **taskargs):
    """
    Interprets incoming messages, passes them to an inner handler, gets the
    response and transforms it into an outgoing message, returned by Lambda.

    Arguments:
        task_function -- Required. The function containing the business logic
            of the cumulus task
        cumulus_message -- Required. Either a full Cumulus Message or a Cumulus
            Remote Message
        context -- AWS Lambda context object
        schemas -- Optional. A dict with filepaths of `input`, `config`, and
            `output` schemas that are relative to the task root directory. All
            three properties of this dict are optional. If omitted, the message
            adapter will look in `/<task_root>/schemas/<schema_type>.json`, and
            if not found there, will be ignored.
        taskargs -- Optional. Additional keyword arguments for the
            task_function
    """

    set_sys_path()
    from message_adapter.message_adapter import MessageAdapter

    context_dict = vars(context) if context else {}
    logger = CumulusLogger()
    logger.setMetadata(cumulus_message, context)
    message_adapter_disabled = str(
        os.environ.get('CUMULUS_MESSAGE_ADAPTER_DISABLED')).lower()

    if message_adapter_disabled == 'true':
        try:
            return task_function(cumulus_message, context, **taskargs)
        except Exception as exception:
            name = exception.args[0]
            if isinstance(name, str) and 'WorkflowError' in name:
                cumulus_message['payload'] = None
                cumulus_message['exception'] = name
                logger.error('WorkflowError')
                return cumulus_message
            logger.error(exception)
            raise

    adapter = MessageAdapter(schemas)
    full_event = adapter.load_and_update_remote_event(cumulus_message,
                                                      context_dict)
    nested_event = adapter.load_nested_event(full_event, context_dict)
    message_config = nested_event.get('messageConfig', {})

    try:
        task_response = task_function(nested_event, context, **taskargs)
    except Exception as exception:
        name = exception.args[0]
        if isinstance(name, str) and 'WorkflowError' in name:
            cumulus_message['payload'] = None
            cumulus_message['exception'] = name
            logger.error('WorkflowError')
            return cumulus_message
        logger.error(exception)
        raise

    return adapter.create_next_event(task_response, full_event, message_config)