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
|
/*
* SonarQube
* Copyright (C) 2009-2020 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 com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mockito;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.log.LogTester;
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.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(DataProviderRunner.class)
public class QualityGateCheckTest {
private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS);
private GlobalAnalysisMode analysisMode = mock(GlobalAnalysisMode.class);
private CeTaskReportDataHolder reportMetadataHolder = mock(CeTaskReportDataHolder.class);
private ScanProperties properties = mock(ScanProperties.class);
@Rule
public LogTester logTester = new LogTester();
@Rule
public ExpectedException exception = ExpectedException.none();
QualityGateCheck underTest = new QualityGateCheck(wsClient, analysisMode, reportMetadataHolder, properties);
@Before
public void before() {
when(reportMetadataHolder.getCeTaskId()).thenReturn("task-1234");
when(reportMetadataHolder.getDashboardUrl()).thenReturn("http://dashboard-url.com");
}
@Test
public void should_pass_if_quality_gate_ok() {
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())
.containsOnly(
"Waiting for the analysis report to be processed (max 5s)",
"QUALITY GATE STATUS: PASSED - View details on http://dashboard-url.com");
}
@Test
public void should_wait_and_then_pass_if_quality_gate_ok() {
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
public void should_fail_if_quality_gate_none() {
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();
exception.expect(MessageException.class);
exception.expectMessage("QUALITY GATE STATUS: FAILED - View details on http://dashboard-url.com");
underTest.await();
}
@Test
public void should_fail_if_quality_gate_error() {
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();
exception.expect(MessageException.class);
exception.expectMessage("QUALITY GATE STATUS: FAILED - View details on http://dashboard-url.com");
underTest.await();
}
@Test
public void should_wait_and_then_fail_if_quality_gate_error() {
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();
exception.expect(MessageException.class);
exception.expectMessage("QUALITY GATE STATUS: FAILED - View details on http://dashboard-url.com");
underTest.await();
}
@Test
public void should_fail_if_quality_gate_timeout_exceeded() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(properties.qualityGateWaitTimeout()).thenReturn(1);
MockWsResponse ceTaskWsResponse = getCeTaskWsResponse(TaskStatus.PENDING);
doReturn(ceTaskWsResponse).when(wsClient).call(newGetCeTaskRequest());
underTest.start();
exception.expect(MessageException.class);
exception.expectMessage("Quality Gate check timeout exceeded - View details on http://dashboard-url.com");
underTest.await();
}
@Test
public 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();
exception.expect(MessageException.class);
exception.expectMessage("Failed to get Quality Gate status - HTTP code 400: content");
underTest.await();
}
@Test
public 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();
exception.expect(IllegalStateException.class);
exception.expectMessage("Failed to parse response from quality-gate-url");
underTest.await();
}
@Test
public 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();
exception.expect(MessageException.class);
exception.expectMessage("Failed to get CE Task status - HTTP code 400: content");
underTest.await();
}
@Test
public 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();
exception.expect(IllegalStateException.class);
exception.expectMessage("Failed to parse response from ce-task-url");
underTest.await();
}
@Test
@UseDataProvider("ceTaskNotSucceededStatuses")
public 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();
exception.expect(MessageException.class);
exception.expectMessage("CE Task finished abnormally with status: " + taskStatus.name());
underTest.await();
}
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
public 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
public void should_fail_if_enabled_with_medium_test() {
when(properties.shouldWaitForQualityGate()).thenReturn(true);
when(analysisMode.isMediumTest()).thenReturn(true);
underTest.start();
exception.expect(IllegalStateException.class);
underTest.await();
}
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;
}
@DataProvider
public static Object[][] ceTaskNotSucceededStatuses() {
return new Object[][] {
{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());
}
}
}
|