@@ -11,7 +11,10 @@ import * as gcp from '../src';
1111
1212const assertRejects = require ( 'assert-rejects' ) ;
1313
14+ // the metadata IP entry:
1415const HOST = gcp . HOST_ADDRESS ;
16+ // the metadata DNS entry:
17+ const SECONDARY_HOST = gcp . SECONDARY_HOST_ADDRESS ;
1518const PATH = gcp . BASE_PATH ;
1619const TYPE = 'instance' ;
1720const PROPERTY = 'property' ;
@@ -174,33 +177,106 @@ it('should retry on DNS errors', async () => {
174177 assert ( data ) ;
175178} ) ;
176179
177- it ( 'should report isGCE if the server returns a 500 first' , async ( ) => {
178- const scope = nock ( HOST )
180+ async function secondaryHostRequest (
181+ delay : number ,
182+ responseType = 'success'
183+ ) : Promise < void > {
184+ let secondary : nock . Scope ;
185+ if ( responseType === 'success' ) {
186+ secondary = nock ( SECONDARY_HOST )
187+ . get ( `${ PATH } /${ TYPE } ` )
188+ . delayConnection ( delay )
189+ . reply ( 200 , { } , HEADERS ) ;
190+ } else {
191+ secondary = nock ( SECONDARY_HOST )
192+ . get ( `${ PATH } /${ TYPE } ` )
193+ . delayConnection ( delay )
194+ . replyWithError ( { code : responseType } ) ;
195+ }
196+ return new Promise ( ( resolve , reject ) => {
197+ setTimeout ( ( ) => {
198+ try {
199+ secondary . done ( ) ;
200+ return resolve ( ) ;
201+ } catch ( err ) {
202+ return reject ( err ) ;
203+ }
204+ } , delay + 50 ) ;
205+ } ) ;
206+ }
207+
208+ it ( 'should report isGCE if primary server returns 500 followed by 200' , async ( ) => {
209+ const secondary = secondaryHostRequest ( 500 ) ;
210+ const primary = nock ( HOST )
179211 . get ( `${ PATH } /${ TYPE } ` )
180212 . twice ( )
181213 . reply ( 500 )
182214 . get ( `${ PATH } /${ TYPE } ` )
183215 . reply ( 200 , { } , HEADERS ) ;
184216 const isGCE = await gcp . isAvailable ( ) ;
185- scope . done ( ) ;
217+ await secondary ;
218+ primary . done ( ) ;
186219 assert . strictEqual ( isGCE , true ) ;
187220} ) ;
188221
189222it ( 'should fail fast on isAvailable if ENOTFOUND is returned' , async ( ) => {
190- const scope = nock ( HOST )
223+ const secondary = secondaryHostRequest ( 500 ) ;
224+ const primary = nock ( HOST )
191225 . get ( `${ PATH } /${ TYPE } ` )
192226 . replyWithError ( { code : 'ENOTFOUND' } ) ;
193227 const isGCE = await gcp . isAvailable ( ) ;
194- scope . done ( ) ;
228+ await secondary ;
229+ primary . done ( ) ;
195230 assert . strictEqual ( false , isGCE ) ;
196231} ) ;
197232
198233it ( 'should fail fast on isAvailable if ENOENT is returned' , async ( ) => {
199- const scope = nock ( HOST )
234+ const secondary = secondaryHostRequest ( 500 ) ;
235+ const primary = nock ( HOST )
200236 . get ( `${ PATH } /${ TYPE } ` )
201237 . replyWithError ( { code : 'ENOENT' } ) ;
202238 const isGCE = await gcp . isAvailable ( ) ;
203- scope . done ( ) ;
239+ await secondary ;
240+ primary . done ( ) ;
241+ assert . strictEqual ( false , isGCE ) ;
242+ } ) ;
243+
244+ it ( 'should fail on isAvailable if request times out' , async ( ) => {
245+ const secondary = secondaryHostRequest ( 5000 ) ;
246+ const primary = nock ( HOST )
247+ . get ( `${ PATH } /${ TYPE } ` )
248+ . delayConnection ( 3500 )
249+ // this should never get called, as the 3000 timeout will trigger.
250+ . reply ( 200 , { } , HEADERS ) ;
251+ const isGCE = await gcp . isAvailable ( ) ;
252+ // secondary is allowed to simply timeout in the aether, to avoid
253+ // having a test that waits 5000 ms.
254+ primary . done ( ) ;
255+ assert . strictEqual ( false , isGCE ) ;
256+ } ) ;
257+
258+ it ( 'should report isGCE if secondary responds before primary' , async ( ) => {
259+ const secondary = secondaryHostRequest ( 10 ) ;
260+ const primary = nock ( HOST )
261+ . get ( `${ PATH } /${ TYPE } ` )
262+ . delayConnection ( 3500 )
263+ // this should never get called, as the 3000 timeout will trigger.
264+ . reply ( 200 , { } , HEADERS ) ;
265+ const isGCE = await gcp . isAvailable ( ) ;
266+ await secondary ;
267+ primary . done ( ) ;
268+ assert . strictEqual ( isGCE , true ) ;
269+ } ) ;
270+
271+ it ( 'should fail fast on isAvailable if ENOENT is returned by secondary' , async ( ) => {
272+ const secondary = secondaryHostRequest ( 10 , 'ENOENT' ) ;
273+ const primary = nock ( HOST )
274+ . get ( `${ PATH } /${ TYPE } ` )
275+ . delayConnection ( 250 )
276+ . replyWithError ( { code : 'ENOENT' } ) ;
277+ const isGCE = await gcp . isAvailable ( ) ;
278+ await secondary ;
279+ primary . done ( ) ;
204280 assert . strictEqual ( false , isGCE ) ;
205281} ) ;
206282
0 commit comments