1515
1616var spawn = require ( 'child_process' ) . spawn ;
1717var request = require ( 'request' ) ;
18+ var async = require ( 'async' ) ;
1819
1920var cwd = process . cwd ( ) ;
2021
@@ -25,6 +26,7 @@ function getPath(dir) {
2526var 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.'
@@ -93,6 +95,36 @@ if (process.env.TRAVIS_NODE_VERSION !== 'stable') {
9395 } ) ;
9496}
9597
98+ function end ( timeoutId , intervalId , proc ) {
99+ clearTimeout ( timeoutId ) ;
100+ clearInterval ( intervalId ) ;
101+ proc . kill ( 'SIGKILL' ) ;
102+ }
103+
104+ function testRequest ( url , sample , cb ) {
105+ request ( url , function ( err , res , body ) {
106+ if ( err ) {
107+ cb ( err , false ) ;
108+ } else {
109+ if ( body && body . indexOf ( sample . msg ) !== - 1 &&
110+ ( res . statusCode === 200 || res . statusCode === sample . code ) ) {
111+ cb ( null , true ) ;
112+ } else {
113+ cb ( null , false ) ;
114+ }
115+ }
116+ } ) ;
117+ }
118+
119+ function shouldHide ( data ) {
120+ if ( data . indexOf ( '.../' ) !== - 1 ||
121+ data . indexOf ( '...-' ) !== - 1 ||
122+ data . indexOf ( '...\\' ) !== - 1 ||
123+ data . indexOf ( '...|' ) !== - 1 ) {
124+ return true ;
125+ }
126+ }
127+
96128describe ( 'appengine/' , function ( ) {
97129 sampleTests . forEach ( function ( sample ) {
98130 it ( sample . dir + ': dependencies should install' , function ( done ) {
@@ -166,24 +198,105 @@ describe('appengine/', function () {
166198 }
167199 } ) ;
168200
169- timeoutId = setTimeout ( end , 5000 ) ;
170- intervalId = setInterval ( testRequest , 1000 ) ;
201+ timeoutId = setTimeout ( function ( ) {
202+ end ( timeoutId , intervalId , proc ) ;
203+ } , 5000 ) ;
204+ intervalId = setInterval ( function ( ) {
205+ testRequest ( 'http://localhost:8080' , sample , function ( err , _success ) {
206+ if ( err ) {
207+ console . log ( err ) ;
208+ } else {
209+ success = _success ;
210+ }
211+ end ( timeoutId , intervalId , proc ) ;
212+ } ) ;
213+ } , 1000 ) ;
214+ } ) ;
215+ } ) ;
171216
172- function end ( ) {
173- clearTimeout ( timeoutId ) ;
174- clearInterval ( intervalId ) ;
175- proc . kill ( 'SIGKILL' ) ;
176- }
217+ if ( ! process . env . TRAVIS ) {
218+ return ;
219+ }
177220
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 ( ) ;
221+ it ( 'should deploy all samples' , function ( done ) {
222+ this . timeout ( 10 * 60 * 1000 ) ; // 10 minutes
223+ async . parallel ( sampleTests . map ( function ( sample ) {
224+ if ( sample . projectId ) {
225+ return function ( cb ) {
226+ var calledDone = false ;
227+ var proc = spawn ( 'gcloud' , [
228+ 'preview' ,
229+ 'app' ,
230+ 'deploy' ,
231+ 'app.yaml' ,
232+ '-q' ,
233+ '--project' ,
234+ sample . projectId ,
235+ '--promote' ,
236+ '--version' ,
237+ 'demo'
238+ ] , {
239+ cwd : getPath ( sample . dir )
240+ } ) ;
241+
242+ function finish ( err ) {
243+ if ( ! calledDone ) {
244+ calledDone = true ;
245+ cb ( err ) ;
246+ }
184247 }
185- } ) ;
248+
249+ proc . stderr . on ( 'data' , function ( data ) {
250+ if ( shouldHide ( data ) ) {
251+ return ;
252+ }
253+ console . log ( sample . projectId + ' stderr: ' + data ) ;
254+ } ) ;
255+ proc . stdout . on ( 'data' , function ( data ) {
256+ if ( shouldHide ( data ) ) {
257+ return ;
258+ }
259+ console . log ( sample . projectId + ' stdout: ' + data ) ;
260+ } ) ;
261+ proc . on ( 'error' , finish ) ;
262+ proc . on ( 'exit' , function ( code ) {
263+ if ( code !== 0 ) {
264+ finish ( new Error ( sample . dir + ': failed to deploy!' ) ) ;
265+ } else {
266+ var url = 'http://' + sample . projectId + '.appspot.com' ;
267+ var demoUrl = 'http://demo.' + sample . projectId + '.appspot.com' ;
268+ async . waterfall ( [
269+ function ( cb ) {
270+ setTimeout ( cb , 10000 ) ;
271+ } ,
272+ function ( cb ) {
273+ // Test "default" module
274+ testRequest ( url , sample , cb ) ;
275+ } ,
276+ function ( result , cb ) {
277+ if ( ! result ) {
278+ cb ( new Error ( sample . dir + ': failed verification!' ) ) ;
279+ } else {
280+ cb ( ) ;
281+ }
282+ } ,
283+ function ( cb ) {
284+ // Test versioned url of "default" module
285+ testRequest ( demoUrl , sample , cb ) ;
286+ } ,
287+ function ( result , cb ) {
288+ if ( ! result ) {
289+ cb ( new Error ( sample . dir + ': failed verification!' ) ) ;
290+ } else {
291+ cb ( ) ;
292+ }
293+ }
294+ ] , finish ) ;
295+ }
296+ } ) ;
297+ } ;
186298 }
187- } ) ;
299+ return function ( cb ) { cb ( ) ; } ;
300+ } ) , done ) ;
188301 } ) ;
189302} ) ;
0 commit comments