Skip to content

Commit

Permalink
Add support for function environment variable definition to compileFu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
David Tabachnikov committed Sep 22, 2018
1 parent 298c0db commit a36dff9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 39 deletions.
9 changes: 5 additions & 4 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ module.exports = {
funcTemplate.properties.timeout = _.get(funcObject, 'timeout')
|| _.get(this, 'serverless.service.provider.timeout')
|| '60s';
funcTemplate.properties.environmentVariables = _.get(funcObject, 'environment')
|| _.get(this, 'serverless.service.provider.environment')
|| null;
funcTemplate.properties.environmentVariables = _.merge(
_.get(this, 'serverless.service.provider.environment'),
funcObject.environment,
);

if (funcTemplate.properties.environmentVariables === null) {
if (!_.size(funcTemplate.properties.environmentVariables)) {
delete funcTemplate.properties.environmentVariables;
}

Expand Down
110 changes: 75 additions & 35 deletions package/lib/compileFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('CompileFunctions', () => {
});
});

it('should set the environment variables based on the provider configuration', () => {
it('should set the memory size based on the provider configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
Expand All @@ -142,9 +142,7 @@ describe('CompileFunctions', () => {
],
},
};
googlePackage.serverless.service.provider.environment = {
TEST_VAR: 'test',
};
googlePackage.serverless.service.provider.memorySize = 1024;

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
Expand All @@ -153,10 +151,7 @@ describe('CompileFunctions', () => {
location: 'us-central1',
runtime: 'nodejs8',
function: 'func1',
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
},
availableMemoryMb: 1024,
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
httpsTrigger: {
Expand All @@ -173,16 +168,16 @@ describe('CompileFunctions', () => {
});
});

it('should set the memory size based on the provider configuration', () => {
it('should set the timout based on the functions configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
timeout: '120s',
events: [
{ http: 'foo' },
],
},
};
googlePackage.serverless.service.provider.memorySize = 1024;

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
Expand All @@ -191,8 +186,8 @@ describe('CompileFunctions', () => {
location: 'us-central1',
runtime: 'nodejs8',
function: 'func1',
availableMemoryMb: 1024,
timeout: '60s',
availableMemoryMb: 256,
timeout: '120s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
Expand All @@ -208,16 +203,16 @@ describe('CompileFunctions', () => {
});
});

it('should set the timout based on the functions configuration', () => {
it('should set the timeout based on the provider configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
timeout: '120s',
events: [
{ http: 'foo' },
],
},
};
googlePackage.serverless.service.provider.timeout = '120s';

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
Expand All @@ -243,16 +238,18 @@ describe('CompileFunctions', () => {
});
});

it('should set the timeout based on the provider configuration', () => {
it('should set the labels based on the functions configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
labels: {
test: 'label',
},
events: [
{ http: 'foo' },
],
},
};
googlePackage.serverless.service.provider.timeout = '120s';

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
Expand All @@ -262,12 +259,14 @@ describe('CompileFunctions', () => {
runtime: 'nodejs8',
function: 'func1',
availableMemoryMb: 256,
timeout: '120s',
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
labels: {},
labels: {
test: 'label',
},
},
}];

Expand All @@ -278,18 +277,18 @@ describe('CompileFunctions', () => {
});
});

it('should set the labels based on the functions configuration', () => {
it('should set the labels based on the provider configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
labels: {
test: 'label',
},
events: [
{ http: 'foo' },
],
},
};
googlePackage.serverless.service.provider.labels = {
test: 'label',
};

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
Expand Down Expand Up @@ -317,17 +316,21 @@ describe('CompileFunctions', () => {
});
});

it('should set the labels based on the provider configuration', () => {
it('should set the labels based on the merged provider and function configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
events: [
{ http: 'foo' },
],
labels: {
test: 'functionLabel',
},
},
};
googlePackage.serverless.service.provider.labels = {
test: 'label',
test: 'providerLabel',
secondTest: 'tested',
};

const compiledResources = [{
Expand All @@ -344,7 +347,8 @@ describe('CompileFunctions', () => {
url: 'foo',
},
labels: {
test: 'label',
test: 'functionLabel',
secondTest: 'tested',
},
},
}];
Expand All @@ -356,21 +360,57 @@ describe('CompileFunctions', () => {
});
});

it('should set the labels based on the merged provider and function configuration', () => {
it('should set the environment variables based on the function configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: 'test',
},
events: [
{ http: 'foo' },
],
labels: {
test: 'functionLabel',
}
};

const compiledResources = [{
type: 'cloudfunctions.v1beta2.function',
name: 'my-service-dev-func1',
properties: {
location: 'us-central1',
runtime: 'nodejs8',
function: 'func1',
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
labels: {},
},
}];

return googlePackage.compileFunctions().then(() => {
expect(consoleLogStub.calledOnce).toEqual(true);
expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources)
.toEqual(compiledResources);
});
});

it('should set the environment variables based on the provider configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
events: [
{ http: 'foo' },
],
},
};
googlePackage.serverless.service.provider.labels = {
test: 'providerLabel',
secondTest: 'tested',
googlePackage.serverless.service.provider.environment = {
TEST_VAR: 'test',
};

const compiledResources = [{
Expand All @@ -381,15 +421,15 @@ describe('CompileFunctions', () => {
runtime: 'nodejs8',
function: 'func1',
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
httpsTrigger: {
url: 'foo',
},
labels: {
test: 'functionLabel',
secondTest: 'tested',
},
labels: {},
},
}];

Expand Down

0 comments on commit a36dff9

Please sign in to comment.