Skip to content

Commit

Permalink
Merge pull request #161 from garethjevans/test-checks
Browse files Browse the repository at this point in the history
fix: added tests around github checks
  • Loading branch information
garethjevans authored May 5, 2021
2 parents df5ef86 + 6adbdb1 commit b6ca375
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public String createPipelineRun(InputStream inputStream, EnvVars envVars) throws
.withName("tekton")
.withOutput(new ChecksOutput.ChecksOutputBuilder()
.withTitle(updatedPipelineRun.getMetadata().getName())
.withSummary("PipelineRun is running...")
.build())
.withStartedAt(LocalDateTime.now())
.withStatus(ChecksStatus.IN_PROGRESS)
Expand Down Expand Up @@ -382,7 +383,6 @@ public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNul
protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
URL url = null;
byte[] data = null;
//File inputFile = null;
String inputData = this.getInput();
String inputType = this.getInputType();
String createdResourceName = "";
Expand Down Expand Up @@ -421,6 +421,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
.withName("tekton")
.withOutput(new ChecksOutput.ChecksOutputBuilder()
.withTitle(createdResourceName)
.withSummary("PipelineRun completed")
.build())
.withCompletedAt(LocalDateTime.now())
.withStatus(ChecksStatus.COMPLETED)
Expand Down Expand Up @@ -449,6 +450,7 @@ protected String runCreate(Run<?, ?> run, FilePath workspace, EnvVars envVars) {
.withConclusion(ChecksConclusion.FAILURE)
.withOutput(new ChecksOutput.ChecksOutputBuilder()
.withTitle(createdResourceName)
.withSummary("PipelineRun Failed")
.withText(buffer.toString())
.build())
.withDetailsURL(DisplayURLProvider.get().getRunURL(run))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,41 @@

import io.jenkins.plugins.checks.api.ChecksDetails;
import io.jenkins.plugins.checks.api.ChecksPublisher;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class FakeChecksPublisher extends ChecksPublisher {

private int counter = 0;
private List<ChecksDetails> details = new ArrayList<>();

@Override
public void publish(ChecksDetails details) {
public void publish(ChecksDetails detail) {
details.add(detail);
counter++;
}

public int getCounter() {
return counter;
}

public void validate() {
for (ChecksDetails c: details) {
System.out.println("[FakeChecksPublisher] " + c);
assertThat(c, is(notNullValue()));
assertThat(c.getName().get(), is("tekton"));
assertThat(c.getConclusion(), is(notNullValue()));
assertThat(c.getStatus(), is(notNullValue()));

assertThat(c.getOutput(), is(notNullValue()));
assertThat(c.getOutput().get(), is(notNullValue()));
assertThat(c.getOutput().get().getTitle().get(), is(notNullValue()));
assertThat(c.getOutput().get().getSummary().get(), is(notNullValue()));
//assertThat(c, is(notNullValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;

import io.fabric8.tekton.pipeline.v1beta1.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
Expand All @@ -18,8 +20,6 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.waveywaves.jenkins.plugins.tekton.client.build.FakeChecksPublisher;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -30,11 +30,22 @@
public class CreateRawMockServerTest {

private boolean enableCatalog = false;
private String namespace;
private String namespace = "test";
private FakeChecksPublisher checksPublisher;

@Rule
public KubernetesServer server = new KubernetesServer();

@Before
public void before() {
checksPublisher = new FakeChecksPublisher();
}

@After
public void after() {
checksPublisher.validate();
}

@Test
public void testTaskCreate() {
// Given
Expand Down Expand Up @@ -227,7 +238,6 @@ public void streamPipelineRunLogsToConsole(PipelineRun pipelineRun) {
createRaw.setTektonClient(client);
createRaw.setPipelineRunClient(pipelineRunClient);

FakeChecksPublisher checksPublisher = new FakeChecksPublisher();
createRaw.setChecksPublisher(checksPublisher);

String createdPipelineName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import io.fabric8.tekton.pipeline.v1beta1.PipelineRun;
import io.fabric8.tekton.pipeline.v1beta1.PipelineRunBuilder;
import java.util.List;
import java.util.Optional;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.waveywaves.jenkins.plugins.tekton.client.TektonUtils;
import org.waveywaves.jenkins.plugins.tekton.client.build.FakeChecksPublisher;
Expand All @@ -26,6 +27,17 @@ public class CreateRawTest {

private Run<?,?> run;
private String namespace;
private FakeChecksPublisher checksPublisher;

@Before
public void before() {
checksPublisher = new FakeChecksPublisher();
}

@After
public void after() {
checksPublisher.validate();
}

@Test
public void runCreateTaskTest() {
Expand Down Expand Up @@ -79,7 +91,7 @@ public void runCreatePipelineRunTest() {
createRaw.setNamespace(namespace);
createRaw.setClusterName(TektonUtils.DEFAULT_CLIENT_KEY);
createRaw.setEnableCatalog(false);
createRaw.setChecksPublisher(new FakeChecksPublisher());
createRaw.setChecksPublisher(checksPublisher);
String created = createRaw.runCreate(run, null, null);
assert created.equals(TektonUtils.TektonResourceType.pipelinerun.toString());
}
Expand Down

0 comments on commit b6ca375

Please sign in to comment.