From ee18a8d6757ed8a9849c65e5067d91a068b97b44 Mon Sep 17 00:00:00 2001 From: Jason Butz Date: Wed, 24 Jan 2018 18:56:47 -0500 Subject: [PATCH 1/2] Add labels support to packaging functions --- package/lib/compileFunctions.js | 4 + package/lib/compileFunctions.test.js | 126 +++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/package/lib/compileFunctions.js b/package/lib/compileFunctions.js index 54333ad..de5034d 100644 --- a/package/lib/compileFunctions.js +++ b/package/lib/compileFunctions.js @@ -37,6 +37,10 @@ module.exports = { funcTemplate.properties.timeout = _.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s'; + funcTemplate.properties.labels = _.assign({}, + _.get(this, 'serverless.service.provider.labels') || {}, + _.get(funcObject, 'labels') || {}, + ); const eventType = Object.keys(funcObject.events[0])[0]; diff --git a/package/lib/compileFunctions.test.js b/package/lib/compileFunctions.test.js index a000c92..c00646e 100644 --- a/package/lib/compileFunctions.test.js +++ b/package/lib/compileFunctions.test.js @@ -120,6 +120,7 @@ describe('CompileFunctions', () => { httpsTrigger: { url: 'foo', }, + labels: {}, }, }]; @@ -153,6 +154,7 @@ describe('CompileFunctions', () => { httpsTrigger: { url: 'foo', }, + labels: {}, }, }]; @@ -186,6 +188,7 @@ describe('CompileFunctions', () => { httpsTrigger: { url: 'foo', }, + labels: {}, }, }]; @@ -219,6 +222,126 @@ describe('CompileFunctions', () => { 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 labels based on the functions configuration', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + labels: { + test: 'label' + }, + events: [ + { http: 'foo' }, + ], + }, + }; + + const compiledResources = [{ + type: 'cloudfunctions.v1beta2.function', + name: 'my-service-dev-func1', + properties: { + location: 'us-central1', + function: 'func1', + availableMemoryMb: 256, + timeout: '60s', + sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', + httpsTrigger: { + url: 'foo', + }, + labels: { + test: 'label' + }, + }, + }]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.calledOnce).toEqual(true); + expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources) + .toEqual(compiledResources); + }); + }); + + it('should set the labels based on the provider configuration', () => { + googlePackage.serverless.service.functions = { + func1: { + handler: 'func1', + events: [ + { http: 'foo' }, + ], + }, + }; + googlePackage.serverless.service.provider.labels = { + test: 'label' + }; + + const compiledResources = [{ + type: 'cloudfunctions.v1beta2.function', + name: 'my-service-dev-func1', + properties: { + location: 'us-central1', + function: 'func1', + availableMemoryMb: 256, + timeout: '60s', + sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', + httpsTrigger: { + url: 'foo', + }, + labels: { + test: 'label' + }, + }, + }]; + + return googlePackage.compileFunctions().then(() => { + expect(consoleLogStub.calledOnce).toEqual(true); + expect(googlePackage.serverless.service.provider.compiledConfigurationTemplate.resources) + .toEqual(compiledResources); + }); + }); + + 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: 'providerLabel', + secondTest: 'tested', + }; + + const compiledResources = [{ + type: 'cloudfunctions.v1beta2.function', + name: 'my-service-dev-func1', + properties: { + location: 'us-central1', + function: 'func1', + availableMemoryMb: 256, + timeout: '60s', + sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip', + httpsTrigger: { + url: 'foo', + }, + labels: { + test: 'functionLabel', + secondTest: 'tested', + }, }, }]; @@ -251,6 +374,7 @@ describe('CompileFunctions', () => { httpsTrigger: { url: 'foo', }, + labels: {}, }, }]; @@ -303,6 +427,7 @@ describe('CompileFunctions', () => { path: 'some-path', resource: 'some-resource', }, + labels: {}, }, }, { @@ -318,6 +443,7 @@ describe('CompileFunctions', () => { eventType: 'foo', resource: 'some-resource', }, + labels: {}, }, }, ]; From 3e8f8bd6d2c6b1cae40fa2a0badd05f7cc31b382 Mon Sep 17 00:00:00 2001 From: Jason Butz Date: Wed, 24 Jan 2018 19:09:32 -0500 Subject: [PATCH 2/2] Fix compileFunctions test linting errors --- package/lib/compileFunctions.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/package/lib/compileFunctions.test.js b/package/lib/compileFunctions.test.js index c00646e..ce2ccb2 100644 --- a/package/lib/compileFunctions.test.js +++ b/package/lib/compileFunctions.test.js @@ -238,7 +238,7 @@ describe('CompileFunctions', () => { func1: { handler: 'func1', labels: { - test: 'label' + test: 'label', }, events: [ { http: 'foo' }, @@ -259,7 +259,7 @@ describe('CompileFunctions', () => { url: 'foo', }, labels: { - test: 'label' + test: 'label', }, }, }]; @@ -281,7 +281,7 @@ describe('CompileFunctions', () => { }, }; googlePackage.serverless.service.provider.labels = { - test: 'label' + test: 'label', }; const compiledResources = [{ @@ -297,7 +297,7 @@ describe('CompileFunctions', () => { url: 'foo', }, labels: { - test: 'label' + test: 'label', }, }, }]; @@ -317,8 +317,8 @@ describe('CompileFunctions', () => { { http: 'foo' }, ], labels: { - test: 'functionLabel' - } + test: 'functionLabel', + }, }, }; googlePackage.serverless.service.provider.labels = {