-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.test.js
141 lines (124 loc) · 4.98 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict'
const _ = require('lodash')
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider')
const createChangeSet = require('./lib/createChangeSet')
const expect = require('chai').expect
const Serverless = require('serverless/lib/Serverless')
const ServerlessCloudFormationChangeSets = require('./index')
const setBucketName = require('serverless/lib/plugins/aws/lib/setBucketName')
const sinon = require('sinon')
describe('ServerlessCloudFormationChangeSets', () => {
let serverless
let serverlessChangeSets
let options
beforeEach(() => {
serverless = new Serverless()
options = {
stage: 'dev',
region: 'us-east-1',
changeset: 'test'
}
serverless.setProvider('aws', new AwsProvider(serverless))
serverlessChangeSets = new ServerlessCloudFormationChangeSets(serverless, options)
serverlessChangeSets.serverless.service.provider.shouldNotDeploy = false
})
describe('#constructor()', () => {
it('should set the serverless instance', () => {
expect(serverlessChangeSets.serverless).to.equal(serverless)
})
it('should set options from CLI command', () => {
expect(serverlessChangeSets.options).to.deep.equal({
stage: 'dev',
region: 'us-east-1',
requireChangeSet: true,
changeSetName: 'test'
})
})
it('should set options from YAML config file', () => {
_.set(serverless, 'service.custom.cf-changesets.changeSetName', 'test')
_.set(serverless, 'service.custom.cf-changesets.requireChangeSet', true)
const serverlessChangeSets = new ServerlessCloudFormationChangeSets(serverless, {
stage: 'dev',
region: 'us-east-1'
})
expect(serverlessChangeSets.options).to.deep.equal({
stage: 'dev',
region: 'us-east-1',
requireChangeSet: true,
changeSetName: 'test'
})
})
it('should override requireChangeSet when calling from CLI', () => {
_.set(serverless, 'service.custom.cf-changesets.changeSetName', 'test')
_.set(serverless, 'service.custom.cf-changesets.requireChangeSet', false)
const serverlessChangeSets = new ServerlessCloudFormationChangeSets(serverless, {
stage: 'dev',
region: 'us-east-1',
changeset: true
})
expect(serverlessChangeSets.options).to.deep.equal({
stage: 'dev',
region: 'us-east-1',
requireChangeSet: true,
changeSetName: 'test'
})
})
it('should override changeSetName when calling from CLI', () => {
_.set(serverless, 'service.custom.cf-changesets.changeSetName', 'test')
const serverlessChangeSets = new ServerlessCloudFormationChangeSets(serverless, {
stage: 'dev',
region: 'us-east-1',
changeset: 'whatever'
})
expect(serverlessChangeSets.options).to.deep.equal({
stage: 'dev',
region: 'us-east-1',
requireChangeSet: true,
changeSetName: 'whatever'
})
})
it('should set the provider variable to an instance of AwsProvider', () =>
expect(serverlessChangeSets.provider).to.be.instanceof(AwsProvider))
it('should have hooks', () => expect(serverlessChangeSets.hooks).to.be.not.empty)
it('should have no hooks if changset options are not defined', () => {
const options = {
stage: 'dev',
region: 'us-east-1'
}
const serverlessChangeSets = new ServerlessCloudFormationChangeSets(serverless, options)
expect(serverlessChangeSets.hooks).to.be.undefined
})
})
describe('hooks', () => {
beforeEach(() => {
sinon.stub(setBucketName, 'setBucketName').resolves()
sinon.stub(createChangeSet, 'createChangeSet').resolves()
})
afterEach(() => {
setBucketName.setBucketName.restore()
createChangeSet.createChangeSet.restore()
})
it('should load deployment bucket name', () => {
serverlessChangeSets.hooks['aws:deploy:deploy:updateStack']().then(() => {
sinon.assert.calledOnce(setBucketName.setBucketName)
sinon.assert.calledOn(setBucketName.setBucketName, serverlessChangeSets)
})
})
it('should call createChangeSet', () => {
serverlessChangeSets.hooks['aws:deploy:deploy:updateStack']().then(() => {
sinon.assert.calledOnce(createChangeSet.createChangeSet)
sinon.assert.calledOn(createChangeSet.createChangeSet, serverlessChangeSets)
sinon.assert.callOrder(setBucketName.setBucketName, createChangeSet.createChangeSet)
})
})
it('should prevent regular deployment', () => {
serverlessChangeSets.hooks['before:aws:deploy:deploy:updateStack']()
expect(serverlessChangeSets.serverless.service.provider.shouldNotDeploy).to.equal(true)
})
it('should restore shouldNotDeploy flag', () => {
serverlessChangeSets.hooks['before:aws:deploy:deploy:updateStack']()
serverlessChangeSets.hooks['after:aws:deploy:deploy:updateStack']()
expect(serverlessChangeSets.serverless.service.provider.shouldNotDeploy).to.equal(false)
})
})
})