Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prepare for travis-ci

  • Loading branch information...
commit e3fead9b47571d806060690f405c3d3c6de5d8d4 1 parent b8fb9ff
Elad Ben-Israel authored
View
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+
View
4 bin/nploy
@@ -30,6 +30,10 @@ var server = nploy.start(program, function() {
console.log('listening on http://' + server.endpoint.host + ':' + server.endpoint.port + '/')
});
+process.on('SIGTERM', function() {
+ server.close()
+})
+
console.log('-----------')
console.log('-- nploy --')
console.log('-----------')
View
8 lib/nploy.js
@@ -17,7 +17,7 @@ var createRouter = exports.createRouter = function(opts) {
if (!opts.routes) opts.routes = {}
if (!opts.debug) opts.debug = false
if (!opts.dir) opts.dir ='.'
- if (!('output' in opts)) opts.output = 'process'
+ if (!('output' in opts)) opts.output = false
if (opts.output === "no" || opts.output === "none" || opts.output == false) delete opts.output
var startPort = opts.range && opts.range[0] || 7000
@@ -58,14 +58,14 @@ var createRouter = exports.createRouter = function(opts) {
var child = app.child = new forever.Monitor(app.app, {
max: 0
- , silent: false
+ , silent: output ? false : true
, forever: false
, env: { PORT: app.port }
})
if (output) {
-
- child.on('stdout', function (data) {
+ console.log('output will be capured!')
+ child.on('stdout', function (data) {
if (output === 'console') log('info', app, data.toString())
else process.stdout.write(data)
})
View
4 package.json
@@ -2,7 +2,7 @@
"author": "George Stagas <gstagas@gmail.com> (http://stagas.com)",
"name": "nploy",
"description": "Lazy spawn node apps on single ip",
- "version": "0.1.2",
+ "version": "0.1.3",
"repository": {
"type": "git",
"url": "git://github.com/stagas/nploy.git"
@@ -27,6 +27,6 @@
"nodeunit": "latest"
},
"scripts": {
- "test": "nodeunit test/nutest.js"
+ "test": "nodeunit test/"
}
}
View
6 test/b/index.js
@@ -1,10 +1,12 @@
var http = require('http')
-// take 3 seconds and then start listening
+// take some time seconds and then start listening
+// this is used to verify that even scripts that take some
+// lead time to listen work
setTimeout(function() {
http.createServer(function (req, res) {
res.writeHead(200)
res.end('B: Hello')
}).listen(process.env.PORT, process.env.HOST)
-}, 3000)
+}, 1000)
View
48 test/cli-tests.js
@@ -0,0 +1,48 @@
+var testCase = require('nodeunit').testCase
+var nploy = require('../lib/nploy')
+var request = require('request')
+var spawn = require('child_process').spawn
+var common = require('./lib/common')
+
+var TEST_PORT = 4000
+
+if (!common.hostsExistSync(['a.localhost', 'b.localhost'])) return
+
+exports.cli = testCase({
+ setUp: function(cb) {
+ this.child = spawn(process.execPath, ['../bin/nploy', '-p', TEST_PORT], { cwd: __dirname })
+ setTimeout(cb, 500)
+ }
+
+, tearDown: function(cb) {
+ this.child.on('exit', function() { cb() })
+ this.child.kill()
+ }
+
+, makeRequestA: function(test) {
+ assertResponse('http://a.localhost:' + TEST_PORT, 'A: Hello', function (err, body) {
+ test.ok(!err, err)
+ test.ok(body, "got response from A")
+ test.done()
+ })
+ }
+
+, makeRequestB: function(test) {
+ assertResponse('http://b.localhost:' + TEST_PORT, 'B: Hello', function (err, body) {
+ test.ok(!err, err)
+ test.ok(body)
+ test.done()
+ })
+ }
+})
+
+function assertResponse (url, data, callback) {
+ request.get(url, function (err, res, body) {
+ if (err) return callback(err)
+ if (body == data) {
+ callback(null, body)
+ } else {
+ callback(new Error('Not equal'))
+ }
+ })
+}
View
35 test/lib/common.js
@@ -0,0 +1,35 @@
+var fs = require('fs')
+var os = require('os')
+var windows = os.platform() === 'win32' ? true : false
+
+exports.apitest = function(test, obj, functions, props) {
+ functions.forEach(function(f) {
+ test.ok(obj[f], f + " not found")
+ test.equal(typeof obj[f], "function", f + " is not a function")
+ })
+
+ props.forEach(function(p) {
+ test.ok(obj[p] !== null && obj[p] !== undefined)
+ })
+}
+
+exports.hostsExistSync = function(expected) {
+ var file = windows ? path.join(process.env.SystemRoot, "System32", "drivers", "etc", "hosts") : "/etc/hosts"
+ var data = fs.readFileSync(file)
+ var lines = data.toString().split('\n')
+
+ function _defined(lines, hostname) {
+ var found = false
+ lines.forEach(function(line) { if (line.indexOf(hostname) !== -1) found = true })
+ return found
+ }
+
+ var notFound = []
+ expected.forEach(function(host) {
+ if (!_defined(lines, host)) {
+ console.error('host "%s" is not defined in %s', host, file)
+ notFound.push(host) 
+ }
+ })
+ return notFound.length === 0
+}
View
265 test/nutest.js
@@ -1,265 +0,0 @@
-var testCase = require('nodeunit').testCase
-var nploy = require('../lib/nploy')
-var request = require('request')
-var path = require('path')
-var spawn = require('child_process').spawn
-
-var TEST_PORT = 4000
-
-exports.router = testCase({
- setUp: function(cb) {
- this.router = nploy.createRouter({ dir: __dirname, range: [ 7000, 7999 ], output: true, debug: true })
- this.router.setRoute('a.localhost', 'a')
- this.router.setRoute('b.localhost', 'b')
- cb()
- }
-
-, tearDown: function(cb) {
- this.router.close()
- cb()
- }
-
-, api: function(test) {
- var functions = [ 'setRoute', 'setRoutes', 'getRoute', 'clearRoutes', 'getchild' ]
- var props = [ 'range', 'idletime', 'options' ]
- apitest(test, this.router, functions, props)
- test.done()
- }
-
-, fields: function(test) {
- test.deepEqual(this.router.range, [7000, 7999])
- test.deepEqual(this.router.idletime, 15)
- test.done()
- }
-
-, getRouteNotFound: function(test) {
- var self = this
- self.router.getRoute('x.localhost', function(err, route) {
- test.ok(err, "expecting an error")
- test.ok(!route, "route should be null when there is an error")
- test.done()
- })
- }
-
-, getRouteExists: function(test) {
- var self = this
- self.router.getRoute('a.localhost', function(err, route) {
- test.ok(!err, err)
- test.ok(route && route.host && route.port)
- test.done()
- })
- }
-
-, getRouteScriptNotFound: function(test) {
- var self = this
- self.router.setRoute('uu', 'not-found.js')
- self.router.getRoute('uu', function(err, route) {
- test.ok(err, "expecting an error")
- test.ok(!route, "no route")
- test.done()
- })
- }
-
-, loadError: function(test) {
- var self = this
- self.router.setRoute('c.localhost', 'c')
- self.router.getRoute('c.localhost', function(err, route) {
- test.ok(err, "expecting an error")
- test.ok(!route)
- test.done()
- })
- }
-
-, absolutePath: function(test) {
- var self = this
- self.router.setRoute('xxx', path.join(__dirname, 'a', 'index.js'))
- self.router.getRoute('xxx', function(err, route) {
- test.ok(!err, err)
- test.ok(route && route.host && route.port)
- test.done()
- })
- }
-
-, setRoutes: function(test) {
- var self = this
- self.router.setRoutes({ 'x/z/123.xxx': 'a', '8899xx!': 'b' })
- self.router.getRoute('x/z/123.xxx', function(err, route) {
- test.ok(!err, err)
- test.ok(route && route.host && route.port)
-
- self.router.getRoute('8899xx!', function(err, route) {
- test.ok(!err, err)
- test.ok(route && route.host && route.port)
- test.done()
- })
- })
- }
-
-, clearRoutes: function(test) {
- var self = this
- self.router.clearRoutes()
- self.router.getRoute('a.localhost', function(err, route) {
- test.ok(err)
- test.ok(!route)
- test.done()
- })
- }
-
-, getchild: function(test) {
- var self = this
- test.ok(!self.router.getchild('a.localhost'))
- self.router.getRoute('a.localhost', function(err, route) {
- test.ok(self.router.getchild('a.localhost'))
- test.done()
- })
- }
-
-, kill: function(test) {
- var self = this
- test.ok(self.router.kill)
- self.router.setRoute('uu', 'b')
- self.router.getRoute('uu', function(err, route) {
- test.ok(!err, err)
- test.ok(route)
- test.ok(self.router.getchild('uu'))
- self.router.kill('uu', function(err) {
- test.ok(!err, err)
- test.ok(!self.router.getchild('uu'))
- test.done()
- })
- })
- }
-})
-
-function apitest(test, obj, functions, props) {
- functions.forEach(function(f) {
- test.ok(obj[f], f + " not found")
- test.equal(typeof obj[f], "function", f + " is not a function")
- })
-
- props.forEach(function(p) {
- test.ok(obj[p] !== null && obj[p] !== undefined)
- })
-}
-
-exports.proxy = testCase({
- setUp: function(cb) {
- this.req = function(hostname, callback) { request('http://' + hostname + ':' + TEST_PORT, callback) }
- var opts = {
- dir: __dirname
- , port: TEST_PORT
- }
- this.server = nploy.start(opts, function(err) {
- cb(err)
- })
- }
-
-, tearDown: function(cb) {
- this.server.close()
- cb()
- }
-
-, api: function(test) {
- var functions = [ 'close' ]
- var props = [ 'config', 'router', 'endpoint', 'proxy' ]
- apitest(test, this.server, functions, props)
- test.done()
- }
-
-, config: function(test) {
- test.equals(this.server.config, path.join(__dirname, './nploy.cfg'))
- test.done()
- }
-
-, endpoint: function(test) {
- test.deepEqual(this.server.endpoint, { host: '0.0.0.0', port: TEST_PORT })
- test.done()
- }
-
-, notFound: function(test) {
- this.req('localhost', function(err, res, body) {
- test.ok(!err, err)
- test.equals(res.statusCode, 404, "404 is expected when a request is sent to an undefined app")
- test.done()
- })
- }
-
-, appA: function(test) {
- this.req('a.localhost', function(err, res, body) {
- test.ok(!err, err)
- test.equals(res.statusCode, 200)
- test.equals(body, 'A: Hello')
- test.done()
- })
- }
-
-, appB : function(test) {
- this.req('b.localhost', function(err, res, body) {
- test.ok(!err, err)
- test.equals(res.statusCode, 200)
- test.equals(body, 'B: Hello')
- test.done()
- })
- }
-
-, kill: function(test) {
- var self = this
-
- test.ok(!self.server.router.getchild('a.localhost'), "getchild() should return null for a non started app")
-
- self.server.router.kill('a.localhost', function(err) {
- test.ok(err, "expecting an error because a is not started yet")
-
- self.req('a.localhost', function(err, res, body) {
- test.ok(self.server.router.getchild('a.localhost'), "getchild() should return a pid")
- test.ok(res.statusCode, 200)
-
- self.server.router.kill('a.localhost', function(err) {
- test.ok(!err, err)
- var pid = self.server.router.getchild('a.localhost')
- test.ok(!self.server.router.getchild('a.localhost'), "getchild() should return null for a non started app")
- test.done()
- })
- })
- })
- }
-})
-
-exports.cli = testCase({
- setUp: function(cb) {
- this.child = spawn(process.execPath, ['../bin/nploy', '-p', TEST_PORT], { cwd: __dirname })
- setTimeout(cb, 500)
- }
-
-, tearDown: function(cb) {
- this.child.on('exit', function() { cb() })
- this.child.kill()
- }
-
-, makeRequestA: function(test) {
- assertResponse('http://a.localhost:' + TEST_PORT, 'A: Hello', function (err, body) {
- test.ok(!err, err)
- test.ok(body, "got response from A")
- test.done()
- })
- }
-
-, makeRequestB: function(test) {
- assertResponse('http://b.localhost:' + TEST_PORT, 'B: Hello', function (err, body) {
- test.ok(!err, err)
- test.ok(body)
- test.done()
- })
- }
-})
-
-function assertResponse (url, data, callback) {
- request.get(url, function (err, res, body) {
- if (err) return callback(err)
- if (body == data) {
- callback(null, body)
- } else {
- callback(new Error('Not equal'))
- }
- })
-}
View
92 test/proxy-tests.js
@@ -0,0 +1,92 @@
+var testCase = require('nodeunit').testCase
+var nploy = require('../lib/nploy')
+var request = require('request')
+var path = require('path')
+var common = require('./lib/common')
+
+var TEST_PORT = 4000
+
+if (!common.hostsExistSync(['a.localhost', 'b.localhost', 'c.localhost'])) return
+
+exports.proxy = testCase({
+ setUp: function(cb) {
+ this.req = function(hostname, callback) { request('http://' + hostname + ':' + TEST_PORT, callback) }
+ var opts = {
+ dir: __dirname
+ , port: TEST_PORT
+ }
+ this.server = nploy.start(opts, function(err) {
+ cb(err)
+ })
+ }
+
+, tearDown: function(cb) {
+ this.server.close()
+ cb()
+ }
+
+, api: function(test) {
+ var functions = [ 'close' ]
+ var props = [ 'config', 'router', 'endpoint', 'proxy' ]
+ common.apitest(test, this.server, functions, props)
+ test.done()
+ }
+
+, config: function(test) {
+ test.equals(this.server.config, path.join(__dirname, './nploy.cfg'))
+ test.done()
+ }
+
+, endpoint: function(test) {
+ test.deepEqual(this.server.endpoint, { host: '0.0.0.0', port: TEST_PORT })
+ test.done()
+ }
+
+, notFound: function(test) {
+ this.req('localhost', function(err, res, body) {
+ test.ok(!err, err)
+ test.equals(res.statusCode, 404, "404 is expected when a request is sent to an undefined app")
+ test.done()
+ })
+ }
+
+, appA: function(test) {
+ this.req('a.localhost', function(err, res, body) {
+ test.ok(!err, err)
+ test.equals(res.statusCode, 200)
+ test.equals(body, 'A: Hello')
+ test.done()
+ })
+ }
+
+, appB : function(test) {
+ this.req('b.localhost', function(err, res, body) {
+ test.ok(!err, err)
+ test.equals(res.statusCode, 200)
+ test.equals(body, 'B: Hello')
+ test.done()
+ })
+ }
+
+, kill: function(test) {
+ var self = this
+
+ test.ok(!self.server.router.getchild('a.localhost'), "getchild() should return null for a non started app")
+
+ self.server.router.kill('a.localhost', function(err) {
+ test.ok(err, "expecting an error because a is not started yet")
+
+ self.req('a.localhost', function(err, res, body) {
+ test.ok(self.server.router.getchild('a.localhost'), "getchild() should return a pid")
+ test.ok(res.statusCode, 200)
+
+ self.server.router.kill('a.localhost', function(err) {
+ test.ok(!err, err)
+ var pid = self.server.router.getchild('a.localhost')
+ test.ok(!self.server.router.getchild('a.localhost'), "getchild() should return null for a non started app")
+ test.done()
+ })
+ })
+ })
+ }
+})
View
129 test/router-tests.js
@@ -0,0 +1,129 @@
+var testCase = require('nodeunit').testCase
+var nploy = require('../lib/nploy')
+var path = require('path')
+var apitest = require('./lib/common').apitest
+
+exports.router = testCase({
+ setUp: function(cb) {
+ this.router = nploy.createRouter({ dir: __dirname, range: [ 7000, 7999 ], output: false, debug: false })
+ this.router.setRoute('a.localhost', 'a')
+ this.router.setRoute('b.localhost', 'b')
+ cb()
+ }
+
+, tearDown: function(cb) {
+ this.router.close()
+ cb()
+ }
+
+, api: function(test) {
+ var functions = [ 'setRoute', 'setRoutes', 'getRoute', 'clearRoutes', 'getchild' ]
+ var props = [ 'range', 'idletime', 'options' ]
+ apitest(test, this.router, functions, props)
+ test.done()
+ }
+
+, fields: function(test) {
+ test.deepEqual(this.router.range, [7000, 7999])
+ test.deepEqual(this.router.idletime, 15)
+ test.done()
+ }
+
+, getRouteNotFound: function(test) {
+ var self = this
+ self.router.getRoute('x.localhost', function(err, route) {
+ test.ok(err, "expecting an error")
+ test.ok(!route, "route should be null when there is an error")
+ test.done()
+ })
+ }
+
+, getRouteExists: function(test) {
+ var self = this
+ self.router.getRoute('a.localhost', function(err, route) {
+ test.ok(!err, err)
+ test.ok(route && route.host && route.port)
+ test.done()
+ })
+ }
+
+, getRouteScriptNotFound: function(test) {
+ var self = this
+ self.router.setRoute('uu', 'not-found.js')
+ self.router.getRoute('uu', function(err, route) {
+ test.ok(err, "expecting an error")
+ test.ok(!route, "no route")
+ test.done()
+ })
+ }
+
+, loadError: function(test) {
+ var self = this
+ self.router.setRoute('c.localhost', 'c')
+ self.router.getRoute('c.localhost', function(err, route) {
+ test.ok(err, "expecting an error")
+ test.ok(!route)
+ test.done()
+ })
+ }
+
+, absolutePath: function(test) {
+ var self = this
+ self.router.setRoute('xxx', path.join(__dirname, 'a', 'index.js'))
+ self.router.getRoute('xxx', function(err, route) {
+ test.ok(!err, err)
+ test.ok(route && route.host && route.port)
+ test.done()
+ })
+ }
+
+, setRoutes: function(test) {
+ var self = this
+ self.router.setRoutes({ 'x/z/123.xxx': 'a', '8899xx!': 'b' })
+ self.router.getRoute('x/z/123.xxx', function(err, route) {
+ test.ok(!err, err)
+ test.ok(route && route.host && route.port)
+
+ self.router.getRoute('8899xx!', function(err, route) {
+ test.ok(!err, err)
+ test.ok(route && route.host && route.port)
+ test.done()
+ })
+ })
+ }
+
+, clearRoutes: function(test) {
+ var self = this
+ self.router.clearRoutes()
+ self.router.getRoute('a.localhost', function(err, route) {
+ test.ok(err)
+ test.ok(!route)
+ test.done()
+ })
+ }
+
+, getchild: function(test) {
+ var self = this
+ test.ok(!self.router.getchild('a.localhost'))
+ self.router.getRoute('a.localhost', function(err, route) {
+ test.ok(self.router.getchild('a.localhost'))
+ test.done()
+ })
+ }
+
+, kill: function(test) {
+ var self = this
+ test.ok(self.router.kill)
+ self.router.setRoute('uu', 'b')
+ self.router.getRoute('uu', function(err, route) {
+ test.ok(!err, err)
+ test.ok(route)
+ test.ok(self.router.getchild('uu'))
+ self.router.kill('uu', function(err) {
+ test.ok(!err, err)
+ test.ok(!self.router.getchild('uu'))
+ test.done()
+ })
+ })
+ }
+})
Please sign in to comment.
Something went wrong with that request. Please try again.