1515 */
1616'use strict' ;
1717
18- var proxyquire = require ( 'proxyquire' ) ;
19- proxyquire ( 'gcp-metadata' , {
20- 'retry-request' : require ( 'request' )
21- } ) ;
22-
2318var _ = require ( 'lodash' ) ;
2419var assert = require ( 'assert' ) ;
2520var DEFAULT_CONFIG = require ( '../build/src/agent/config.js' ) . default ;
@@ -61,6 +56,12 @@ function verifyBreakpointRejection(re, body) {
6156 return status . isError && hasCorrectDescription ;
6257}
6358
59+ function mockedGetProjectId ( cb ) {
60+ setImmediate ( ( ) => {
61+ cb ( new Error ( 'network unavailable' ) ) ;
62+ } ) ;
63+ } ;
64+
6465describe ( 'Debuglet' , function ( ) {
6566 describe ( 'setup' , function ( ) {
6667 before ( function ( ) { oldGP = process . env . GCLOUD_PROJECT ; } ) ;
@@ -87,10 +88,10 @@ describe('Debuglet', function() {
8788 } ) ;
8889
8990 it ( 'should not start when projectId is not available' , function ( done ) {
90- this . timeout ( 8000 ) ;
9191 var debug = require ( '../build/src/debug.js' ) . Debug ( ) ;
9292 var debuglet = new Debuglet ( debug , defaultConfig ) ;
9393
94+ debuglet . getProjectId_ = mockedGetProjectId ;
9495 debuglet . once ( 'initError' , function ( err ) {
9596 assert . ok ( err ) ;
9697 // no need to stop the debuggee.
@@ -101,10 +102,10 @@ describe('Debuglet', function() {
101102 } ) ;
102103
103104 it ( 'should not crash without project num' , function ( done ) {
104- this . timeout ( 8000 ) ;
105105 var debug = require ( '../build/src/debug.js' ) . Debug ( ) ;
106106 var debuglet = new Debuglet ( debug , defaultConfig ) ;
107107
108+ debuglet . getProjectId_ = mockedGetProjectId ;
108109 debuglet . once ( 'started' , function ( ) { assert . fail ( ) ; } ) ;
109110 debuglet . once ( 'initError' , function ( ) {
110111 done ( ) ;
@@ -118,6 +119,7 @@ describe('Debuglet', function() {
118119 { projectId : projectId , credentials : fakeCredentials } ) ;
119120 var debuglet = new Debuglet ( debug , defaultConfig ) ;
120121
122+ nocks . projectId ( 'project-via-metadata' ) ;
121123 var scope = nock ( API )
122124 . post ( REGISTER_PATH )
123125 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } ) ;
@@ -144,6 +146,7 @@ describe('Debuglet', function() {
144146 var debug = require ( '../build/src/debug.js' ) . Debug ( { credentials : fakeCredentials } ) ;
145147 var debuglet = new Debuglet ( debug , defaultConfig ) ;
146148
149+ nocks . projectId ( 'project-via-metadata' ) ;
147150 var scope = nock ( API )
148151 . post ( REGISTER_PATH )
149152 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } ) ;
@@ -168,8 +171,7 @@ describe('Debuglet', function() {
168171 } ) ;
169172 var debuglet = new Debuglet ( debug , defaultConfig ) ;
170173
171- // TODO: also make sure we don't request the project from metadata
172- // service.
174+ nocks . projectId ( 'project-via-metadata' ) ;
173175 var scope = nock ( API )
174176 . post ( REGISTER_PATH )
175177 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } ) ;
@@ -191,6 +193,7 @@ describe('Debuglet', function() {
191193 var debug = require ( '../build/src/debug.js' ) . Debug ( { credentials : fakeCredentials } ) ;
192194 var debuglet = new Debuglet ( debug , defaultConfig ) ;
193195
196+ nocks . projectId ( 'project-via-metadata' ) ;
194197 var scope = nock ( API )
195198 . post ( REGISTER_PATH )
196199 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } ) ;
@@ -311,6 +314,7 @@ describe('Debuglet', function() {
311314 { projectId : 'fake-project' , credentials : fakeCredentials } ) ;
312315 var debuglet = new Debuglet ( debug , defaultConfig ) ;
313316
317+ nocks . projectId ( 'project-via-metadata' ) ;
314318 var scope =
315319 nock ( API ) . post ( REGISTER_PATH , function ( body ) {
316320 assert . ok (
@@ -334,6 +338,7 @@ describe('Debuglet', function() {
334338 var debuglet = new Debuglet ( debug , defaultConfig ) ;
335339
336340 nocks . oauth2 ( ) ;
341+ nocks . projectId ( 'project-via-metadata' ) ;
337342 var scope =
338343 nock ( API ) . post ( REGISTER_PATH , function ( body ) {
339344 assert . ok ( _ . isString ( body . debuggee . labels . minorversion ) ) ;
@@ -357,6 +362,7 @@ describe('Debuglet', function() {
357362 { projectId : '11020304f2934' , credentials : fakeCredentials } ) ;
358363 var debuglet = new Debuglet ( debug , defaultConfig ) ;
359364
365+ nocks . projectId ( 'project-via-metadata' ) ;
360366 var scope = nock ( API )
361367 . post ( REGISTER_PATH )
362368 . reply ( 404 )
@@ -396,6 +402,7 @@ describe('Debuglet', function() {
396402 var debuglet = new Debuglet ( debug , defaultConfig ) ;
397403
398404 nocks . oauth2 ( ) ;
405+ nocks . projectId ( 'project-via-metadata' ) ;
399406 var scope = nock ( API )
400407 . post ( REGISTER_PATH )
401408 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } ) ;
@@ -422,6 +429,7 @@ describe('Debuglet', function() {
422429 } ;
423430 var debuglet = new Debuglet ( debug , defaultConfig ) ;
424431
432+ nocks . projectId ( 'project-via-metadata' ) ;
425433 var scope = nock ( API ) . post ( REGISTER_PATH , function ( body ) {
426434 return body . debuggee . sourceContexts [ 0 ] &&
427435 body . debuggee . sourceContexts [ 0 ] . a === 5 ;
@@ -445,6 +453,7 @@ describe('Debuglet', function() {
445453 { projectId : 'fake-project' , credentials : fakeCredentials } ) ;
446454 var debuglet = new Debuglet ( debug , defaultConfig ) ;
447455
456+ nocks . projectId ( 'project-via-metadata' ) ;
448457 var scope =
449458 nock ( API )
450459 . post ( REGISTER_PATH )
@@ -466,6 +475,7 @@ describe('Debuglet', function() {
466475 { projectId : 'fake-project' , credentials : fakeCredentials } ) ;
467476 var debuglet = new Debuglet ( debug , defaultConfig ) ;
468477
478+ nocks . projectId ( 'project-via-metadata' ) ;
469479 var scope =
470480 nock ( API )
471481 . post ( REGISTER_PATH )
@@ -495,6 +505,7 @@ describe('Debuglet', function() {
495505 { projectId : 'fake-project' , credentials : fakeCredentials } ) ;
496506 var debuglet = new Debuglet ( debug , defaultConfig ) ;
497507
508+ nocks . projectId ( 'project-via-metadata' ) ;
498509 var scope = nock ( API )
499510 . post ( REGISTER_PATH )
500511 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } )
@@ -522,6 +533,7 @@ describe('Debuglet', function() {
522533 { projectId : 'fake-project' , credentials : fakeCredentials } ) ;
523534 var debuglet = new Debuglet ( debug , defaultConfig ) ;
524535
536+ nocks . projectId ( 'project-via-metadata' ) ;
525537 var scope = nock ( API )
526538 . post ( REGISTER_PATH )
527539 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } )
@@ -549,6 +561,7 @@ describe('Debuglet', function() {
549561 var debuglet = new Debuglet ( debug , defaultConfig ) ;
550562 debuglet . config_ . allowExpressions = false ;
551563
564+ nocks . projectId ( 'project-via-metadata' ) ;
552565 var scope = nock ( API )
553566 . post ( REGISTER_PATH )
554567 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } )
@@ -587,6 +600,7 @@ describe('Debuglet', function() {
587600 var debuglet = new Debuglet ( debug , defaultConfig ) ;
588601 debuglet . config_ . allowExpressions = false ;
589602
603+ nocks . projectId ( 'project-via-metadata' ) ;
590604 var scope = nock ( API )
591605 . post ( REGISTER_PATH )
592606 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } )
@@ -624,6 +638,7 @@ describe('Debuglet', function() {
624638 { projectId : 'fake-project' , credentials : fakeCredentials } ) ;
625639 var debuglet = new Debuglet ( debug , defaultConfig ) ;
626640
641+ nocks . projectId ( 'project-via-metadata' ) ;
627642 var scope = nock ( API )
628643 . post ( REGISTER_PATH )
629644 . reply ( 200 , { debuggee : { id : DEBUGGEE_ID } } )
@@ -665,6 +680,7 @@ describe('Debuglet', function() {
665680 { breakpointExpirationSec : 1 , forceNewAgent_ : true } ) ;
666681 this . timeout ( 6000 ) ;
667682
683+ nocks . projectId ( 'project-via-metadata' ) ;
668684 var scope =
669685 nock ( API )
670686 . post ( REGISTER_PATH )
@@ -713,6 +729,7 @@ describe('Debuglet', function() {
713729 } ) ;
714730 this . timeout ( 6000 ) ;
715731
732+ nocks . projectId ( 'project-via-metadata' ) ;
716733 var scope =
717734 nock ( API )
718735 . post ( REGISTER_PATH )
0 commit comments