forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestPublishIntegTestResults.groovy
288 lines (256 loc) · 10.9 KB
/
TestPublishIntegTestResults.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
/*
* 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.
*/
package jenkins.tests
import org.junit.Before
import static org.mockito.Mockito.*
import groovy.json.JsonSlurper
import org.junit.Test
class TestPublishIntegTestResults extends BuildPipelineTest {
@Override
@Before
void setUp() {
super.setUp()
binding.setVariable('currentBuild', [
number: 123,
startTimeInMillis: System.currentTimeMillis(),
])
binding.setVariable('env', [
RUN_DISPLAY_URL: 'http://example.com/build/123'
])
binding.setVariable('sh', { cmd -> println cmd })
binding.setVariable('readFile', { filePath -> 'components:\n- name: component1\n configs:\n - name: with-security\n status: pass\n - name: without-security\n status: fail' })
binding.setVariable('writeFile', { params -> println params.text })
binding.setVariable('withCredentials', { creds, closure -> closure() })
helper.registerAllowedMethod("withAWS", [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
binding.setVariable('curl', { params -> println params })
}
@Test
void testIndexFailedTestData() {
def indexName = 'test-index'
def testRecordsFile = 'test-records.ndjson'
def script = loadScript('vars/publishIntegTestResults.groovy')
def calledCommands = new ArrayList()
script.metaClass.sh = { String command ->
calledCommands << command
if (command.contains("curl -I")) {
return "HTTP/1.1 200 OK"
} else if (command.contains("curl -s -XPUT") && command.contains("test-index")) {
return '{"acknowledged":true}'
} else if (command.contains("curl -XPOST") && command.contains("test-index")) {
return '{"took":10, "errors":false}'
} else {
throw new IllegalArgumentException("Unexpected command: $command")
}
}
script.indexFailedTestData(indexName, testRecordsFile)
def expectedCommandBlock = '''set +e
set +x
echo "INDEX NAME IS test-index"
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/test-index" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" | 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/test-index" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" -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 test-records.ndjson ]; then
echo "File Exists, indexing results."
curl -XPOST "METRICS_HOST_URL/test-index/_bulk" --aws-sigv4 "aws:amz:us-east-1:es" --user "null:null" -H "x-amz-security-token:null" -H "Content-Type: application/x-ndjson" --data-binary "@test-records.ndjson"
else
echo "File Does not exist. No tests records to process."
fi'''
assert calledCommands.size() == 1
assert normalizeString(calledCommands[0]) == normalizeString(expectedCommandBlock)
}
@Test
void testGenerateJson() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def result = script.generateJson(
'component1', '1.0', 123,
'http://example.com/build/123', 456, 'http://example.com/distribution/456',
System.currentTimeMillis(), 'rc1', 1, 'linux', 'x64', 'tar', 'test-category',
'failed', 'http://example.com/test-report.yml', 'pass', 'yml1', ['stdout1'], ['stderr1'],
'fail', 'yml2', ['stdout2'], ['stderr2']
)
def parsedResult = new JsonSlurper().parseText(result)
def expectedJson = [
component: 'component1',
version: '1.0',
integ_test_build_number: 123,
integ_test_build_url: 'http://example.com/build/123',
distribution_build_number: 456,
distribution_build_url: 'http://example.com/distribution/456',
// Ignore build_start_time for comparison
rc: 'rc1',
rc_number: 1,
platform: 'linux',
architecture: 'x64',
distribution: 'tar',
component_category: 'test-category',
component_build_result: 'failed',
test_report_manifest_yml: 'http://example.com/test-report.yml',
with_security: 'pass',
with_security_build_yml: 'yml1',
with_security_cluster_stdout: ['stdout1'],
with_security_cluster_stderr: ['stderr1'],
without_security: 'fail',
without_security_build_yml: 'yml2',
without_security_cluster_stdout: ['stdout2'],
without_security_cluster_stderr: ['stderr2']
]
// Remove the dynamic field for comparison
parsedResult.remove('build_start_time')
assert parsedResult == expectedJson
}
@Test
void testComponentResultWithSecurityFail() {
def withSecurity = 'fail'
def withoutSecurity = 'pass'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'
assert componentResult == 'failed'
}
@Test
void testComponentResultWithoutSecurityFail() {
def withSecurity = 'pass'
def withoutSecurity = 'fail'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'
assert componentResult == 'failed'
}
@Test
void testComponentResultWithSecurityNotAvailable() {
def withSecurity = 'Not Available'
def withoutSecurity = 'pass'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'
assert componentResult == 'failed'
}
@Test
void testComponentResultWithoutSecurityNotAvailable() {
def withSecurity = 'pass'
def withoutSecurity = 'Not Available'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'
assert componentResult == 'failed'
}
@Test
void testComponentResultBothPass() {
def withSecurity = 'pass'
def withoutSecurity = 'pass'
def componentResult = (withSecurity == 'fail' || withoutSecurity == 'fail' || withSecurity == 'Not Available' || withoutSecurity == 'Not Available') ? 'failed' : 'passed'
assert componentResult == 'passed'
}
@Test
void testCallWithMissingArgs() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def args = [
distributionBuildUrl: "http://example.com/distribution/456",
testReportManifestYml: "path/to/testReportManifest.yml",
//jobName: "test-job" // Missing required argument
]
def result = script.call(args)
assert result == null
}
@Test
void testCallWithEmptyArgs() {
def script = loadScript('vars/publishIntegTestResults.groovy')
def args = [
distributionBuildUrl: "http://example.com/distribution/456",
testReportManifestYml: "path/to/testReportManifest.yml",
jobName: "" // Empty required argument
]
def result = script.call(args)
assert result == null
}
def normalizeString(String str) {
return str.replaceAll(/\s+/, " ").trim()
}
}