serverless/lib/plugins/aws/utils/formatLambdaLogEvent.test.js
Dan Root add4951a4b Better handling for AWS Lambda logging lines.
Refactor duplicated log line formatting from invoke and logs plugins
into utils/formatLambdaLogEvent.js and add tests.

Make log line formatter understand default python logging module output
from Lambda functions.

Add payload output testing to invoke plugin.

Fixes #1796 and #3594
2017-05-19 12:42:02 -07:00

56 lines
2.2 KiB
JavaScript

'use strict';
const expect = require('chai').expect;
const moment = require('moment');
const chalk = require('chalk');
const os = require('os');
const formatLambdaLogEvent = require('./formatLambdaLogEvent');
describe('#formatLambdaLogEvent()', () => {
it('should format invocation report', () => {
const msg = 'REPORT\tRequestId: 99c30000-b01a-11e5-93f7-b8e85631a00e\tDuration: 0.40 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 30 MB'; // eslint-disable-line
expect(formatLambdaLogEvent(msg)).to.equal(chalk.grey(msg + os.EOL));
});
it('should format invocation failures', () => {
const msg = 'Process exited before completing request';
expect(formatLambdaLogEvent(msg)).to.equal(chalk.red(msg));
});
it('should format lambda console.log lines', () => {
const nodeLogLine = '2016-01-01T12:00:00Z\t99c30000-b01a-11e5-93f7-b8e85631a00e\ttest';
let expectedLogMessage = '';
const momentDate = moment('2016-01-01T12:00:00Z').format('YYYY-MM-DD HH:mm:ss.SSS (Z)');
expectedLogMessage += `${chalk.green(momentDate)}\t`;
expectedLogMessage += `${chalk.yellow('99c30000-b01a-11e5-93f7-b8e85631a00e')}\t`;
expectedLogMessage += 'test';
expect(formatLambdaLogEvent(nodeLogLine)).to.equal(expectedLogMessage);
});
it('should format lambda python logger lines', () => {
const pythonLoggerLine = '[INFO]\t2016-01-01T12:00:00Z\t99c30000-b01a-11e5-93f7-b8e85631a00e\ttest'; // eslint-disable-line
let expectedLogMessage = '';
const momentDate = moment('2016-01-01T12:00:00Z').format('YYYY-MM-DD HH:mm:ss.SSS (Z)');
expectedLogMessage += `${chalk.green(momentDate)}\t`;
expectedLogMessage += `${chalk.yellow('99c30000-b01a-11e5-93f7-b8e85631a00e')}\t`;
expectedLogMessage += `${chalk.white('[INFO]')}\t`;
expectedLogMessage += 'test';
expect(formatLambdaLogEvent(pythonLoggerLine)).to.equal(expectedLogMessage);
});
it('should pass through log lines with no tabs', () => {
expect(formatLambdaLogEvent('test')).to.equal('test');
});
it('should pass through log lines with tabs but no date', () => {
const tabLine = 'foo\tbar\tbaz';
expect(formatLambdaLogEvent(tabLine)).to.equal(tabLine);
});
});