From 0a422e5749288c18418f15a6e2a3cb36fe88f9a0 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Fri, 10 Feb 2012 18:14:50 +1100 Subject: [PATCH] fixed up gelf tests --- lib/appenders/gelf.js | 6 +- test/gelfAppender.js | 63 +++++++--- test/smtpAppender.js | 286 +++++++++++++++++++++--------------------- 3 files changed, 191 insertions(+), 164 deletions(-) diff --git a/lib/appenders/gelf.js b/lib/appenders/gelf.js index e255681..e0c7fc2 100644 --- a/lib/appenders/gelf.js +++ b/lib/appenders/gelf.js @@ -13,7 +13,7 @@ var util = require('util'); * @param hostname - hostname of the current host (default:os hostname) * @param facility - facility to log to (default:nodejs-server) */ -function gelfAppender (layout, host, port, hostname, facility, externalClient) { +function gelfAppender (layout, host, port, hostname, facility) { var logEventBuffer = []; @@ -42,7 +42,7 @@ function gelfAppender (layout, host, port, hostname, facility, externalClient) { facility = facility || 'nodejs-server'; layout = layout || layouts.patternLayout('%m'); - var client = externalClient || dgram.createSocket("udp4"); + var client = dgram.createSocket("udp4"); process.on('exit', function() { if (client) client.close(); @@ -90,7 +90,7 @@ function configure(config) { if (config.layout) { layout = layouts.layout(config.layout.type, config.layout); } - return gelfAppender(layout, config.host, config.port, config.hostname, config.facility, config.client); + return gelfAppender(layout, config.host, config.port, config.hostname, config.facility); } exports.name = "gelf"; diff --git a/test/gelfAppender.js b/test/gelfAppender.js index 82b7ed6..8257766 100644 --- a/test/gelfAppender.js +++ b/test/gelfAppender.js @@ -1,30 +1,57 @@ var vows = require('vows') -, log4js = require('../lib/log4js') , assert = require('assert') -, dgram = require("dgram"); +, sandbox = require('sandboxed-module') +, fakeDgram = { + socket: { + packetLength: 0, + close: function() { + }, + send: function(pkt, offset, pktLength, port, host) { + this.packet = pkt; + this.offset = offset; + this.packetLength = pktLength; + this.port = port; + this.host = host; + } + }, + createSocket: function(type) { + this.type = type; + return this.socket; + } +} +, fakeCompressBuffer = { + compress: function(objectToCompress) { + this.uncompressed = objectToCompress; + return "I've been compressed"; + } +} +, appender = sandbox.require('../lib/appenders/gelf', { + requires: { + dgram: fakeDgram, + "compress-buffer": fakeCompressBuffer + } +}) +, log4js = require('../lib/log4js'); -var fakeClient = { - packetLength: 0, - close: function() { - }, - send: function(pkt, offset, pktLength, port, host) { - this.packetLength = pktLength; - } -}; - -log4js.configure({ "appenders": [{"type": "gelf", "client": fakeClient}] }, undefined); +log4js.clearAppenders(); +log4js.addAppender(appender.configure({}), "gelf-test"); vows.describe('log4js gelfAppender').addBatch({ 'with default gelfAppender settings': { topic: function() { - var logger = log4js.getLogger(); - var self = this; - logger.info('Fake log message'); - callback(); + log4js.getLogger("gelf-test").info("This is a test"); + return fakeDgram; }, - 'should receive log messages at the local gelf server': function(err, packet) { - assert.ok(fakeClient.packetLength > 0, "Recevied blank message"); + 'should send log messages via udp to the localhost gelf server': function(dgram) { + assert.equal(dgram.type, "udp4"); + assert.equal(dgram.socket.host, "localhost"); + assert.equal(dgram.socket.port, 12201); + assert.equal(dgram.socket.offset, 0); + assert.ok(dgram.socket.packetLength > 0, "Received blank message"); + }, + 'should compress the log message': function(dgram) { + assert.equal(dgram.socket.packet, "I've been compressed"); } } }).export(module); \ No newline at end of file diff --git a/test/smtpAppender.js b/test/smtpAppender.js index 959c579..0eca594 100644 --- a/test/smtpAppender.js +++ b/test/smtpAppender.js @@ -1,162 +1,162 @@ var vows = require('vows'), - assert = require('assert'), - log4js = require('../lib/log4js'), - sandbox = require('sandboxed-module'); + assert = require('assert'), + log4js = require('../lib/log4js'), + sandbox = require('sandboxed-module'); function setupLogging(category, options) { - var msgs = []; - - var fakeMailer = { - send_mail: function (msg, callback) { - msgs.push(msg); - callback(null, true); - } - }; + var msgs = []; - var smtpModule = sandbox.require('../lib/appenders/smtp', { - requires: { - 'nodemailer': fakeMailer - } - }); + var fakeMailer = { + send_mail: function (msg, callback) { + msgs.push(msg); + callback(null, true); + } + }; - log4js.addAppender(smtpModule.configure(options), category); - - return { - logger: log4js.getLogger(category), - mailer: fakeMailer, - results: msgs - }; + var smtpModule = sandbox.require('../lib/appenders/smtp', { + requires: { + 'nodemailer': fakeMailer + } + }); + + log4js.addAppender(smtpModule.configure(options), category); + + return { + logger: log4js.getLogger(category), + mailer: fakeMailer, + results: msgs + }; } function checkMessages (result, sender, subject) { - for (var i = 0; i < result.results.length; ++i) { - assert.equal(result.results[i].sender, sender ? sender : result.mailer.SMTP.user); - assert.equal(result.results[i].to, 'recipient@domain.com'); - assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1)); - assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].body)); - } + for (var i = 0; i < result.results.length; ++i) { + assert.equal(result.results[i].sender, sender ? sender : result.mailer.SMTP.user); + assert.equal(result.results[i].to, 'recipient@domain.com'); + assert.equal(result.results[i].subject, subject ? subject : 'Log event #' + (i+1)); + assert.ok(new RegExp('.+Log event #' + (i+1) + '\n$').test(result.results[i].body)); + } } log4js.clearAppenders(); vows.describe('log4js smtpAppender').addBatch({ - 'minimal config': { - topic: function() { - var setup = setupLogging('minimal config', { - recipients: 'recipient@domain.com', - smtp: { - port: 25, - user: 'user@domain.com' - } - }); - setup.logger.info('Log event #1'); - return setup; - }, - 'mailer should be configured properly': function (result) { - assert.ok(result.mailer.SMTP); - assert.equal(result.mailer.SMTP.port, 25); - assert.equal(result.mailer.SMTP.user, 'user@domain.com'); - }, - 'there should be one message only': function (result) { - assert.equal(result.results.length, 1); - }, - 'message should contain proper data': function (result) { - checkMessages(result); + 'minimal config': { + topic: function() { + var setup = setupLogging('minimal config', { + recipients: 'recipient@domain.com', + smtp: { + port: 25, + user: 'user@domain.com' } + }); + setup.logger.info('Log event #1'); + return setup; }, - 'fancy config': { - topic: function() { - var setup = setupLogging('fancy config', { - recipients: 'recipient@domain.com', - sender: 'sender@domain.com', - subject: 'This is subject', - smtp: { - port: 25, - user: 'user@domain.com' - } - }); - setup.logger.info('Log event #1'); - return setup; - }, - 'mailer should be configured properly': function (result) { - assert.ok(result.mailer.SMTP); - assert.equal(result.mailer.SMTP.port, 25); - assert.equal(result.mailer.SMTP.user, 'user@domain.com'); - }, - 'there should be one message only': function (result) { - assert.equal(result.results.length, 1); - }, - 'message should contain proper data': function (result) { - checkMessages(result, 'sender@domain.com', 'This is subject'); - } + 'mailer should be configured properly': function (result) { + assert.ok(result.mailer.SMTP); + assert.equal(result.mailer.SMTP.port, 25); + assert.equal(result.mailer.SMTP.user, 'user@domain.com'); }, - 'separate email for each event': { - topic: function() { - var self = this; - var setup = setupLogging('separate email for each event', { - recipients: 'recipient@domain.com', - smtp: { - port: 25, - user: 'user@domain.com' - } - }); - setTimeout(function () { - setup.logger.info('Log event #1'); - }, 0); - setTimeout(function () { - setup.logger.info('Log event #2'); - }, 500); - setTimeout(function () { - setup.logger.info('Log event #3'); - }, 1050); - setTimeout(function () { - self.callback(null, setup); - }, 2100); - }, - 'there should be three messages': function (result) { - assert.equal(result.results.length, 3); - }, - 'messages should contain proper data': function (result) { - checkMessages(result); - } + 'there should be one message only': function (result) { + assert.equal(result.results.length, 1); }, - 'multiple events in one email': { - topic: function() { - var self = this; - var setup = setupLogging('multiple events in one email', { - recipients: 'recipient@domain.com', - sendInterval: 1, - smtp: { - port: 25, - user: 'user@domain.com' - } - }); - setTimeout(function () { - setup.logger.info('Log event #1'); - }, 0); - setTimeout(function () { - setup.logger.info('Log event #2'); - }, 500); - setTimeout(function () { - setup.logger.info('Log event #3'); - }, 1050); - setTimeout(function () { - self.callback(null, setup); - }, 2100); - }, - 'there should be two messages': function (result) { - assert.equal(result.results.length, 2); - }, - 'messages should contain proper data': function (result) { - assert.equal(result.results[0].sender, result.mailer.SMTP.user); - assert.equal(result.results[0].to, 'recipient@domain.com'); - assert.equal(result.results[0].subject, 'Log event #1'); - assert.equal(result.results[0].body.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2); - - assert.equal(result.results[1].sender, result.mailer.SMTP.user); - assert.equal(result.results[1].to, 'recipient@domain.com'); - assert.equal(result.results[1].subject, 'Log event #3'); - assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].body)); - } + 'message should contain proper data': function (result) { + checkMessages(result); } + }, + 'fancy config': { + topic: function() { + var setup = setupLogging('fancy config', { + recipients: 'recipient@domain.com', + sender: 'sender@domain.com', + subject: 'This is subject', + smtp: { + port: 25, + user: 'user@domain.com' + } + }); + setup.logger.info('Log event #1'); + return setup; + }, + 'mailer should be configured properly': function (result) { + assert.ok(result.mailer.SMTP); + assert.equal(result.mailer.SMTP.port, 25); + assert.equal(result.mailer.SMTP.user, 'user@domain.com'); + }, + 'there should be one message only': function (result) { + assert.equal(result.results.length, 1); + }, + 'message should contain proper data': function (result) { + checkMessages(result, 'sender@domain.com', 'This is subject'); + } + }, + 'separate email for each event': { + topic: function() { + var self = this; + var setup = setupLogging('separate email for each event', { + recipients: 'recipient@domain.com', + smtp: { + port: 25, + user: 'user@domain.com' + } + }); + setTimeout(function () { + setup.logger.info('Log event #1'); + }, 0); + setTimeout(function () { + setup.logger.info('Log event #2'); + }, 500); + setTimeout(function () { + setup.logger.info('Log event #3'); + }, 1050); + setTimeout(function () { + self.callback(null, setup); + }, 2100); + }, + 'there should be three messages': function (result) { + assert.equal(result.results.length, 3); + }, + 'messages should contain proper data': function (result) { + checkMessages(result); + } + }, + 'multiple events in one email': { + topic: function() { + var self = this; + var setup = setupLogging('multiple events in one email', { + recipients: 'recipient@domain.com', + sendInterval: 1, + smtp: { + port: 25, + user: 'user@domain.com' + } + }); + setTimeout(function () { + setup.logger.info('Log event #1'); + }, 0); + setTimeout(function () { + setup.logger.info('Log event #2'); + }, 500); + setTimeout(function () { + setup.logger.info('Log event #3'); + }, 1050); + setTimeout(function () { + self.callback(null, setup); + }, 2100); + }, + 'there should be two messages': function (result) { + assert.equal(result.results.length, 2); + }, + 'messages should contain proper data': function (result) { + assert.equal(result.results[0].sender, result.mailer.SMTP.user); + assert.equal(result.results[0].to, 'recipient@domain.com'); + assert.equal(result.results[0].subject, 'Log event #1'); + assert.equal(result.results[0].body.match(new RegExp('.+Log event #[1-2]$', 'gm')).length, 2); + + assert.equal(result.results[1].sender, result.mailer.SMTP.user); + assert.equal(result.results[1].to, 'recipient@domain.com'); + assert.equal(result.results[1].subject, 'Log event #3'); + assert.ok(new RegExp('.+Log event #3\n$').test(result.results[1].body)); + } + } }).export(module);