Skip to content

Commit fb13c02

Browse files
committed
Add deployment tests. Closes #21.
1 parent 5469269 commit fb13c02

File tree

5 files changed

+152
-16
lines changed

5 files changed

+152
-16
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
**/node_modules/**
22
npm-debug.log
3-
coverage/
3+
coverage/
4+
5+
test/encrypted/secrets.tar
6+
test/encrypted/express-demo.json
7+
test/encrypted/hapi-demo.json

.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,22 @@ before_install:
4444
printf '\ny\n\ny\ny\n' | ./google-cloud-sdk/install.sh &&
4545
cd $TRAVIS_BUILD_DIR;
4646
fi
47+
- gcloud components update -q
48+
- gcloud components update preview -q
4749
- openssl aes-256-cbc -K $encrypted_95e832a36b06_key -iv $encrypted_95e832a36b06_iv -in nodejs-docs-samples.json.enc -out nodejs-docs-samples.json -d
4850
- if [ -a nodejs-docs-samples.json ]; then
4951
gcloud auth activate-service-account --key-file nodejs-docs-samples.json;
5052
fi
53+
- openssl aes-256-cbc -K $encrypted_4e84c7c7ab67_key -iv $encrypted_4e84c7c7ab67_iv -in test/encrypted/secrets.tar.enc -out test/encrypted/secrets.tar -d
54+
- if [ -a test/encrypted/secrets.tar ]; then
55+
cd test/encrypted && tar xvf secrets.tar && cd ../..;
56+
fi
57+
- if [ -a test/encrypted/express-demo.json ]; then
58+
gcloud auth activate-service-account --key-file test/encrypted/express-demo.json;
59+
fi
60+
- if [ -a test/encrypted/hapi-demo.json ]; then
61+
gcloud auth activate-service-account --key-file test/encrypted/hapi-demo.json;
62+
fi
5163

5264
install:
5365
#Add app specific setup here

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"googleapis": "~2.1.3"
2727
},
2828
"devDependencies": {
29+
"async": "^1.5.0",
2930
"coveralls": "^2.11.4",
3031
"istanbul": "^0.4.0",
3132
"jshint": "~2.8.0",

test/appengine/test.js

+134-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
var spawn = require('child_process').spawn;
1717
var request = require('request');
18+
var async = require('async');
1819

1920
var cwd = process.cwd();
2021

@@ -25,6 +26,7 @@ function getPath(dir) {
2526
var sampleTests = [
2627
{
2728
dir: 'express',
29+
projectId: 'express-demo',
2830
cmd: 'node',
2931
arg1: './bin/www',
3032
msg: 'Hello World! Express.js on Google App Engine.'
@@ -43,6 +45,7 @@ var sampleTests = [
4345
},
4446
{
4547
dir: 'hapi',
48+
projectId: 'hapi-demo',
4649
cmd: 'node',
4750
arg1: 'index.js',
4851
msg: 'Hello World! Hapi.js on Google App Engine.'
@@ -93,6 +96,36 @@ if (process.env.TRAVIS_NODE_VERSION !== 'stable') {
9396
});
9497
}
9598

99+
function end(timeoutId, intervalId, proc) {
100+
clearTimeout(timeoutId);
101+
clearInterval(intervalId);
102+
proc.kill('SIGKILL');
103+
}
104+
105+
function testRequest(url, sample, cb) {
106+
request(url, function (err, res, body) {
107+
if (err) {
108+
cb(err, false);
109+
} else {
110+
if (body && body.indexOf(sample.msg) !== -1 &&
111+
(res.statusCode === 200 || res.statusCode === sample.code)) {
112+
cb(null, true);
113+
} else {
114+
cb(null, false);
115+
}
116+
}
117+
});
118+
}
119+
120+
function shouldHide(data) {
121+
if (data && typeof data === 'function' && (data.indexOf('.../') !== -1 ||
122+
data.indexOf('...-') !== -1 ||
123+
data.indexOf('...\\') !== -1 ||
124+
data.indexOf('...|') !== -1)) {
125+
return true;
126+
}
127+
}
128+
96129
describe('appengine/', function () {
97130
sampleTests.forEach(function (sample) {
98131
it(sample.dir + ': dependencies should install', function (done) {
@@ -166,24 +199,110 @@ describe('appengine/', function () {
166199
}
167200
});
168201

169-
timeoutId = setTimeout(end, 5000);
170-
intervalId = setInterval(testRequest, 1000);
202+
timeoutId = setTimeout(function () {
203+
end(timeoutId, intervalId, proc);
204+
}, 10000);
205+
206+
// Give the server time to start up
207+
setTimeout(function () {
208+
intervalId = setInterval(function () {
209+
var url = 'http://localhost:8080';
210+
testRequest(url, sample, function (err, _success) {
211+
if (err) {
212+
console.log(err);
213+
} else {
214+
success = _success;
215+
}
216+
end(timeoutId, intervalId, proc);
217+
});
218+
}, 1000);
219+
}, 2000);
220+
});
221+
});
222+
223+
if (!process.env.TRAVIS || process.env.TRAVIS_NODE_VERSION !== 'stable') {
224+
return;
225+
}
171226

172-
function end() {
173-
clearTimeout(timeoutId);
174-
clearInterval(intervalId);
175-
proc.kill('SIGKILL');
176-
}
227+
it('should deploy all samples', function (done) {
228+
this.timeout(10 * 60 * 1000); // 10 minutes
229+
async.parallel(sampleTests.map(function (sample) {
230+
if (sample.projectId) {
231+
return function (cb) {
232+
var calledDone = false;
233+
var proc = spawn('gcloud', [
234+
'preview',
235+
'app',
236+
'deploy',
237+
'app.yaml',
238+
'-q',
239+
'--project',
240+
sample.projectId,
241+
'--promote',
242+
'--version',
243+
'demo'
244+
], {
245+
cwd: getPath(sample.dir)
246+
});
177247

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();
248+
function finish(err) {
249+
if (!calledDone) {
250+
calledDone = true;
251+
cb(err);
252+
}
184253
}
185-
});
254+
255+
proc.stderr.on('data', function (data) {
256+
if (shouldHide(data)) {
257+
return;
258+
}
259+
console.log(sample.projectId + ' stderr: ' + data);
260+
});
261+
proc.stdout.on('data', function (data) {
262+
if (shouldHide(data)) {
263+
return;
264+
}
265+
console.log(sample.projectId + ' stdout: ' + data);
266+
});
267+
proc.on('error', finish);
268+
proc.on('exit', function (code) {
269+
if (code !== 0) {
270+
finish(new Error(sample.dir + ': failed to deploy!'));
271+
} else {
272+
var url = 'http://' + sample.projectId + '.appspot.com';
273+
var demoUrl = 'http://demo.' + sample.projectId + '.appspot.com';
274+
async.waterfall([
275+
function (cb) {
276+
setTimeout(cb, 10000);
277+
},
278+
function (cb) {
279+
// Test "default" module
280+
testRequest(url, sample, cb);
281+
},
282+
function (result, cb) {
283+
if (!result) {
284+
cb(new Error(sample.dir + ': failed verification!'));
285+
} else {
286+
cb();
287+
}
288+
},
289+
function (cb) {
290+
// Test versioned url of "default" module
291+
testRequest(demoUrl, sample, cb);
292+
},
293+
function (result, cb) {
294+
if (!result) {
295+
cb(new Error(sample.dir + ': failed verification!'));
296+
} else {
297+
cb();
298+
}
299+
}
300+
], finish);
301+
}
302+
});
303+
};
186304
}
187-
});
305+
return function (cb) { cb(); };
306+
}), done);
188307
});
189308
});

test/encrypted/secrets.tar.enc

13 KB
Binary file not shown.

0 commit comments

Comments
 (0)