Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

201 interaction kubernetes plugin #203

Merged
merged 6 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
============

* [#202](https://github.com/jenkinsci/ansicolor-plugin/pull/202): Render logs correctly for long running jobs which generate lots of output - [@tszmytka](https://github.com/tszmytka).
* [#203](https://github.com/jenkinsci/ansicolor-plugin/pull/203): Interaction with kubernetes plugin - [@tszmytka](https://github.com/tszmytka).
* Your contribution here.


Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<version>3.17.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>timestamper</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/hudson/plugins/ansicolor/AnsiColorStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -124,10 +123,17 @@ public Set<? extends Class<?>> getRequiredContext() {
private static class AnsiColorExecution extends BodyExecutionCallback {
private static final Logger LOGGER = Logger.getLogger(AnsiColorExecution.class.getName());

private static final Map<Class<?>, String[]> EXTENSIONS_NL = new HashMap<>();

private final String colorMapName;

private Boolean needsPrintln;

static {
EXTENSIONS_NL.put(DynamicContext.Typed.class, new String[]{"kubernetes.pipeline.SecretsMasker"});
EXTENSIONS_NL.put(TaskListenerDecorator.Factory.class, new String[]{"timestamper.pipeline.GlobalDecorator"});
}

public AnsiColorExecution(String colorMapName) {
this.colorMapName = colorMapName;
}
Expand Down Expand Up @@ -172,8 +178,9 @@ private void issueAction(StepContext context, ColorizedAction action) {

private boolean needsPrintln() {
if (needsPrintln == null) {
needsPrintln = ExtensionList.lookup(TaskListenerDecorator.Factory.class).stream()
.anyMatch(f -> f.getClass().getName().contains("hudson.plugins.timestamper.pipeline.GlobalDecorator"));
needsPrintln = EXTENSIONS_NL.entrySet().stream().anyMatch(e ->
ExtensionList.lookup(e.getKey()).stream().map(ext -> ext.getClass().getName()).anyMatch(n -> Arrays.stream(e.getValue()).anyMatch(n::contains))
);
}
return needsPrintln;
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/hudson/plugins/ansicolor/AnsiColorStepTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package hudson.plugins.ansicolor;

import hudson.ExtensionList;
import hudson.model.queue.QueueTaskFuture;
import hudson.plugins.ansicolor.mock.kubernetes.pipeline.SecretsMasker;
import hudson.plugins.ansicolor.mock.timestamper.pipeline.GlobalDecorator;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.log.TaskListenerDecorator;
import org.jenkinsci.plugins.workflow.steps.DynamicContext;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -12,7 +17,11 @@
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.mockito.Mockito;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.StringWriter;
import java.time.Duration;
import java.util.Arrays;
Expand Down Expand Up @@ -167,6 +176,18 @@ public void evaluate() throws Throwable {
});
}

@Test
public void willPrintAdditionalNlOnKubernetesPlugin() {
ExtensionList.lookup(DynamicContext.Typed.class).add(0, new SecretsMasker());
assertNlsOnRunningPipeline();
}

@Test
public void willPrintAdditionalNlOnTimestamperPlugin() {
ExtensionList.lookup(TaskListenerDecorator.Factory.class).add(0, new GlobalDecorator());
assertNlsOnRunningPipeline();
}

private void assertOutputOnRunningPipeline(Collection<String> expectedOutput, Collection<String> notExpectedOutput, String pipelineScript) {
story.addStep(new Statement() {

Expand All @@ -183,4 +204,27 @@ public void evaluate() throws Throwable {
}
});
}

private void assertNlsOnRunningPipeline() {
story.addStep(new Statement() {

@Override
public void evaluate() throws Throwable {
final String script = "ansiColor('xterm') {\n" +
"echo '\033[34mHello\033[0m \033[33mcolorful\033[0m \033[35mworld!\033[0m'" +
"}";
final WorkflowJob project = story.j.jenkins.createProject(WorkflowJob.class, "willPrintAdditionalNlOnKubernetesPlugin");
project.setDefinition(new CpsFlowDefinition(script, true));
story.j.assertBuildStatusSuccess(project.scheduleBuild2(0));
StringWriter writer = new StringWriter();
assertTrue(project.getLastBuild().getLogText().writeHtmlTo(0, writer) > 0);
final String html = writer.toString().replaceAll("<!--.+?-->", "")
.replaceAll("</span>", "")
.replaceAll("<span.+?>", "")
.replaceAll("<div.+?/div>", "");
final String nl = System.lineSeparator();
assertThat(html).contains("ansiColor" + nl + "[Pipeline] {" + nl + nl).contains("[Pipeline] }" + nl + nl + "[Pipeline] // ansiColor");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void assertOutputOnRunningPipeline(Collection<String> expectedOutput,

@Override
public void evaluate() throws Throwable {
final WorkflowJob project = jenkinsRule.j.jenkins.createProject(WorkflowJob.class, "p");
final WorkflowJob project = jenkinsRule.j.jenkins.createProject(WorkflowJob.class, "test-project-" + JenkinsTestSupport.this.getClass().getSimpleName());
project.setDefinition(new CpsFlowDefinition(pipelineScript, true));
jenkinsRule.j.assertBuildStatusSuccess(project.scheduleBuild2(0));
StringWriter writer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hudson.plugins.ansicolor;

import org.junit.Test;

import java.util.Arrays;

public class TimestamperIntegrationTest extends JenkinsTestSupport {

@Test
public void canTriggerFunctionalityTimestamperFirst() {
final String script = "timestamps {" +
"ansiColor('xterm') {" +
"echo '\033[34mHello\033[0m \033[33mcolorful\033[0m \033[35mworld!\033[0m'" +
"}" +
"}";
canTriggerFunctionality(script);
}


@Test
public void canTriggerFunctionalityTimestamperLast() {
final String script = "ansiColor('xterm') {" +
"timestamps {" +
"echo '\033[34mHello\033[0m \033[33mcolorful\033[0m \033[35mworld!\033[0m'" +
"}" +
"}";
canTriggerFunctionality(script);
}

private void canTriggerFunctionality(String script) {
assertOutputOnRunningPipeline(
Arrays.asList(
"<span style=\"color: #1E90FF;\">Hello</span>",
"<span style=\"color: #CDCD00;\">colorful</span>",
"<span style=\"color: #CD00CD;\">world!</span>"
),
Arrays.asList(
"\033[34mHello",
"\033[33mcolorful",
"\033[35mworld!"
),
script,
false
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hudson.plugins.ansicolor.mock.kubernetes.pipeline;

import org.jenkinsci.plugins.workflow.steps.DynamicContext;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;

public class SecretsMasker extends DynamicContext.Typed<String> {
@Nonnull
@Override
protected Class<String> type() {
return String.class;
}

@CheckForNull
@Override
protected String get(DelegatedContext context) throws IOException, InterruptedException {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hudson.plugins.ansicolor.mock.timestamper.pipeline;

import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.jenkinsci.plugins.workflow.log.TaskListenerDecorator;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public class GlobalDecorator implements TaskListenerDecorator.Factory {
@CheckForNull
@Override
public TaskListenerDecorator of(@Nonnull FlowExecutionOwner owner) {
return null;
}
}