Skip to content

Commit

Permalink
update: coerce numbers to string, error on other non-string types for…
Browse files Browse the repository at this point in the history
… env values
  • Loading branch information
edaniszewski committed Jun 24, 2020
1 parent c63fa18 commit f65c7cf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
25 changes: 18 additions & 7 deletions package/lib/compileFunctions.js
Expand Up @@ -47,13 +47,10 @@ module.exports = {
'nodejs8';
funcTemplate.properties.timeout =
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
funcTemplate.properties.environmentVariables = _.mapValues(
_.merge(
{},
_.get(this, 'serverless.service.provider.environment'),
funcObject.environment // eslint-disable-line comma-dangle
),
(value) => value.toString()
funcTemplate.properties.environmentVariables = _.transform(
_.merge({}, _.get(this, 'serverless.service.provider.environment'), funcObject.environment),
(result, value, key) => coerceEnvOrError(result, key, value),
{}
);
funcTemplate.accessControl.gcpIamPolicy.bindings = _.unionBy(
_.get(funcObject, 'iam.bindings'),
Expand Down Expand Up @@ -201,6 +198,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: 58 additions & 8 deletions package/lib/compileFunctions.test.js
Expand Up @@ -370,6 +370,64 @@ 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: {
Expand All @@ -378,7 +436,6 @@ describe('CompileFunctions', () => {
TEST_VAR: 'test',
INT_VAR: 1,
FLOAT_VAR: 3.141,
BOOL_VAR: true,
},
events: [{ http: 'foo' }],
},
Expand All @@ -398,7 +455,6 @@ describe('CompileFunctions', () => {
TEST_VAR: 'test',
INT_VAR: '1',
FLOAT_VAR: '3.141',
BOOL_VAR: 'true',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand Down Expand Up @@ -429,7 +485,6 @@ describe('CompileFunctions', () => {
TEST_VAR: 'test',
INT_VAR: 1,
FLOAT_VAR: 3.141,
BOOL_VAR: true,
};

const compiledResources = [
Expand All @@ -446,7 +501,6 @@ describe('CompileFunctions', () => {
TEST_VAR: 'test',
INT_VAR: '1',
FLOAT_VAR: '3.141',
BOOL_VAR: 'true',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand All @@ -473,15 +527,13 @@ describe('CompileFunctions', () => {
environment: {
TEST_VAR: 'test_var',
TEST_VALUE: 'foobar',
TEST_BOOL: true,
},
events: [{ http: 'foo' }],
},
};
googlePackage.serverless.service.provider.environment = {
TEST_VAR: 'test',
TEST_FOO: 'foo',
TEST_BOOL: false,
};

const compiledResources = [
Expand All @@ -498,7 +550,6 @@ describe('CompileFunctions', () => {
TEST_VAR: 'test_var',
TEST_VALUE: 'foobar',
TEST_FOO: 'foo',
TEST_BOOL: 'true',
},
timeout: '60s',
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
Expand All @@ -518,7 +569,6 @@ describe('CompileFunctions', () => {
expect(googlePackage.serverless.service.provider.environment).toEqual({
TEST_VAR: 'test',
TEST_FOO: 'foo',
TEST_BOOL: false,
});
});
});
Expand Down

0 comments on commit f65c7cf

Please sign in to comment.