Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cast environment variable values to string #218

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions package/lib/compileFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ module.exports = {
'nodejs8';
funcTemplate.properties.timeout =
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
funcTemplate.properties.environmentVariables = _.merge(
{},
_.get(this, 'serverless.service.provider.environment'),
funcObject.environment // eslint-disable-line comma-dangle
funcTemplate.properties.environmentVariables = _.mapValues(
_.merge({}, _.get(this, 'serverless.service.provider.environment'), funcObject.environment),
(value, key, result) => coerceEnvOrError(result, key, value)
);
funcTemplate.accessControl.gcpIamPolicy.bindings = _.unionBy(
_.get(funcObject, 'iam.bindings'),
Expand Down Expand Up @@ -198,6 +197,20 @@ const validateIamProperty = (funcObject, functionName) => {
}
};

const coerceEnvOrError = (result, key, value) => {
if (typeof value === 'string') {
result[key] = value;
} else if (typeof value === 'number') {
result[key] = value.toString();
} else {
const errorMessage = [
`The value for environment variable ${key} is an unsupported type: ${typeof value}.`,
' Values must either be strings or numbers (which are coerced into strings at package time).',
].join('');
throw new Error(errorMessage);
}
};

const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl) => {
//eslint-disable-line
return {
Expand Down
66 changes: 66 additions & 0 deletions package/lib/compileFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,72 @@ describe('CompileFunctions', () => {
});
});

it('should fail setting environment variable due to unsupported type (bool)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: true,
},
events: [{ http: 'foo' }],
},
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should fail setting environment variable due to unsupported type (null)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: null,
},
events: [{ http: 'foo' }],
},
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should fail setting environment variable due to unsupported type (object)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
dev: {
TEST_VAR: 'test',
},
},
events: [{ http: 'foo' }],
},
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should fail setting environment variable from provider due to unsupported type (bool)', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
events: [{ http: 'foo' }],
},
};
googlePackage.serverless.service.provider.environment = {
TEST_VAR: true,
};

expect(() => googlePackage.compileFunctions()).toThrow(Error);
});

it('should set the environment variables based on the function configuration', () => {
googlePackage.serverless.service.functions = {
func1: {
handler: 'func1',
environment: {
TEST_VAR: 'test',
INT_VAR: 1,
FLOAT_VAR: 3.141,
},
events: [{ http: 'foo' }],
},
Expand All @@ -393,6 +453,8 @@ describe('CompileFunctions', () => {
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
INT_VAR: '1',
FLOAT_VAR: '3.141',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand Down Expand Up @@ -421,6 +483,8 @@ describe('CompileFunctions', () => {
};
googlePackage.serverless.service.provider.environment = {
TEST_VAR: 'test',
INT_VAR: 1,
FLOAT_VAR: 3.141,
};

const compiledResources = [
Expand All @@ -435,6 +499,8 @@ describe('CompileFunctions', () => {
availableMemoryMb: 256,
environmentVariables: {
TEST_VAR: 'test',
INT_VAR: '1',
FLOAT_VAR: '3.141',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand Down