From 177cd77a774e8d87dbede9079b0cb5f33b845b1a Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Mon, 2 Nov 2015 16:09:56 -0800 Subject: [PATCH] Closes #20 --- .travis.yml | 3 - package.json | 10 ++- test/appengine/test.js | 167 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 7 deletions(-) create mode 100644 test/appengine/test.js diff --git a/.travis.yml b/.travis.yml index c716d9e2f38..436e718a753 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,3 @@ before_install: install: #Add app specific setup here #Use '-q' to disable interactive prompts - -script: - - jshint --exclude-path=.jshintignore . diff --git a/package.json b/package.json index 1b8c0f1b05f..2d2748cc296 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "description": "Samples used in the nodejs documentation on cloud.google.com", "main": "index.js", "scripts": { - "test": "mocha --recursive", - "jshint": "jshint --exclude-path=.jshintignore ." + "jshint": "jshint --exclude-path=.jshintignore .", + "mocha": "mocha --timeout 10000 --recursive", + "test": "npm run jshint && npm run mocha" }, "author": "jerjou@google.com", "license": "Apache 2", @@ -14,9 +15,10 @@ "googleapis": "~2.1.3" }, "devDependencies": { - "mocha": "~2.2.5", "jshint": "~2.8.0", - "lodash": "~3.10.1" + "lodash": "~3.10.1", + "mocha": "~2.2.5", + "request": "^2.65.0" }, "repository": { "type": "git", diff --git a/test/appengine/test.js b/test/appengine/test.js new file mode 100644 index 00000000000..8372ab96d24 --- /dev/null +++ b/test/appengine/test.js @@ -0,0 +1,167 @@ +// Copyright 2015, Google, Inc. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +var spawn = require('child_process').spawn; +var request = require('request'); + +var cwd = process.cwd(); + +function getPath(dir) { + return cwd + '/appengine/' + dir; +} + +var sampleTests = [ + { + dir: 'express', + cmd: 'node', + arg1: './bin/www', + msg: 'Hello World! Express.js on Google App Engine.' + }, + { + dir: 'geddy', + cmd: 'node', + arg1: 'node_modules/geddy/bin/cli.js', + msg: 'Hello, World! Geddy.js on Google App Engine.' + }, + { + dir: 'grunt', + cmd: 'node', + arg1: './src/bin/www', + msg: 'Hello World! Express.js + Grunt.js on Google App Engine.' + }, + { + dir: 'hapi', + cmd: 'node', + arg1: 'index.js', + msg: 'Hello World! Hapi.js on Google App Engine.' + }, + { + dir: 'kraken', + cmd: 'node', + arg1: 'server.js', + msg: 'Hello World! Kraken.js on Google App Engine.', + code: 304 + }, + { + dir: 'loopback', + cmd: 'node', + arg1: 'server/server.js', + msg: 'LoopBack.js on Google App Engine.', + code: 304 + }, + { + dir: 'mailgun', + cmd: 'node', + arg1: 'app.js', + msg: 'Express.js + Mailgun on Google App Engine.' + }, + { + dir: 'restify', + cmd: 'node', + arg1: 'server.js', + msg: 'Hello World! Restify.js on Google App Engine.' + } +]; + +describe('appengine/', function () { + sampleTests.forEach(function (sample) { + it(sample.dir + ': dependencies should install', function (done) { + this.timeout(120000); + var calledDone = false; + + var proc = spawn('npm', ['install'], { + cwd: getPath(sample.dir) + }); + + proc.on('error', function (err) { + if (!calledDone) { + calledDone = true; + done(err); + } + }); + + proc.stderr.on('data', function (data) { + console.log('stderr: ' + data); + }); + + proc.on('exit', function (code) { + if (!calledDone) { + calledDone = true; + if (code !== 0) { + done(new Error(sample.dir + ': failed to install dependencies!')); + } else { + done(); + } + } + }); + }); + + it(sample.dir + ': should return 200 and Hello World', function (done) { + var timeoutId; + var intervalId; + var success = false; + var calledDone = false; + + var proc = spawn(sample.cmd, [sample.arg1], { + cwd: getPath(sample.dir) + }); + + proc.on('error', function (err) { + if (!calledDone) { + calledDone = true; + done(err); + } + }); + + proc.stderr.on('data', function (data) { + console.log('stderr: ' + data); + }); + + proc.on('exit', function (code, signal) { + if (!calledDone) { + calledDone = true; + if (code !== 0 && signal !== 'SIGTERM') { + done(new Error(sample.dir + ': failed to run!')); + } else { + if (!success) { + done(new Error(sample.dir + ': failed verification!')); + } else { + done(); + } + } + } + }); + + timeoutId = setTimeout(end, 5000); + intervalId = setInterval(testRequest, 1000); + + function end() { + clearTimeout(timeoutId); + clearInterval(intervalId); + proc.kill('SIGTERM'); + } + + function testRequest() { + request('http://localhost:8080', function (err, response, body) { + if (body.indexOf(sample.msg) !== -1 && (response.statusCode === 200 || + response.statusCode === sample.code)) { + success = true; + end(); + } + }); + } + }); + }); +}); \ No newline at end of file