Skip to content

Commit

Permalink
EnvWorkflowTest#isNodeNameAvailable robustness (#335)
Browse files Browse the repository at this point in the history
Despite Label.parse returning a TreeSet, Node#getAssignedLabels returns a Set and NODE_LABELS doesn't make any promise to return a sorted set.

So better match labels unordered.
  • Loading branch information
Vlatombe authored Aug 24, 2023
1 parent a96d25d commit f1a54d0
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@

package org.jenkinsci.plugins.workflow;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.is;

import hudson.slaves.DumbSlave;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand Down Expand Up @@ -62,16 +69,35 @@ public class EnvWorkflowTest {
" echo \"My name on an agent is ${env.NODE_NAME} using labels ${env.NODE_LABELS}\"\n" +
"}\n",
true));
// Label.parse returns TreeSet so the result is guaranteed to be sorted:
r.assertLogContains("My name on an agent is node-test using labels fast node-test unix", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
matchLabelsInAnyOrder(
r.assertBuildStatusSuccess(p.scheduleBuild2(0)),
"My name on an agent is node-test using labels (.*)",
"fast",
"node-test",
"unix");

p.setDefinition(new CpsFlowDefinition( // JENKINS-41446 ensure variable still available in a ws step
"node('node-test') {\n ws('workspace/foo') {" +
" echo \"My name on an agent is ${env.NODE_NAME} using labels ${env.NODE_LABELS}\"\n" +
" }\n}\n",
true));
// Label.parse returns TreeSet so the result is guaranteed to be sorted:
r.assertLogContains("My name on an agent is node-test using labels fast node-test unix", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
matchLabelsInAnyOrder(
r.assertBuildStatusSuccess(p.scheduleBuild2(0)),
"My name on an agent is node-test using labels (.*)",
"fast",
"node-test",
"unix");
}

private void matchLabelsInAnyOrder(WorkflowRun run, String regex, String... labels) throws Exception {
String runLog = JenkinsRule.getLog(run);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(runLog);
assertThat("Could not match the expected log pattern", matcher.find(), is(true));
assertThat(
"Did not find the expected labels",
StringUtils.split(matcher.group(1)),
arrayContainingInAnyOrder(labels));
}


Expand Down

0 comments on commit f1a54d0

Please sign in to comment.