Skip to content

Commit b6ca375

Browse files
authored
Merge pull request #161 from garethjevans/test-checks
fix: added tests around github checks
2 parents df5ef86 + 6adbdb1 commit b6ca375

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

src/main/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/CreateRaw.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public String createPipelineRun(InputStream inputStream, EnvVars envVars) throws
261261
.withName("tekton")
262262
.withOutput(new ChecksOutput.ChecksOutputBuilder()
263263
.withTitle(updatedPipelineRun.getMetadata().getName())
264+
.withSummary("PipelineRun is running...")
264265
.build())
265266
.withStartedAt(LocalDateTime.now())
266267
.withStatus(ChecksStatus.IN_PROGRESS)
@@ -382,7 +383,6 @@ public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNul
382383
protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
383384
URL url = null;
384385
byte[] data = null;
385-
//File inputFile = null;
386386
String inputData = this.getInput();
387387
String inputType = this.getInputType();
388388
String createdResourceName = "";
@@ -421,6 +421,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
421421
.withName("tekton")
422422
.withOutput(new ChecksOutput.ChecksOutputBuilder()
423423
.withTitle(createdResourceName)
424+
.withSummary("PipelineRun completed")
424425
.build())
425426
.withCompletedAt(LocalDateTime.now())
426427
.withStatus(ChecksStatus.COMPLETED)
@@ -449,6 +450,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
449450
.withConclusion(ChecksConclusion.FAILURE)
450451
.withOutput(new ChecksOutput.ChecksOutputBuilder()
451452
.withTitle(createdResourceName)
453+
.withSummary("PipelineRun Failed")
452454
.withText(buffer.toString())
453455
.build())
454456
.withDetailsURL(DisplayURLProvider.get().getRunURL(run))

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/FakeChecksPublisher.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,41 @@
22

33
import io.jenkins.plugins.checks.api.ChecksDetails;
44
import io.jenkins.plugins.checks.api.ChecksPublisher;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.hamcrest.CoreMatchers.is;
9+
import static org.hamcrest.CoreMatchers.notNullValue;
10+
import static org.hamcrest.MatcherAssert.assertThat;
511

612
public class FakeChecksPublisher extends ChecksPublisher {
713

814
private int counter = 0;
15+
private List<ChecksDetails> details = new ArrayList<>();
916

1017
@Override
11-
public void publish(ChecksDetails details) {
18+
public void publish(ChecksDetails detail) {
19+
details.add(detail);
1220
counter++;
1321
}
1422

1523
public int getCounter() {
1624
return counter;
1725
}
26+
27+
public void validate() {
28+
for (ChecksDetails c: details) {
29+
System.out.println("[FakeChecksPublisher] " + c);
30+
assertThat(c, is(notNullValue()));
31+
assertThat(c.getName().get(), is("tekton"));
32+
assertThat(c.getConclusion(), is(notNullValue()));
33+
assertThat(c.getStatus(), is(notNullValue()));
34+
35+
assertThat(c.getOutput(), is(notNullValue()));
36+
assertThat(c.getOutput().get(), is(notNullValue()));
37+
assertThat(c.getOutput().get().getTitle().get(), is(notNullValue()));
38+
assertThat(c.getOutput().get().getSummary().get(), is(notNullValue()));
39+
//assertThat(c, is(notNullValue()));
40+
}
41+
}
1842
}

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/CreateRawMockServerTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
1111

1212
import io.fabric8.tekton.pipeline.v1beta1.*;
13+
import org.junit.After;
14+
import org.junit.Before;
1315
import org.junit.Rule;
1416
import org.junit.Test;
1517
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
@@ -18,8 +20,6 @@
1820
import java.io.InputStream;
1921
import java.net.HttpURLConnection;
2022
import java.nio.charset.StandardCharsets;
21-
import java.util.ArrayList;
22-
import java.util.List;
2323
import org.waveywaves.jenkins.plugins.tekton.client.build.FakeChecksPublisher;
2424

2525
import static org.hamcrest.CoreMatchers.is;
@@ -30,11 +30,22 @@
3030
public class CreateRawMockServerTest {
3131

3232
private boolean enableCatalog = false;
33-
private String namespace;
33+
private String namespace = "test";
34+
private FakeChecksPublisher checksPublisher;
3435

3536
@Rule
3637
public KubernetesServer server = new KubernetesServer();
3738

39+
@Before
40+
public void before() {
41+
checksPublisher = new FakeChecksPublisher();
42+
}
43+
44+
@After
45+
public void after() {
46+
checksPublisher.validate();
47+
}
48+
3849
@Test
3950
public void testTaskCreate() {
4051
// Given
@@ -227,7 +238,6 @@ public void streamPipelineRunLogsToConsole(PipelineRun pipelineRun) {
227238
createRaw.setTektonClient(client);
228239
createRaw.setPipelineRunClient(pipelineRunClient);
229240

230-
FakeChecksPublisher checksPublisher = new FakeChecksPublisher();
231241
createRaw.setChecksPublisher(checksPublisher);
232242

233243
String createdPipelineName = "";

src/test/java/org/waveywaves/jenkins/plugins/tekton/client/build/create/CreateRawTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import io.fabric8.tekton.pipeline.v1beta1.PipelineRun;
88
import io.fabric8.tekton.pipeline.v1beta1.PipelineRunBuilder;
99
import java.util.List;
10-
import java.util.Optional;
10+
import org.junit.After;
11+
import org.junit.Before;
1112
import org.junit.Test;
1213
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
1314
import org.waveywaves.jenkins.plugins.tekton.client.build.FakeChecksPublisher;
@@ -26,6 +27,17 @@ public class CreateRawTest {
2627

2728
private Run<?,?> run;
2829
private String namespace;
30+
private FakeChecksPublisher checksPublisher;
31+
32+
@Before
33+
public void before() {
34+
checksPublisher = new FakeChecksPublisher();
35+
}
36+
37+
@After
38+
public void after() {
39+
checksPublisher.validate();
40+
}
2941

3042
@Test
3143
public void runCreateTaskTest() {
@@ -79,7 +91,7 @@ public void runCreatePipelineRunTest() {
7991
createRaw.setNamespace(namespace);
8092
createRaw.setClusterName(TektonUtils.DEFAULT_CLIENT_KEY);
8193
createRaw.setEnableCatalog(false);
82-
createRaw.setChecksPublisher(new FakeChecksPublisher());
94+
createRaw.setChecksPublisher(checksPublisher);
8395
String created = createRaw.runCreate(run, null, null);
8496
assert created.equals(TektonUtils.TektonResourceType.pipelinerun.toString());
8597
}

0 commit comments

Comments
 (0)