Skip to content

Commit b70ce52

Browse files
committed
Added integration tests to check that samples run. Closes #20.
1 parent 04a6d8c commit b70ce52

File tree

5 files changed

+218
-27
lines changed

5 files changed

+218
-27
lines changed

.travis.yml

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ node_js:
77
cache:
88
directories:
99
- $HOME/gcloud/
10+
- appengine/express/node_modules/
11+
- appengine/geddy/node_modules/
12+
- appengine/grunt/node_modules/
13+
- appengine/hapi/node_modules/
14+
- appengine/kraken/node_modules/
15+
- appengine/loopback/node_modules/
16+
- appengine/mailgun/node_modules/
17+
- appengine/redis/node_modules/
18+
- appengine/restify/node_modules/
19+
- appengine/sails/node_modules/
20+
21+
services:
22+
- redis-server
23+
1024
env:
1125
- PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/nodejs-docs-samples.json TEST_BUCKET_NAME=cloud-samples-tests TEST_PROJECT_ID=cloud-samples-tests #Other environment variables on same line
1226

@@ -38,6 +52,3 @@ before_install:
3852
install:
3953
#Add app specific setup here
4054
#Use '-q' to disable interactive prompts
41-
42-
script:
43-
- jshint --exclude-path=.jshintignore .

appengine/redis/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ For convenience, you can use an npm script to run the deploy command. Modify you
4949

5050
```json
5151
"scripts": {
52-
"start": "node ./bin/www",
52+
"start": "node server.js",
5353
"deploy": "gcloud preview app deploy app.yaml --promote --project <your-project-id>"
5454
}
5555
```

appengine/redis/package.json

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
{
22
"name": "appengine-redis",
3-
"version": "1.0.0",
4-
"description": "An example of using redis with Google App Engine.",
5-
"main": "server.js",
3+
"description": "An example of using Redis with Node.js on Google App Engine.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"engines": {
8+
"node": "~0.12.7"
9+
},
610
"scripts": {
11+
"start": "node server.js",
712
"deploy": "gcloud preview app deploy app.yaml --promote --project node-redis-demo"
813
},
9-
"repository": {
10-
"type": "git",
11-
"url": "http://github.com/JustinBeckwith/appengine-nodejs-samples"
12-
},
13-
"keywords": [
14-
"nodejs",
15-
"redis",
16-
"appengine",
17-
"google"
18-
],
19-
"author": "Justin Beckwith",
20-
"license": "Apache Version 2.0",
21-
"bugs": {
22-
"url": "https://github.com/JustinBeckwith/appengine-nodejs-samples/issues"
23-
},
24-
"homepage": "https://github.com/JustinBeckwith/appengine-nodejs-samples",
2514
"dependencies": {
2615
"nconf": "^0.8.0",
2716
"redis": "^2.0.1"

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

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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: 'redis',
72+
cmd: 'node',
73+
arg1: 'server.js',
74+
msg: '127.0.0.1'
75+
},
76+
{
77+
dir: 'restify',
78+
cmd: 'node',
79+
arg1: 'server.js',
80+
msg: 'Hello World! Restify.js on Google App Engine.'
81+
}
82+
];
83+
84+
if (process.env.TRAVIS_NODE_VERSION !== 'stable') {
85+
// For some reason the "npm install" step for the Sails sample doesn't work on
86+
// Travis when using Node.js stable. It works locally, however.
87+
sampleTests.push({
88+
dir: 'sails',
89+
cmd: 'node',
90+
arg1: 'app.js',
91+
msg: 'Hello World! Sails.js on Google App Engine.',
92+
timeout: 240000
93+
});
94+
}
95+
96+
describe('appengine/', function () {
97+
sampleTests.forEach(function (sample) {
98+
it(sample.dir + ': dependencies should install', function (done) {
99+
this.timeout(sample.timeout || 120000);
100+
var calledDone = false;
101+
102+
var proc = spawn('npm', ['install'], {
103+
cwd: getPath(sample.dir)
104+
});
105+
106+
proc.on('error', function (err) {
107+
if (!calledDone) {
108+
calledDone = true;
109+
done(err);
110+
}
111+
});
112+
113+
if (!process.env.TRAVIS) {
114+
proc.stderr.on('data', function (data) {
115+
console.log('stderr: ' + data);
116+
});
117+
}
118+
119+
proc.on('exit', function (code) {
120+
if (!calledDone) {
121+
calledDone = true;
122+
if (code !== 0) {
123+
done(new Error(sample.dir + ': failed to install dependencies!'));
124+
} else {
125+
done();
126+
}
127+
}
128+
});
129+
});
130+
131+
it(sample.dir + ': should return 200 and Hello World', function (done) {
132+
var timeoutId;
133+
var intervalId;
134+
var success = false;
135+
var calledDone = false;
136+
137+
var proc = spawn(sample.cmd, [sample.arg1], {
138+
cwd: getPath(sample.dir)
139+
});
140+
141+
proc.on('error', function (err) {
142+
if (!calledDone) {
143+
calledDone = true;
144+
done(err);
145+
}
146+
});
147+
148+
if (!process.env.TRAVIS) {
149+
proc.stderr.on('data', function (data) {
150+
console.log('stderr: ' + data);
151+
});
152+
}
153+
154+
proc.on('exit', function (code, signal) {
155+
if (!calledDone) {
156+
calledDone = true;
157+
if (code !== 0 && signal !== 'SIGKILL') {
158+
done(new Error(sample.dir + ': failed to run!'));
159+
} else {
160+
if (!success) {
161+
done(new Error(sample.dir + ': failed verification!'));
162+
} else {
163+
done();
164+
}
165+
}
166+
}
167+
});
168+
169+
timeoutId = setTimeout(end, 5000);
170+
intervalId = setInterval(testRequest, 1000);
171+
172+
function end() {
173+
clearTimeout(timeoutId);
174+
clearInterval(intervalId);
175+
proc.kill('SIGKILL');
176+
}
177+
178+
function testRequest() {
179+
request('http://localhost:8080', function (err, res, body) {
180+
if (body && body.indexOf(sample.msg) !== -1 &&
181+
(res.statusCode === 200 || res.statusCode === sample.code)) {
182+
success = true;
183+
end();
184+
}
185+
});
186+
}
187+
});
188+
});
189+
});

0 commit comments

Comments
 (0)