Skip to content
Browse files

finished unit tests for log.syslog and added always_ok option.

  • Loading branch information...
1 parent c1ecd3f commit fddbe8101a6b9c964513bdf08f250d9fa8e6f8ae @godsflaw godsflaw committed
Showing with 112 additions and 148 deletions.
  1. +1 −0 config/log.syslog.ini
  2. +8 −0 docs/plugins/log.syslog.md
  3. +20 −11 plugins/log.syslog.js
  4. +83 −137 tests/plugins/log.syslog.js
View
1 config/log.syslog.ini
@@ -6,3 +6,4 @@ odelay=1
cons=0
ndelay=0
nowait=0
+always_ok=false
View
8 docs/plugins/log.syslog.md
@@ -61,3 +61,11 @@ chosen for you.
Don't wait for child processes that may have been created while
logging the message.
+
+
+* log.syslog.general.always_ok (default: false)
+
+ If false, then this plugin will return with just next() allowing other
+ plugins that have registered for the log hook to run. To speed things up,
+ if no other log hooks need to run (daemon), then one can make this true.
+ This will case the plugin to always call next(OK).
View
31 plugins/log.syslog.js
@@ -1,18 +1,21 @@
require('function-bind');
// send logs to syslog
-var Syslog = require('node-syslog');
+var Syslog = exports.Syslog = require('node-syslog');
exports.register = function() {
- var options = 0;
- var ini = this.config.get('log.syslog.ini');
- var name = ini.general && (ini.general['name'] || 'haraka');
- var facility = ini.general && (ini.general['facility'] || 'MAIL');
- var pid = ini.general && (ini.general['log_pid'] || 1);
- var odelay = ini.general && (ini.general['log_odelay'] || 1);
- var cons = ini.general && (ini.general['log_cons'] || 0);
- var ndelay = ini.general && (ini.general['log_ndelay'] || 0);
- var nowait = ini.general && (ini.general['log_nowait'] || 0);
+ var options = 0;
+ var ini = this.config.get('log.syslog.ini');
+ var name = ini.general && (ini.general['name'] || 'haraka');
+ var facility = ini.general && (ini.general['facility'] || 'MAIL');
+ var pid = ini.general && (ini.general['log_pid'] || 1);
+ var odelay = ini.general && (ini.general['log_odelay'] || 1);
+ var cons = ini.general && (ini.general['log_cons'] || 0);
+ var ndelay = ini.general && (ini.general['log_ndelay'] || 0);
+ var nowait = ini.general && (ini.general['log_nowait'] || 0);
+ var always_ok = ini.general && (ini.general['always_ok'] || false);
+
+ this.always_ok = always_ok;
if (pid)
options |= Syslog.LOG_PID;
@@ -89,6 +92,8 @@ exports.register = function() {
};
exports.syslog = function (next, logger, log) {
+ var plugin = this;
+
switch(log.level.toUpperCase()) {
case 'INFO':
Syslog.log(Syslog.LOG_INFO, log.data);
@@ -118,5 +123,9 @@ exports.syslog = function (next, logger, log) {
Syslog.log(Syslog.LOG_DEBUG, log.data);
}
- return next();
+ if (plugin.always_ok) {
+ return next(OK);
+ } else {
+ return next();
+ }
};
View
220 tests/plugins/log.syslog.js
@@ -2,6 +2,7 @@ require('function-bind.js');
var stub = require('tests/fixtures/stub'),
constants = require('../../constants'),
+ util = require('util'),
Logger = require('tests/fixtures/stub_logger'),
Plugin = require('tests/fixtures/stub_plugin');
@@ -17,29 +18,32 @@ function _set_up(callback) {
// backup modifications
this.backup.plugin = {};
+ this.backup.plugin.Syslog = {};
this.backup.plugin.register_hook = this.plugin.register_hook;
// stub out functions
this.plugin.register_hook = stub();
this.plugin.config = stub();
this.log = stub();
- this.log.level = stub();
- this.log.level.toUpperCase = stub();
+ this.log.level = 'info';
+ this.log.data = "this is a test log message";
// some test data
- this.plugin.config.get = function (file) {
- return {
- general : {
- name : 'haraka',
- facility : 'MAIL',
- log_pid : 1,
- log_odelay : 1,
- log_cons : 0,
- log_ndelay : 0,
- log_nowait : 0
- }
- };
+ this.configfile = {
+ general : {
+ name : 'haraka',
+ facility : 'MAIL',
+ log_pid : 1,
+ log_odelay : 1,
+ log_cons : 0,
+ log_ndelay : 0,
+ log_nowait : 0,
+ always_ok : false
+ }
};
+ this.plugin.config.get = function (file) {
+ return this.configfile;
+ }.bind(this);
// going to need these in multiple tests
this.plugin.register();
@@ -80,7 +84,39 @@ exports.log_syslog = {
test.isFunction(this.plugin.syslog);
test.done();
},
- 'hook always returns next()' : function (test) {
+ 'register calls Syslog.init()' : function (test) {
+ // local setup
+ this.backup.plugin.Syslog.init = this.plugin.Syslog.init;
+ this.plugin.Syslog.init = stub();
+ this.plugin.register();
+
+ test.expect(1);
+ test.ok(this.plugin.Syslog.init.called);
+ test.done();
+
+ // local teardown
+ this.plugin.Syslog.init = this.backup.plugin.Syslog.init;
+ },
+ 'register calls Syslog.init() with correct args' : function (test) {
+ // local setup
+ this.backup.plugin.Syslog.init = this.plugin.Syslog.init;
+ this.plugin.Syslog.init = stub();
+ this.plugin.register();
+
+ test.expect(4);
+ test.ok(this.plugin.Syslog.init.called);
+ test.equals(this.plugin.Syslog.init.args[0],
+ this.plugin.config.get("test").general.name);
+ test.equals(this.plugin.Syslog.init.args[1],
+ this.plugin.Syslog.LOG_PID | this.plugin.Syslog.LOG_ODELAY);
+ test.equals(this.plugin.Syslog.init.args[2],
+ this.plugin.Syslog.LOG_MAIL);
+ test.done();
+
+ // local teardown
+ this.plugin.Syslog.init = this.backup.plugin.Syslog.init;
+ },
+ 'hook returns just next() if configured to do so' : function (test) {
var next = function (action) {
test.expect(1);
test.isUndefined(action);
@@ -88,128 +124,38 @@ exports.log_syslog = {
};
this.plugin.syslog(next, this.logger, this.log);
- }
-};
+ },
+ 'hook returns next(OK) if configured to do so' : function (test) {
+ // local setup
+ this.backup.configfile = this.configfile;
+ this.configfile.general.always_ok = true;
+ this.plugin.register();
+ var next = function (action) {
+ test.expect(1);
+ test.equals(action, constants.ok);
+ test.done();
+ };
+ this.plugin.syslog(next, this.logger, this.log);
-// send logs to syslog
-//var Syslog = require('node-syslog');
-//
-//exports.register = function() {
-// var options = 0;
-// var ini = this.config.get('log.syslog.ini');
-// var name = ini.general && (ini.general['name'] || 'haraka');
-// var facility = ini.general && (ini.general['facility'] || 'MAIL');
-// var pid = ini.general && (ini.general['log_pid'] || 1);
-// var odelay = ini.general && (ini.general['log_odelay'] || 1);
-// var cons = ini.general && (ini.general['log_cons'] || 0);
-// var ndelay = ini.general && (ini.general['log_ndelay'] || 0);
-// var nowait = ini.general && (ini.general['log_nowait'] || 0);
-//
-// if (pid)
-// options |= Syslog.LOG_PID;
-//
-// if (odelay)
-// options |= Syslog.LOG_ODELAY;
-//
-// if (cons)
-// options |= Syslog.LOG_CONS;
-//
-// if (ndelay)
-// options |= Syslog.LOG_NDELAY;
-//
-// if (nowait)
-// options |= Syslog.LOG_NOWAIT;
-//
-// switch(facility.toUpperCase()) {
-// case 'MAIL':
-// Syslog.init(name, options, Syslog.LOG_MAIL);
-// break;
-// case 'KERN':
-// Syslog.init(name, options, Syslog.LOG_KERN);
-// break;
-// case 'USER':
-// Syslog.init(name, options, Syslog.LOG_USER);
-// break;
-// case 'DAEMON':
-// Syslog.init(name, options, Syslog.LOG_DAEMON);
-// break;
-// case 'AUTH':
-// Syslog.init(name, options, Syslog.LOG_AUTH);
-// break;
-// case 'SYSLOG':
-// Syslog.init(name, options, Syslog.LOG_SYSLOG);
-// break;
-// case 'LPR':
-// Syslog.init(name, options, Syslog.LOG_LPR);
-// break;
-// case 'NEWS':
-// Syslog.init(name, options, Syslog.LOG_NEWS);
-// break;
-// case 'UUCP':
-// Syslog.init(name, options, Syslog.LOG_UUCP);
-// break;
-// case 'LOCAL0':
-// Syslog.init(name, options, Syslog.LOG_LOCAL0);
-// break;
-// case 'LOCAL1':
-// Syslog.init(name, options, Syslog.LOG_LOCAL1);
-// break;
-// case 'LOCAL2':
-// Syslog.init(name, options, Syslog.LOG_LOCAL2);
-// break;
-// case 'LOCAL3':
-// Syslog.init(name, options, Syslog.LOG_LOCAL3);
-// break;
-// case 'LOCAL4':
-// Syslog.init(name, options, Syslog.LOG_LOCAL4);
-// break;
-// case 'LOCAL5':
-// Syslog.init(name, options, Syslog.LOG_LOCAL5);
-// break;
-// case 'LOCAL6':
-// Syslog.init(name, options, Syslog.LOG_LOCAL6);
-// break;
-// case 'LOCAL7':
-// Syslog.init(name, options, Syslog.LOG_LOCAL7);
-// break;
-// default:
-// Syslog.init(name, options, Syslog.LOG_MAIL);
-// }
-//
-// this.register_hook('log', 'syslog');
-//}
-//
-//exports.syslog = function (next, logger, log) {
-// switch(log.level.toUpperCase()) {
-// case 'INFO':
-// Syslog.log(Syslog.LOG_INFO, log.data);
-// break;
-// case 'NOTICE':
-// Syslog.log(Syslog.LOG_NOTICE, log.data);
-// break;
-// case 'WARN':
-// Syslog.log(Syslog.LOG_WARNING, log.data);
-// break;
-// case 'ERROR':
-// Syslog.log(Syslog.LOG_ERR, log.data);
-// break;
-// case 'CRIT':
-// Syslog.log(Syslog.LOG_CRIT, log.data);
-// break;
-// case 'ALERT':
-// Syslog.log(Syslog.LOG_ALERT, log.data);
-// break;
-// case 'EMERG':
-// Syslog.log(Syslog.LOG_EMERG, log.data);
-// break;
-// case 'DATA':
-// case 'PROTOCOL':
-// case 'DEBUG':
-// default:
-// Syslog.log(Syslog.LOG_DEBUG, log.data);
-// }
-//
-// return next();
-//}
+ // local teardown
+ this.configfile = this.backup.configfile;
+ },
+ 'syslog hook logs correct thing' : function (test) {
+ // local setup
+ var next = stub();
+ this.backup.plugin.Syslog.log = this.plugin.Syslog.log;
+ this.plugin.Syslog.log = stub();
+ this.plugin.syslog(next, this.logger, this.log);
+
+ test.expect(3);
+ test.ok(this.plugin.Syslog.log.called);
+ test.equals(this.plugin.Syslog.log.args[0], this.plugin.Syslog.LOG_INFO);
+ test.equals(this.plugin.Syslog.log.args[1], this.log.data);
+ test.done();
+
+ // local teardown
+ this.plugin.Syslog.log = this.backup.plugin.Syslog.log;
+ }
+};

0 comments on commit fddbe81

Please sign in to comment.
Something went wrong with that request. Please try again.