@@ -856,6 +856,80 @@ describe('Debuglet', () => {
856856 debuglet . start ( ) ;
857857 } ) ;
858858
859+ it ( 'should attempt to retreive region correctly if needed' , done => {
860+ const savedGetPlatform = Debuglet . getPlatform ;
861+ Debuglet . getPlatform = ( ) => Platforms . CLOUD_FUNCTION ;
862+
863+ const clusterScope = nock ( gcpMetadata . HOST_ADDRESS )
864+ . get ( '/computeMetadata/v1/instance/region' )
865+ . once ( )
866+ . reply ( 200 , '123/456/region_name' , gcpMetadata . HEADERS ) ;
867+
868+ const debug = new Debug (
869+ { projectId : 'fake-project' , credentials : fakeCredentials } ,
870+ packageInfo
871+ ) ;
872+
873+ nocks . oauth2 ( ) ;
874+
875+ const config = debugletConfig ( ) ;
876+ const debuglet = new Debuglet ( debug , config ) ;
877+ const scope = nock ( config . apiUrl )
878+ . post ( REGISTER_PATH )
879+ . reply ( 200 , {
880+ debuggee : { id : DEBUGGEE_ID } ,
881+ } ) ;
882+
883+ debuglet . once ( 'registered' , ( ) => {
884+ Debuglet . getPlatform = savedGetPlatform ;
885+ assert . strictEqual (
886+ ( debuglet . debuggee as Debuggee ) . labels ?. region ,
887+ 'region_name'
888+ ) ;
889+ debuglet . stop ( ) ;
890+ clusterScope . done ( ) ;
891+ scope . done ( ) ;
892+ done ( ) ;
893+ } ) ;
894+
895+ debuglet . start ( ) ;
896+ } ) ;
897+
898+ it ( 'should continue to register when could not get region' , done => {
899+ const savedGetPlatform = Debuglet . getPlatform ;
900+ Debuglet . getPlatform = ( ) => Platforms . CLOUD_FUNCTION ;
901+
902+ const clusterScope = nock ( gcpMetadata . HOST_ADDRESS )
903+ . get ( '/computeMetadata/v1/instance/region' )
904+ . once ( )
905+ . reply ( 400 ) ;
906+
907+ const debug = new Debug (
908+ { projectId : 'fake-project' , credentials : fakeCredentials } ,
909+ packageInfo
910+ ) ;
911+
912+ nocks . oauth2 ( ) ;
913+
914+ const config = debugletConfig ( ) ;
915+ const debuglet = new Debuglet ( debug , config ) ;
916+ const scope = nock ( config . apiUrl )
917+ . post ( REGISTER_PATH )
918+ . reply ( 200 , {
919+ debuggee : { id : DEBUGGEE_ID } ,
920+ } ) ;
921+
922+ debuglet . once ( 'registered' , ( ) => {
923+ Debuglet . getPlatform = savedGetPlatform ;
924+ debuglet . stop ( ) ;
925+ clusterScope . done ( ) ;
926+ scope . done ( ) ;
927+ done ( ) ;
928+ } ) ;
929+
930+ debuglet . start ( ) ;
931+ } ) ;
932+
859933 it ( 'should pass config source context to api' , done => {
860934 const REPO_URL =
861935 'https://github.com/non-existent-users/non-existend-repo' ;
@@ -1462,7 +1536,8 @@ describe('Debuglet', () => {
14621536 { service : 'some-service' , version : 'production' } ,
14631537 { } ,
14641538 false ,
1465- packageInfo
1539+ packageInfo ,
1540+ Platforms . DEFAULT
14661541 ) ;
14671542 assert . ok ( debuggee ) ;
14681543 assert . ok ( debuggee . labels ) ;
@@ -1477,7 +1552,8 @@ describe('Debuglet', () => {
14771552 { service : 'default' , version : 'yellow.5' } ,
14781553 { } ,
14791554 false ,
1480- packageInfo
1555+ packageInfo ,
1556+ Platforms . DEFAULT
14811557 ) ;
14821558 assert . ok ( debuggee ) ;
14831559 assert . ok ( debuggee . labels ) ;
@@ -1493,6 +1569,7 @@ describe('Debuglet', () => {
14931569 { } ,
14941570 false ,
14951571 packageInfo ,
1572+ Platforms . DEFAULT ,
14961573 undefined ,
14971574 'Some Error Message'
14981575 ) ;
@@ -1507,7 +1584,8 @@ describe('Debuglet', () => {
15071584 { enableCanary : true , allowCanaryOverride : true } ,
15081585 { } ,
15091586 false ,
1510- packageInfo
1587+ packageInfo ,
1588+ Platforms . DEFAULT
15111589 ) ;
15121590 assert . strictEqual ( debuggee . canaryMode , 'CANARY_MODE_DEFAULT_ENABLED' ) ;
15131591 } ) ;
@@ -1519,7 +1597,8 @@ describe('Debuglet', () => {
15191597 { enableCanary : true , allowCanaryOverride : false } ,
15201598 { } ,
15211599 false ,
1522- packageInfo
1600+ packageInfo ,
1601+ Platforms . DEFAULT
15231602 ) ;
15241603 assert . strictEqual ( debuggee . canaryMode , 'CANARY_MODE_ALWAYS_ENABLED' ) ;
15251604 } ) ;
@@ -1531,7 +1610,8 @@ describe('Debuglet', () => {
15311610 { enableCanary : false , allowCanaryOverride : true } ,
15321611 { } ,
15331612 false ,
1534- packageInfo
1613+ packageInfo ,
1614+ Platforms . DEFAULT
15351615 ) ;
15361616 assert . strictEqual ( debuggee . canaryMode , 'CANARY_MODE_DEFAULT_DISABLED' ) ;
15371617 } ) ;
@@ -1543,54 +1623,65 @@ describe('Debuglet', () => {
15431623 { enableCanary : false , allowCanaryOverride : false } ,
15441624 { } ,
15451625 false ,
1546- packageInfo
1626+ packageInfo ,
1627+ Platforms . DEFAULT
15471628 ) ;
15481629 assert . strictEqual ( debuggee . canaryMode , 'CANARY_MODE_ALWAYS_DISABLED' ) ;
15491630 } ) ;
1631+ } ) ;
15501632
1633+ describe ( 'getPlatform' , ( ) => {
15511634 it ( 'should correctly identify default platform.' , ( ) => {
1552- const debuggee = Debuglet . createDebuggee (
1553- 'some project' ,
1554- 'id' ,
1555- { service : 'some-service' , version : 'production' } ,
1556- { } ,
1557- false ,
1558- packageInfo
1559- ) ;
1560- assert . ok ( debuggee . labels ! . platform === Platforms . DEFAULT ) ;
1635+ assert . ok ( Debuglet . getPlatform ( ) === Platforms . DEFAULT ) ;
15611636 } ) ;
15621637
15631638 it ( 'should correctly identify GCF (legacy) platform.' , ( ) => {
15641639 // GCF sets this env var on older runtimes.
15651640 process . env . FUNCTION_NAME = 'mock' ;
1566- const debuggee = Debuglet . createDebuggee (
1567- 'some project' ,
1568- 'id' ,
1569- { service : 'some-service' , version : 'production' } ,
1570- { } ,
1571- false ,
1572- packageInfo
1573- ) ;
1574- assert . ok ( debuggee . labels ! . platform === Platforms . CLOUD_FUNCTION ) ;
1641+ assert . ok ( Debuglet . getPlatform ( ) === Platforms . CLOUD_FUNCTION ) ;
15751642 delete process . env . FUNCTION_NAME ; // Don't contaminate test environment.
15761643 } ) ;
15771644
15781645 it ( 'should correctly identify GCF (modern) platform.' , ( ) => {
15791646 // GCF sets this env var on modern runtimes.
15801647 process . env . FUNCTION_TARGET = 'mock' ;
1581- const debuggee = Debuglet . createDebuggee (
1582- 'some project' ,
1583- 'id' ,
1584- { service : 'some-service' , version : 'production' } ,
1585- { } ,
1586- false ,
1587- packageInfo
1588- ) ;
1589- assert . ok ( debuggee . labels ! . platform === Platforms . CLOUD_FUNCTION ) ;
1648+ assert . ok ( Debuglet . getPlatform ( ) === Platforms . CLOUD_FUNCTION ) ;
15901649 delete process . env . FUNCTION_TARGET ; // Don't contaminate test environment.
15911650 } ) ;
15921651 } ) ;
15931652
1653+ describe ( 'getRegion' , ( ) => {
1654+ it ( 'should return function region for older GCF runtime' , async ( ) => {
1655+ process . env . FUNCTION_REGION = 'mock' ;
1656+
1657+ assert . ok ( ( await Debuglet . getRegion ( ) ) === 'mock' ) ;
1658+
1659+ delete process . env . FUNCTION_REGION ;
1660+ } ) ;
1661+
1662+ it ( 'should return region for newer GCF runtime' , async ( ) => {
1663+ const clusterScope = nock ( gcpMetadata . HOST_ADDRESS )
1664+ . get ( '/computeMetadata/v1/instance/region' )
1665+ . once ( )
1666+ . reply ( 200 , '123/456/region_name' , gcpMetadata . HEADERS ) ;
1667+
1668+ assert . ok ( ( await Debuglet . getRegion ( ) ) === 'region_name' ) ;
1669+
1670+ clusterScope . done ( ) ;
1671+ } ) ;
1672+
1673+ it ( 'should return undefined when cannot get region metadata' , async ( ) => {
1674+ const clusterScope = nock ( gcpMetadata . HOST_ADDRESS )
1675+ . get ( '/computeMetadata/v1/instance/region' )
1676+ . once ( )
1677+ . reply ( 400 ) ;
1678+
1679+ assert . ok ( ( await Debuglet . getRegion ( ) ) === undefined ) ;
1680+
1681+ clusterScope . done ( ) ;
1682+ } ) ;
1683+ } ) ;
1684+
15941685 describe ( '_createUniquifier' , ( ) => {
15951686 it ( 'should create a unique string' , ( ) => {
15961687 const fn = Debuglet . _createUniquifier ;
0 commit comments