Skip to content

Commit

Permalink
Closes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Nov 3, 2015
1 parent 04a6d8c commit 59cb050
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 4 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -14,9 +15,11 @@
"googleapis": "~2.1.3"
},
"devDependencies": {
"mocha": "~2.2.5",
"async": "^1.5.0",
"jshint": "~2.8.0",
"lodash": "~3.10.1"
"lodash": "~3.10.1",
"mocha": "~2.2.5",
"request": "^2.65.0"
},
"repository": {
"type": "git",
Expand Down
167 changes: 167 additions & 0 deletions test/appengine/test.js
Original file line number Diff line number Diff line change
@@ -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('close', 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();
}
});
}
});
});
});

0 comments on commit 59cb050

Please sign in to comment.