Skip to content

Commit a36dff9

Browse files
author
David Tabachnikov
committed
Add support for function environment variable definition to compileFunction
1 parent 298c0db commit a36dff9

File tree

2 files changed

+80
-39
lines changed

2 files changed

+80
-39
lines changed

package/lib/compileFunctions.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ module.exports = {
4343
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
4444
|| _.get(this, 'serverless.service.provider.timeout')
4545
|| '60s';
46-
funcTemplate.properties.environmentVariables = _.get(funcObject, 'environment')
47-
|| _.get(this, 'serverless.service.provider.environment')
48-
|| null;
46+
funcTemplate.properties.environmentVariables = _.merge(
47+
_.get(this, 'serverless.service.provider.environment'),
48+
funcObject.environment,
49+
);
4950

50-
if (funcTemplate.properties.environmentVariables === null) {
51+
if (!_.size(funcTemplate.properties.environmentVariables)) {
5152
delete funcTemplate.properties.environmentVariables;
5253
}
5354

package/lib/compileFunctions.test.js

+75-35
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('CompileFunctions', () => {
133133
});
134134
});
135135

136-
it('should set the environment variables based on the provider configuration', () => {
136+
it('should set the memory size based on the provider configuration', () => {
137137
googlePackage.serverless.service.functions = {
138138
func1: {
139139
handler: 'func1',
@@ -142,9 +142,7 @@ describe('CompileFunctions', () => {
142142
],
143143
},
144144
};
145-
googlePackage.serverless.service.provider.environment = {
146-
TEST_VAR: 'test',
147-
};
145+
googlePackage.serverless.service.provider.memorySize = 1024;
148146

149147
const compiledResources = [{
150148
type: 'cloudfunctions.v1beta2.function',
@@ -153,10 +151,7 @@ describe('CompileFunctions', () => {
153151
location: 'us-central1',
154152
runtime: 'nodejs8',
155153
function: 'func1',
156-
availableMemoryMb: 256,
157-
environmentVariables: {
158-
TEST_VAR: 'test',
159-
},
154+
availableMemoryMb: 1024,
160155
timeout: '60s',
161156
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
162157
httpsTrigger: {
@@ -173,16 +168,16 @@ describe('CompileFunctions', () => {
173168
});
174169
});
175170

176-
it('should set the memory size based on the provider configuration', () => {
171+
it('should set the timout based on the functions configuration', () => {
177172
googlePackage.serverless.service.functions = {
178173
func1: {
179174
handler: 'func1',
175+
timeout: '120s',
180176
events: [
181177
{ http: 'foo' },
182178
],
183179
},
184180
};
185-
googlePackage.serverless.service.provider.memorySize = 1024;
186181

187182
const compiledResources = [{
188183
type: 'cloudfunctions.v1beta2.function',
@@ -191,8 +186,8 @@ describe('CompileFunctions', () => {
191186
location: 'us-central1',
192187
runtime: 'nodejs8',
193188
function: 'func1',
194-
availableMemoryMb: 1024,
195-
timeout: '60s',
189+
availableMemoryMb: 256,
190+
timeout: '120s',
196191
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
197192
httpsTrigger: {
198193
url: 'foo',
@@ -208,16 +203,16 @@ describe('CompileFunctions', () => {
208203
});
209204
});
210205

211-
it('should set the timout based on the functions configuration', () => {
206+
it('should set the timeout based on the provider configuration', () => {
212207
googlePackage.serverless.service.functions = {
213208
func1: {
214209
handler: 'func1',
215-
timeout: '120s',
216210
events: [
217211
{ http: 'foo' },
218212
],
219213
},
220214
};
215+
googlePackage.serverless.service.provider.timeout = '120s';
221216

222217
const compiledResources = [{
223218
type: 'cloudfunctions.v1beta2.function',
@@ -243,16 +238,18 @@ describe('CompileFunctions', () => {
243238
});
244239
});
245240

246-
it('should set the timeout based on the provider configuration', () => {
241+
it('should set the labels based on the functions configuration', () => {
247242
googlePackage.serverless.service.functions = {
248243
func1: {
249244
handler: 'func1',
245+
labels: {
246+
test: 'label',
247+
},
250248
events: [
251249
{ http: 'foo' },
252250
],
253251
},
254252
};
255-
googlePackage.serverless.service.provider.timeout = '120s';
256253

257254
const compiledResources = [{
258255
type: 'cloudfunctions.v1beta2.function',
@@ -262,12 +259,14 @@ describe('CompileFunctions', () => {
262259
runtime: 'nodejs8',
263260
function: 'func1',
264261
availableMemoryMb: 256,
265-
timeout: '120s',
262+
timeout: '60s',
266263
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
267264
httpsTrigger: {
268265
url: 'foo',
269266
},
270-
labels: {},
267+
labels: {
268+
test: 'label',
269+
},
271270
},
272271
}];
273272

@@ -278,18 +277,18 @@ describe('CompileFunctions', () => {
278277
});
279278
});
280279

281-
it('should set the labels based on the functions configuration', () => {
280+
it('should set the labels based on the provider configuration', () => {
282281
googlePackage.serverless.service.functions = {
283282
func1: {
284283
handler: 'func1',
285-
labels: {
286-
test: 'label',
287-
},
288284
events: [
289285
{ http: 'foo' },
290286
],
291287
},
292288
};
289+
googlePackage.serverless.service.provider.labels = {
290+
test: 'label',
291+
};
293292

294293
const compiledResources = [{
295294
type: 'cloudfunctions.v1beta2.function',
@@ -317,17 +316,21 @@ describe('CompileFunctions', () => {
317316
});
318317
});
319318

320-
it('should set the labels based on the provider configuration', () => {
319+
it('should set the labels based on the merged provider and function configuration', () => {
321320
googlePackage.serverless.service.functions = {
322321
func1: {
323322
handler: 'func1',
324323
events: [
325324
{ http: 'foo' },
326325
],
326+
labels: {
327+
test: 'functionLabel',
328+
},
327329
},
328330
};
329331
googlePackage.serverless.service.provider.labels = {
330-
test: 'label',
332+
test: 'providerLabel',
333+
secondTest: 'tested',
331334
};
332335

333336
const compiledResources = [{
@@ -344,7 +347,8 @@ describe('CompileFunctions', () => {
344347
url: 'foo',
345348
},
346349
labels: {
347-
test: 'label',
350+
test: 'functionLabel',
351+
secondTest: 'tested',
348352
},
349353
},
350354
}];
@@ -356,21 +360,57 @@ describe('CompileFunctions', () => {
356360
});
357361
});
358362

359-
it('should set the labels based on the merged provider and function configuration', () => {
363+
it('should set the environment variables based on the function configuration', () => {
360364
googlePackage.serverless.service.functions = {
361365
func1: {
362366
handler: 'func1',
367+
environment: {
368+
TEST_VAR: 'test',
369+
},
363370
events: [
364371
{ http: 'foo' },
365372
],
366-
labels: {
367-
test: 'functionLabel',
373+
}
374+
};
375+
376+
const compiledResources = [{
377+
type: 'cloudfunctions.v1beta2.function',
378+
name: 'my-service-dev-func1',
379+
properties: {
380+
location: 'us-central1',
381+
runtime: 'nodejs8',
382+
function: 'func1',
383+
availableMemoryMb: 256,
384+
environmentVariables: {
385+
TEST_VAR: 'test',
368386
},
387+
timeout: '60s',
388+
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
389+
httpsTrigger: {
390+
url: 'foo',
391+
},
392+
labels: {},
393+
},
394+
}];
395+
396+
return googlePackage.compileFunctions().then(() => {
397+
expect(consoleLogStub.calledOnce).toEqual(true);
398+
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
399+
.toEqual(compiledResources);
400+
});
401+
});
402+
403+
it('should set the environment variables based on the provider configuration', () => {
404+
googlePackage.serverless.service.functions = {
405+
func1: {
406+
handler: 'func1',
407+
events: [
408+
{ http: 'foo' },
409+
],
369410
},
370411
};
371-
googlePackage.serverless.service.provider.labels = {
372-
test: 'providerLabel',
373-
secondTest: 'tested',
412+
googlePackage.serverless.service.provider.environment = {
413+
TEST_VAR: 'test',
374414
};
375415

376416
const compiledResources = [{
@@ -381,15 +421,15 @@ describe('CompileFunctions', () => {
381421
runtime: 'nodejs8',
382422
function: 'func1',
383423
availableMemoryMb: 256,
424+
environmentVariables: {
425+
TEST_VAR: 'test',
426+
},
384427
timeout: '60s',
385428
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
386429
httpsTrigger: {
387430
url: 'foo',
388431
},
389-
labels: {
390-
test: 'functionLabel',
391-
secondTest: 'tested',
392-
},
432+
labels: {},
393433
},
394434
}];
395435

0 commit comments

Comments
 (0)