@@ -1243,6 +1243,66 @@ describe('._checkIsGCE', () => {
12431243 assert . equal ( false , auth . isGCE ) ;
12441244 } ) ;
12451245
1246+ it ( 'should retry the check for isGCE if it fails the first time' ,
1247+ async ( ) => {
1248+ const auth = new GoogleAuth ( ) ;
1249+ assert . notEqual ( true , auth . isGCE ) ;
1250+ // the first request will fail
1251+ const scope1 =
1252+ nock ( 'http://metadata.google.internal' ) . get ( '/' ) . reply ( 500 ) ;
1253+ // the second one will succeed
1254+ const scope2 =
1255+ nock ( 'http://metadata.google.internal' ) . get ( '/' ) . reply ( 200 , null , {
1256+ 'metadata-flavor' : 'Google'
1257+ } ) ;
1258+ const isGCE = await auth . _checkIsGCE ( ) ;
1259+ assert . equal ( true , auth . isGCE ) ;
1260+ assert ( scope1 . isDone ( ) ) ;
1261+ assert ( scope2 . isDone ( ) ) ;
1262+ } ) ;
1263+
1264+ it ( 'should not retry the check for isGCE if it fails with a 404' ,
1265+ async ( ) => {
1266+ const auth = new GoogleAuth ( ) ;
1267+ assert . notEqual ( true , auth . isGCE ) ;
1268+ // the first request will fail
1269+ const scope1 =
1270+ nock ( 'http://metadata.google.internal' ) . get ( '/' ) . reply ( 404 ) ;
1271+ // the second one should never happen
1272+ const scope2 =
1273+ nock ( 'http://metadata.google.internal' ) . get ( '/' ) . reply ( 404 ) ;
1274+ try {
1275+ const isGCE = await auth . _checkIsGCE ( ) ;
1276+ assert . notEqual ( true , auth . isGCE ) ;
1277+ } catch ( e ) {
1278+ return ;
1279+ }
1280+ assert ( scope1 . isDone ( ) ) ;
1281+ assert ( ! scope2 . isDone ( ) ) ;
1282+ assert . fail ( 'Expected to throw' ) ;
1283+ } ) ;
1284+
1285+ it ( 'should not retry the check for isGCE if it fails with an ENOTFOUND' ,
1286+ async ( ) => {
1287+ const auth = new GoogleAuth ( ) ;
1288+ assert . notEqual ( true , auth . isGCE ) ;
1289+ // the first request will fail
1290+ const scope1 =
1291+ nock ( 'http://metadata.google.internal' ) . get ( '/' ) . replyWithError ( {
1292+ code : 'ENOTFOUND'
1293+ } ) ;
1294+ // the second one should never happen
1295+ const scope2 =
1296+ nock ( 'http://metadata.google.internal' ) . get ( '/' ) . replyWithError ( {
1297+ code : 'ENOTFOUND'
1298+ } ) ;
1299+
1300+ const isGCE = await auth . _checkIsGCE ( ) ;
1301+ assert . notEqual ( true , auth . isGCE ) ;
1302+ assert ( scope1 . isDone ( ) ) ;
1303+ assert ( ! scope2 . isDone ( ) ) ;
1304+ } ) ;
1305+
12461306 it ( 'Does not execute the second time when running on GCE' , async ( ) => {
12471307 const auth = new GoogleAuth ( ) ;
12481308
0 commit comments