Skip to content
Open
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>4023.va_eeb_b_4e45f07</version>
<version>5054.v620b_5d2b_d5e6</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

package org.jenkinsci.plugins.workflow;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
Expand All @@ -34,11 +34,11 @@
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.jvnet.hudson.test.junit.jupiter.JenkinsSessionExtension;
import org.xml.sax.SAXException;

import org.htmlunit.Page;
Expand All @@ -48,12 +48,14 @@
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BuildQueueTasksTest {
class BuildQueueTasksTest {

@Rule public JenkinsSessionRule sessions = new JenkinsSessionRule();
@RegisterExtension
private final JenkinsSessionExtension sessions = new JenkinsSessionExtension();

@Issue("JENKINS-28649")
@Test public void queueAPI() throws Throwable {
@Test
void queueAPI() throws Throwable {
// This is implicitly testing ExecutorStepExecution$PlaceholderTask as exported bean
sessions.then(j -> {
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
Expand All @@ -66,7 +68,8 @@ public class BuildQueueTasksTest {
}

@Issue("JENKINS-28649")
@Test public void queueAPIRestartable() throws Throwable {
@Test
void queueAPIRestartable() throws Throwable {
// This is implicitly testing AfterRestartTask as exported bean
sessions.then(j -> {
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
Expand All @@ -84,15 +87,19 @@ public class BuildQueueTasksTest {
}

@Issue("JENKINS-28649")
@Test public void computerAPI() throws Throwable {
@Test
void computerAPI() throws Throwable {
// This is implicitly testing ExecutorStepExecution$PlaceholderTask$PlaceholderExecutable as exported bean
sessions.then(j -> {
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" echo 'test'\n " +
" semaphore 'watch'\n " +
"}", true));
"""
node {
echo 'test'

semaphore 'watch'

}""", true));

WorkflowRun b = p.scheduleBuild2(0).getStartCondition().get();
SemaphoreStep.waitForStart("watch/1", b);
Expand Down Expand Up @@ -128,7 +135,7 @@ private static void assertQueueAPIStatusOKAndAbort(WorkflowRun b, JenkinsRule j)

JSONObject o = JSONObject.fromObject(queue.getWebResponse().getContentAsString());
JSONArray items = o.getJSONArray("items");
// Just check that the request returns HTTP 200 and there is some content.
// Just check that the request returns HTTP 200 and there is some content.
// Not going into de the content in this test
assertEquals(1, items.size());

Expand Down
31 changes: 21 additions & 10 deletions src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,30 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

/**
* Verifies that specific environment variables are available.
*
*/
public class EnvWorkflowTest {
@WithJenkins
class EnvWorkflowTest {

@Rule public JenkinsRule r = new JenkinsRule();
private JenkinsRule r;

@BeforeEach
void setUp(JenkinsRule rule) {
r = rule;
}
/**
* Verifies if NODE_NAME environment variable is available on an agent node and on the built-in node.
*/
@Test public void isNodeNameAvailable() throws Exception {
@Test
void isNodeNameAvailable() throws Exception {
r.createSlave("node-test", "unix fast", null);
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "workflow-test");

Expand Down Expand Up @@ -109,7 +116,8 @@ private void matchLabelsInAnyOrder(WorkflowRun run, String regex, String... labe
/**
* Verifies if EXECUTOR_NUMBER environment variable is available on an agent node and on the built-in node.
*/
@Test public void isExecutorNumberAvailable() throws Exception {
@Test
void isExecutorNumberAvailable() throws Exception {
r.jenkins.setNumExecutors(1);
r.createSlave("node-test", null, null);
String builtInNodeLabel = r.jenkins.getSelfLabel().getExpression(); // compatibility with 2.307+
Expand All @@ -123,15 +131,18 @@ private void matchLabelsInAnyOrder(WorkflowRun run, String regex, String... labe
r.assertLogContains("Executor number on built-in node is 0", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));

p.setDefinition(new CpsFlowDefinition(
"node('node-test') {\n" +
" echo \"My number on an agent is ${env.EXECUTOR_NUMBER}\"\n" +
"}\n",
"""
node('node-test') {
echo "My number on an agent is ${env.EXECUTOR_NUMBER}"
}
""",
true));
r.assertLogContains("My number on an agent is 0", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}

@Issue("JENKINS-33511")
@Test public void isWorkspaceAvailable() throws Exception {
@Test
void isWorkspaceAvailable() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
String builtInNodeLabel = r.jenkins.getSelfLabel().getExpression(); // compatibility with 2.307+
p.setDefinition(new CpsFlowDefinition("node('" + builtInNodeLabel + "') {echo(/running in ${env.WORKSPACE}/)}", true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import hudson.slaves.ComputerListener;
import java.io.File;
import java.time.Duration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.slaves.restarter.JnlpSlaveRestarterInstaller;
Expand All @@ -45,40 +44,43 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.log.FileLogStorage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.Parameter;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.ValueSource;
import org.jvnet.hudson.test.InboundAgentRule;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.PrefixedOutputStream;
import org.jvnet.hudson.test.RealJenkinsRule;
import org.jvnet.hudson.test.TailLog;
import org.jvnet.hudson.test.junit.jupiter.InboundAgentExtension;
import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension;

@RunWith(Parameterized.class)
public final class DurableTaskStepTest {

@Parameterized.Parameters(name = "watching={0}") public static List<Boolean> data() {
return List.of(false, true);
}

@Parameterized.Parameter public boolean useWatching;
@ParameterizedClass(name = "watching={0}")
@ValueSource(booleans = {true, false})
class DurableTaskStepTest {

private static final Logger LOGGER = Logger.getLogger(DurableTaskStepTest.class.getName());

@Rule public RealJenkinsRule rr = new RealJenkinsRule().
@Parameter
private boolean useWatching;

@RegisterExtension
private final RealJenkinsExtension rr = new RealJenkinsExtension().
javaOptions("-Dorg.jenkinsci.plugins.workflow.support.pickles.ExecutorPickle.timeoutForNodeMillis=" + Duration.ofMinutes(5).toMillis()). // reconnection could be >15s esp. on Windows
withColor(PrefixedOutputStream.Color.BLUE).
withLogger(DurableTaskStep.class, Level.FINE).
withLogger(FileMonitoringTask.class, Level.FINE);

@Rule public InboundAgentRule inboundAgents = new InboundAgentRule();
@RegisterExtension
private final InboundAgentExtension inboundAgents = new InboundAgentExtension();

@Test public void scriptExitingAcrossRestart() throws Throwable {
@Test
void scriptExitingAcrossRestart() throws Throwable {
rr.javaOptions("-D" + DurableTaskStep.class.getName() + ".USE_WATCHING=" + useWatching);
rr.startJenkins();
rr.runRemotely(DurableTaskStepTest::disableJnlpSlaveRestarterInstaller);
inboundAgents.createAgent(rr, InboundAgentRule.Options.newBuilder().
inboundAgents.createAgent(rr, InboundAgentExtension.Options.newBuilder().
color(PrefixedOutputStream.Color.MAGENTA).
label("remote").
withLogger(FileMonitoringTask.class, Level.FINER).
Expand All @@ -103,7 +105,7 @@ public final class DurableTaskStepTest {
* Most users should be using cloud agents,
* and this lets us preserve {@link InboundAgentRule.Options.Builder#withLogger(Class, Level)}.
*/
private static void disableJnlpSlaveRestarterInstaller(JenkinsRule r) throws Throwable {
private static void disableJnlpSlaveRestarterInstaller(JenkinsRule r) {
ComputerListener.all().remove(ExtensionList.lookupSingleton(JnlpSlaveRestarterInstaller.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,28 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.durable_task.exitDuringShutdownTest.FinishProcess;
import static org.junit.Assume.assumeFalse;
import org.junit.Rule;
import org.junit.Test;
import static org.junit.jupiter.api.Assumptions.assumeFalse;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.PrefixedOutputStream;
import org.jvnet.hudson.test.RealJenkinsRule;
import org.jvnet.hudson.test.TailLog;
import org.jvnet.hudson.test.junit.jupiter.RealJenkinsExtension;

public final class ExitDuringShutdownTest {
class ExitDuringShutdownTest {

@Rule public RealJenkinsRule rr = new RealJenkinsRule().
addSyntheticPlugin(new RealJenkinsRule.SyntheticPlugin(FinishProcess.class).shortName("ExitDuringShutdownTest").header("Plugin-Dependencies", "workflow-cps:0")).
@RegisterExtension
private final RealJenkinsExtension rr = new RealJenkinsExtension().
addSyntheticPlugin(new RealJenkinsExtension.SyntheticPlugin(FinishProcess.class).shortName("ExitDuringShutdownTest").header("Plugin-Dependencies", "workflow-cps:0")).
javaOptions("-Dorg.jenkinsci.plugins.workflow.support.pickles.ExecutorPickle.timeoutForNodeMillis=" + Duration.ofMinutes(5).toMillis()). // reconnection could be >15s esp. on Windows
javaOptions("-D" + DurableTaskStep.class.getName() + ".USE_WATCHING=true").
withColor(PrefixedOutputStream.Color.BLUE).
withLogger(DurableTaskStep.class, Level.FINE).
withLogger(FileMonitoringTask.class, Level.FINE);

@Test public void scriptExitingDuringShutdown() throws Throwable {
assumeFalse("TODO Windows version TBD", Functions.isWindows());
@Test
void scriptExitingDuringShutdown() throws Throwable {
assumeFalse(Functions.isWindows(), "TODO Windows version TBD");

Check warning on line 63 in src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/ExitDuringShutdownTest.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Windows version TBD");
rr.startJenkins();
try (var tailLog = new TailLog(rr, "p", 1).withColor(PrefixedOutputStream.Color.YELLOW)) {
rr.run(r -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,63 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps.durable_task;

import org.junit.Rule;
import org.junit.Test;
import org.junit.Before;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assume;
import org.jvnet.hudson.test.JenkinsRule;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import hudson.Functions;

import java.nio.charset.StandardCharsets;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public class PowerShellStepTest {
@WithJenkins
class PowerShellStepTest {

@Rule public JenkinsRule j = new JenkinsRule();
private JenkinsRule j;

// Ensure that powershell exists and is at least version 3
@Before public void ensurePowerShellSufficientVersion() throws Exception {
@BeforeEach
void setUp(JenkinsRule rule) throws Exception {
j = rule;
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "pretest");
p.setDefinition(new CpsFlowDefinition(Functions.isWindows() ?
"node { bat('powershell.exe -ExecutionPolicy Bypass -NonInteractive -Command \"if ($PSVersionTable.PSVersion.Major -ge 3) {exit 0} else {exit 1}\"') }" :
"node { sh('powershell -NonInteractive -Command \"if ($PSVersionTable.PSVersion.Major -ge 3) {exit 0} else {exit 1}\"') }", true));
WorkflowRun b = p.scheduleBuild2(0).get();
Result r = b.getResult();
Assume.assumeTrue("This test should only run if the pretest workflow job succeeded", r == Result.SUCCESS);
assumeTrue(r == Result.SUCCESS, "This test should only run if the pretest workflow job succeeded");
}

// Test that a powershell step producing particular output indeed has a log containing that output
@Test public void testOutput() throws Exception {
@Test
void testOutput() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "foo");
p.setDefinition(new CpsFlowDefinition("node {powershell 'Write-Output \"a moon full of stars and astral cars\"'}", true));
j.assertLogContains("a moon full of stars and astral cars", j.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}

// Test that a powershell step that fails indeed causes the underlying build to fail
@Test public void testFailure() throws Exception {
@Test
void testFailure() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "bar");
p.setDefinition(new CpsFlowDefinition("node {powershell 'throw \"bogus error\"'}", true));
j.assertLogContains("bogus error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0)));
}

@Test public void testUnicode() throws Exception {

@Test
void testUnicode() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "foobar");
p.setDefinition(new CpsFlowDefinition("node {def x = powershell(returnStdout: true, script: 'write-output \"Hëllö Wórld\"'); println x.replace(\"\ufeff\",\"\")}", true));
String log = new String(JenkinsRule.getLog(j.assertBuildStatusSuccess(p.scheduleBuild2(0))).getBytes(), StandardCharsets.UTF_8);
Assume.assumeTrue("Correct UTF-8 output should be produced",log.contains("Hëllö Wórld"));
assumeTrue(log.contains("Hëllö Wórld"),"Correct UTF-8 output should be produced");
}

}
Expand Down
Loading
Loading