Permalink
Browse files

[bin test fix] Updates for bin/carapace and tests to use custom plugins

  • Loading branch information...
1 parent 8cae7b5 commit b5d4560013e5b364515a49ab3be25cfb633a0fa3 @indexzero indexzero committed Aug 3, 2011
View
@@ -11,14 +11,14 @@ var path = require('path'),
var argv = carapace.cli.argv(),
script = argv._[0];
-function onPluginError (plugin, ex) {
+function onPluginError (info) {
//
// If any of the plugins have failed to load then short
// circuit this carapace process because it requested them all.
//
- console.log('Error loading plugin: ' + plugin);
- console.log(ex.message);
- console.dir(ex.stack.split('\n'));
+ console.log('Error loading plugin: ' + info.plugin);
+ console.log(info.error.message);
+ console.dir(info.error.stack.split('\n'));
process.exit(1);
}
@@ -45,12 +45,24 @@ carapace.start(function () {
});
carapace.use(plugins, function () {
- async.forEachSeries(argv.plugin, function (plugin, next) {
- if (carapace[plugin]) {
- carapace[plugin](argv[plugin], function (err) {
- return err ? onPluginError(plugin, err) : next();
+ var names = argv.plugin.map(function (plugin) {
+ return path.basename(plugin, '.js');
+ })
+
+ async.forEachSeries(names, function (plugin, next) {
+ if (!carapace[plugin]) {
+ return onPluginError({
+ plugin: plugin,
+ error: new Error('No plugin `' + plugin + '` was found after loading.')
});
}
+
+ carapace[plugin](argv[plugin], function (err) {
+ return !err ? next() : onPluginError({
+ plugin: plugin,
+ error: err
+ });
+ });
}, next);
});
}
View
@@ -3,7 +3,6 @@ var daemon = require('daemon'),
module.exports = function chrootPlugin (carapace) {
if (!carapace.chroot) {
-
carapace.chroot = function (value, done) {
try { daemon.chroot(path.resolve(value)) }
catch (ex) { return done ? done(ex) : null }
View
@@ -4,7 +4,19 @@
var enabled = false;
module.exports = function (carapace) {
- carapace.heartbeat = function heartbeat (done, interval) {
+ carapace.heartbeat = function heartbeat () {
+ var args = Array.prototype.slice.call(arguments),
+ interval = 1000,
+ done;
+
+ args.forEach(function (a) {
+ switch (typeof a) {
+ case 'number': interval = a; break;
+ case 'function': done = a; break;
+ default: break;
+ }
+ });
+
enabled = !enabled;
if (!enabled) {
@@ -20,6 +32,7 @@ module.exports = function (carapace) {
else {
carapace.cli.defaultOptions['heartbeat'].default = interval;
}
+
//
// start the timer
//
View
@@ -0,0 +1,18 @@
+/*
+ * checkargs.js: Test fixture for a custom plugin in haibu-carapace which uses custom arguments.
+ *
+ * (C) 2011 Nodejitsu Inc.
+ *
+ */
+
+module.exports = function checkargsPlugin (carapace) {
+ if (!carapace.checkargs) {
+ carapace.checkargs = function () {
+ setInterval(function () {
+ carapace.emit('carapace::checkargs', { id: carapace.id, checkargs: process.argv['checkargs'] });
+ }, 1000);
+ };
+
+ carapace.checkargs();
+ }
+};
View
@@ -4,13 +4,25 @@
* (C) 2011 Nodejitsu Inc.
*
*/
+
+var enabled = false;
module.exports = function customPlugin (carapace) {
if (!carapace.custom) {
- carapace.custom = function () {
- setInterval(function () {
+ carapace.custom = function (args, done) {
+ enabled = !enabled;
+
+ if (!enabled) {
+ return done ? done() : null;
+ }
+
+ this.interval = setInterval(function () {
carapace.emit('carapace::custom', { id: carapace.id, custom: true });
}, 1000);
+
+ if (done) {
+ done();
+ }
};
carapace.custom();
View
@@ -1,18 +0,0 @@
-/*
- * relative-plugin.js: Test fixture for a custom plugin in haibu-carapace.
- *
- * (C) 2011 Nodejitsu Inc.
- *
- */
-
-module.exports = function relativePlugin (carapace) {
- if (!carapace.relative) {
- carapace.relative = function () {
- setInterval(function () {
- carapace.emit('carapace::relative', { id: carapace.id, relative: true });
- }, 1000);
- };
-
- carapace.relative();
- }
-};
View
@@ -124,13 +124,13 @@ macros.assertParentSpawn = function (PORT, script, argv, cwd, vows) {
assert.notEqual(info.port, carapace['hook-port'])
},
"should correctly start the HTTP server": {
- topic: function (_,_,_,child) {
+ topic: function (_, _, _, child) {
request({ uri: 'http://localhost:' + this.port }, this.callback.bind(null, null, child));
},
"that responds with a cwd": function (_, child, err, res, body) {
child.kill();
assert.equal(body, cwd);
- },
+ }
}
}
}
@@ -0,0 +1,37 @@
+/*
+ * use-test.js: Basic tests for the carapace module
+ *
+ * (C) 2011 Nodejitsu Inc
+ * MIT LICENCE
+ *
+ */
+
+var assert = require('assert'),
+ path = require('path'),
+ exec = require('child_process').exec,
+ http = require('http'),
+ request = require('request'),
+ vows = require('vows'),
+ helper = require('../helper/macros.js'),
+ carapace = require('../../lib/carapace');
+
+var jail = path.join(__dirname, '..', '..', 'examples', 'chroot-jail'),
+ custom = path.join(__dirname, '..', 'fixtures', 'custom.js'),
+ script = path.join(jail, 'server.js'),
+ argv = ['--plugin', custom, '--hook-name', '--plugin', 'heartbeat'],
+ PORT = 5060;
+
+vows.describe('carapace/spawn/custom-plugin').addBatch({
+ "When using haibu-carapace": helper.assertListen(PORT, {
+ "spawning a child carapace with a custom plugin": helper.assertParentSpawn(PORT, script, argv, process.cwd(), {
+ "after the plugin is loaded": {
+ topic: function () {
+ carapace.on('*::carapace::custom', this.callback.bind(carapace, null));
+ },
+ "should emit the `carapace::custom` event": function (_, info) {
+ assert.isTrue(info.custom);
+ }
+ }
+ })
+ })
+}).export(module);

0 comments on commit b5d4560

Please sign in to comment.