-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathBuildStatusChecksPublisherITest.java
More file actions
354 lines (302 loc) · 14.5 KB
/
Copy pathBuildStatusChecksPublisherITest.java
File metadata and controls
354 lines (302 loc) · 14.5 KB
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
package io.jenkins.plugins.checks.status;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import io.jenkins.plugins.checks.api.ChecksConclusion;
import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksStatus;
import io.jenkins.plugins.checks.util.CapturingChecksPublisher;
import io.jenkins.plugins.util.IntegrationTestWithJenkinsPerTest;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.After;
import org.junit.Test;
import org.junit.internal.Checks;
import org.jvnet.hudson.test.TestExtension;
import java.util.List;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests that the {@link BuildStatusChecksPublisher} listens to the status of a {@link Run} and publishes status
* accordingly.
*/
@SuppressWarnings("PMD.AddEmptyString")
@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")
public class BuildStatusChecksPublisherITest extends IntegrationTestWithJenkinsPerTest {
/**
* Provide a {@link io.jenkins.plugins.checks.util.CapturingChecksPublisher} to capture details.
*/
@TestExtension
public static final CapturingChecksPublisher.Factory PUBLISHER_FACTORY = new CapturingChecksPublisher.Factory();
/**
* Clean captured checks between tests.
*/
@After
public void clearChecks() {
PUBLISHER_FACTORY.getPublishedChecks().clear();
}
/**
* Provide inject an implementation of {@link AbstractStatusChecksProperties} to control the checks.
*/
@TestExtension
public static final ChecksProperties PROPERTIES = new ChecksProperties();
/**
* Tests when the implementation of {@link AbstractStatusChecksProperties} is not applicable,
* a status checks should not be published.
*/
@Test
public void shouldNotPublishStatusWhenNotApplicable() {
PROPERTIES.setApplicable(false);
buildSuccessfully(createFreeStyleProject());
assertThat(PUBLISHER_FACTORY.getPublishedChecks()).hasSize(0);
}
/**
* Tests when status checks is skipped, a status checks should not be published.
*/
@Test
public void shouldNotPublishStatusWhenSkipped() {
PROPERTIES.setApplicable(true);
PROPERTIES.setSkipped(true);
PROPERTIES.setName("Test Status");
buildSuccessfully(createFreeStyleProject());
assertThat(PUBLISHER_FACTORY.getPublishedChecks()).hasSize(0);
}
/**
* Tests when an implementation of {@link AbstractStatusChecksProperties} is applicable and not skipped,
* a status checks using the specified name should be published.
*/
@Test
public void shouldPublishStatusWithProperties() {
PROPERTIES.setApplicable(true);
PROPERTIES.setSkipped(false);
PROPERTIES.setName("Test Status");
buildSuccessfully(createFreeStyleProject());
assertThat(PUBLISHER_FACTORY.getPublishedChecks()).hasSize(3);
ChecksDetails details = PUBLISHER_FACTORY.getPublishedChecks().get(0);
assertThat(details.getName()).isPresent().get().isEqualTo("Test Status");
assertThat(details.getStatus()).isEqualTo(ChecksStatus.QUEUED);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.NONE);
details = PUBLISHER_FACTORY.getPublishedChecks().get(1);
assertThat(details.getName()).isPresent().get().isEqualTo("Test Status");
assertThat(details.getStatus()).isEqualTo(ChecksStatus.IN_PROGRESS);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.NONE);
details = PUBLISHER_FACTORY.getPublishedChecks().get(2);
assertThat(details.getName()).isPresent().get().isEqualTo("Test Status");
assertThat(details.getStatus()).isEqualTo(ChecksStatus.COMPLETED);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.SUCCESS);
}
/**
* Test checks output includes pipeline details.
*/
@Test
public void shouldPublishStageDetails() {
PROPERTIES.setApplicable(true);
PROPERTIES.setSkipped(false);
PROPERTIES.setSuppressLogs(false);
PROPERTIES.setName("Test Status");
WorkflowJob job = createPipeline();
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " stage('Simple Stage') {\n"
+ " }\n"
+ " stage('In parallel') {\n"
+ " parallel 'p1': {\n"
+ " stage('p1s1') {\n"
+ " unstable('something went wrong')\n"
+ " }\n"
+ " stage('p1s2') {\n"
+ " }\n"
+ " }, 'p2': {}\n"
+ " }\n"
+ " stage('Fails') {\n"
+ " error('a fatal error occurs')\n"
+ " }\n"
+ "}", true));
buildWithResult(job, Result.FAILURE);
List<ChecksDetails> checksDetails = PUBLISHER_FACTORY.getPublishedChecks();
assertThat(checksDetails).hasSize(9);
// Details 0, queued
ChecksDetails details = checksDetails.get(0);
assertThat(details.getStatus()).isEqualTo(ChecksStatus.QUEUED);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.NONE);
assertThat(details.getName()).isPresent().get().isEqualTo("Test Status");
assertThat(details.getOutput()).isNotPresent();
// Details 1, first stage
details = checksDetails.get(1);
assertThat(details.getStatus()).isEqualTo(ChecksStatus.IN_PROGRESS);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.NONE);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> {
assertThat(output.getTitle()).isPresent().get().isEqualTo("In progress");
assertThat(output.getSummary()).isPresent().get().satisfies(StringUtils::isBlank);
assertThat(output.getText()).isPresent().get().asString().contains("* Simple Stage *(running)*");
});
// Details 2, first stage finished, parallel started
details = checksDetails.get(2);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> {
assertThat(output.getSummary()).isPresent().get().satisfies(StringUtils::isBlank);
assertThat(output.getText()).isPresent().get().satisfies(text -> {
assertThat(output.getTitle()).isPresent().get().isEqualTo("In progress");
assertThat(text).matches(Pattern.compile(".*\\* Simple Stage \\*\\([^)]+\\)\\*.*", Pattern.DOTALL));
assertThat(text).contains(" * In parallel *(running)*");
});
});
// Details 6, p1s1 has finished and emitted unstable
details = checksDetails.get(6);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> {
assertThat(output.getTitle()).isPresent().get().isEqualTo("In parallel/p1/p1s1: warning in 'unstable' step");
assertThat(output.getSummary()).isPresent().get().asString().isEqualToIgnoringNewLines(""
+ "### `In parallel / p1 / p1s1 / Set stage result to unstable`\n"
+ "```\n"
+ "something went wrong\n"
+ "```\n"
+ "\n");
assertThat(output.getText()).isPresent().get().asString().matches(Pattern.compile(".*"
+ " \\* Simple Stage \\*\\([^)]+\\)\\*\n"
+ " \\* In parallel \\*\\(running\\)\\*\n"
+ " \\* p1 \\*\\(running\\)\\*\n"
+ " \\* p1s1 \\*\\([^)]+\\)\\*\n"
+ " \\*\\*Unstable\\*\\*: \\*something went wrong\\*\n"
+ " \\* p1s2 \\*\\(running\\)\\*\n"
+ " \\* p2 \\*\\([^)]+\\)\\*\n.*", Pattern.DOTALL));
});
// Details 8, final checks
details = checksDetails.get(8);
assertThat(details.getStatus()).isEqualTo(ChecksStatus.COMPLETED);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.FAILURE);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> {
assertThat(output.getTitle()).isPresent().get().isEqualTo("Fails: a fatal error occurs");
assertThat(output.getSummary()).isPresent().get().asString().matches(Pattern.compile(".*"
+ "### `In parallel / p1 / p1s1 / Set stage result to unstable`\\s+"
+ "```\\s+"
+ "something went wrong\\s+"
+ "```\\s+"
+ "### `Fails / Error signal`\\s+"
+ "```\\s+"
+ "a fatal error occurs\\s+"
+ "```\\s+", Pattern.DOTALL));
assertThat(output.getText()).isPresent().asString().matches(Pattern.compile(".*"
+ " \\* Simple Stage \\*\\([^)]+\\)\\*\n"
+ " \\* In parallel \\*\\([^)]+\\)\\*\n"
+ " \\* p1 \\*\\([^)]+\\)\\*\n"
+ " \\* p1s1 \\*\\([^)]+\\)\\*\n"
+ " \\*\\*Unstable\\*\\*: \\*something went wrong\\*\n"
+ " \\* p1s2 \\*\\([^)]+\\)\\*\n"
+ " \\* p2 \\*\\([^)]+\\)\\*\n"
+ " \\* Fails \\*\\([^)]+\\)\\*\n"
+ " \\*\\*Error\\*\\*: \\*a fatal error occurs\\*\n.*",
Pattern.DOTALL));
});
}
/**
* Test checks output includes pipeline details, but not logs, when requested.
*/
@Test
public void shouldPublishStageDetailsWithoutLogsIfRequested() {
PROPERTIES.setApplicable(true);
PROPERTIES.setSkipped(false);
PROPERTIES.setName("Test Status");
PROPERTIES.setSuppressLogs(true);
WorkflowJob job = createPipeline();
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " stage('Simple Stage') {\n"
+ " }\n"
+ " stage('In parallel') {\n"
+ " parallel 'p1': {\n"
+ " stage('p1s1') {\n"
+ " unstable('something went wrong')\n"
+ " }\n"
+ " stage('p1s2') {\n"
+ " }\n"
+ " }, 'p2': {}\n"
+ " }\n"
+ " stage('Fails') {\n"
+ " archiveArtifacts artifacts: 'oh dear', fingerprint: true\n"
+ " }\n"
+ "}", true));
buildWithResult(job, Result.FAILURE);
List<ChecksDetails> checksDetails = PUBLISHER_FACTORY.getPublishedChecks();
assertThat(checksDetails).hasSize(9);
ChecksDetails details = checksDetails.get(8);
assertThat(details.getStatus()).isEqualTo(ChecksStatus.COMPLETED);
assertThat(details.getConclusion()).isEqualTo(ChecksConclusion.FAILURE);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> {
assertThat(output.getTitle()).isPresent().get().isEqualTo("Fails: error in 'archiveArtifacts' step");
assertThat(output.getSummary()).isPresent().get().asString().matches(Pattern.compile(".*"
+ "### `In parallel / p1 / p1s1 / Set stage result to unstable`\\s+"
+ "```\\s+"
+ "something went wrong\\s+"
+ "```\\s+"
+ "### `Fails / Archive the artifacts`\\s+"
+ "Error in `archiveArtifacts` step\\.\\s+"
+ "```\\s+"
+ "No artifacts found that match the file pattern \"oh dear\"\\. Configuration error\\?\\s+"
+ "```\\s+", Pattern.DOTALL));
assertThat(output.getText()).isPresent().asString().matches(Pattern.compile(".*"
+ " \\* Simple Stage \\*\\([^)]+\\)\\*\n"
+ " \\* In parallel \\*\\([^)]+\\)\\*\n"
+ " \\* p1 \\*\\([^)]+\\)\\*\n"
+ " \\* p1s1 \\*\\([^)]+\\)\\*\n"
+ " \\*\\*Unstable\\*\\*: \\*something went wrong\\*\n"
+ " \\* p1s2 \\*\\([^)]+\\)\\*\n"
+ " \\* p2 \\*\\([^)]+\\)\\*\n"
+ " \\* Fails \\*\\([^)]+\\)\\*\n"
+ " \\*\\*Error\\*\\*: \\*No artifacts found that match the file pattern \"oh dear\". Configuration error\\?\\*\n.*",
Pattern.DOTALL));
});
}
/**
* Validates the a simple successful pipeline works.
*/
@Test
public void shouldPublishSimplePipeline() {
PROPERTIES.setApplicable(true);
PROPERTIES.setSkipped(false);
PROPERTIES.setName("Test Status");
WorkflowJob job = createPipeline();
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " echo 'Hello, world'"
+ "}", true));
buildWithResult(job, Result.SUCCESS);
List<ChecksDetails> checksDetails = PUBLISHER_FACTORY.getPublishedChecks();
ChecksDetails details = checksDetails.get(1);
assertThat(details.getOutput()).isPresent().get().satisfies(output -> assertThat(output.getTitle()).isPresent().get().isEqualTo("Success"));
}
static class ChecksProperties extends AbstractStatusChecksProperties {
private boolean applicable;
private boolean skipped;
private String name;
private boolean suppressLogs;
public void setApplicable(final boolean applicable) {
this.applicable = applicable;
}
public void setSkipped(final boolean skipped) {
this.skipped = skipped;
}
public void setName(final String name) {
this.name = name;
}
public void setSuppressLogs(final boolean suppressLogs) {
this.suppressLogs = suppressLogs;
}
@Override
public boolean isApplicable(final Job<?, ?> job) {
return applicable;
}
@Override
public String getName(final Job<?, ?> job) {
return name;
}
@Override
public boolean isSkipped(final Job<?, ?> job) {
return skipped;
}
@Override
public boolean isSuppressLogs(final Job<?, ?> job) {
return suppressLogs;
}
}
}