@@ -36,22 +36,26 @@ function sterilizeServiceConfigEnv() {
3636 delete process . env [ key ] ;
3737 } ) ;
3838}
39- function setEnv (
40- serviceName : string | null ,
41- serviceVersion : string ,
42- moduleName : string | null ,
43- mv : string ,
44- fn : string | null
45- ) {
39+ function setEnv ( envData : {
40+ gaeServiceName : string | null ;
41+ gaeServiceVersion : string ;
42+ gaeModuleName : string | null ;
43+ gaeModuleVersion : string ;
44+ functionName : string | null ;
45+ kService : string | null ;
46+ kRevision : string | null ;
47+ } ) {
4648 Object . assign (
4749 process . env ,
4850 omitBy (
4951 {
50- GAE_SERVICE : serviceName ,
51- GAE_VERSION : serviceVersion ,
52- GAE_MODULE_NAME : moduleName ,
53- GAE_MODULE_VERSION : mv ,
54- FUNCTION_NAME : fn ,
52+ GAE_SERVICE : envData . gaeServiceName ,
53+ GAE_VERSION : envData . gaeServiceVersion ,
54+ GAE_MODULE_NAME : envData . gaeModuleName ,
55+ GAE_MODULE_VERSION : envData . gaeModuleVersion ,
56+ FUNCTION_NAME : envData . functionName ,
57+ K_SERVICE : envData . kService ,
58+ K_REVISION : envData . kRevision ,
5559 } ,
5660 val => {
5761 return ! is . string ( val ) ;
@@ -74,13 +78,15 @@ describe('Testing service configuration', () => {
7478 'A Configuration uses the function name as the service name on GCF ' +
7579 'if the service name is not given in the given config' ,
7680 ( ) => {
77- setEnv (
78- 'someModuleName' ,
79- '1.0' ,
80- 'InvalidName' ,
81- 'InvalidVersion' ,
82- 'someFunction'
83- ) ;
81+ setEnv ( {
82+ gaeServiceName : 'someModuleName' ,
83+ gaeServiceVersion : '1.0' ,
84+ gaeModuleName : 'InvalidName' ,
85+ gaeModuleVersion : 'InvalidVersion' ,
86+ functionName : 'someFunction' ,
87+ kService : null ,
88+ kRevision : null ,
89+ } ) ;
8490 const c = new Configuration ( { } , logger ) ;
8591 deepStrictEqual ( c . getServiceContext ( ) . service , 'someFunction' ) ;
8692 // FUNCTION_NAME is set and the user didn't specify a version, and so
@@ -93,7 +99,15 @@ describe('Testing service configuration', () => {
9399 'if the service name is not given in the given config ' +
94100 'even if the GAE_SERVICE was not set' ,
95101 ( ) => {
96- setEnv ( null , '1.0' , null , 'InvalidVersion' , 'someFunction' ) ;
102+ setEnv ( {
103+ gaeServiceName : null ,
104+ gaeServiceVersion : '1.0' ,
105+ gaeModuleName : null ,
106+ gaeModuleVersion : 'InvalidVersion' ,
107+ functionName : 'someFunction' ,
108+ kService : null ,
109+ kRevision : null ,
110+ } ) ;
97111 const c = new Configuration ( { } , logger ) ;
98112 deepStrictEqual ( c . getServiceContext ( ) . service , 'someFunction' ) ;
99113 // The user didn't specify a version and FUNCTION_NAME is defined, and
@@ -106,7 +120,15 @@ describe('Testing service configuration', () => {
106120 'if the FUNCTION_NAME env variable is not set and the given config ' +
107121 'does not specify the service name' ,
108122 ( ) => {
109- setEnv ( 'someModuleName' , '1.0' , 'InvalidName' , 'InvalidVersion' , null ) ;
123+ setEnv ( {
124+ gaeServiceName : 'someModuleName' ,
125+ gaeServiceVersion : '1.0' ,
126+ gaeModuleName : 'InvalidName' ,
127+ gaeModuleVersion : 'InvalidVersion' ,
128+ functionName : null ,
129+ kService : null ,
130+ kRevision : null ,
131+ } ) ;
110132 const c = new Configuration ( { } , logger ) ;
111133 deepStrictEqual ( c . getServiceContext ( ) . service , 'someModuleName' ) ;
112134 // The user didn't specify a version, and FUNCTION_NAME is not defined,
@@ -119,13 +141,15 @@ describe('Testing service configuration', () => {
119141 'was specified and both the GAE_SERVICE and FUNCTION_NAME ' +
120142 'env vars are set' ,
121143 ( ) => {
122- setEnv (
123- 'someModuleName' ,
124- '1.0' ,
125- 'InvalidName' ,
126- 'InvalidVersion' ,
127- 'someFunction'
128- ) ;
144+ setEnv ( {
145+ gaeServiceName : 'someModuleName' ,
146+ gaeServiceVersion : '1.0' ,
147+ gaeModuleName : 'InvalidName' ,
148+ gaeModuleVersion : 'InvalidVersion' ,
149+ functionName : 'someFunction' ,
150+ kService : null ,
151+ kRevision : null ,
152+ } ) ;
129153 const c = new Configuration (
130154 {
131155 serviceContext : {
@@ -145,13 +169,15 @@ describe('Testing service configuration', () => {
145169 'they were both specified and both the GAE_SERVICE and FUNCTION_NAME ' +
146170 'env vars are set' ,
147171 ( ) => {
148- setEnv (
149- 'someModuleName' ,
150- '1.0' ,
151- 'InvalidName' ,
152- 'InvalidVersion' ,
153- 'someFunction'
154- ) ;
172+ setEnv ( {
173+ gaeServiceName : 'someModuleName' ,
174+ gaeServiceVersion : '1.0' ,
175+ gaeModuleName : 'InvalidName' ,
176+ gaeModuleVersion : 'InvalidVersion' ,
177+ functionName : 'someFunction' ,
178+ kService : null ,
179+ kRevision : null ,
180+ } ) ;
155181 const c = new Configuration (
156182 {
157183 serviceContext : {
@@ -170,7 +196,15 @@ describe('Testing service configuration', () => {
170196 'A Configuration uses the service name in the given config if it ' +
171197 'was specified and only the GAE_SERVICE env const is set' ,
172198 ( ) => {
173- setEnv ( 'someModuleName' , '1.0' , 'InvalidName' , 'InvalidVersion' , null ) ;
199+ setEnv ( {
200+ gaeServiceName : 'someModuleName' ,
201+ gaeServiceVersion : '1.0' ,
202+ gaeModuleName : 'InvalidName' ,
203+ gaeModuleVersion : 'InvalidVersion' ,
204+ functionName : null ,
205+ kService : null ,
206+ kRevision : null ,
207+ } ) ;
174208 const c = new Configuration (
175209 {
176210 serviceContext : {
@@ -189,7 +223,15 @@ describe('Testing service configuration', () => {
189223 'A Configuration uses the service name and version in the given config ' +
190224 'they were both specified and only the GAE_SERVICE env const is set' ,
191225 ( ) => {
192- setEnv ( 'someModuleName' , '1.0' , 'InvalidName' , 'InvalidVersion' , null ) ;
226+ setEnv ( {
227+ gaeServiceName : 'someModuleName' ,
228+ gaeServiceVersion : '1.0' ,
229+ gaeModuleName : 'InvalidName' ,
230+ gaeModuleVersion : 'InvalidVersion' ,
231+ functionName : null ,
232+ kService : null ,
233+ kRevision : null ,
234+ } ) ;
193235 const c = new Configuration (
194236 {
195237 serviceContext : {
@@ -208,7 +250,15 @@ describe('Testing service configuration', () => {
208250 'A Configuration uses the service name in the given config if it ' +
209251 'was specified and only the FUNCTION_NAME env const is set' ,
210252 ( ) => {
211- setEnv ( null , '1.0' , null , 'InvalidVersion' , 'someFunction' ) ;
253+ setEnv ( {
254+ gaeServiceName : null ,
255+ gaeServiceVersion : '1.0' ,
256+ gaeModuleName : null ,
257+ gaeModuleVersion : 'InvalidVersion' ,
258+ functionName : 'someFunction' ,
259+ kService : null ,
260+ kRevision : null ,
261+ } ) ;
212262 const c = new Configuration (
213263 {
214264 serviceContext : {
@@ -227,7 +277,15 @@ describe('Testing service configuration', () => {
227277 'A Configuration uses the service name and version in the given config ' +
228278 'if they were both specified and only the FUNCTION_NAME env const is set' ,
229279 ( ) => {
230- setEnv ( null , '1.0' , null , 'InvalidVersion' , 'someFunction' ) ;
280+ setEnv ( {
281+ gaeServiceName : null ,
282+ gaeServiceVersion : '1.0' ,
283+ gaeModuleName : null ,
284+ gaeModuleVersion : 'InvalidVersion' ,
285+ functionName : 'someFunction' ,
286+ kService : null ,
287+ kRevision : null ,
288+ } ) ;
231289 const c = new Configuration (
232290 {
233291 serviceContext : {
@@ -258,7 +316,15 @@ describe('Testing service configuration', () => {
258316 'not specified a service name or version even if GAE_VERSION has ' +
259317 'been set' ,
260318 ( ) => {
261- setEnv ( null , 'InvalidVersion' , null , 'InvalidVersion' , null ) ;
319+ setEnv ( {
320+ gaeServiceName : null ,
321+ gaeServiceVersion : 'InvalidVersion' ,
322+ gaeModuleName : null ,
323+ gaeModuleVersion : 'InvalidVersion' ,
324+ functionName : null ,
325+ kService : null ,
326+ kRevision : null ,
327+ } ) ;
262328 const c = new Configuration ( { } , logger ) ;
263329 assert . strictEqual ( c . getServiceContext ( ) . service , 'node' ) ;
264330 assert . strictEqual ( c . getServiceContext ( ) . version , undefined ) ;
@@ -281,4 +347,32 @@ describe('Testing service configuration', () => {
281347 deepStrictEqual ( c . getServiceContext ( ) . version , '2.0' ) ;
282348 }
283349 ) ;
350+ it ( 'A Configuration uses the K_SERVICE and K_REVISION env variables if set' , ( ) => {
351+ setEnv ( {
352+ gaeServiceName : null ,
353+ gaeServiceVersion : 'x' ,
354+ gaeModuleName : null ,
355+ gaeModuleVersion : 'y' ,
356+ functionName : null ,
357+ kService : 'custom-service' ,
358+ kRevision : 'custom-revision' ,
359+ } ) ;
360+ const c = new Configuration ( { } , logger ) ;
361+ assert . strictEqual ( c . getServiceContext ( ) . service , 'custom-service' ) ;
362+ assert . strictEqual ( c . getServiceContext ( ) . version , 'custom-revision' ) ;
363+ } ) ;
364+ it ( 'A Configuration gives priority to K_SERVICE and K_REVISION env variables' , ( ) => {
365+ setEnv ( {
366+ gaeServiceName : 'gae-service-name' ,
367+ gaeServiceVersion : 'gae-service-version' ,
368+ gaeModuleName : 'gae-module-name' ,
369+ gaeModuleVersion : 'gae-module-version' ,
370+ functionName : 'function-name' ,
371+ kService : 'k-service' ,
372+ kRevision : 'k-revision' ,
373+ } ) ;
374+ const c = new Configuration ( { } , logger ) ;
375+ assert . strictEqual ( c . getServiceContext ( ) . service , 'k-service' ) ;
376+ assert . strictEqual ( c . getServiceContext ( ) . version , 'k-revision' ) ;
377+ } ) ;
284378} ) ;
0 commit comments