Browse files

[test] Update test macros and create tests for newer `carapace.ports`…

… functionality
  • Loading branch information...
1 parent d458d8f commit 6cc5fc22df3d43ee6ae71ba63dbea7596c18dcbd @indexzero indexzero committed Aug 8, 2011
View
8 test/fixtures/custom.js
@@ -10,12 +10,12 @@ var enabled = false;
module.exports = function customPlugin (carapace) {
if (!carapace.custom) {
carapace.custom = function (args, done) {
- enabled = !enabled;
-
- if (!enabled) {
+ if (enabled) {
return done ? done() : null;
}
+ enabled = true;
+
this.interval = setInterval(function () {
carapace.emit('carapace::custom', { id: carapace.id, custom: true });
}, 1000);
@@ -24,7 +24,5 @@ module.exports = function customPlugin (carapace) {
done();
}
};
-
- carapace.custom();
}
};
View
38 test/fixtures/pluginserver.js
@@ -0,0 +1,38 @@
+/*
+ * pluginserver.js: Test fixture for a custom plugin in haibu-carapace that starts an HTTP server .
+ *
+ * (C) 2011 Nodejitsu Inc.
+ *
+ */
+
+var http = require('http');
+
+var enabled = false;
+
+module.exports = function pluginserver (carapace) {
+ if (!carapace.pluginserver) {
+ carapace.pluginserver = function (args, done) {
+ if (enabled) {
+ return done();
+ }
+
+ var port = 1337, server;
+
+ server = http.createServer(function (req, res) {
+ res.end('from-pluginserver');
+ });
+
+ //
+ // Append the port of the plugin server to `carapace.ports.ignore`
+ // so that `haibu-carapace` will not emit `carapace::port` events when
+ // it attempts to listen.
+ //
+ carapace.ports.ignore.push(port);
+ server.listen(port, function () {
+ if (done) {
+ done();
+ }
+ });
+ };
+ }
+};
View
32 test/helper/macros.js
@@ -93,15 +93,23 @@ macros.assertSpawn = function (PORT, script, argv, vows) {
return extendContext(context, vows);
};
-macros.assertParentSpawn = function (PORT, script, argv, cwd, vows) {
- argv = argv.slice(0);
- argv.push(script);
+macros.assertParentSpawn = function (options, /*PORT, script, argv, cwd,*/ vows) {
+ options.argv = options.argv.slice(0);
+ options.argv.push(options.script);
var context = {
"when spawning a child carapace": {
topic: function () {
var that = this,
- child = spawn(carapace.bin, ['--hook-port', PORT].concat(argv));
+ child = spawn(carapace.bin, ['--hook-port', options.port].concat(options.argv));
+
+ child.stdout.on('data', function (data) {
+ process.stdout.write(data);
+ });
+
+ child.stderr.on('data', function (data) {
+ process.stdout.write(data);
+ });
carapace.on('*::carapace::port', function onPort (info) {
if (info.port && info.port !== carapace['hook-port']) {
@@ -126,15 +134,25 @@ macros.assertParentSpawn = function (PORT, script, argv, cwd, vows) {
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);
+ if (!options.keepalive) {
+ child.kill();
+ }
+
+ assert.equal(body, options.cwd);
}
}
}
}
};
- return extendContext(context, vows);
+ if (options.keepalive) {
+ context['when spawning a child carapace'] = extendContext(context['when spawning a child carapace'], vows);
+ }
+ else {
+ context = extendContext(context, vows);
+ }
+
+ return context;
};
function extendContext (context, vows) {
View
1 test/simple/use-custom-plugin-test.js
@@ -19,6 +19,7 @@ vows.describe('carapace/simple/use-custom-plugin').addBatch({
"with an absolute path": helper.assertUse([path.join(__dirname, '..', 'fixtures', 'custom.js')], {
"after the plugin is loaded": {
topic: function () {
+ carapace.custom();
carapace.on('carapace::custom', this.callback.bind(carapace, null));
},
"should emit the `carapace::custom` event": function (_, info) {
View
35 test/simple/use-pluginserver-test.js
@@ -0,0 +1,35 @@
+/*
+ * use-pluginserver-test.js: Tests to ensure that custom plugins which start servers load correctly
+ *
+ * (C) 2011 Nodejitsu Inc.
+ *
+ */
+
+var assert = require('assert'),
+ path = require('path'),
+ request = require('request'),
+ vows = require('vows'),
+ helper = require('../helper/macros.js'),
+ carapace = require('../../lib/carapace');
+
+var PORT = 5050;
+
+vows.describe('carapace/simple/use-pluginserver').addBatch({
+ "When using haibu-carapace": helper.assertListen(PORT, {
+ "a custom plugin that starts a server" : helper.assertUse([path.join(__dirname, '..', 'fixtures', 'pluginserver.js')], {
+ "a request to the server started by pluginserver.js": {
+ topic: function () {
+ var that = this;
+
+ carapace.pluginserver(null, function () {
+ request({ uri: 'http://localhost:1337' }, that.callback);
+ });
+ },
+ "should respond with `from-pluginserver`": function (err, res, body) {
+ assert.isTrue(!err);
+ assert.equal(body, 'from-pluginserver');
+ }
+ }
+ })
+ })
+}).export(module);
View
7 test/spawn/parent-chroot-run-test.js
@@ -22,6 +22,11 @@ var jail = path.join(__dirname, '..', '..', 'examples', 'chroot-jail'),
vows.describe('carapace/run').addBatch({
"When using haibu-carapace": helper.assertListen(PORT, {
- "spawning a child carapace in a chroot jail": helper.assertParentSpawn(PORT, script, argv, '/')
+ "spawning a child carapace in a chroot jail": helper.assertParentSpawn({
+ port: PORT,
+ script: script,
+ argv: argv,
+ cwd: '/'
+ })
})
}).export(module);
View
15 test/spawn/parent-custom-plugin-test.js
@@ -17,13 +17,18 @@ var assert = require('assert'),
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;
+ options;
+
+options = {
+ port: 5060,
+ script: path.join(jail, 'server.js'),
+ argv: ['--plugin', custom, '--hook-name', '--plugin', 'heartbeat'],
+ cwd: process.cwd()
+};
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(), {
+ "When using haibu-carapace": helper.assertListen(options.port, {
+ "spawning a child carapace with a custom plugin": helper.assertParentSpawn(options, {
"after the plugin is loaded": {
topic: function () {
carapace.on('*::carapace::custom', this.callback.bind(carapace, null));
View
58 test/spawn/parent-pluginserver-test.js
@@ -0,0 +1,58 @@
+/*
+ * 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', 'pluginserver.js'),
+ options,
+ child;
+
+options = {
+ port: 5060,
+ script: path.join(jail, 'server.js'),
+ argv: ['--plugin', custom],
+ cwd: process.cwd(),
+ keepalive: true
+};
+
+vows.describe('carapace/spawn/custom-plugin').addBatch({
+ "When using haibu-carapace": helper.assertListen(options.port, {
+ "spawning a child carapace with a custom plugin": helper.assertParentSpawn(options, {
+ "a request to the server started by pluginserver.js": {
+ topic: function (info, _child) {
+ child = _child;
+ request({ uri: 'http://localhost:1337' }, this.callback.bind(this, null, child));
+ },
+ "should respond with `from-pluginserver`": function (_, child, err, res, body) {
+ assert.isTrue(!err);
+ assert.equal(body, 'from-pluginserver');
+ },
+ //
+ // Remark: There is not a good way to do asynchronous teardown in vows
+ // currently, so this context is merely a stop-gap for that.
+ //
+ "when everything is over": {
+ topic: function () {
+ setTimeout(this.callback, 200);
+ },
+ "kill the child process": function () {
+ child.kill();
+ }
+ }
+ }
+ })
+ })
+}).export(module);
View
17 test/spawn/parent-run-test.js
@@ -16,14 +16,19 @@ var assert = require('assert'),
carapace = require('../../lib/carapace');
var jail = path.join(__dirname, '..', '..', 'examples', 'chroot-jail'),
- script = path.join(jail, 'server.js'),
- argv = ['--hook-name', 'carapace'],
- PORT = 5060;
+ options;
+
+options = {
+ port: 5060,
+ argv: ['--hook-name', 'carapace'],
+ script: script = path.join(jail, 'server.js'),
+ cwd: process.cwd()
+};
vows.describe('carapace/spawn/parent').addBatch({
- "When using haibu-carapace": helper.assertListen(PORT, {
- "an initial spawn of the child": helper.assertParentSpawn(PORT, script, argv, process.cwd())
+ "When using haibu-carapace": helper.assertListen(options.port, {
+ "an initial spawn of the child": helper.assertParentSpawn(options)
})
}).addBatch({
- "followed by a second spawn of the same child": helper.assertParentSpawn(PORT, script, argv, process.cwd())
+ "followed by a second spawn of the same child": helper.assertParentSpawn(options)
}).export(module);

0 comments on commit 6cc5fc2

Please sign in to comment.