This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBasicJavaApplicationDeployer.test.js
233 lines (207 loc) · 9.52 KB
/
BasicJavaApplicationDeployer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict'
const expect = require('expect')
const sandbox = require('sinon').createSandbox()
const { OpenShiftClientX } = require('@bcgov/pipeline-cli')
const Jira = require('../lib/Jira')
const Verifier = require('../lib/InputDeployerVerify')
const BasicJavaApplicationDeployer = require('../lib/BasicJavaApplicationDeployer')
describe('BasicJavaApplicationDeployer:', function() {
this.timeout(50000)
let ocApplyRecommendedLabelsStub
let ocApplyAndDeployStub
let ocImportImageStreamsStub
let verifyBeforeDeploymentStub
let jiraTransitionRFDpostDeploymentStub
// stubing
this.beforeEach(function() {
ocApplyRecommendedLabelsStub = sandbox.stub(OpenShiftClientX.prototype, 'applyRecommendedLabels')
ocApplyAndDeployStub = sandbox.stub(OpenShiftClientX.prototype, 'applyAndDeploy')
ocImportImageStreamsStub = sandbox.stub(OpenShiftClientX.prototype, 'importImageStreams')
verifyBeforeDeploymentStub = sandbox.stub(Verifier.prototype, 'verifyBeforeDeployment')
jiraTransitionRFDpostDeploymentStub = sandbox.stub(Jira.prototype, 'transitionRFDpostDeployment')
})
afterEach(function() {
sandbox.restore()
})
context("When 'CI' deployment ...", function() {
it('Just do deploy...', async function() {
// Arrange
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'sandbox'
const deployer = new BasicJavaApplicationDeployer(settingsStub)
const isCDdeploymentStub = sandbox.stub(deployer, 'isCDdeployment')
isCDdeploymentStub.returns(false)
const deployOpenshiftStub = sandbox.stub(deployer, 'deployOpenshift')
// Act
await deployer.deploy()
// Verify
sandbox.assert.calledOnce(isCDdeploymentStub)
sandbox.assert.calledOnce(deployOpenshiftStub)
sandbox.assert.notCalled(verifyBeforeDeploymentStub)
sandbox.assert.notCalled(jiraTransitionRFDpostDeploymentStub)
})
})
context("When 'CD' deployment ...", function() {
it('When JIRA verify conditon pass, do RFD transition in its env then deploy...', async function() {
// Arrange
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'test'
const deployer = new BasicJavaApplicationDeployer(settingsStub)
const isCDdeploymentStub = sandbox.stub(deployer, 'isCDdeployment')
isCDdeploymentStub.returns(true)
verifyBeforeDeploymentStub.returns(Promise.resolve('Ready'))
const deployOpenshiftStub = sandbox.stub(deployer, 'deployOpenshift')
// Act
await expect(deployer.deploy()).rejects.toThrow()
// Verify
sandbox.assert.calledOnce(isCDdeploymentStub)
sandbox.assert.notCalled(deployOpenshiftStub)
sandbox.assert.calledOnce(verifyBeforeDeploymentStub)
sandbox.assert.notCalled(jiraTransitionRFDpostDeploymentStub)
// sandbox.assert.calledWith(jiraTransitionRFDpostDeploymentStub, settingsStub.options.env)
})
it('When JIRA verify conditon fail, faile pipeline', async function() {
// Arrange
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'test'
const deployer = new BasicJavaApplicationDeployer(settingsStub)
const isCDdeploymentStub = sandbox.stub(deployer, 'isCDdeployment')
isCDdeploymentStub.returns(true)
verifyBeforeDeploymentStub.returns(Promise.resolve('Not Ready'))
const deployOpenshiftStub = sandbox.stub(deployer, 'deployOpenshift')
// Act
try {
await deployer.deploy()
} catch (err) {
expect(err.message).toContain('Not Ready for Deployment')
}
// Verify
sandbox.assert.calledOnce(isCDdeploymentStub)
sandbox.assert.calledOnce(verifyBeforeDeploymentStub)
sandbox.assert.notCalled(deployOpenshiftStub)
sandbox.assert.notCalled(jiraTransitionRFDpostDeploymentStub)
})
})
it("When env is either 'dev', 'sbox' or 'sandbox', it should be CI deployment...", async function() {
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'dev'
let deployer = new BasicJavaApplicationDeployer(settingsStub)
let isCDdeployment = deployer.isCDdeployment()
expect(isCDdeployment).toBe(false)
settingsStub.options.env = 'sbox'
deployer = new BasicJavaApplicationDeployer(settingsStub)
isCDdeployment = deployer.isCDdeployment()
expect(isCDdeployment).toBe(false)
settingsStub.options.env = 'sandbox'
deployer = new BasicJavaApplicationDeployer(settingsStub)
isCDdeployment = deployer.isCDdeployment()
expect(isCDdeployment).toBe(false)
})
it("When env is either 'dlvr', 'test' or 'prod', it should be CD deployment...", async function() {
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'dlvr'
let deployer = new BasicJavaApplicationDeployer(settingsStub)
let isCDdeployment = deployer.isCDdeployment()
expect(isCDdeployment).toBe(true)
settingsStub.options.env = 'test'
deployer = new BasicJavaApplicationDeployer(settingsStub)
isCDdeployment = deployer.isCDdeployment()
expect(isCDdeployment).toBe(true)
settingsStub.options.env = 'prod'
deployer = new BasicJavaApplicationDeployer(settingsStub)
isCDdeployment = deployer.isCDdeployment()
expect(isCDdeployment).toBe(true)
})
it("When 'env' is unknown value, it stops pipeline", function() {
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'unknown-env'
const deployer = new BasicJavaApplicationDeployer(settingsStub)
expect(() => deployer.isCDdeployment()).toThrow()
})
context('Deploy to OpenShift', function() {
it('When called, will use pipeline-cli to apply labels, import Images and deploy', async function() {
// Arrange
const settingsStub = getDefaultSettings()
settingsStub.options.env = 'test'
const deployer = new BasicJavaApplicationDeployer(settingsStub)
const processedTemplateStub = { processTemplates: 'myTemplate' }
sandbox.stub(deployer, 'processTemplates').callsFake(() => processedTemplateStub)
// Act
await deployer.deployOpenshift()
// Verify
sandbox.assert.calledOnce(deployer.processTemplates)
sandbox.assert.calledOnce(ocApplyRecommendedLabelsStub)
sandbox.assert.calledWith(
ocApplyRecommendedLabelsStub,
processedTemplateStub,
settingsStub.phases[settingsStub.phase].name,
settingsStub.phase,
settingsStub.phases[settingsStub.phase].changeId,
settingsStub.phases[settingsStub.phase].instance
)
sandbox.assert.calledOnce(ocImportImageStreamsStub)
sandbox.assert.calledWith(
ocImportImageStreamsStub,
processedTemplateStub,
settingsStub.phases[settingsStub.phase].tag,
settingsStub.phases.build.namespace,
settingsStub.phases.build.tag
)
sandbox.assert.calledOnce(ocApplyAndDeployStub)
sandbox.assert.calledWith(
ocApplyAndDeployStub,
processedTemplateStub,
settingsStub.phases[settingsStub.phase].instance
)
})
})
function getDefaultSettings() {
const defaultSettings = {
phases: {
build: {
namespace: 'wp9gel-tools',
phase: 'build',
tag: 'build-1.0-1',
},
test: {
namespace: 'wp9gel-test',
name: 'siwe',
phase: 'test',
changeId: '1',
tag: 'test-1.0',
instance: 'siwe-test',
transient: false,
host: 'siwe-test-wp9gel-test.pathfinder.bcgov',
credentials: {
idir: {
user: '[email protected]',
pass: 'stub',
},
},
},
},
options: {
git: {
branch: {
name: 'master',
merge: 'master',
remote: 'master',
},
url: 'https://bwa.nrs.gov.bc.ca/int/stash/scm/siwe/siwe-siwe-ear.git',
dir: '/Users/iliu/Workspace/Repo/spi/spi-siwe-ear',
uri: 'https://bwa.nrs.gov.bc.ca/int/stash/scm/siwe/siwe-siwe-ear.git',
http_url: 'https://bwa.nrs.gov.bc.ca/int/stash/scm/siwe/siwe-siwe-ear.git',
owner: 'SIWE',
repository: 'siwe-siwe-ear',
},
env: 'test',
pr: '1',
cwd: '/some/user/Workspace/Repo/spi/spi-siwe-ear',
},
jiraUrl: 'bwa.nrs.gov.bc.ca/int/jira',
bitbucketUrl: 'https://bwa.nrs.gov.bc.ca/int/stash',
phase: 'test',
}
return defaultSettings
} // getDefaultSettings
})