|
| 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