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

#5947: Ensure invoke local docker runs lambda with the dependencies #5977

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 23 additions & 21 deletions lib/plugins/aws/invokeLocal/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class AwsInvokeLocal {
return BbPromise.resolve(); return BbPromise.resolve();
} }
mkdirp.sync(path.join( mkdirp.sync(path.join(
'.serverless', 'invokeLocal', 'artifact')); '.serverless', 'invokeLocal', 'artifact', path.dirname(filename)));
return fs.writeFileAsync(path.join( return fs.writeFileAsync(path.join(
'.serverless', 'invokeLocal', 'artifact', filename), fileData, { '.serverless', 'invokeLocal', 'artifact', filename), fileData, {
mode: zip.files[filename].unixPermissions, mode: zip.files[filename].unixPermissions,
Expand All @@ -319,26 +319,28 @@ class AwsInvokeLocal {
invokeLocalDocker() { invokeLocalDocker() {
const handler = this.options.functionObj.handler; const handler = this.options.functionObj.handler;


return BbPromise.all([ return this.serverless.pluginManager.spawn('package').then(() =>
this.checkDockerDaemonStatus(), BbPromise.all([
this.checkDockerImage().then(exists => (exists ? {} : this.pullDockerImage())), this.checkDockerDaemonStatus(),
this.getLayerPaths().then(layerPaths => this.buildDockerImage(layerPaths)), this.checkDockerImage().then(exists => (exists ? {} : this.pullDockerImage())),
this.extractArtifact(), this.getLayerPaths().then(layerPaths => this.buildDockerImage(layerPaths)),
]) this.extractArtifact(),
.then((results) => new BbPromise((resolve, reject) => { ])
const imageName = results[2]; .then((results) => new BbPromise((resolve, reject) => {
const artifactPath = results[3]; const imageName = results[2];
const dockerArgsFromOptions = this.getDockerArgsFromOptions(); const artifactPath = results[3];
const dockerArgs = _.concat([ const dockerArgsFromOptions = this.getDockerArgsFromOptions();
'run', '--rm', '-v', `${artifactPath}:/var/task`, const dockerArgs = _.concat([
], dockerArgsFromOptions, [ 'run', '--rm', '-v', `${artifactPath}:/var/task`,
imageName, handler, JSON.stringify(this.options.data), ], dockerArgsFromOptions, [
]); imageName, handler, JSON.stringify(this.options.data),
const docker = spawn('docker', dockerArgs); ]);
docker.stdout.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString())); const docker = spawn('docker', dockerArgs);
docker.stderr.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString())); docker.stdout.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString()));
docker.on('exit', error => (error ? reject(error) : resolve(imageName))); docker.stderr.on('data', (buf) => this.serverless.cli.consoleLog(buf.toString()));
})); docker.on('exit', error => (error ? reject(error) : resolve(imageName)));
}))
);
} }


getDockerArgsFromOptions() { getDockerArgsFromOptions() {
Expand Down
10 changes: 9 additions & 1 deletion lib/plugins/aws/invokeLocal/index.test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1118,6 +1118,8 @@ describe('AwsInvokeLocal', () => {
describe('#invokeLocalDocker()', () => { describe('#invokeLocalDocker()', () => {
let awsInvokeLocalMocked; let awsInvokeLocalMocked;
let spawnStub; let spawnStub;
let pluginMangerSpawnStub;
let pluginMangerSpawnPackageStub;


beforeEach(() => { beforeEach(() => {
awsInvokeLocal.provider.options.stage = 'dev'; awsInvokeLocal.provider.options.stage = 'dev';
Expand Down Expand Up @@ -1163,16 +1165,22 @@ describe('AwsInvokeLocal', () => {
data: {}, data: {},
'docker-arg': '-p 9292:9292', 'docker-arg': '-p 9292:9292',
}; };

pluginMangerSpawnStub = sinon
.stub(serverless.pluginManager, 'spawn');
pluginMangerSpawnPackageStub = pluginMangerSpawnStub.withArgs('package').resolves();
}); });




afterEach(() => { afterEach(() => {
delete require.cache[require.resolve('./index')]; delete require.cache[require.resolve('./index')];
delete require.cache[require.resolve('child_process')]; delete require.cache[require.resolve('child_process')];
serverless.pluginManager.spawn.restore();
}); });


it('calls docker', () => it('calls docker with packaged artifact', () =>
awsInvokeLocalMocked.invokeLocalDocker().then(() => { awsInvokeLocalMocked.invokeLocalDocker().then(() => {
expect(pluginMangerSpawnPackageStub.calledOnce).to.equal(true);
expect(spawnStub.getCall(0).args).to.deep.equal(['docker', ['version']]); expect(spawnStub.getCall(0).args).to.deep.equal(['docker', ['version']]);
expect(spawnStub.getCall(1).args).to.deep.equal(['docker', expect(spawnStub.getCall(1).args).to.deep.equal(['docker',
['images', '-q', 'lambci/lambda:nodejs8.10']]); ['images', '-q', 'lambci/lambda:nodejs8.10']]);
Expand Down