From f1a54d094d61f24fc21dd440aa946dcfc11c28cd Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Thu, 24 Aug 2023 05:12:37 +0200 Subject: [PATCH] EnvWorkflowTest#isNodeNameAvailable robustness (#335) 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. --- .../plugins/workflow/EnvWorkflowTest.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java b/src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java index e6112fb9..80e0b127 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/EnvWorkflowTest.java @@ -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; @@ -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)); }