-
-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New unit testing with nodeunit and a log.syslog change #164
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7bb2f2b
The start of our unit tests. This sets up a submodule for nodeunit and
godsflaw da3b154
Adding function bind for those times where we need to set this.
godsflaw c1ecd3f
Started some tests for log.syslog plugin.
godsflaw fddbe81
finished unit tests for log.syslog and added always_ok option.
godsflaw 0457513
This was only there for testing.
godsflaw 1c96d36
no need for function-bind.
godsflaw 3c09231
a little less code for general.ini.
godsflaw 810025f
that last push was a syntax error
godsflaw 72dc530
removed hte else cuddle.
godsflaw 319e47e
four spaces instead of two.
godsflaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
!node_modules/nodeunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "node_modules/nodeunit"] | ||
path = node_modules/nodeunit | ||
url = git://github.com/godsflaw/nodeunit.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ odelay=1 | |
cons=0 | ||
ndelay=0 | ||
nowait=0 | ||
always_ok=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule nodeunit
added at
bdb457
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
// 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'); | ||
ini.general = ini.general || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This passes tests and is more clear. |
||
var name = ini.general['name'] || 'haraka'; | ||
var facility = ini.general['facility'] || 'MAIL'; | ||
var pid = ini.general['log_pid'] || 1; | ||
var odelay = ini.general['log_odelay'] || 1; | ||
var cons = ini.general['log_cons'] || 0; | ||
var ndelay = ini.general['log_ndelay'] || 0; | ||
var nowait = ini.general['log_nowait'] || 0; | ||
var always_ok = ini.general['always_ok'] || false; | ||
|
||
this.always_ok = always_ok; | ||
|
||
if (pid) | ||
options |= Syslog.LOG_PID; | ||
|
@@ -84,9 +88,11 @@ exports.register = function() { | |
} | ||
|
||
this.register_hook('log', 'syslog'); | ||
} | ||
}; | ||
|
||
exports.syslog = function (next, logger, log) { | ||
var plugin = this; | ||
|
||
switch(log.level.toUpperCase()) { | ||
case 'INFO': | ||
Syslog.log(Syslog.LOG_INFO, log.data); | ||
|
@@ -116,5 +122,9 @@ exports.syslog = function (next, logger, log) { | |
Syslog.log(Syslog.LOG_DEBUG, log.data); | ||
} | ||
|
||
return next(); | ||
} | ||
if (plugin.always_ok) { | ||
return next(OK); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uncuddle please :) |
||
return next(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env node | ||
|
||
require.paths.unshift(__dirname); | ||
|
||
try { | ||
var reporter = require('nodeunit').reporters.default; | ||
} | ||
catch(e) { | ||
console.log("Error: " + e.message); | ||
console.log(""); | ||
console.log("Cannot find nodeunit module."); | ||
console.log("You can download submodules for this project by doing:"); | ||
console.log(""); | ||
console.log(" git submodule init"); | ||
console.log(" git submodule update"); | ||
console.log(""); | ||
process.exit(); | ||
} | ||
|
||
process.chdir(__dirname); | ||
reporter.run(['tests/plugins']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = function (returnValue) { | ||
function stub() { | ||
stub.called = true; | ||
stub.args = arguments; | ||
stub.thisArg = this; | ||
return returnValue; | ||
} | ||
|
||
stub.called = false; | ||
|
||
return stub; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use strict"; | ||
|
||
var stub = require('tests/fixtures/stub'); | ||
|
||
var connection = exports; | ||
|
||
function Connection(client, server) { | ||
this.client = client; | ||
this.server = server; | ||
this.relaying = false; | ||
} | ||
|
||
connection.createConnection = function(client, server) { | ||
if (typeof(client) === 'undefined') { | ||
client = {}; | ||
} | ||
|
||
if (typeof(server) === 'undefined') { | ||
server = {}; | ||
} | ||
|
||
var obj = new Connection(client, server); | ||
return obj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"use strict"; | ||
|
||
var stub = require('tests/fixtures/stub'); | ||
|
||
var logger = exports; | ||
|
||
function Logger() { | ||
} | ||
|
||
logger.createLogger = function() { | ||
var obj = new Logger(); | ||
return obj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use strict"; | ||
|
||
var stub = require('tests/fixtures/stub'); | ||
|
||
var plugin = exports; | ||
|
||
function Plugin(name) { | ||
} | ||
|
||
plugin.createPlugin = function(name) { | ||
var obj = new Plugin(name); | ||
var plug = require(name); | ||
|
||
for (var k in plug) { | ||
if (plug.hasOwnProperty(k)) { | ||
obj[k] = plug[k]; | ||
} | ||
} | ||
|
||
return obj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
var stub = require('tests/fixtures/stub'), | ||
constants = require('../../constants'), | ||
Logger = require('tests/fixtures/stub_logger'), | ||
Plugin = require('tests/fixtures/stub_plugin'); | ||
|
||
// huge hack here, but plugin tests need constants | ||
constants.import(global); | ||
|
||
function _set_up(callback) { | ||
this.backup = {}; | ||
|
||
// needed for tests | ||
this.plugin = Plugin.createPlugin('plugins/log.syslog'); | ||
this.logger = Logger.createLogger(); | ||
|
||
// 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 = 'info'; | ||
this.log.data = "this is a test log message"; | ||
|
||
// some test data | ||
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(); | ||
|
||
callback(); | ||
} | ||
|
||
function _tear_down(callback) { | ||
// restore backed up functions | ||
this.plugin.register_hook = this.backup.plugin.register_hook; | ||
|
||
callback(); | ||
} | ||
|
||
exports.log_syslog = { | ||
setUp : _set_up, | ||
tearDown : _tear_down, | ||
'should have register function' : function (test) { | ||
test.expect(2); | ||
test.isNotNull(this.plugin); | ||
test.isFunction(this.plugin.register); | ||
test.done(); | ||
}, | ||
'register function should call register_hook()' : function (test) { | ||
test.expect(1); | ||
test.ok(this.plugin.register_hook.called); | ||
test.done(); | ||
}, | ||
'register_hook() should register for propper hook' : function (test) { | ||
test.expect(1); | ||
test.equals(this.plugin.register_hook.args[0], 'log'); | ||
test.done(); | ||
}, | ||
'register_hook() should register available function' : function (test) { | ||
test.expect(3); | ||
test.equals(this.plugin.register_hook.args[1], 'syslog'); | ||
test.isNotNull(this.plugin.syslog); | ||
test.isFunction(this.plugin.syslog); | ||
test.done(); | ||
}, | ||
'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); | ||
test.done(); | ||
}; | ||
|
||
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); | ||
|
||
// 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; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you just add in: