forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishIntegTestResults.groovy
227 lines (216 loc) · 11.7 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
/*
* 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 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 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 withSecurityStdout = component.configs.find { it.name == 'with-security' }?.cluster_stdout ?: []
def withSecurityStderr = component.configs.find { it.name == 'with-security' }?.cluster_stderr ?: []
def withoutSecurityYml = component.configs.find { it.name == 'without-security' }?.yml ?: ''
def withoutSecurityStdout = component.configs.find { it.name == 'without-security' }?.cluster_stdout ?: []
def withoutSecurityStderr = component.configs.find { it.name == 'without-security' }?.cluster_stderr ?: []
def jsonContent = generateJson(
componentName, version, integTestBuildNumber,
integTestBuildUrl, distributionBuildNumber, distributionBuildUrl,
buildStartTime, rc, rcNumber,
platform, architecture, distribution,
componentCategory, componentResult, testReportManifestYmlUrl,
withSecurity, withSecurityYml, withSecurityStdout,
withSecurityStderr, withoutSecurity, withoutSecurityYml,
withoutSecurityStdout, withoutSecurityStderr
)
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")
}
boolean argCheck(String str) { return (str == null || str.allWhitespace || str.isEmpty()) }
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"
},
"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_cluster_stderr": {
"type": "keyword"
},
"without_security": {
"type": "keyword"
},
"without_security_build_yml": {
"type": "keyword"
},
"without_security_cluster_stdout": {
"type": "keyword"
},
"without_security_cluster_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."
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 generateJson(component, version, integTestBuildNumber, integTestBuildUrl, distributionBuildNumber, distributionBuildUrl, buildStartTime, rc, rcNumber, platform, architecture, distribution, componentCategory, componentResult, testReportManifestYmlUrl, withSecurity, withSecurityYml, withSecurityStdout, withSecurityStderr, withoutSecurity, withoutSecurityYml, withoutSecurityStdout, withoutSecurityStderr) {
def json = [
component: component,
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: withSecurityStdout,
with_security_cluster_stderr: withSecurityStderr,
without_security: withoutSecurity,
without_security_build_yml: withoutSecurityYml,
without_security_cluster_stdout: withoutSecurityStdout,
without_security_cluster_stderr: withoutSecurityStderr
]
return JsonOutput.toJson(json)
}