upload individual artifacts to S3

This commit is contained in:
John McKim 2016-08-22 09:34:56 +10:00
parent 5a2c9d2224
commit 59cd51917f
2 changed files with 62 additions and 10 deletions

View File

@ -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);
},
};

View File

@ -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();