-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathOdsComponentStageRolloutOpenShiftDeploymentSpec.groovy
More file actions
326 lines (286 loc) · 13.8 KB
/
OdsComponentStageRolloutOpenShiftDeploymentSpec.groovy
File metadata and controls
326 lines (286 loc) · 13.8 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package vars
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
import org.ods.component.Context
import org.ods.component.IContext
import org.ods.services.OpenShiftService
import org.ods.services.JenkinsService
import org.ods.services.ServiceRegistry
import org.ods.util.Logger
import org.ods.util.PodData
import util.PipelineSteps
import vars.test_helper.PipelineSpockTestBase
import spock.lang.*
class OdsComponentStageRolloutOpenShiftDeploymentSpec extends PipelineSpockTestBase {
private Logger logger = Mock(Logger)
@Shared
def config = [
gitUrl: 'https://example.com/scm/foo/bar.git',
gitCommit: 'cd3e9082d7466942e1de86902bb9e663751dae8e',
gitCommitMessage: 'Foo',
gitCommitAuthor: 'John Doe',
gitCommitTime: '2020-03-23 12:27:08 +0100',
gitBranch: 'master',
buildUrl: 'https://jenkins.example.com/job/foo-cd/job/foo-cd-bar-master/11/console',
buildTime: '2020-03-23 12:27:08 +0100',
odsSharedLibVersion: '2.x',
projectId: 'foo',
componentId: 'bar',
cdProject: 'foo-cd',
artifactUriStore: [builds: [bar: [:]]]
]
def "run successfully without Tailor [DeploymentConfig]"() {
given:
def c = config + [environment: 'dev', targetProject: 'foo-dev', openshiftRolloutTimeoutRetries: 6]
IContext context = new Context(null, c, logger)
OpenShiftService openShiftService = Stub(OpenShiftService.class)
openShiftService.getResourcesForComponent('foo-dev', ['Deployment', 'DeploymentConfig'], 'app=foo-bar') >> [DeploymentConfig: ['bar']]
openShiftService.getRevision('foo-dev', 'DeploymentConfig', 'bar') >> 123
openShiftService.rollout('foo-dev', 'DeploymentConfig', 'bar', 123, 15) >> "bar-124"
// test the handover of the poddata retries
openShiftService.getPodDataForDeployment('foo-dev', 'DeploymentConfig', 'bar-124', 6) >> [new PodData([ deploymentId: "bar-124" ])]
openShiftService.getImagesOfDeployment('foo-dev', 'DeploymentConfig', 'bar') >> [[ repository: 'foo', name: 'bar' ]]
ServiceRegistry.instance.add(OpenShiftService, openShiftService)
JenkinsService jenkinsService = Stub(JenkinsService.class)
jenkinsService.maybeWithPrivateKeyCredentials(*_) >> { args -> args[1]('/tmp/file') }
ServiceRegistry.instance.add(JenkinsService, jenkinsService)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
helper.registerAllowedMethod('fileExists', [ String ]) { String args ->
false
}
def deploymentInfo = script.call(context)
then:
printCallStack()
assertJobStatusSuccess()
deploymentInfo['DeploymentConfig/bar'][0].deploymentId == "bar-124"
// test artifact URIS
def buildArtifacts = context.getBuildArtifactURIs()
buildArtifacts.size() > 0
buildArtifacts.deployments.containsKey (config.componentId)
buildArtifacts.deployments[config.componentId].deploymentId == deploymentInfo['DeploymentConfig/bar'][0].deploymentId
}
def "run successfully without Tailor [Deployment]"() {
given:
def c = config + [environment: 'dev', targetProject: 'foo-dev', openshiftRolloutTimeoutRetries: 6]
IContext context = new Context(null, c, logger)
OpenShiftService openShiftService = Stub(OpenShiftService.class)
openShiftService.getResourcesForComponent('foo-dev', ['Deployment', 'DeploymentConfig'], 'app=foo-bar') >> [Deployment: ['bar']]
openShiftService.getRevision('foo-dev', 'Deployment', 'bar') >> 123
openShiftService.rollout('foo-dev', 'Deployment', 'bar', 123, 15) >> "bar-6f8db5fb69"
// test the handover of the poddata retries
openShiftService.getPodDataForDeployment('foo-dev', 'Deployment', 'bar-6f8db5fb69', 6) >> [new PodData([ deploymentId: "bar-6f8db5fb69" ])]
openShiftService.getImagesOfDeployment('foo-dev', 'Deployment', 'bar') >> [[ repository: 'foo', name: 'bar' ]]
ServiceRegistry.instance.add(OpenShiftService, openShiftService)
JenkinsService jenkinsService = Stub(JenkinsService.class)
jenkinsService.maybeWithPrivateKeyCredentials(*_) >> { args -> args[1]('/tmp/file') }
ServiceRegistry.instance.add(JenkinsService, jenkinsService)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
helper.registerAllowedMethod('fileExists', [ String ]) { String args ->
false
}
def deploymentInfo = script.call(context)
then:
printCallStack()
assertJobStatusSuccess()
deploymentInfo['Deployment/bar'][0].deploymentId == "bar-6f8db5fb69"
// test artifact URIS
def buildArtifacts = context.getBuildArtifactURIs()
buildArtifacts.size() > 0
buildArtifacts.deployments.containsKey (config.componentId)
buildArtifacts.deployments[config.componentId].deploymentId == deploymentInfo['Deployment/bar'][0].deploymentId
}
def "run successfully with Tailor"() {
given:
def c = config + [environment: 'dev', targetProject: 'foo-dev', openshiftRolloutTimeoutRetries: 5]
IContext context = new Context(null, c, logger)
OpenShiftService openShiftService = Mock(OpenShiftService.class)
openShiftService.getResourcesForComponent('foo-dev', ['Deployment', 'DeploymentConfig'], 'app=foo-bar') >> [DeploymentConfig: ['bar']]
openShiftService.getRevision(*_) >> 123
openShiftService.rollout(*_) >> "${config.componentId}-124"
openShiftService.getPodDataForDeployment(*_) >> [new PodData([ deploymentId: "${config.componentId}-124" ])]
openShiftService.getImagesOfDeployment(*_) >> [[ repository: 'foo', name: 'bar' ]]
ServiceRegistry.instance.add(OpenShiftService, openShiftService)
JenkinsService jenkinsService = Stub(JenkinsService.class)
jenkinsService.maybeWithPrivateKeyCredentials(*_) >> { args -> args[1]('/tmp/file') }
ServiceRegistry.instance.add(JenkinsService, jenkinsService)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
helper.registerAllowedMethod('fileExists', [ String ]) { String args ->
args == 'openshift'
}
def deploymentInfo = script.call(context)
then:
printCallStack()
assertJobStatusSuccess()
deploymentInfo['DeploymentConfig/bar'][0].deploymentId == "bar-124"
// test artifact URIS
def buildArtifacts = context.getBuildArtifactURIs()
buildArtifacts.size() > 0
buildArtifacts.deployments.containsKey(config.componentId)
buildArtifacts.deployments[config.componentId].deploymentId == deploymentInfo['DeploymentConfig/bar'][0].deploymentId
1 * openShiftService.tailorApply(
'foo-dev',
[selector: 'app=foo-bar', exclude: 'bc,is'],
'',
[],
[],
'/tmp/file',
true
)
}
def "run successfully with Helm"() {
given:
def c = config + [environment: 'dev',targetProject: 'foo-dev',openshiftRolloutTimeoutRetries: 5,chartDir: 'chart']
IContext context = new Context(null, c, logger)
OpenShiftService openShiftService = Mock(OpenShiftService.class)
openShiftService.getResourcesForComponent('foo-dev', ['Deployment', 'DeploymentConfig'], 'app=foo-bar') >> [Deployment: ['bar']]
openShiftService.getRevision(*_) >> 123
openShiftService.rollout(*_) >> "${config.componentId}-124"
openShiftService.getPodDataForDeployment(*_) >> [new PodData([ deploymentId: "${config.componentId}-124" ])]
openShiftService.getImagesOfDeployment(*_) >> [[ repository: 'foo', name: 'bar' ]]
openShiftService.checkForPodData(*_) >> [new PodData([deploymentId: "${config.componentId}-124"])]
ServiceRegistry.instance.add(OpenShiftService, openShiftService)
JenkinsService jenkinsService = Stub(JenkinsService.class)
jenkinsService.maybeWithPrivateKeyCredentials(*_) >> { args -> args[1]('/tmp/file') }
ServiceRegistry.instance.add(JenkinsService, jenkinsService)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
helper.registerAllowedMethod('fileExists', [ String ]) { String args ->
args == 'chart/Chart.yaml' || args == 'metadata.yml'
}
helper.registerAllowedMethod('readYaml', [ Map ]) { Map args ->
def testSteps = new PipelineSteps()
testSteps.readYaml(args) { file ->
def metadata = null
switch (file) {
case 'metadata.yml':
metadata = [
name: 'Name',
description: 'Description',
supplier: 'none',
version: '1.0',
type: 'ods',
]
break
case 'chart/Chart.yaml':
metadata = [
name: 'myChart',
version: '1.0+01',
]
break
}
return metadata
}
}
def deploymentInfo = script.call(context)
then:
printCallStack()
assertJobStatusSuccess()
deploymentInfo['Deployment/bar'][0].deploymentId == "bar-124"
// test artifact URIS
def buildArtifacts = context.getBuildArtifactURIs()
buildArtifacts.size() > 0
buildArtifacts.deployments['bar-deploymentMean']['type'] == 'helm'
1 * openShiftService.helmUpgrade('foo-dev', 'bar', ['values.yaml'], [:], ['registry':null, 'componentId':'bar', 'global.registry':null, 'global.componentId':'bar', 'imageNamespace':'foo-dev', 'imageTag':'cd3e9082', 'global.imageNamespace':'foo-dev', 'global.imageTag':'cd3e9082'], ['--install', '--atomic'], [], true)
}
@Unroll
def "fails when rollout info cannot be retrieved"() {
given:
def cfg = config + [environment: 'dev', targetProject: 'foo-dev', openshiftRolloutTimeoutRetries: 5]
IContext context = new Context(null, cfg, logger)
OpenShiftService openShiftService = Stub(OpenShiftService.class)
openShiftService.getResourcesForComponent(_, { it == ['Deployment', 'DeploymentConfig'] }, _) >> [DeploymentConfig: ['bar']]
openShiftService.resourceExists(_, { it == 'ImageStream' }, _) >> isExists
openShiftService.getImagesOfDeployment(*_) >> images
openShiftService.getRevision(*_) >> latestVersion
openShiftService.rollout(*_) >> { a, b, c, d, e ->
throw new RuntimeException('Boom!')
}
ServiceRegistry.instance.add(OpenShiftService, openShiftService)
JenkinsService jenkinsService = Stub(JenkinsService.class)
jenkinsService.maybeWithPrivateKeyCredentials(*_) >> { args -> args[1]('/tmp/file') }
ServiceRegistry.instance.add(JenkinsService, jenkinsService)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
helper.registerAllowedMethod('fileExists', [ String ]) { String args ->
false
}
script.call(context)
then:
printCallStack()
assertCallStackContains(errorMessage)
assertJobStatusFailure()
where:
dcExists | isExists | images | latestVersion || errorMessage
true | false | [[repository: 'foo-dev', name: 'baz']] | 0 || "The following ImageStream resources for DeploymentConfig 'bar' do not exist: '[foo-dev/baz]'."
true | true | [[repository: 'foo-dev', name: 'baz']] | 123 || "Boom!"
}
def "fail when triggered by orchestration and kind=Deployment"() {
given:
def c = config + [
environment: 'dev',
targetProject: 'foo-dev',
openshiftRolloutTimeoutRetries: 5,
triggeredByOrchestrationPipeline: true
]
IContext context = new Context(null, c, logger)
OpenShiftService openShiftService = Stub(OpenShiftService.class)
openShiftService.getResourcesForComponent(*_) >> [Deployment: ['bar']]
ServiceRegistry.instance.add(OpenShiftService, openShiftService)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
helper.registerAllowedMethod('fileExists', [ String ]) { String args ->
args == 'openshift'
}
script.call(context)
then:
printCallStack()
assertCallStackContains("Deployment resources cannot be used in a NON HELM orchestration pipeline.")
assertJobStatusFailure()
}
def "fail when resourceName is given"() {
given:
IContext context = new Context(null, [:], logger)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
script.call(context, [resourceName: 'foo'])
then:
printCallStack()
assertCallStackContains("The config option 'resourceName' has been removed from odsComponentStageRolloutOpenShiftDeployment")
assertJobStatusFailure()
}
def "skip when no environment given"() {
given:
def config = [environment: null, gitCommit: 'cd3e9082d7466942e1de86902bb9e663751dae8e', openshiftRolloutTimeoutRetries: 5]
def context = new Context(null, config, logger)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
script.call(context)
then:
printCallStack()
assertCallStackContains("WARN: Skipping because of empty (target) environment ...")
assertJobStatusSuccess()
}
def "fails on incorrect options"() {
given:
def config = [
environment: null,
gitBranch: 'master',
gitCommit: 'cd3e9082d7466942e1de86902bb9e663751dae8e',
openshiftRolloutTimeoutRetries: 5,
branchToEnvironmentMapping: [:]
]
def context = new Context(null, config, logger)
when:
def script = loadScript('vars/odsComponentStageRolloutOpenShiftDeployment.groovy')
script.call(context, options)
then:
def exception = thrown(wantEx)
exception.message == wantExMessage
where:
options || wantEx | wantExMessage
[branches: 'abc'] || GroovyCastException | "Cannot cast object 'abc' with class 'java.lang.String' to class 'java.util.List'"
[foobar: 'abc'] || MissingPropertyException | "No such property: foobar for class: org.ods.component.RolloutOpenShiftDeploymentOptions"
}
}