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
|
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.qualitygate;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Answers;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
import org.sonar.scanner.bootstrap.GlobalAnalysisMode;
import org.sonar.scanner.report.CeTaskReportDataHolder;
import org.sonar.scanner.scan.ScanProperties;
import org.sonarqube.ws.Ce;
import org.sonarqube.ws.Ce.TaskStatus;
import org.sonarqube.ws.Qualitygates;
import org.sonarqube.ws.Qualitygates.ProjectStatusResponse.Status;
import org.sonarqube.ws.client.HttpException;
import org.sonarqube.ws.client.MockWsResponse;
import org.sonarqube.ws.client.WsRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class QualityGateCheckTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private DefaultScannerWsClient wsClient;
@Mock
private GlobalAnalysisMode analysisMode;
@Mock
private CeTaskReportDataHolder reportMetadataHolder;
@Mock
private ScanProperties properties;
@RegisterExtension
private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
private QualityGateCheck underTest;
@BeforeEach
void before() {
underTest = new QualityGateCheck(wsClient, analysisMode, reportMetadataHolder, properties);
logTester.setLevel(Level.DEBUG);
}
@Test
void should_pass_if_quality_gate_ok() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
MockWsResponse qualityGateResponse = getQualityGateWsResponse(Status.OK);
doReturn(qualityGateResponse).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
underTest.await();
underTest.stop();
assertThat(logTester.logs(Level.INFO))
.contains(
"Waiting for the analysis report to be processed (max 5s)",
"QUALITY GATE STATUS: PASSED - View details on http://dashboard-url.com");
}
@Test
void should_wait_and_then_pass_if_quality_gate_ok() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(10);
MockWsResponse pendingTask = getCeTaskWsResponse(TaskStatus.PENDING);
MockWsResponse successTask = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(pendingTask, successTask).when(wsClient).call(newGetCeTaskRequest());
MockWsResponse qualityGateResponse = getQualityGateWsResponse(Status.OK);
doReturn(qualityGateResponse).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
underTest.await();
assertThat(logTester.logs())
.contains("QUALITY GATE STATUS: PASSED - View details on http://dashboard-url.com");
}
@Test
void should_fail_if_quality_gate_none() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
MockWsResponse qualityGateResponse = getQualityGateWsResponse(Status.ERROR);
doReturn(qualityGateResponse).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessage("QUALITY GATE STATUS: FAILED - View details on http://dashboard-url.com");
}
@Test
void should_fail_if_quality_gate_error() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
MockWsResponse qualityGateResponse = getQualityGateWsResponse(Status.ERROR);
doReturn(qualityGateResponse).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessage("QUALITY GATE STATUS: FAILED - View details on http://dashboard-url.com");
}
@Test
void should_wait_and_then_fail_if_quality_gate_error() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(10);
MockWsResponse pendingTask = getCeTaskWsResponse(TaskStatus.PENDING);
MockWsResponse successTask = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(pendingTask, successTask).when(wsClient).call(newGetCeTaskRequest());
MockWsResponse qualityGateResponse = getQualityGateWsResponse(Status.ERROR);
doReturn(qualityGateResponse).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessage("QUALITY GATE STATUS: FAILED - View details on http://dashboard-url.com");
}
@Test
void should_fail_if_quality_gate_timeout_exceeded() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(1);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.PENDING);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessage("Quality Gate check timeout exceeded - View details on http://dashboard-url.com");
}
@Test
void should_fail_if_cant_call_ws_for_quality_gate() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
doThrow(new HttpException("quality-gate-url", 400, "content")).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessage("Failed to get Quality Gate status - HTTP code 400: content");
}
@Test
void should_fail_if_invalid_response_from_quality_gate_ws() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.SUCCESS);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
MockWsResponse qualityGateResponse = new MockWsResponse();
qualityGateResponse.setRequestUrl("quality-gate-url");
qualityGateResponse.setContent("blabla");
doReturn(qualityGateResponse).when(wsClient).call(newGetQualityGateRequest());
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(IllegalStateException.class)
.hasMessage("Failed to parse response from quality-gate-url");
}
@Test
void should_fail_if_cant_call_ws_for_task() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
when(wsClient.call(newGetCeTaskRequest())).thenThrow(new HttpException("task-url", 400, "content"));
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessage("Failed to get CE Task status - HTTP code 400: content");
}
@Test
void should_fail_if_invalid_response_from_ws_task() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse getCeTaskRequest = new MockWsResponse();
getCeTaskRequest.setRequestUrl("ce-task-url");
getCeTaskRequest.setContent("blabla");
when(wsClient.call(newGetCeTaskRequest())).thenReturn(getCeTaskRequest);
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(IllegalStateException.class)
.hasMessage("Failed to parse response from ce-task-url");
}
@ParameterizedTest
@MethodSource("ceTaskNotSucceededStatuses")
void should_fail_if_task_not_succeeded(TaskStatus taskStatus) {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(5);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(taskStatus);
when(wsClient.call(newGetCeTaskRequest())).thenReturn(ceTaskWsResponse);
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(MessageException.class)
.hasMessageContaining("CE Task finished abnormally with status: " + taskStatus.name());
}
private WsRequest newGetCeTaskRequest() {
return argThat(new WsRequestPathMatcher("api/ce/task"));
}
private MockWsResponse getCeTaskWsResponse(TaskStatus status) {
MockWsResponse submitMockResponse = new MockWsResponse();
submitMockResponse.setContent(Ce.TaskResponse.newBuilder()
.setTask(Ce.Task.newBuilder().setStatus(status))
.build()
.toByteArray());
return submitMockResponse;
}
@Test
void should_skip_wait_if_disabled() {
when(properties.shouldWaitForQualityGate()).thenReturn(false);
underTest.start();
underTest.await();
assertThat(logTester.logs())
.contains("Quality Gate check disabled - skipping");
}
@Test
void should_fail_if_enabled_with_medium_test() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(analysisMode.isMediumTest()).thenReturn(true);
underTest.start();
assertThatThrownBy(() -> underTest.await())
.isInstanceOf(IllegalStateException.class);
}
private WsRequest newGetQualityGateRequest() {
return argThat(new WsRequestPathMatcher("api/qualitygates/project_status"));
}
private MockWsResponse getQualityGateWsResponse(Status status) {
MockWsResponse qualityGateWsResponse = new MockWsResponse();
qualityGateWsResponse.setContent(Qualitygates.ProjectStatusResponse.newBuilder()
.setProjectStatus(Qualitygates.ProjectStatusResponse.ProjectStatus.newBuilder()
.setStatus(status)
.build())
.build()
.toByteArray());
return qualityGateWsResponse;
}
private static Stream<TaskStatus> ceTaskNotSucceededStatuses() {
return Stream.of(
TaskStatus.CANCELED,
TaskStatus.FAILED
);
}
private static class WsRequestPathMatcher implements ArgumentMatcher<WsRequest> {
String path;
WsRequestPathMatcher(String path) {
this.path = path;
}
@Override
public boolean matches(WsRequest right) {
return path.equals(right.getPath());
}
}
}
|