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

refactor: ignore handlers with image.name property #1885

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
30 changes: 7 additions & 23 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ describe('validate', () => {
}
]
},
dockerfunc: {
dockerFuncNameOnly: {
image: {
name: 'some-docker-image',
command: ['com.serverless.Handler']
Expand All @@ -486,7 +486,7 @@ describe('validate', () => {
}
]
},
dockerfuncuri: {
dockerFuncUri: {
image: {
name: 'some-image-with-uri',
uri: 'http://hub.dock.er/image',
Expand Down Expand Up @@ -543,15 +543,14 @@ 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',
'handlers/module2/func3/module2': './handlers/module2/func3/module2.js'
};

expect(lib.entries).toEqual(expectedLibEntries);
expect(globMock.sync).toHaveBeenCalledTimes(6);
expect(globMock.sync).toHaveBeenCalledTimes(5);
expect(serverless.cli.log).toHaveBeenCalledTimes(0);
return null;
});
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -749,7 +747,6 @@ describe('validate', () => {
}
],
image: {
name: 'custom-image',
command: []
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
});
Expand All @@ -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;
});
Expand Down