-
Notifications
You must be signed in to change notification settings - Fork 25
/
publishIntegTestResults.groovy
473 lines (457 loc) · 26 KB
/
publishIntegTestResults.groovy
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/** Library to fetch the failing Integration test details at the end of Integration Test Jenkins build and index the results to OpenSearch Metrics cluster.
*
* @param Map args = [:] args A map of the following parameters.
* @param args.distributionBuildUrl <required> - The jenkins distribution build number.
* @param args.testReportManifestYml <required> - The generated test report YAML file using test report workflow.
* @param args.jobName <required> - The integ test job name, used in `testReportManifestYmlUrl`.
*/
import groovy.json.JsonOutput
import java.text.SimpleDateFormat
import java.util.Date
void call(Map args = [:]) {
// To ensure the test TestOpenSearchIntegTest from opensearch-build repo passes.
def isNullOrEmpty = { str ->
str == null || (str instanceof String && str.trim().isEmpty())
}
// Check if any args is equals to null or it is a test run
if (isNullOrEmpty(args.distributionBuildUrl) || isNullOrEmpty(args.testReportManifestYml) || isNullOrEmpty(args.jobName) || args.jobName.equals('dummy_job')) {
return null
}
def integTestBuildNumber = currentBuild.number
def integTestBuildUrl = env.RUN_DISPLAY_URL
def distributionBuildUrl = args.distributionBuildUrl
def buildStartTime = currentBuild.startTimeInMillis
def currentDate = new Date()
def formattedDate = new SimpleDateFormat("MM-yyyy").format(currentDate)
def testReportManifestYml = args.testReportManifestYml
def jobName = args.jobName
def manifestFile = readFile testReportManifestYml
def manifest = readYaml text: manifestFile
def indexName = "opensearch-integration-test-results-${formattedDate}"
def testFailuresindexName = "opensearch-integration-test-failures-${formattedDate}"
def finalJsonDoc = ""
def version = manifest.version.toString()
def distributionBuildNumber = manifest.id
def rcNumber = manifest.rc.toInteger()
def rc = (rcNumber > 0)
def platform = manifest.platform
def architecture = manifest.architecture
def distribution = manifest.distribution
def testReportManifestYmlUrl = "https://ci.opensearch.org/ci/dbc/${jobName}/${version}/${distributionBuildNumber}/${platform}/${architecture}/${distribution}/test-results/${integTestBuildNumber}/integ-test/test-report.yml"
manifest.components.each { component ->
def componentName = component.name
def componentRepo = component.repository.split('/')[-1].replace('.git', '')
def componentRepoUrl = component.repository.substring(component.repository.indexOf("github.com")).replace(".git", "")
def componentCategory = manifest.name
def withSecurity = component.configs.find { it.name == 'with-security' }?.status?.toLowerCase() ?: 'unknown'
def withoutSecurity = component.configs.find { it.name == 'without-security' }?.status?.toLowerCase() ?: 'unknown'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'
def withSecurityYml = component.configs.find { it.name == 'with-security' }?.yml ?: ''
def withSecurityClusterStdout = component.configs.find { it.name == 'with-security' }?.cluster_stdout ?: []
def withSecurityClusterStderr = component.configs.find { it.name == 'with-security' }?.cluster_stderr ?: []
def withSecurityTestStdout = component.configs.find { it.name == 'with-security' }?.test_stdout ?: ''
def withSecurityTestStderr = component.configs.find { it.name == 'with-security' }?.test_stderr ?: ''
def withoutSecurityYml = component.configs.find { it.name == 'without-security' }?.yml ?: ''
def withoutSecurityClusterStdout = component.configs.find { it.name == 'without-security' }?.cluster_stdout ?: []
def withoutSecurityClusterStderr = component.configs.find { it.name == 'without-security' }?.cluster_stderr ?: []
def withoutSecurityTestStdout = component.configs.find { it.name == 'without-security' }?.test_stdout ?: ''
def withoutSecurityTestStderr = component.configs.find { it.name == 'without-security' }?.test_stderr ?: ''
def withSecurityFailedTests = component.configs.find { it.name == 'with-security' }?.failed_test ?: []
processFailedTests(withSecurityFailedTests, componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber,
platform, architecture, distribution, componentCategory, "with-security", testFailuresindexName)
def withoutSecurityFailedTests = component.configs.find { it.name == 'without-security' }?.failed_test ?: []
processFailedTests(withoutSecurityFailedTests, componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber,
platform, architecture, distribution, componentCategory, "without-security", testFailuresindexName)
def jsonContent = generateJson(
componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl,
buildStartTime, rc, rcNumber,
platform, architecture, distribution,
componentCategory, componentResult, testReportManifestYmlUrl,
withSecurity, withSecurityYml, withSecurityClusterStdout,
withSecurityClusterStderr, withSecurityTestStdout, withSecurityTestStderr,
withoutSecurity, withoutSecurityYml, withoutSecurityClusterStdout, withoutSecurityClusterStderr,
withoutSecurityTestStdout, withoutSecurityTestStderr
)
finalJsonDoc += "{\"index\": {\"_index\": \"${indexName}\"}}\n" + "${jsonContent}\n"
}
writeFile file: "test-records.json", text: finalJsonDoc
def fileContents = readFile(file: "test-records.json").trim()
indexFailedTestData(indexName, "test-records.json")
}
def processFailedTests(failedTests, componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime,
rc, rcNumber, platform, architecture, distribution, componentCategory, securityType, testFailuresindexName) {
def finalFailedTestsJsonDoc = ""
switch (true) {
case failedTests.isEmpty():
break
case failedTests.contains("Test Result Not Available"):
def testResultJsonContent = generateFailedTestJson(componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber,
platform, architecture, distribution, componentCategory, securityType, "Result Not Available", "Result Not Available")
finalFailedTestsJsonDoc += "{\"index\": {\"_index\": \"${testFailuresindexName}\"}}\n${testResultJsonContent}\n"
break
case failedTests.contains("Test Result Files List Not Available"):
def testResultJsonContent = generateFailedTestJson(componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber,
platform, architecture, distribution, componentCategory, securityType, "Report Not Available", "Report Not Available")
finalFailedTestsJsonDoc += "{\"index\": {\"_index\": \"${testFailuresindexName}\"}}\n${testResultJsonContent}\n"
break
case failedTests.contains("No Failed Test"):
break
default:
failedTests.collect { failedTest ->
def match = failedTest.split("#")
if (match) {
def testResultJsonContent = generateFailedTestJson(componentName, componentRepo, componentRepoUrl, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber,
platform, architecture, distribution, componentCategory, securityType, match[0].trim(), match[1].trim())
finalFailedTestsJsonDoc += "{\"index\": {\"_index\": \"${testFailuresindexName}\"}}\n${testResultJsonContent}\n"
}
}
break
}
if (!finalFailedTestsJsonDoc.isEmpty()) {
writeFile file: "test-failures.json", text: finalFailedTestsJsonDoc
def fileContents = readFile(file: "test-failures.json").trim()
indexTestFailuresData(testFailuresindexName, "test-failures.json")
}
return finalFailedTestsJsonDoc
}
boolean argCheck(String str) { return (str == null || str.allWhitespace || str.isEmpty()) }
void indexTestFailuresData(testFailuresindexName, testFailuresFile) {
withCredentials([
string(credentialsId: 'jenkins-health-metrics-account-number', variable: 'METRICS_HOST_ACCOUNT'),
string(credentialsId: 'jenkins-health-metrics-cluster-endpoint', variable: 'METRICS_HOST_URL')
]) {
withAWS(role: 'OpenSearchJenkinsAccessRole', roleAccount: "${METRICS_HOST_ACCOUNT}", duration: 900, roleSessionName: 'jenkins-session') {
def awsAccessKey = env.AWS_ACCESS_KEY_ID
def awsSecretKey = env.AWS_SECRET_ACCESS_KEY
def awsSessionToken = env.AWS_SESSION_TOKEN
sh """
set +e
set +x
echo "INDEX NAME IS ${testFailuresindexName}"
INDEX_MAPPING='{
"mappings": {
"properties": {
"component": {
"type": "keyword"
},
"component_repo": {
"type": "keyword"
},
"component_repo_url": {
"type": "keyword"
},
"version": {
"type": "keyword"
},
"integ_test_build_number": {
"type": "integer"
},
"integ_test_build_url": {
"type": "keyword"
},
"distribution_build_number": {
"type": "integer"
},
"distribution_build_url": {
"type": "keyword"
},
"build_start_time": {
"type": "date",
"format": "epoch_millis"
},
"rc": {
"type": "keyword"
},
"rc_number": {
"type": "integer"
},
"platform": {
"type": "keyword"
},
"architecture": {
"type": "keyword"
},
"distribution": {
"type": "keyword"
},
"component_category": {
"type": "keyword"
},
"test_type": {
"type": "keyword"
},
"test_class": {
"type": "keyword"
},
"test_name": {
"type": "keyword"
}
}
}
}'
curl -I "${METRICS_HOST_URL}/${testFailuresindexName}" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" | grep -E "HTTP\\/[0-9]+(\\.[0-9]+)? 200"
if [ \$? -eq 0 ]; then
echo "Index already exists. Indexing Results"
else
echo "Index does not exist. Creating..."
create_index_response=\$(curl -s -XPUT "${METRICS_HOST_URL}/${testFailuresindexName}" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" -H 'Content-Type: application/json' -d "\${INDEX_MAPPING}")
if [[ \$create_index_response == *'"acknowledged":true'* ]]; then
echo "Index created successfully."
echo "Updating alias..."
update_alias_response=\$(curl -s -XPOST "${METRICS_HOST_URL}/_aliases" --aws-sigv4 "aws:amz:us-east-1:es" --user "${awsAccessKey}:${awsSecretKey}" -H "x-amz-security-token:${awsSessionToken}" -H "Content-Type: application/json" -d '{
"actions": [
{
"add": {
"index": "${testFailuresindexName}",
"alias": "opensearch-integration-test-failures"
}
}
]
}')
if [[ \$update_alias_response == *'"acknowledged":true'* ]]; then
echo "Alias updated successfully."
else
echo "Failed to update alias. Error message: \$update_alias_response"
fi
else
echo "Failed to create index. Error message: \$create_index_response"
exit 1
fi
fi
if [ -s ${testFailuresFile} ]; then
echo "File Exists, indexing failed tests."
curl -XPOST "${METRICS_HOST_URL}/$testFailuresindexName/_bulk" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" -H "Content-Type: application/x-ndjson" --data-binary "@${testFailuresFile}"
else
echo "File Does not exist. No tests records to process."
fi
"""
}
}
}
void indexFailedTestData(indexName, testRecordsFile) {
withCredentials([
string(credentialsId: 'jenkins-health-metrics-account-number', variable: 'METRICS_HOST_ACCOUNT'),
string(credentialsId: 'jenkins-health-metrics-cluster-endpoint', variable: 'METRICS_HOST_URL')
]) {
withAWS(role: 'OpenSearchJenkinsAccessRole', roleAccount: "${METRICS_HOST_ACCOUNT}", duration: 900, roleSessionName: 'jenkins-session') {
def awsAccessKey = env.AWS_ACCESS_KEY_ID
def awsSecretKey = env.AWS_SECRET_ACCESS_KEY
def awsSessionToken = env.AWS_SESSION_TOKEN
sh """
set +e
set +x
echo "INDEX NAME IS ${indexName}"
INDEX_MAPPING='{
"mappings": {
"properties": {
"component": {
"type": "keyword"
},
"component_repo": {
"type": "keyword"
},
"component_repo_url": {
"type": "keyword"
},
"version": {
"type": "keyword"
},
"integ_test_build_number": {
"type": "integer"
},
"integ_test_build_url": {
"type": "keyword"
},
"distribution_build_number": {
"type": "integer"
},
"distribution_build_url": {
"type": "keyword"
},
"build_start_time": {
"type": "date",
"format": "epoch_millis"
},
"rc": {
"type": "keyword"
},
"rc_number": {
"type": "integer"
},
"platform": {
"type": "keyword"
},
"architecture": {
"type": "keyword"
},
"distribution": {
"type": "keyword"
},
"component_category": {
"type": "keyword"
},
"component_build_result": {
"type": "keyword"
},
"test_report_manifest_yml": {
"type": "keyword"
},
"with_security": {
"type": "keyword"
},
"with_security_build_yml": {
"type": "keyword"
},
"with_security_cluster_stdout": {
"type": "keyword"
},
"with_security_test_stdout": {
"type": "keyword"
},
"with_security_cluster_stderr": {
"type": "keyword"
},
"with_security_test_stderr": {
"type": "keyword"
},
"without_security": {
"type": "keyword"
},
"without_security_build_yml": {
"type": "keyword"
},
"without_security_cluster_stdout": {
"type": "keyword"
},
"without_security_test_stdout": {
"type": "keyword"
},
"without_security_cluster_stderr": {
"type": "keyword"
},
"without_security_test_stderr": {
"type": "keyword"
}
}
}
}'
curl -I "${METRICS_HOST_URL}/${indexName}" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" | grep -E "HTTP\\/[0-9]+(\\.[0-9]+)? 200"
if [ \$? -eq 0 ]; then
echo "Index already exists. Indexing Results"
else
echo "Index does not exist. Creating..."
create_index_response=\$(curl -s -XPUT "${METRICS_HOST_URL}/${indexName}" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" -H 'Content-Type: application/json' -d "\${INDEX_MAPPING}")
if [[ \$create_index_response == *'"acknowledged":true'* ]]; then
echo "Index created successfully."
echo "Updating alias..."
update_alias_response=\$(curl -s -XPOST "${METRICS_HOST_URL}/_aliases" --aws-sigv4 "aws:amz:us-east-1:es" --user "${awsAccessKey}:${awsSecretKey}" -H "x-amz-security-token:${awsSessionToken}" -H "Content-Type: application/json" -d '{
"actions": [
{
"add": {
"index": "${indexName}",
"alias": "opensearch-integration-test-results"
}
}
]
}')
if [[ \$update_alias_response == *'"acknowledged":true'* ]]; then
echo "Alias updated successfully."
else
echo "Failed to update alias. Error message: \$update_alias_response"
fi
else
echo "Failed to create index. Error message: \$create_index_response"
exit 1
fi
fi
if [ -s ${testRecordsFile} ]; then
echo "File Exists, indexing results."
curl -XPOST "${METRICS_HOST_URL}/$indexName/_bulk" --aws-sigv4 \"aws:amz:us-east-1:es\" --user \"${awsAccessKey}:${awsSecretKey}\" -H \"x-amz-security-token:${awsSessionToken}\" -H "Content-Type: application/x-ndjson" --data-binary "@${testRecordsFile}"
else
echo "File Does not exist. No tests records to process."
fi
"""
}
}
}
def generateFailedTestJson(componentName, componentRepo, componentRepoUrl, version,
integTestBuildNumber, integTestBuildUrl, distributionBuildNumber, distributionBuildUrl,
buildStartTime, rc, rcNumber, platform, architecture, distribution, componentCategory,
testType, testClass, testName) {
def json = [
component: componentName,
component_repo: componentRepo,
component_repo_url: componentRepoUrl,
version: version,
integ_test_build_number: integTestBuildNumber,
integ_test_build_url: integTestBuildUrl,
distribution_build_number: distributionBuildNumber,
distribution_build_url: distributionBuildUrl,
build_start_time: buildStartTime,
rc: rc,
rc_number: rcNumber,
platform: platform,
architecture: architecture,
distribution: distribution,
component_category: componentCategory,
test_type: testType,
test_class: testClass,
test_name: testName
]
return JsonOutput.toJson(json)
}
def generateJson(componentName, componentRepo, componentRepoUrl, version,
integTestBuildNumber, integTestBuildUrl, distributionBuildNumber, distributionBuildUrl,
buildStartTime, rc, rcNumber, platform, architecture, distribution, componentCategory,
componentResult, testReportManifestYmlUrl, withSecurity, withSecurityYml, withSecurityClusterStdout,
withSecurityClusterStderr, withSecurityTestStdout,withSecurityTestStderr, withoutSecurity,
withoutSecurityYml, withoutSecurityClusterStdout, withoutSecurityClusterStderr, withoutSecurityTestStdout,
withoutSecurityTestStderr) {
def json = [
component: componentName,
component_repo: componentRepo,
component_repo_url: componentRepoUrl,
version: version,
integ_test_build_number: integTestBuildNumber,
integ_test_build_url: integTestBuildUrl,
distribution_build_number: distributionBuildNumber,
distribution_build_url: distributionBuildUrl,
build_start_time: buildStartTime,
rc: rc,
rc_number: rcNumber,
platform: platform,
architecture: architecture,
distribution: distribution,
component_category: componentCategory,
component_build_result: componentResult,
test_report_manifest_yml: testReportManifestYmlUrl,
with_security: withSecurity,
with_security_build_yml: withSecurityYml,
with_security_cluster_stdout: withSecurityClusterStdout,
with_security_cluster_stderr: withSecurityClusterStderr,
with_security_test_stdout: withSecurityTestStdout,
with_security_test_stderr: withSecurityTestStderr,
without_security: withoutSecurity,
without_security_build_yml: withoutSecurityYml,
without_security_cluster_stdout: withoutSecurityClusterStdout,
without_security_cluster_stderr: withoutSecurityClusterStderr,
without_security_test_stdout: withoutSecurityTestStdout,
without_security_test_stderr: withoutSecurityTestStderr
]
return JsonOutput.toJson(json)
}