-
-
Notifications
You must be signed in to change notification settings - Fork 80
Catch errors in TaskListenerDecorator.decorate even when wrapped with merge
#290
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d16e7f
Catch errors in `TaskListenerDecorator.decorate` even when wrapped wi…
jglick d191982
Ensure that stack traces also appear in the build log https://github.…
jglick 696e4f8
Pipeline logs are always UTF-8 (https://jenkins.io/jep/206)
jglick 57bc77f
Javadoc for `decorateAll`
jglick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/test/java/org/jenkinsci/plugins/workflow/log/TaskListenerDecoratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright 2023 CloudBees, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package org.jenkinsci.plugins.workflow.log; | ||
|
|
||
| import hudson.console.LineTransformationOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Set; | ||
| import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
| import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
| import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback; | ||
| import org.jenkinsci.plugins.workflow.steps.Step; | ||
| import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
| import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
| import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.jvnet.hudson.test.BuildWatcher; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
| import org.jvnet.hudson.test.TestExtension; | ||
| import org.kohsuke.stapler.DataBoundConstructor; | ||
|
|
||
| public final class TaskListenerDecoratorTest { | ||
|
|
||
| @ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); | ||
|
|
||
| @Rule public JenkinsRule r = new JenkinsRule(); | ||
|
|
||
| @Test public void brokenMergedTaskListenerDecorator() throws Exception { | ||
| var p = r.createProject(WorkflowJob.class, "p"); | ||
| p.setDefinition(new CpsFlowDefinition("mask {broken {echo 'please mask s3cr3t'}}; broken {mask {echo 'please also mask s3cr3t'}}", true)); | ||
| var b = r.buildAndAssertSuccess(p); | ||
| r.assertLogNotContains("s3cr3t", b); | ||
| r.assertLogContains("please mask ****", b); | ||
| r.assertLogContains("please also mask ****", b); | ||
| r.assertLogContains("IllegalStateException: oops", b); | ||
| r.assertLogContains(BrokenStep.BrokenDecorator.class.getName(), b); | ||
| } | ||
|
|
||
| public static final class MaskStep extends Step { | ||
| @DataBoundConstructor public MaskStep() {} | ||
| @Override public StepExecution start(StepContext context) throws Exception { | ||
| return new Execution(context); | ||
| } | ||
| private static final class Execution extends StepExecution { | ||
| private static final long serialVersionUID = 1L; | ||
| Execution(StepContext context) { | ||
| super(context); | ||
| } | ||
| @Override public boolean start() throws Exception { | ||
| getContext().newBodyInvoker().withContext(TaskListenerDecorator.merge(getContext().get(TaskListenerDecorator.class), new MaskingDecorator())).withCallback(BodyExecutionCallback.wrap(getContext())).start(); | ||
| return false; | ||
| } | ||
| } | ||
| private static final class MaskingDecorator extends TaskListenerDecorator { | ||
| @Override public OutputStream decorate(OutputStream logger) throws IOException, InterruptedException { | ||
| return new LineTransformationOutputStream.Delegating(logger) { | ||
| @Override protected void eol(byte[] b, int len) throws IOException { | ||
| out.write(new String(b, 0, len, StandardCharsets.UTF_8).replace("s3cr3t", "****").getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| @TestExtension("brokenMergedTaskListenerDecorator") public static final class DescriptorImpl extends StepDescriptor { | ||
| @Override public Set<? extends Class<?>> getRequiredContext() { | ||
| return Set.of(); | ||
| } | ||
| @Override public String getFunctionName() { | ||
| return "mask"; | ||
| } | ||
| @Override public boolean takesImplicitBlockArgument() { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static final class BrokenStep extends Step { | ||
| @DataBoundConstructor public BrokenStep() {} | ||
| @Override public StepExecution start(StepContext context) throws Exception { | ||
| return new Execution(context); | ||
| } | ||
| private static final class Execution extends StepExecution { | ||
| private static final long serialVersionUID = 1L; | ||
| Execution(StepContext context) { | ||
| super(context); | ||
| } | ||
| @Override public boolean start() throws Exception { | ||
| getContext().newBodyInvoker().withContext(TaskListenerDecorator.merge(getContext().get(TaskListenerDecorator.class), new BrokenDecorator())).withCallback(BodyExecutionCallback.wrap(getContext())).start(); | ||
| return false; | ||
| } | ||
| } | ||
| private static final class BrokenDecorator extends TaskListenerDecorator { | ||
| @Override public OutputStream decorate(OutputStream logger) throws IOException, InterruptedException { | ||
| throw new IllegalStateException("oops"); | ||
| } | ||
| } | ||
| @TestExtension("brokenMergedTaskListenerDecorator") public static final class DescriptorImpl extends StepDescriptor { | ||
| @Override public Set<? extends Class<?>> getRequiredContext() { | ||
| return Set.of(); | ||
| } | ||
| @Override public String getFunctionName() { | ||
| return "broken"; | ||
| } | ||
| @Override public boolean takesImplicitBlockArgument() { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.