|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const expect = require('chai').expect; |
| 4 | +const sinon = require('sinon'); |
| 5 | +const BbPromise = require('bluebird'); |
| 6 | +const Serverless = require('serverless/lib/Serverless'); |
| 7 | +const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider'); |
| 8 | +const ServerlessStepFunctions = require('./index'); |
| 9 | + |
| 10 | +describe('#index', () => { |
| 11 | + let serverless; |
| 12 | + let serverlessStepFunctions; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + serverless = new Serverless(); |
| 16 | + serverless.service.service = 'step-functions'; |
| 17 | + const options = { |
| 18 | + stage: 'dev', |
| 19 | + region: 'us-east-1', |
| 20 | + }; |
| 21 | + serverless.init(); |
| 22 | + serverless.setProvider('aws', new AwsProvider(serverless)); |
| 23 | + serverlessStepFunctions = new ServerlessStepFunctions(serverless, options); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('#constructor()', () => { |
| 27 | + it('should have hooks', () => expect(serverlessStepFunctions.hooks).to.be.not.empty); |
| 28 | + |
| 29 | + it('should set the provider variable to an instance of AwsProvider', () => |
| 30 | + expect(serverlessStepFunctions.provider).to.be.instanceof(AwsProvider)); |
| 31 | + |
| 32 | + it('should have access to the serverless instance', () => |
| 33 | + expect(serverlessStepFunctions.serverless).to.deep.equal(serverless)); |
| 34 | + |
| 35 | + it('should set the options variable', () => |
| 36 | + expect(serverlessStepFunctions.options).to.deep.equal({ |
| 37 | + stage: 'dev', |
| 38 | + region: 'us-east-1', |
| 39 | + }) |
| 40 | + ); |
| 41 | + |
| 42 | + it('should set the region variable', () => |
| 43 | + expect(serverlessStepFunctions.region).to.be.equal('us-east-1')); |
| 44 | + |
| 45 | + it('should set the stage variable', () => |
| 46 | + expect(serverlessStepFunctions.stage).to.be.equal('dev')); |
| 47 | + |
| 48 | + it('should run invoke:stepf:invoke promise chain in order', () => { |
| 49 | + const invokeStub = sinon |
| 50 | + .stub(serverlessStepFunctions, 'invoke').returns(BbPromise.resolve()); |
| 51 | + return serverlessStepFunctions.hooks['invoke:stepf:invoke']() |
| 52 | + .then(() => { |
| 53 | + expect(invokeStub.calledOnce).to.be.equal(true); |
| 54 | + serverlessStepFunctions.invoke.restore(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should run deploy:initialize promise chain in order', () => { |
| 59 | + const yamlParseStub = sinon |
| 60 | + .stub(serverlessStepFunctions, 'yamlParse').returns(BbPromise.resolve()); |
| 61 | + return serverlessStepFunctions.hooks['deploy:initialize']() |
| 62 | + .then(() => { |
| 63 | + expect(yamlParseStub.calledOnce).to.be.equal(true); |
| 64 | + serverlessStepFunctions.yamlParse.restore(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should run deploy:compileFunctions promise chain in order', () => { |
| 69 | + const compileIamRoleStub = sinon |
| 70 | + .stub(serverlessStepFunctions, 'compileIamRole').returns(BbPromise.resolve()); |
| 71 | + const compileStateMachinesStub = sinon |
| 72 | + .stub(serverlessStepFunctions, 'compileStateMachines').returns(BbPromise.resolve()); |
| 73 | + const compileActivitiesStub = sinon |
| 74 | + .stub(serverlessStepFunctions, 'compileActivities').returns(BbPromise.resolve()); |
| 75 | + return serverlessStepFunctions.hooks['deploy:compileFunctions']() |
| 76 | + .then(() => { |
| 77 | + expect(compileIamRoleStub.calledOnce).to.be.equal(true); |
| 78 | + expect(compileStateMachinesStub.calledAfter(compileIamRoleStub)).to.be.equal(true); |
| 79 | + expect(compileActivitiesStub.calledAfter(compileStateMachinesStub)).to.be.equal(true); |
| 80 | + serverlessStepFunctions.compileIamRole.restore(); |
| 81 | + serverlessStepFunctions.compileStateMachines.restore(); |
| 82 | + serverlessStepFunctions.compileActivities.restore(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should run deploy:compileEvents promise chain in order when http event is empty', |
| 87 | + () => { |
| 88 | + const httpValidateStub = sinon |
| 89 | + .stub(serverlessStepFunctions, 'httpValidate').returns({ events: [] }); |
| 90 | + const compileRestApiStub = sinon |
| 91 | + .stub(serverlessStepFunctions, 'compileRestApi').returns(BbPromise.resolve()); |
| 92 | + const compileResourcesStub = sinon |
| 93 | + .stub(serverlessStepFunctions, 'compileResources').returns(BbPromise.resolve()); |
| 94 | + const compileMethodsStub = sinon |
| 95 | + .stub(serverlessStepFunctions, 'compileMethods').returns(BbPromise.resolve()); |
| 96 | + const compileHttpIamRoleStub = sinon |
| 97 | + .stub(serverlessStepFunctions, 'compileHttpIamRole').returns(BbPromise.resolve()); |
| 98 | + const compileDeploymentStub = sinon |
| 99 | + .stub(serverlessStepFunctions, 'compileDeployment').returns(BbPromise.resolve()); |
| 100 | + return serverlessStepFunctions.hooks['deploy:compileEvents']() |
| 101 | + .then(() => { |
| 102 | + expect(httpValidateStub.calledOnce).to.be.equal(true); |
| 103 | + expect(compileRestApiStub.notCalled).to.be.equal(true); |
| 104 | + expect(compileResourcesStub.notCalled).to.be.equal(true); |
| 105 | + expect(compileMethodsStub.notCalled).to.be.equal(true); |
| 106 | + expect(compileHttpIamRoleStub.notCalled).to.be.equal(true); |
| 107 | + expect(compileDeploymentStub.notCalled).to.be.equal(true); |
| 108 | + serverlessStepFunctions.httpValidate.restore(); |
| 109 | + serverlessStepFunctions.compileRestApi.restore(); |
| 110 | + serverlessStepFunctions.compileResources.restore(); |
| 111 | + serverlessStepFunctions.compileMethods.restore(); |
| 112 | + serverlessStepFunctions.compileHttpIamRole.restore(); |
| 113 | + serverlessStepFunctions.compileDeployment.restore(); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should run deploy:compileEvents promise chain in order', |
| 118 | + () => { |
| 119 | + const httpValidateStub = sinon |
| 120 | + .stub(serverlessStepFunctions, 'httpValidate').returns({ events: [1, 2, 3] }); |
| 121 | + const compileRestApiStub = sinon |
| 122 | + .stub(serverlessStepFunctions, 'compileRestApi').returns(BbPromise.resolve()); |
| 123 | + const compileResourcesStub = sinon |
| 124 | + .stub(serverlessStepFunctions, 'compileResources').returns(BbPromise.resolve()); |
| 125 | + const compileMethodsStub = sinon |
| 126 | + .stub(serverlessStepFunctions, 'compileMethods').returns(BbPromise.resolve()); |
| 127 | + const compileHttpIamRoleStub = sinon |
| 128 | + .stub(serverlessStepFunctions, 'compileHttpIamRole').returns(BbPromise.resolve()); |
| 129 | + const compileDeploymentStub = sinon |
| 130 | + .stub(serverlessStepFunctions, 'compileDeployment').returns(BbPromise.resolve()); |
| 131 | + return serverlessStepFunctions.hooks['deploy:compileEvents']() |
| 132 | + .then(() => { |
| 133 | + expect(httpValidateStub.calledOnce).to.be.equal(true); |
| 134 | + expect(compileRestApiStub.calledOnce).to.be.equal(true); |
| 135 | + expect(compileResourcesStub.calledAfter(compileRestApiStub)).to.be.equal(true); |
| 136 | + expect(compileMethodsStub.calledAfter(compileResourcesStub)).to.be.equal(true); |
| 137 | + expect(compileHttpIamRoleStub.calledAfter(compileMethodsStub)).to.be.equal(true); |
| 138 | + expect(compileDeploymentStub.calledAfter(compileHttpIamRoleStub)).to.be.equal(true); |
| 139 | + |
| 140 | + serverlessStepFunctions.httpValidate.restore(); |
| 141 | + serverlessStepFunctions.compileRestApi.restore(); |
| 142 | + serverlessStepFunctions.compileResources.restore(); |
| 143 | + serverlessStepFunctions.compileMethods.restore(); |
| 144 | + serverlessStepFunctions.compileHttpIamRole.restore(); |
| 145 | + serverlessStepFunctions.compileDeployment.restore(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + it('should run after:deploy:deploy promise chain in order', () => { |
| 149 | + const getEndpointInfoStub = sinon |
| 150 | + .stub(serverlessStepFunctions, 'getEndpointInfo').returns(BbPromise.resolve()); |
| 151 | + const displayStub = sinon |
| 152 | + .stub(serverlessStepFunctions, 'display').returns(BbPromise.resolve()); |
| 153 | + return serverlessStepFunctions.hooks['after:deploy:deploy']() |
| 154 | + .then(() => { |
| 155 | + expect(getEndpointInfoStub.calledOnce).to.be.equal(true); |
| 156 | + expect(displayStub.calledAfter(getEndpointInfoStub)).to.be.equal(true); |
| 157 | + serverlessStepFunctions.getEndpointInfo.restore(); |
| 158 | + serverlessStepFunctions.display.restore(); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('#invoke()', () => { |
| 164 | + it('should run promise chain in order', () => { |
| 165 | + const getStateMachineArnStub = sinon |
| 166 | + .stub(serverlessStepFunctions, 'getStateMachineArn').returns(BbPromise.resolve()); |
| 167 | + const startExecutionStub = sinon |
| 168 | + .stub(serverlessStepFunctions, 'startExecution').returns(BbPromise.resolve()); |
| 169 | + const describeExecutionStub = sinon |
| 170 | + .stub(serverlessStepFunctions, 'describeExecution') |
| 171 | + .returns(BbPromise.resolve({ status: 'SUCCEED' })); |
| 172 | + |
| 173 | + return serverlessStepFunctions.invoke() |
| 174 | + .then(() => { |
| 175 | + expect(getStateMachineArnStub.calledOnce).to.be.equal(true); |
| 176 | + expect(startExecutionStub.calledAfter(getStateMachineArnStub)).to.be.equal(true); |
| 177 | + expect(describeExecutionStub.calledAfter(startExecutionStub)).to.be.equal(true); |
| 178 | + |
| 179 | + serverlessStepFunctions.getStateMachineArn.restore(); |
| 180 | + serverlessStepFunctions.startExecution.restore(); |
| 181 | + serverlessStepFunctions.describeExecution.restore(); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should run promise chain in order when invocation error occurs', () => { |
| 186 | + const getStateMachineArnStub = sinon |
| 187 | + .stub(serverlessStepFunctions, 'getStateMachineArn').returns(BbPromise.resolve()); |
| 188 | + const startExecutionStub = sinon |
| 189 | + .stub(serverlessStepFunctions, 'startExecution').returns(BbPromise.resolve()); |
| 190 | + const describeExecutionStub = sinon |
| 191 | + .stub(serverlessStepFunctions, 'describeExecution') |
| 192 | + .returns(BbPromise.resolve({ status: 'FAILED' })); |
| 193 | + const getExecutionHistoryStub = sinon |
| 194 | + .stub(serverlessStepFunctions, 'getExecutionHistory').returns(BbPromise.resolve({ |
| 195 | + events: [{ |
| 196 | + executionFailedEventDetails: '', |
| 197 | + }], |
| 198 | + })); |
| 199 | + |
| 200 | + return serverlessStepFunctions.invoke() |
| 201 | + .then(() => { |
| 202 | + expect(getStateMachineArnStub.calledOnce).to.be.equal(true); |
| 203 | + expect(startExecutionStub.calledAfter(getStateMachineArnStub)).to.be.equal(true); |
| 204 | + expect(describeExecutionStub.calledAfter(startExecutionStub)).to.be.equal(true); |
| 205 | + expect(getExecutionHistoryStub.calledAfter(describeExecutionStub)).to.be.equal(true); |
| 206 | + |
| 207 | + serverlessStepFunctions.getStateMachineArn.restore(); |
| 208 | + serverlessStepFunctions.startExecution.restore(); |
| 209 | + serverlessStepFunctions.describeExecution.restore(); |
| 210 | + serverlessStepFunctions.getExecutionHistory.restore(); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments