forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestComponentBuildStatus.groovy
176 lines (167 loc) · 7.42 KB
/
TestComponentBuildStatus.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
/*
* 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 jenkins.ComponentBuildStatus
import org.junit.Before
import org.junit.Test
import groovy.json.JsonOutput
class TestComponentBuildStatus {
private ComponentBuildStatus componentBuildStatus
private final String metricsUrl = 'http://example.com'
private final String awsAccessKey = 'testAccessKey'
private final String awsSecretKey = 'testSecretKey'
private final String awsSessionToken = 'testSessionToken'
private final String indexName = 'opensearch-distribution-build-results-*'
private final String product = "OpenSearch"
private final String version = "2.18.0"
private final String qualifier = "None"
private final String distributionBuildNumber = "4891"
private final String buildStartTimeFrom = "now-6h"
private final String buildStartTimeTo = "now"
private def script
@Before
void setUp() {
script = new Expando()
script.sh = { Map args ->
if (args.containsKey("script")) {
return """
{
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "opensearch-distribution-build-results-09-2024",
"_id": "QTVbQZIBOi-lzDIlekCk",
"_score": 0,
"_source": {
"component": "performance-analyzer"
}
},
{
"_index": "opensearch-distribution-build-results-09-2024",
"_id": "PzVbQZIBOi-lzDIlekCk",
"_score": 0,
"_source": {
"component": "security-analytics"
}
}
]
}
}
"""
}
return ""
}
componentBuildStatus = new ComponentBuildStatus(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, product, version, qualifier, distributionBuildNumber, buildStartTimeFrom, buildStartTimeTo, script)
}
@Test
void testGetQueryReturnsExpectedQuery() {
def expectedOutput = JsonOutput.toJson([
_source: [
"component",
],
query: [
bool: [
filter: [
[
match_phrase: [
component_category: "OpenSearch"
]
],
[
match_phrase: [
component_build_result: "failed"
]
],
[
match_phrase: [
version: "2.18.0"
]
],
[
match_phrase : [
distribution_build_number : "4891"
]
],
[
range: [
build_start_time: [
from: "now-6h",
to: "now"
]
]
]
]
]
]
]).replace('"', '\\"')
def result = componentBuildStatus.getQuery('failed')
assert result == expectedOutput
}
@Test
void testGetQueryReturnsExpectedQueryWithQualifier() {
def expectedOutput = JsonOutput.toJson([
_source: [
"component",
],
query: [
bool: [
filter: [
[
match_phrase: [
component_category: "OpenSearch"
]
],
[
match_phrase: [
component_build_result: "failed"
]
],
[
match_phrase: [
version: "2.18.0"
]
],
[
match_phrase : [
distribution_build_number : "4891"
]
],
[
range: [
build_start_time: [
from: "now-6h",
to: "now"
]
]
],
[
match_phrase : [
qualifier : "alpha1"
]
]
]
]
]
]).replace('"', '\\"')
def componentBuildStatusNew = new ComponentBuildStatus(metricsUrl, awsAccessKey, awsSecretKey, awsSessionToken, indexName, product, version, 'alpha1', distributionBuildNumber, buildStartTimeFrom, buildStartTimeTo, script)
def result = componentBuildStatusNew.getQuery('failed')
assert result == expectedOutput
}
@Test
void testComponentBuildStatusReturns() {
def expectedOutput = ['performance-analyzer', 'security-analytics']
def result = componentBuildStatus.getComponents('failed')
assert result == expectedOutput
}
}