-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from GoogleCloudPlatform/tests
Added integration tests to check that samples run. Closes #20.
- Loading branch information
Showing
5 changed files
with
218 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": "[email protected]", | ||
"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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// 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: 'redis', | ||
cmd: 'node', | ||
arg1: 'server.js', | ||
msg: '127.0.0.1' | ||
}, | ||
{ | ||
dir: 'restify', | ||
cmd: 'node', | ||
arg1: 'server.js', | ||
msg: 'Hello World! Restify.js on Google App Engine.' | ||
} | ||
]; | ||
|
||
if (process.env.TRAVIS_NODE_VERSION !== 'stable') { | ||
// For some reason the "npm install" step for the Sails sample doesn't work on | ||
// Travis when using Node.js stable. It works locally, however. | ||
sampleTests.push({ | ||
dir: 'sails', | ||
cmd: 'node', | ||
arg1: 'app.js', | ||
msg: 'Hello World! Sails.js on Google App Engine.', | ||
timeout: 240000 | ||
}); | ||
} | ||
|
||
describe('appengine/', function () { | ||
sampleTests.forEach(function (sample) { | ||
it(sample.dir + ': dependencies should install', function (done) { | ||
this.timeout(sample.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); | ||
} | ||
}); | ||
|
||
if (!process.env.TRAVIS) { | ||
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); | ||
} | ||
}); | ||
|
||
if (!process.env.TRAVIS) { | ||
proc.stderr.on('data', function (data) { | ||
console.log('stderr: ' + data); | ||
}); | ||
} | ||
|
||
proc.on('exit', function (code, signal) { | ||
if (!calledDone) { | ||
calledDone = true; | ||
if (code !== 0 && signal !== 'SIGKILL') { | ||
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('SIGKILL'); | ||
} | ||
|
||
function testRequest() { | ||
request('http://localhost:8080', function (err, res, body) { | ||
if (body && body.indexOf(sample.msg) !== -1 && | ||
(res.statusCode === 200 || res.statusCode === sample.code)) { | ||
success = true; | ||
end(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |