From 1fef52fe8354c6020e4d6c27c4b792acf6117a64 Mon Sep 17 00:00:00 2001 From: ilyesBen Date: Sat, 10 Aug 2024 13:15:57 +0100 Subject: [PATCH 1/2] refactor: ignore handlers with image.uri or image.name properties in getAllNodeFunctions --- lib/utils.js | 4 +-- tests/utils.test.js | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index f9e6cd396..753d3d480 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -123,9 +123,9 @@ function getAllNodeFunctions() { return _.filter(functions, funcName => { const func = this.serverless.service.getFunction(funcName); - // if `uri` is provided or simple remote image path, it means the + // if `uri` or `name` is provided or simple remote image path, it means the // image isn't built by Serverless so we shouldn't take care of it - if ((func.image && func.image.uri) || (func.image && typeof func.image == 'string')) { + if ((func.image && (func.image.uri || func.image.name)) || (func.image && typeof func.image == 'string')) { return false; } diff --git a/tests/utils.test.js b/tests/utils.test.js index 59e578fe4..9ddf3ac27 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -126,6 +126,65 @@ describe('Utils', () => { }); }); + describe('getAllNodeFunctions', () => { + it('should return all functions with node runtime', () => { + const mockServerless = { + service: { + getAllFunctions: jest.fn(() => ['foo', 'bar', 'baz']), + getFunction: jest.fn(name => { + if (name === 'foo') { + return { runtime: 'nodejs6.10' }; + } + if (name === 'bar') { + return { runtime: 'nodejs8.10' }; + } + if (name === 'baz') { + return { runtime: 'python3.6' }; + } + }) + } + }; + + expect(Utils.getAllNodeFunctions.call({ serverless: mockServerless })).toEqual(['foo', 'bar']); + }); + + it('should ignore handlers with image.uri property', () => { + const mockServerless = { + service: { + getAllFunctions: jest.fn(() => ['foo', 'bar']), + getFunction: jest.fn(name => { + if (name === 'foo') { + return { runtime: 'nodejs6.10' }; + } + if (name === 'bar') { + return { runtime: 'nodejs8.10', image: { uri: 'fake-image-uri' } }; + } + }) + } + }; + + expect(Utils.getAllNodeFunctions.call({ serverless: mockServerless })).toEqual(['foo']); + }); + + it('should ignore handlers with image.name property', () => { + const mockServerless = { + service: { + getAllFunctions: jest.fn(() => ['foo', 'bar']), + getFunction: jest.fn(name => { + if (name === 'foo') { + return { runtime: 'nodejs6.10' }; + } + if (name === 'bar') { + return { runtime: 'nodejs8.10', image: { name: 'fake-image-name' } }; + } + }) + } + }; + + expect(Utils.getAllNodeFunctions.call({ serverless: mockServerless })).toEqual(['foo']); + }); + }); + describe('isProviderGoogle', () => { describe('when the provider is set to "google"', () => { const mockServerless = { From 45cc516d1fa1bf96be2d8fb55923e700a61379ec Mon Sep 17 00:00:00 2001 From: ilyesBen Date: Sat, 12 Oct 2024 13:50:58 +0100 Subject: [PATCH 2/2] fix tests in validate.test.js - Remove checks around docker functions --- tests/validate.test.js | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/tests/validate.test.js b/tests/validate.test.js index abcfaebc7..e3ae9ef5f 100644 --- a/tests/validate.test.js +++ b/tests/validate.test.js @@ -472,7 +472,7 @@ describe('validate', () => { } ] }, - dockerfunc: { + dockerFuncNameOnly: { image: { name: 'some-docker-image', command: ['com.serverless.Handler'] @@ -486,7 +486,7 @@ describe('validate', () => { } ] }, - dockerfuncuri: { + dockerFuncUri: { image: { name: 'some-image-with-uri', uri: 'http://hub.dock.er/image', @@ -543,7 +543,6 @@ describe('validate', () => { .then(() => { const lib = require('../lib/index'); const expectedLibEntries = { - 'com.serverless': './com.serverless.js', module1: './module1.js', module2: './module2.js', 'handlers/func3/module2': './handlers/func3/module2.js', @@ -551,7 +550,7 @@ describe('validate', () => { }; expect(lib.entries).toEqual(expectedLibEntries); - expect(globMock.sync).toHaveBeenCalledTimes(6); + expect(globMock.sync).toHaveBeenCalledTimes(5); expect(serverless.cli.log).toHaveBeenCalledTimes(0); return null; }); @@ -674,12 +673,11 @@ describe('validate', () => { .then(() => { const lib = require('../lib/index'); const expectedLibEntries = { - module1: './module1.js', - module4: './module4.js' + module1: './module1.js' }; expect(lib.entries).toEqual(expectedLibEntries); - expect(globMock.sync).toHaveBeenCalledTimes(2); + expect(globMock.sync).toHaveBeenCalledTimes(1); expect(serverless.cli.log).toHaveBeenCalledTimes(0); return null; }); @@ -749,7 +747,6 @@ describe('validate', () => { } ], image: { - name: 'custom-image', command: [] } } @@ -949,15 +946,6 @@ describe('validate', () => { key: 'handlers/module2/func3/module2', value: './handlers/module2/func3/module2.js' } - }, - { - handlerFile: 'com.serverless', - funcName: 'dockerfunc', - func: testFunctionsConfig.dockerfunc, - entry: { - key: 'com.serverless', - value: './com.serverless.js' - } } ]); return null; @@ -971,13 +959,12 @@ describe('validate', () => { return expect(module.validate()) .resolves.toBeUndefined() .then(() => { - expect(module.webpackConfig).toHaveLength(6); + expect(module.webpackConfig).toHaveLength(5); expect(module.webpackConfig[0].output.path).toEqual(path.join('output', 'func1')); expect(module.webpackConfig[1].output.path).toEqual(path.join('output', 'layerFunc')); expect(module.webpackConfig[2].output.path).toEqual(path.join('output', 'func2')); expect(module.webpackConfig[3].output.path).toEqual(path.join('output', 'func3')); expect(module.webpackConfig[4].output.path).toEqual(path.join('output', 'func4')); - expect(module.webpackConfig[5].output.path).toEqual(path.join('output', 'dockerfunc')); return null; }); @@ -1000,27 +987,24 @@ describe('validate', () => { return expect(module.validate()) .resolves.toBeUndefined() .then(() => { - expect(module.webpackConfig).toHaveLength(6); + expect(module.webpackConfig).toHaveLength(5); expect(module.webpackConfig[0].devtool).toEqual('source-map'); expect(module.webpackConfig[1].devtool).toEqual('source-map'); expect(module.webpackConfig[2].devtool).toEqual('source-map'); expect(module.webpackConfig[3].devtool).toEqual('source-map'); expect(module.webpackConfig[4].devtool).toEqual('source-map'); - expect(module.webpackConfig[5].devtool).toEqual('source-map'); expect(module.webpackConfig[0].context).toEqual('some context'); expect(module.webpackConfig[1].context).toEqual('some context'); expect(module.webpackConfig[2].context).toEqual('some context'); expect(module.webpackConfig[3].context).toEqual('some context'); expect(module.webpackConfig[4].context).toEqual('some context'); - expect(module.webpackConfig[5].context).toEqual('some context'); expect(module.webpackConfig[0].output.libraryTarget).toEqual('commonjs'); expect(module.webpackConfig[1].output.libraryTarget).toEqual('commonjs'); expect(module.webpackConfig[2].output.libraryTarget).toEqual('commonjs'); expect(module.webpackConfig[3].output.libraryTarget).toEqual('commonjs'); expect(module.webpackConfig[4].output.libraryTarget).toEqual('commonjs'); - expect(module.webpackConfig[5].output.libraryTarget).toEqual('commonjs'); return null; });