serverless/lib/plugins/aws/deploy/tests/uploadDeploymentPackage.js
Nick Chisiu 16aa6581ab fix #1854 - update npm devDependencies packages (#1879)
* fix #1854 - added latest version of eslint-config-airbnb-base package

* fix #1854 - added latest npm shrinkwrap scheme

* fix #1854 - added latest npm coveralls package

* fix #1854 - added latest npm eslint package

* fix #1854 - added latest npm eslint-config-airbnb package

* fix #1854 - added latest npm eslint-plugin-import package

* fix #1854 - added latest npm eslint-plugin-jsx-a11y package

* fix #1854 - added latest npm eslint-plugin-react package

* fix #1854 - fixed estlint new-parens errors for integration test

* fix #1854 - fixed estlint new-parens errors for yaml parsers tests

* fix #1854 - fixed estlint max-len errors for util tests

* fix #1854 - disabled no-extraneous-dependencies on eslintrc for NodeJS 4 incapability on this feature

* fix #1854 - fixed eslint new-parens errors for Service tests

* fix #1854 - fixed eslint new-parens errors for Serverless tests

* fix #1854 - fixed eslint new-parens errors for plugin manager tests

* fix #1854 - fixed eslint new-parens errors for plugin tracking tests

* fix #1854 - fixed eslint new-parens errors for plugin package zipService lib

* fix #1854 - fixed eslint new-parens errors for plugin package zipService tests

* fix #1854 - fixed eslint trailing spaces errors for plugin package zipService lib

* fix #1854 - fixed eslint new-parens errors for plugin package cleanup  tests

* fix #1854 - fixed eslint new-parens errors for plugin create tests

* fix #1854 - fixed eslint dot same line expectation error on plugin aws logs index

* fix #1854 - fixed eslint operator assignment error on plugin aws logs index

* fix #1854 - fixed eslint dot location error on plugin aws invoke tests

* fix #1854 - fixed eslint new-parens error on plugin aws invoke tests

* fix #1854 - fixed eslint new-parens error on plugin aws deployFunction tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy uploadDeploymentPackage tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy updateStack tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy createStack tests

* fix #1854 - fixed eslint new-parens error on plugin aws deploy apiGateway lib deployment

* fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class

* fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class ( second fix )

* fix #1854 - fixed eslint no-lonely-if req error on Serverless Service class

* fix #1854 - disabled react/require-extension on eslintrc because it's deprecated

* fix #1854 - AwsCompileApigEvents #constructor() should resolve if no functions are given:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - createStack #postCreate() should resolve:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - emptyS3Bucket #deleteObjects() should resolve if objectsInBucket is empty:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - AwsInvoke #extendedValidate() should resolve if path is not given:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - #cleanup() should resolve if the .serverless directory is not present:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 -#validate() should resolve if servicePath is given:
                Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 -  Service #load() should resolve if no servicePath is found:
                 Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

* fix #1854 - added latest mocha package

* fix #1854 - added latest sinon npm package

* fix #1854 - serverless/lib/plugins/aws/deploy/tests/createStack.js 136:48  error  Missing semicolon  semi

* fix #1854 - serverless/lib/plugins/package/tests/cleanup.js 35:7  error  Missing semicolon  semi

* fix #1854 - serverless/lib/plugins/package/tests/validate.js 22:49  error  Missing semicolon  semi

* fix #1854 - added latest npm shrinkwrap

* fix #1854 - fixed no-extra-boolean-cast eslint error on aws deploy apiGateway methods

* fix #1854 - fixed  new-parens eslint error on serverless tests for Service class
2016-08-18 11:51:09 +02:00

254 lines
9.2 KiB
JavaScript

'use strict';
const sinon = require('sinon');
const os = require('os');
const path = require('path');
const BbPromise = require('bluebird');
const expect = require('chai').expect;
const AwsDeploy = require('../index');
const Serverless = require('../../../../Serverless');
describe('uploadDeploymentPackage', () => {
let serverless;
let awsDeploy;
beforeEach(() => {
serverless = new Serverless();
const options = {
stage: 'dev',
region: 'us-east-1',
};
awsDeploy = new AwsDeploy(serverless, options);
awsDeploy.serverless.cli = new serverless.classes.CLI();
});
describe('#setServerlessDeploymentBucketName()', () => {
it('should store the name of the Serverless deployment bucket in the "this" variable', () => {
const getServerlessDeploymentBucketNameStub = sinon
.stub(awsDeploy.sdk, 'getServerlessDeploymentBucketName')
.returns(BbPromise.resolve('new-service-dev-us-east-1-12345678'));
return awsDeploy.setServerlessDeploymentBucketName().then(() => {
expect(awsDeploy.bucketName).to.equal('new-service-dev-us-east-1-12345678');
expect(getServerlessDeploymentBucketNameStub.calledOnce).to.be.equal(true);
expect(getServerlessDeploymentBucketNameStub
.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.getServerlessDeploymentBucketName.restore();
});
});
});
describe('#getServiceObjectsFromS3Bucket()', () => {
it('should resolve if no service objects are found', () => {
const serviceObjects = {
Contents: [],
};
const listObjectsStub = sinon
.stub(awsDeploy.sdk, 'request').returns(BbPromise.resolve(serviceObjects));
return awsDeploy.getServiceObjectsFromS3Bucket().then(() => {
expect(listObjectsStub.calledOnce).to.be.equal(true);
expect(listObjectsStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.request.restore();
});
});
it('should return all to be removed service objects (except the last 4)', () => {
const serviceObjects = {
Contents: [
{
Key: 'first-service-1.zip',
},
{
Key: 'first-service-8.zip',
},
{
Key: 'first-service-2.zip',
},
{
Key: 'first-service-5.zip',
},
{
Key: 'first-service-6.zip',
},
{
Key: 'first-service-4.zip',
},
{
Key: 'first-service-7.zip',
},
{
Key: 'first-service-3.zip',
},
],
};
const listObjectsStub = sinon
.stub(awsDeploy.sdk, 'request').returns(BbPromise.resolve(serviceObjects));
return awsDeploy.getServiceObjectsFromS3Bucket().then((objectsToRemove) => {
expect(objectsToRemove).to.not.include({ Key: 'first-service-5.zip' });
expect(objectsToRemove).to.not.include({ Key: 'first-service-6.zip' });
expect(objectsToRemove).to.not.include({ Key: 'first-service-7.zip' });
expect(objectsToRemove).to.not.include({ Key: 'first-service-8.zip' });
expect(listObjectsStub.calledOnce).to.be.equal(true);
expect(listObjectsStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.request.restore();
});
});
it('should resolve if there are less than 4 service files available', () => {
const serviceObjects = {
Contents: [
{
Key: 'first-service-3.zip',
},
{
Key: 'first-service-1.zip',
},
{
Key: 'first-service-2.zip',
},
],
};
const listObjectsStub = sinon
.stub(awsDeploy.sdk, 'request').returns(BbPromise.resolve(serviceObjects));
return awsDeploy.getServiceObjectsFromS3Bucket().then((objectsToRemove) => {
expect(objectsToRemove).to.equal(undefined);
expect(listObjectsStub.calledOnce).to.be.equal(true);
expect(listObjectsStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.request.restore();
});
});
it('should resolve if there are exactly 4 service files available', () => {
const serviceObjects = {
Contents: [
{
Key: 'first-service-1.zip',
},
{
Key: 'first-service-2.zip',
},
{
Key: 'first-service-4.zip',
},
{
Key: 'first-service-3.zip',
},
],
};
const listObjectsStub = sinon
.stub(awsDeploy.sdk, 'request').returns(BbPromise.resolve(serviceObjects));
return awsDeploy.getServiceObjectsFromS3Bucket().then((objectsToRemove) => {
expect(objectsToRemove).to.equal(undefined);
expect(listObjectsStub.calledOnce).to.be.equal(true);
expect(listObjectsStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.request.restore();
});
});
});
describe('#cleanupS3Bucket()', () => {
let deleteObjectsStub;
beforeEach(() => {
deleteObjectsStub = sinon
.stub(awsDeploy.sdk, 'request').returns(BbPromise.resolve());
});
it('should resolve if no service objects are found in the S3 bucket', () => awsDeploy
.cleanupS3Bucket().then(() => {
expect(deleteObjectsStub.calledOnce).to.be.equal(false);
awsDeploy.sdk.request.restore();
})
);
it('should remove all old service files from the S3 bucket if available', () => {
const serviceObjects = [{ Key: 'first-service' }, { Key: 'second-service' }];
return awsDeploy.cleanupS3Bucket(serviceObjects).then(() => {
expect(deleteObjectsStub.calledOnce).to.be.equal(true);
expect(deleteObjectsStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.request.restore();
});
});
});
describe('#uploadZipFileToS3Bucket()', () => {
it('should upload the zip file to the S3 bucket', () => {
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(() => {
expect(putObjectStub.calledOnce).to.be.equal(true);
expect(putObjectStub.calledWith(awsDeploy.options.stage, awsDeploy.options.region));
awsDeploy.sdk.request.restore();
});
});
});
describe('#uploadDeploymentPackage()', () => {
it('should resolve if no deploy', () => {
awsDeploy.options.noDeploy = true;
const setServerlessDeploymentBucketNameStub = sinon
.stub(awsDeploy, 'setServerlessDeploymentBucketName').returns(BbPromise.resolve());
const getServiceObjectsFromS3BucketStub = sinon
.stub(awsDeploy, 'getServiceObjectsFromS3Bucket').returns(BbPromise.resolve());
const cleanupS3BucketStub = sinon
.stub(awsDeploy, 'cleanupS3Bucket').returns(BbPromise.resolve());
const uploadZipFileToS3BucketStub = sinon
.stub(awsDeploy, 'uploadZipFileToS3Bucket').returns(BbPromise.resolve());
return awsDeploy.uploadDeploymentPackage().then(() => {
expect(setServerlessDeploymentBucketNameStub.called).to.be.equal(false);
expect(getServiceObjectsFromS3BucketStub.called).to.be.equal(false);
expect(cleanupS3BucketStub.called).to.be.equal(false);
expect(uploadZipFileToS3BucketStub.called).to.be.equal(false);
awsDeploy.setServerlessDeploymentBucketName.restore();
awsDeploy.getServiceObjectsFromS3Bucket.restore();
awsDeploy.cleanupS3Bucket.restore();
awsDeploy.uploadZipFileToS3Bucket.restore();
});
});
it('should run promise chain in order', () => {
const setServerlessDeploymentBucketNameStub = sinon
.stub(awsDeploy, 'setServerlessDeploymentBucketName').returns(BbPromise.resolve());
const getServiceObjectsFromS3BucketStub = sinon
.stub(awsDeploy, 'getServiceObjectsFromS3Bucket').returns(BbPromise.resolve());
const cleanupS3BucketStub = sinon
.stub(awsDeploy, 'cleanupS3Bucket').returns(BbPromise.resolve());
const uploadZipFileToS3BucketStub = sinon
.stub(awsDeploy, 'uploadZipFileToS3Bucket').returns(BbPromise.resolve());
return awsDeploy.uploadDeploymentPackage().then(() => {
expect(setServerlessDeploymentBucketNameStub.calledOnce).to.be.equal(true);
expect(getServiceObjectsFromS3BucketStub.calledAfter(setServerlessDeploymentBucketNameStub))
.to.be.equal(true);
expect(cleanupS3BucketStub.calledAfter(getServiceObjectsFromS3BucketStub))
.to.be.equal(true);
expect(uploadZipFileToS3BucketStub.calledAfter(cleanupS3BucketStub)).to.be.equal(true);
awsDeploy.setServerlessDeploymentBucketName.restore();
awsDeploy.getServiceObjectsFromS3Bucket.restore();
awsDeploy.cleanupS3Bucket.restore();
awsDeploy.uploadZipFileToS3Bucket.restore();
});
});
});
});