Skip to content

Commit 177cd77

Browse files
committed
Closes #20
1 parent 04a6d8c commit 177cd77

File tree

3 files changed

+173
-7
lines changed

3 files changed

+173
-7
lines changed

.travis.yml

-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,3 @@ before_install:
3838
install:
3939
#Add app specific setup here
4040
#Use '-q' to disable interactive prompts
41-
42-
script:
43-
- jshint --exclude-path=.jshintignore .

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"description": "Samples used in the nodejs documentation on cloud.google.com",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha --recursive",
8-
"jshint": "jshint --exclude-path=.jshintignore ."
7+
"jshint": "jshint --exclude-path=.jshintignore .",
8+
"mocha": "mocha --timeout 10000 --recursive",
9+
"test": "npm run jshint && npm run mocha"
910
},
1011
"author": "[email protected]",
1112
"license": "Apache 2",
@@ -14,9 +15,10 @@
1415
"googleapis": "~2.1.3"
1516
},
1617
"devDependencies": {
17-
"mocha": "~2.2.5",
1818
"jshint": "~2.8.0",
19-
"lodash": "~3.10.1"
19+
"lodash": "~3.10.1",
20+
"mocha": "~2.2.5",
21+
"request": "^2.65.0"
2022
},
2123
"repository": {
2224
"type": "git",

test/appengine/test.js

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var spawn = require('child_process').spawn;
17+
var request = require('request');
18+
19+
var cwd = process.cwd();
20+
21+
function getPath(dir) {
22+
return cwd + '/appengine/' + dir;
23+
}
24+
25+
var sampleTests = [
26+
{
27+
dir: 'express',
28+
cmd: 'node',
29+
arg1: './bin/www',
30+
msg: 'Hello World! Express.js on Google App Engine.'
31+
},
32+
{
33+
dir: 'geddy',
34+
cmd: 'node',
35+
arg1: 'node_modules/geddy/bin/cli.js',
36+
msg: 'Hello, World! Geddy.js on Google App Engine.'
37+
},
38+
{
39+
dir: 'grunt',
40+
cmd: 'node',
41+
arg1: './src/bin/www',
42+
msg: 'Hello World! Express.js + Grunt.js on Google App Engine.'
43+
},
44+
{
45+
dir: 'hapi',
46+
cmd: 'node',
47+
arg1: 'index.js',
48+
msg: 'Hello World! Hapi.js on Google App Engine.'
49+
},
50+
{
51+
dir: 'kraken',
52+
cmd: 'node',
53+
arg1: 'server.js',
54+
msg: 'Hello World! Kraken.js on Google App Engine.',
55+
code: 304
56+
},
57+
{
58+
dir: 'loopback',
59+
cmd: 'node',
60+
arg1: 'server/server.js',
61+
msg: 'LoopBack.js on Google App Engine.',
62+
code: 304
63+
},
64+
{
65+
dir: 'mailgun',
66+
cmd: 'node',
67+
arg1: 'app.js',
68+
msg: 'Express.js + Mailgun on Google App Engine.'
69+
},
70+
{
71+
dir: 'restify',
72+
cmd: 'node',
73+
arg1: 'server.js',
74+
msg: 'Hello World! Restify.js on Google App Engine.'
75+
}
76+
];
77+
78+
describe('appengine/', function () {
79+
sampleTests.forEach(function (sample) {
80+
it(sample.dir + ': dependencies should install', function (done) {
81+
this.timeout(120000);
82+
var calledDone = false;
83+
84+
var proc = spawn('npm', ['install'], {
85+
cwd: getPath(sample.dir)
86+
});
87+
88+
proc.on('error', function (err) {
89+
if (!calledDone) {
90+
calledDone = true;
91+
done(err);
92+
}
93+
});
94+
95+
proc.stderr.on('data', function (data) {
96+
console.log('stderr: ' + data);
97+
});
98+
99+
proc.on('exit', function (code) {
100+
if (!calledDone) {
101+
calledDone = true;
102+
if (code !== 0) {
103+
done(new Error(sample.dir + ': failed to install dependencies!'));
104+
} else {
105+
done();
106+
}
107+
}
108+
});
109+
});
110+
111+
it(sample.dir + ': should return 200 and Hello World', function (done) {
112+
var timeoutId;
113+
var intervalId;
114+
var success = false;
115+
var calledDone = false;
116+
117+
var proc = spawn(sample.cmd, [sample.arg1], {
118+
cwd: getPath(sample.dir)
119+
});
120+
121+
proc.on('error', function (err) {
122+
if (!calledDone) {
123+
calledDone = true;
124+
done(err);
125+
}
126+
});
127+
128+
proc.stderr.on('data', function (data) {
129+
console.log('stderr: ' + data);
130+
});
131+
132+
proc.on('exit', function (code, signal) {
133+
if (!calledDone) {
134+
calledDone = true;
135+
if (code !== 0 && signal !== 'SIGTERM') {
136+
done(new Error(sample.dir + ': failed to run!'));
137+
} else {
138+
if (!success) {
139+
done(new Error(sample.dir + ': failed verification!'));
140+
} else {
141+
done();
142+
}
143+
}
144+
}
145+
});
146+
147+
timeoutId = setTimeout(end, 5000);
148+
intervalId = setInterval(testRequest, 1000);
149+
150+
function end() {
151+
clearTimeout(timeoutId);
152+
clearInterval(intervalId);
153+
proc.kill('SIGTERM');
154+
}
155+
156+
function testRequest() {
157+
request('http://localhost:8080', function (err, response, body) {
158+
if (body.indexOf(sample.msg) !== -1 && (response.statusCode === 200 ||
159+
response.statusCode === sample.code)) {
160+
success = true;
161+
end();
162+
}
163+
});
164+
}
165+
});
166+
});
167+
});

0 commit comments

Comments
 (0)