From 59cd51917fbf5e3ed35668995f87da883d9f8d6d Mon Sep 17 00:00:00 2001 From: John McKim Date: Mon, 22 Aug 2016 09:34:56 +1000 Subject: [PATCH] upload individual artifacts to S3 --- .../aws/deploy/lib/uploadDeploymentPackage.js | 29 ++++++++++--- .../deploy/tests/uploadDeploymentPackage.js | 43 +++++++++++++++++-- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/lib/plugins/aws/deploy/lib/uploadDeploymentPackage.js b/lib/plugins/aws/deploy/lib/uploadDeploymentPackage.js index 62651d615..56acb02b4 100644 --- a/lib/plugins/aws/deploy/lib/uploadDeploymentPackage.js +++ b/lib/plugins/aws/deploy/lib/uploadDeploymentPackage.js @@ -63,15 +63,32 @@ module.exports = { return BbPromise.resolve(); }, - uploadZipFileToS3Bucket() { - this.serverless.cli.log('Uploading .zip file to S3...'); + uploadPackageToS3Bucket() { + this.serverless.cli.log('Uploading .zip files to S3...'); - // TODO: update to upload individual packages if we're packaging individually - const body = fs.readFileSync(this.serverless.service.package.artifact); + if (this.serverless.service.package.individually) { + const functionNames = this.serverless.service.getAllFunctions(); + const uploadPromises = functionNames.map(name => { + const functionObject = this.serverless.service.getFunction(name); + if (functionObject.artifact) { + return this.uploadZipFileToS3Bucket(functionObject.artifact); + } + + return BbPromise.resolve(); + }); + return BbPromise.all(uploadPromises); + } + + const artifactFilePath = this.serverless.service.package.artifact; + return this.uploadZipFileToS3Bucket(artifactFilePath); + }, + + uploadZipFileToS3Bucket(artifactFilePath) { + const body = fs.readFileSync(artifactFilePath); const params = { Bucket: this.bucketName, - Key: this.serverless.service.package.artifact.split(path.sep).pop(), + Key: artifactFilePath.split(path.sep).pop(), Body: body, }; @@ -91,6 +108,6 @@ module.exports = { .then(this.setServerlessDeploymentBucketName) .then(this.getServiceObjectsFromS3Bucket) .then(this.cleanupS3Bucket) - .then(this.uploadZipFileToS3Bucket); + .then(this.uploadPackageToS3Bucket); }, }; diff --git a/lib/plugins/aws/deploy/tests/uploadDeploymentPackage.js b/lib/plugins/aws/deploy/tests/uploadDeploymentPackage.js index 5bd093e78..69507be5d 100644 --- a/lib/plugins/aws/deploy/tests/uploadDeploymentPackage.js +++ b/lib/plugins/aws/deploy/tests/uploadDeploymentPackage.js @@ -180,18 +180,53 @@ describe('uploadDeploymentPackage', () => { }); }); + describe('#uploadPackageToS3Bucket()', () => { + it('it should upload whole service package', () => { + const artifactFilePath = 'artifact.zip'; + awsDeploy.serverless.service.package.individually = false; + awsDeploy.serverless.service.package.artifact = artifactFilePath; + + const uploadZipFileToS3BucketStub = sinon + .stub(awsDeploy, 'uploadZipFileToS3Bucket').returns(BbPromise.resolve()); + + return awsDeploy.uploadPackageToS3Bucket().then(() => { + expect(uploadZipFileToS3BucketStub.calledOnce).to.be.equal(true); + expect(uploadZipFileToS3BucketStub.args[0][0]).to.be.equal(artifactFilePath); + awsDeploy.uploadZipFileToS3Bucket.restore(); + }); + }); + + it('it should upload individual function packages', () => { + const artifactFilePath = 'artifact.zip'; + awsDeploy.serverless.service.package.individually = true; + awsDeploy.serverless.service.functions = { + 'test-one': { + name: 'test-one', + artifact: artifactFilePath, + }, + }; + + const uploadZipFileToS3BucketStub = sinon + .stub(awsDeploy, 'uploadZipFileToS3Bucket').returns(BbPromise.resolve()); + + return awsDeploy.uploadPackageToS3Bucket().then(() => { + expect(uploadZipFileToS3BucketStub.calledOnce).to.be.equal(true); + expect(uploadZipFileToS3BucketStub.args[0][0]).to.be.equal(artifactFilePath); + awsDeploy.uploadZipFileToS3Bucket.restore(); + }); + }); + }); + describe('#uploadZipFileToS3Bucket()', () => { it('should upload the zip file to the S3 bucket', () => { - const tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + const tmpDirPath = path.join(os.tmpdir(), (new Date()).getTime().toString()); const artifactFilePath = path.join(tmpDirPath, 'artifact.zip'); serverless.utils.writeFileSync(artifactFilePath, 'artifact.zip file content'); - awsDeploy.serverless.service.package.artifact = artifactFilePath; - const putObjectStub = sinon .stub(awsDeploy.sdk, 'request').returns(BbPromise.resolve()); - return awsDeploy.uploadZipFileToS3Bucket().then(() => { + return awsDeploy.uploadZipFileToS3Bucket(artifactFilePath).then(() => { expect(putObjectStub.calledOnce).to.be.equal(true); expect(putObjectStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region)); awsDeploy.sdk.request.restore();