more linting

This commit is contained in:
Gareth Jones 2013-05-27 08:17:32 +10:00
parent 2cd27e4293
commit 07869b915f

View File

@ -1,138 +1,139 @@
"use strict";
var vows = require('vows') var vows = require('vows')
, assert = require('assert') , assert = require('assert')
, sandbox = require('sandboxed-module') , sandbox = require('sandboxed-module')
, log4js = require('../lib/log4js') , log4js = require('../lib/log4js')
, setupLogging = function(options, category, compressedLength) { , setupLogging = function(options, category, compressedLength) {
var fakeDgram = { var fakeDgram = {
sent: false, sent: false,
socket: { socket: {
packetLength: 0, packetLength: 0,
close: function() { close: function() {
}, },
send: function(pkt, offset, pktLength, port, host) { send: function(pkt, offset, pktLength, port, host) {
fakeDgram.sent = true; fakeDgram.sent = true;
this.packet = pkt; this.packet = pkt;
this.offset = offset; this.offset = offset;
this.packetLength = pktLength; this.packetLength = pktLength;
this.port = port; this.port = port;
this.host = host; this.host = host;
}
},
createSocket: function(type) {
this.type = type;
return this.socket;
}
}
, fakeZlib = {
gzip: function(objectToCompress, callback) {
fakeZlib.uncompressed = objectToCompress;
if (compressedLength) {
callback(null, { length: compressedLength });
} else {
callback(null, "I've been compressed");
}
} }
},
createSocket: function(type) {
this.type = type;
return this.socket;
}
}
, fakeZlib = {
gzip: function(objectToCompress, callback) {
fakeZlib.uncompressed = objectToCompress;
if (compressedLength) {
callback(null, { length: compressedLength });
} else {
callback(null, "I've been compressed");
}
}
} }
, appender = sandbox.require('../lib/appenders/gelf', { , appender = sandbox.require('../lib/appenders/gelf', {
requires: { requires: {
dgram: fakeDgram, dgram: fakeDgram,
zlib: fakeZlib zlib: fakeZlib
} }
}); });
log4js.clearAppenders(); log4js.clearAppenders();
log4js.addAppender(appender.configure(options || {}), category || "gelf-test"); log4js.addAppender(appender.configure(options || {}), category || "gelf-test");
return { return {
dgram: fakeDgram, dgram: fakeDgram,
compress: fakeZlib, compress: fakeZlib,
logger: log4js.getLogger(category || "gelf-test") logger: log4js.getLogger(category || "gelf-test")
}; };
}; };
//log4js.configure({ doNotReplaceConsole: true }); //log4js.configure({ doNotReplaceConsole: true });
vows.describe('log4js gelfAppender').addBatch({ vows.describe('log4js gelfAppender').addBatch({
'with default gelfAppender settings': { 'with default gelfAppender settings': {
topic: function() { topic: function() {
var setup = setupLogging(); var setup = setupLogging();
setup.logger.info("This is a test"); setup.logger.info("This is a test");
return setup; return setup;
},
'the dgram packet': {
topic: function(setup) {
return setup.dgram;
},
'should be sent 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 be compressed': function(dgram) {
assert.equal(dgram.socket.packet, "I've been compressed");
}
},
'the uncompressed log message': {
topic: function(setup) {
var message = JSON.parse(setup.compress.uncompressed);
return message;
},
'should be in the gelf format': function(message) {
assert.equal(message.version, '1.0');
assert.equal(message.host, require('os').hostname());
assert.equal(message.level, 6); //INFO
assert.equal(message.facility, 'nodejs-server');
assert.equal(message.full_message, message.short_message);
assert.equal(message.full_message, 'This is a test');
}
}
}, },
'with a message longer than 8k': { 'the dgram packet': {
topic: function() { topic: function(setup) {
var setup = setupLogging(undefined, undefined, 10240); return setup.dgram;
setup.logger.info("Blah."); },
return setup; 'should be sent via udp to the localhost gelf server': function(dgram) {
}, assert.equal(dgram.type, "udp4");
'the dgram packet': { assert.equal(dgram.socket.host, "localhost");
topic: function(setup) { assert.equal(dgram.socket.port, 12201);
return setup.dgram; assert.equal(dgram.socket.offset, 0);
}, assert.ok(dgram.socket.packetLength > 0, "Received blank message");
'should not be sent': function(dgram) { },
assert.equal(dgram.sent, false); 'should be compressed': function(dgram) {
} assert.equal(dgram.socket.packet, "I've been compressed");
} }
}, },
'with non-default options': { 'the uncompressed log message': {
topic: function() { topic: function(setup) {
var setup = setupLogging({ var message = JSON.parse(setup.compress.uncompressed);
host: 'somewhere', return message;
port: 12345, },
hostname: 'cheese', 'should be in the gelf format': function(message) {
facility: 'nonsense' assert.equal(message.version, '1.0');
}); assert.equal(message.host, require('os').hostname());
setup.logger.debug("Just testing."); assert.equal(message.level, 6); //INFO
return setup; assert.equal(message.facility, 'nodejs-server');
}, assert.equal(message.full_message, message.short_message);
'the dgram packet': { assert.equal(message.full_message, 'This is a test');
topic: function(setup) { }
return setup.dgram;
},
'should pick up the options': function(dgram) {
assert.equal(dgram.socket.host, 'somewhere');
assert.equal(dgram.socket.port, 12345);
}
},
'the uncompressed packet': {
topic: function(setup) {
var message = JSON.parse(setup.compress.uncompressed);
return message;
},
'should pick up the options': function(message) {
assert.equal(message.host, 'cheese');
assert.equal(message.facility, 'nonsense');
}
}
} }
}).export(module); },
'with a message longer than 8k': {
topic: function() {
var setup = setupLogging(undefined, undefined, 10240);
setup.logger.info("Blah.");
return setup;
},
'the dgram packet': {
topic: function(setup) {
return setup.dgram;
},
'should not be sent': function(dgram) {
assert.equal(dgram.sent, false);
}
}
},
'with non-default options': {
topic: function() {
var setup = setupLogging({
host: 'somewhere',
port: 12345,
hostname: 'cheese',
facility: 'nonsense'
});
setup.logger.debug("Just testing.");
return setup;
},
'the dgram packet': {
topic: function(setup) {
return setup.dgram;
},
'should pick up the options': function(dgram) {
assert.equal(dgram.socket.host, 'somewhere');
assert.equal(dgram.socket.port, 12345);
}
},
'the uncompressed packet': {
topic: function(setup) {
var message = JSON.parse(setup.compress.uncompressed);
return message;
},
'should pick up the options': function(message) {
assert.equal(message.host, 'cheese');
assert.equal(message.facility, 'nonsense');
}
}
}
}).export(module);