From 9ee219ff51e7661b28c65fcdd515ca3f66d9964a Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 31 Jul 2019 10:54:13 -0700 Subject: [PATCH] [JENKINS-54128] Avoid calling Run.getLogFile --- Jenkinsfile | 7 +- pom.xml | 28 ++- .../textfinder/TextFinderPublisher.java | 60 ++++-- .../hudson/plugins/textfinder/TestUtils.java | 59 ++++++ .../TextFinderPublisherAgentTest.java | 64 ++---- .../TextFinderPublisherFreestyleTest.java | 26 +-- .../TextFinderPublisherPipelineTest.java | 195 +++++++++--------- 7 files changed, 245 insertions(+), 194 deletions(-) create mode 100644 src/test/java/hudson/plugins/textfinder/TestUtils.java diff --git a/Jenkinsfile b/Jenkinsfile index 87a086d..a1fc12b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1 +1,6 @@ -buildPlugin(configurations: buildPlugin.recommendedConfigurations()) +buildPlugin(configurations: [ + [ platform: "linux", jdk: "8", jenkins: null ], + [ platform: "windows", jdk: "8", jenkins: null ], + [ platform: "linux", jdk: "8", jenkins: "2.164.1", javaLevel: "8" ], + [ platform: "windows", jdk: "8", jenkins: "2.164.1", javaLevel: "8" ] +]) diff --git a/pom.xml b/pom.xml index c4ebc51..a636a2c 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ 1.12 -SNAPSHOT - 2.60.3 + 2.121.1 8 @@ -68,31 +68,49 @@ symbol-annotation 1.3 + + org.jenkins-ci.plugins.workflow + workflow-step-api + 2.18 + + + + org.jenkins-ci.plugins.workflow + workflow-api + 2.33 + test + org.jenkins-ci.plugins.workflow workflow-basic-steps - 2.1 + 2.15 test org.jenkins-ci.plugins.workflow workflow-cps - 2.10 + 2.58 test org.jenkins-ci.plugins.workflow workflow-durable-task-step - 2.4 + 2.26 test org.jenkins-ci.plugins.workflow workflow-job - 2.4 + 2.31 + test + + + org.jenkins-ci.plugins.workflow + workflow-support + 2.21 test diff --git a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java index 3e72e88..22c5901 100644 --- a/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java +++ b/src/main/java/hudson/plugins/textfinder/TextFinderPublisher.java @@ -4,6 +4,7 @@ import hudson.Extension; import hudson.FilePath; +import hudson.Functions; import hudson.Launcher; import hudson.Util; import hudson.model.AbstractProject; @@ -21,8 +22,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; +import java.io.Reader; import java.io.Serializable; import java.nio.charset.Charset; import java.util.regex.Matcher; @@ -31,7 +34,6 @@ import javax.servlet.ServletException; import jenkins.MasterToSlaveFileCallable; import jenkins.tasks.SimpleBuildStep; -import org.apache.commons.io.IOUtils; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.FileSet; @@ -130,13 +132,7 @@ private void findText(Run run, FilePath workspace, TaskListener listener) if (alsoCheckConsoleOutput) { // Do not mention the pattern we are looking for to avoid false positives logger.println("[Text Finder] Scanning console output..."); - foundText |= - checkFile( - run.getLogFile(), - compilePattern(logger, regexp), - logger, - run.getCharset(), - true); + foundText |= checkConsole(run, compilePattern(logger, regexp), logger); logger.println( "[Text Finder] Finished looking for pattern " + "'" @@ -176,30 +172,31 @@ private void findText(Run run, FilePath workspace, TaskListener listener) } /** - * Search the given regexp pattern in the file. + * Search the given regexp pattern. * * @param abortAfterFirstHit true to return immediately as soon as the first hit is found. this * is necessary when we are scanning the console output, because otherwise we'll loop * forever. */ - private static boolean checkFile( - File f, + private static boolean checkPattern( + Reader r, Pattern pattern, PrintStream logger, - Charset charset, - boolean abortAfterFirstHit) { + String header, + boolean abortAfterFirstHit) + throws IOException { boolean logFilename = true; boolean foundText = false; - BufferedReader reader = null; - try { + try (BufferedReader reader = new BufferedReader(r)) { // Assume default encoding and text files String line; - reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), charset)); while ((line = reader.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (matcher.find()) { if (logFilename) { // first occurrence - logger.println(f + ":"); + if (header != null) { + logger.println(header); + } logFilename = false; } logger.println(line); @@ -209,12 +206,31 @@ private static boolean checkFile( } } } + } + return foundText; + } + + private static boolean checkConsole(Run build, Pattern pattern, PrintStream logger) { + try (Reader r = build.getLogReader()) { + return checkPattern(r, pattern, logger, null, true); + } catch (IOException e) { + logger.println("[Text Finder] Error reading console output -- ignoring"); + Functions.printStackTrace(e, logger); + } + + return false; + } + + private static boolean checkFile(File f, Pattern pattern, PrintStream logger, Charset charset) { + try (InputStream is = new FileInputStream(f); + Reader r = new InputStreamReader(is, charset)) { + return checkPattern(r, pattern, logger, f + ":", false); } catch (IOException e) { logger.println("[Text Finder] Error reading file '" + f + "' -- ignoring"); - } finally { - IOUtils.closeQuietly(reader); + Functions.printStackTrace(e, logger); } - return foundText; + + return false; } private static Pattern compilePattern(PrintStream logger, String regexp) { @@ -284,7 +300,7 @@ public FileChecker(RemoteOutputStream ros, String fileSet, String regexp) { @Override public Boolean invoke(File ws, VirtualChannel channel) throws IOException { - PrintStream logger = new PrintStream(ros, false, Charset.defaultCharset().toString()); + PrintStream logger = new PrintStream(ros, true, Charset.defaultCharset().toString()); // Collect list of files for searching FileSet fs = new FileSet(); @@ -318,7 +334,7 @@ public Boolean invoke(File ws, VirtualChannel channel) throws IOException { continue; } - foundText |= checkFile(f, pattern, logger, Charset.defaultCharset(), false); + foundText |= checkFile(f, pattern, logger, Charset.defaultCharset()); } return foundText; diff --git a/src/test/java/hudson/plugins/textfinder/TestUtils.java b/src/test/java/hudson/plugins/textfinder/TestUtils.java new file mode 100644 index 0000000..7309ef3 --- /dev/null +++ b/src/test/java/hudson/plugins/textfinder/TestUtils.java @@ -0,0 +1,59 @@ +package hudson.plugins.textfinder; + +import static org.junit.Assert.assertNotNull; + +import hudson.Functions; +import hudson.model.Run; +import java.io.File; +import java.io.IOException; +import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; +import org.jenkinsci.plugins.workflow.flow.FlowExecution; +import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; +import org.jenkinsci.plugins.workflow.graph.FlowNode; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.jvnet.hudson.test.JenkinsRule; + +/** Utilities for testing Text Finder */ +public class TestUtils { + + private static void assertContainsMatch( + String header, String text, JenkinsRule rule, Run build, boolean isShell) + throws IOException { + String prompt; + if (isShell) { + prompt = Functions.isWindows() ? ">" : "+ "; + } else { + prompt = ""; + } + rule.assertLogContains(String.format("%s%s%s", header, prompt, text), build); + } + + public static void assertConsoleContainsMatch( + String text, JenkinsRule rule, Run build, boolean isShell) throws IOException { + assertContainsMatch("", text, rule, build, isShell); + } + + public static void assertFileContainsMatch( + File file, String text, JenkinsRule rule, Run build, boolean isShell) + throws IOException { + assertContainsMatch( + String.format("%s:%s", file, System.getProperty("line.separator")), + text, + rule, + build, + isShell); + } + + public static File getWorkspace(WorkflowRun build) { + FlowExecution execution = build.getExecution(); + assertNotNull(execution); + FlowGraphWalker walker = new FlowGraphWalker(execution); + for (FlowNode node : walker) { + WorkspaceAction action = node.getAction(WorkspaceAction.class); + if (action != null) { + return new File(action.getWorkspace().getRemote()); + } + } + throw new IllegalStateException("Failed to find workspace"); + } +} diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java index 4dcdb48..0e5303c 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherAgentTest.java @@ -1,16 +1,9 @@ package hudson.plugins.textfinder; -import hudson.Functions; import hudson.model.Result; import hudson.slaves.DumbSlave; import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; -import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.Rule; @@ -24,61 +17,46 @@ public class TextFinderPublisherAgentTest { @Rule public JenkinsRule rule = new JenkinsRule(); - private void assertLogContainsMatch(File file, String text, WorkflowRun build, boolean isShell) - throws IOException { - String prompt; - if (isShell) { - prompt = Functions.isWindows() ? ">" : "+ "; - } else { - prompt = ""; - } - rule.assertLogContains( - String.format( - "%s:%s%s%s", file, System.getProperty("line.separator"), prompt, text), - build); - } - - private File getWorkspace(WorkflowRun build) { - FlowGraphWalker walker = new FlowGraphWalker(build.getExecution()); - List actions = new ArrayList<>(); - for (FlowNode node : walker) { - WorkspaceAction action = node.getAction(WorkspaceAction.class); - if (action != null) { - return new File(action.getWorkspace().getRemote()); - } - } - throw new IllegalStateException("Failed to find workspace"); - } - @Test public void failureIfFoundInFileOnAgent() throws Exception { DumbSlave agent = rule.createOnlineSlave(); - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( String.format( - "node('%s') {writeFile file: 'out.txt', text: 'foobar'}\n" - + "node('%s') {findText regexp: 'foobar', fileSet: 'out.txt'}\n", - agent.getNodeName(), agent.getNodeName()))); + "node('%s') {\n" + + " writeFile file: 'out.txt', text: 'foobar'\n" + + " findText regexp: 'foobar', fileSet: 'out.txt'\n" + + "}\n", + agent.getNodeName()), + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains( "[Text Finder] Looking for pattern " + "'" + UNIQUE_TEXT + "'" + " in the files at", build); - assertLogContainsMatch(new File(getWorkspace(build), "out.txt"), UNIQUE_TEXT, build, false); + TestUtils.assertFileContainsMatch( + new File(TestUtils.getWorkspace(build), "out.txt"), + UNIQUE_TEXT, + rule, + build, + false); rule.assertBuildStatus(Result.FAILURE, build); } @Test public void failureIfFoundInConsoleOnAgent() throws Exception { DumbSlave agent = rule.createOnlineSlave(); - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( String.format( - "node('%s') {isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")}\n" - + "node('%s') {findText regexp: 'foobar', alsoCheckConsoleOutput: true}\n", - agent.getNodeName(), agent.getNodeName()))); + "node('%s') {\n" + + " isUnix() ? sh('echo foobar') : bat(\"prompt \\$G\\r\\necho foobar\")\n" + + " findText regexp: 'foobar', alsoCheckConsoleOutput: true\n" + + "}\n", + agent.getNodeName()), + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("[Text Finder] Scanning console output...", build); @@ -87,7 +65,7 @@ public void failureIfFoundInConsoleOnAgent() throws Exception { + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); rule.assertBuildStatus(Result.FAILURE, build); } } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java index 6199370..938e45a 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherFreestyleTest.java @@ -13,8 +13,6 @@ import hudson.tasks.BatchFile; import hudson.tasks.CommandInterpreter; import hudson.tasks.Shell; -import java.io.File; -import java.io.IOException; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; @@ -26,20 +24,6 @@ public class TextFinderPublisherFreestyleTest { @Rule public JenkinsRule rule = new JenkinsRule(); - private void assertLogContainsMatch( - File file, String text, FreeStyleBuild build, boolean isShell) throws IOException { - String prompt; - if (isShell) { - prompt = Functions.isWindows() ? ">" : "+ "; - } else { - prompt = ""; - } - rule.assertLogContains( - String.format( - "%s:%s%s%s", file, System.getProperty("line.separator"), prompt, text), - build); - } - @Test public void successIfFoundInConsole() throws Exception { FreeStyleProject project = rule.createFreeStyleProject(); @@ -52,16 +36,14 @@ public void successIfFoundInConsole() throws Exception { textFinder.setSucceedIfFound(true); textFinder.setAlsoCheckConsoleOutput(true); project.getPublishersList().add(textFinder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - rule.waitForCompletion(build); + FreeStyleBuild build = rule.buildAndAssertSuccess(project); rule.assertLogContains("[Text Finder] Scanning console output...", build); rule.assertLogContains( "[Text Finder] Finished looking for pattern '" + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); - rule.assertBuildStatus(Result.SUCCESS, build); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); } @Test @@ -83,7 +65,7 @@ public void failureIfFoundInConsole() throws Exception { + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); rule.assertBuildStatus(Result.FAILURE, build); } @@ -107,7 +89,7 @@ public void unstableIfFoundInConsole() throws Exception { + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); rule.assertBuildStatus(Result.UNSTABLE, build); } diff --git a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java index fd43e53..87ce1ff 100644 --- a/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java +++ b/src/test/java/hudson/plugins/textfinder/TextFinderPublisherPipelineTest.java @@ -1,15 +1,8 @@ package hudson.plugins.textfinder; -import hudson.Functions; import hudson.model.Result; import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import org.jenkinsci.plugins.workflow.actions.WorkspaceAction; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; -import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.Rule; @@ -24,49 +17,25 @@ public class TextFinderPublisherPipelineTest { @Rule public JenkinsRule rule = new JenkinsRule(); - private void assertLogContainsMatch(File file, String text, WorkflowRun build, boolean isShell) - throws IOException { - String prompt; - if (isShell) { - prompt = Functions.isWindows() ? ">" : "+ "; - } else { - prompt = ""; - } - rule.assertLogContains( - String.format( - "%s:%s%s%s", file, System.getProperty("line.separator"), prompt, text), - build); - } - - private File getWorkspace(WorkflowRun build) { - FlowGraphWalker walker = new FlowGraphWalker(build.getExecution()); - List actions = new ArrayList<>(); - for (FlowNode node : walker) { - WorkspaceAction action = node.getAction(WorkspaceAction.class); - if (action != null) { - return new File(action.getWorkspace().getRemote()); - } - } - throw new IllegalStateException("Failed to find workspace"); - } - @Test public void successIfFoundInFile() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {writeFile file: '" + "node {\n" + + " writeFile file: '" + fileSet + "', text: '" + UNIQUE_TEXT - + "'}\n" - + "node {findText regexp: '" + + "'\n" + + " findText regexp: '" + UNIQUE_TEXT + "', fileSet: '" + fileSet - + "', succeedIfFound: true}\n")); - WorkflowRun build = project.scheduleBuild2(0).get(); - rule.waitForCompletion(build); + + "', succeedIfFound: true\n" + + "}\n", + true)); + WorkflowRun build = rule.buildAndAssertSuccess(project); rule.assertLogContains( "[Text Finder] Looking for pattern '" + UNIQUE_TEXT @@ -75,25 +44,28 @@ public void successIfFoundInFile() throws Exception { + fileSet + "'", build); - assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false); - rule.assertBuildStatus(Result.SUCCESS, build); + TestUtils.assertFileContainsMatch( + new File(TestUtils.getWorkspace(build), fileSet), UNIQUE_TEXT, rule, build, false); } @Test public void failureIfFoundInFile() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {writeFile file: '" + "node {\n" + + " writeFile file: '" + fileSet + "', text: '" + UNIQUE_TEXT - + "'}\n" - + "node {findText regexp: '" + + "'\n" + + " findText regexp: '" + UNIQUE_TEXT + "', fileSet: '" + fileSet - + "'}\n")); + + "'\n" + + "}\n", + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains( @@ -104,25 +76,29 @@ public void failureIfFoundInFile() throws Exception { + fileSet + "'", build); - assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false); + TestUtils.assertFileContainsMatch( + new File(TestUtils.getWorkspace(build), fileSet), UNIQUE_TEXT, rule, build, false); rule.assertBuildStatus(Result.FAILURE, build); } @Test public void unstableIfFoundInFile() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {writeFile file: '" + "node {\n" + + " writeFile file: '" + fileSet + "', text: '" + UNIQUE_TEXT - + "'}\n" - + "node {findText regexp: '" + + "'\n" + + " findText regexp: '" + UNIQUE_TEXT + "', fileSet: '" + fileSet - + "', unstableIfFound: true}\n")); + + "', unstableIfFound: true\n" + + "}\n", + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains( @@ -133,25 +109,29 @@ public void unstableIfFoundInFile() throws Exception { + fileSet + "'", build); - assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false); + TestUtils.assertFileContainsMatch( + new File(TestUtils.getWorkspace(build), fileSet), UNIQUE_TEXT, rule, build, false); rule.assertBuildStatus(Result.UNSTABLE, build); } @Test public void notBuiltIfFoundInFile() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {writeFile file: '" + "node {\n" + + " writeFile file: '" + fileSet + "', text: '" + UNIQUE_TEXT - + "'}\n" - + "node {findText regexp: '" + + "'\n" + + " findText regexp: '" + UNIQUE_TEXT + "', fileSet: '" + fileSet - + "', notBuiltIfFound: true}\n")); + + "', notBuiltIfFound: true\n" + + "}\n", + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains( @@ -162,25 +142,28 @@ public void notBuiltIfFoundInFile() throws Exception { + fileSet + "'", build); - assertLogContainsMatch(new File(getWorkspace(build), fileSet), UNIQUE_TEXT, build, false); + TestUtils.assertFileContainsMatch( + new File(TestUtils.getWorkspace(build), fileSet), UNIQUE_TEXT, rule, build, false); rule.assertBuildStatus(Result.NOT_BUILT, build); } @Test public void notFoundInFile() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {writeFile file: '" + "node {\n" + + " writeFile file: '" + fileSet - + "', text: 'foobaz'}\n" - + "node {findText regexp: '" + + "', text: 'foobaz'\n" + + " findText regexp: '" + UNIQUE_TEXT + "', fileSet: '" + fileSet - + "'}\n")); - WorkflowRun build = project.scheduleBuild2(0).get(); - rule.waitForCompletion(build); + + "'\n" + + "}\n", + true)); + WorkflowRun build = rule.buildAndAssertSuccess(project); rule.assertLogContains( "[Text Finder] Looking for pattern '" + UNIQUE_TEXT @@ -189,47 +172,50 @@ public void notFoundInFile() throws Exception { + fileSet + "'", build); - rule.assertBuildStatus(Result.SUCCESS, build); } @Test public void successIfFoundInConsole() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('" + "node {\n" + + " isUnix() ? sh('" + ECHO_UNIQUE_TEXT + "') : bat(\"prompt \\$G\\r\\n" + ECHO_UNIQUE_TEXT - + "\")}\n" - + "node {findText regexp: '" + + "\")\n" + + " findText regexp: '" + UNIQUE_TEXT - + "', succeedIfFound: true, alsoCheckConsoleOutput: true}\n")); - WorkflowRun build = project.scheduleBuild2(0).get(); - rule.waitForCompletion(build); + + "', succeedIfFound: true, alsoCheckConsoleOutput: true\n" + + "}\n", + true)); + WorkflowRun build = rule.buildAndAssertSuccess(project); rule.assertLogContains("[Text Finder] Scanning console output...", build); rule.assertLogContains( "[Text Finder] Finished looking for pattern '" + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); - rule.assertBuildStatus(Result.SUCCESS, build); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); } @Test public void failureIfFoundInConsole() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('" + "node {\n" + + " isUnix() ? sh('" + ECHO_UNIQUE_TEXT + "') : bat(\"prompt \\$G\\r\\n" + ECHO_UNIQUE_TEXT - + "\")}\n" - + "node {findText regexp: '" + + "\")\n" + + " findText regexp: '" + UNIQUE_TEXT - + "', alsoCheckConsoleOutput: true}\n")); + + "', alsoCheckConsoleOutput: true\n" + + "}\n", + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("[Text Finder] Scanning console output...", build); @@ -238,23 +224,26 @@ public void failureIfFoundInConsole() throws Exception { + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); rule.assertBuildStatus(Result.FAILURE, build); } @Test public void unstableIfFoundInConsole() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('" + "node {\n" + + " isUnix() ? sh('" + ECHO_UNIQUE_TEXT + "') : bat(\"prompt \\$G\\r\\n" + ECHO_UNIQUE_TEXT - + "\")}\n" - + "node {findText regexp: '" + + "\")\n" + + " findText regexp: '" + UNIQUE_TEXT - + "', unstableIfFound: true, alsoCheckConsoleOutput: true}\n")); + + "', unstableIfFound: true, alsoCheckConsoleOutput: true\n" + + "}\n", + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("[Text Finder] Scanning console output...", build); @@ -263,23 +252,26 @@ public void unstableIfFoundInConsole() throws Exception { + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); rule.assertBuildStatus(Result.UNSTABLE, build); } @Test public void notBuiltIfFoundInConsole() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {isUnix() ? sh('" + "node {\n" + + " isUnix() ? sh('" + ECHO_UNIQUE_TEXT + "') : bat(\"prompt \\$G\\r\\n" + ECHO_UNIQUE_TEXT - + "\")}\n" - + "node {findText regexp: '" + + "\")\n" + + " findText regexp: '" + UNIQUE_TEXT - + "', notBuiltIfFound: true, alsoCheckConsoleOutput: true}\n")); + + "', notBuiltIfFound: true, alsoCheckConsoleOutput: true\n" + + "}\n", + true)); WorkflowRun build = project.scheduleBuild2(0).get(); rule.waitForCompletion(build); rule.assertLogContains("[Text Finder] Scanning console output...", build); @@ -288,26 +280,27 @@ public void notBuiltIfFoundInConsole() throws Exception { + UNIQUE_TEXT + "' in the console output", build); - assertLogContainsMatch(build.getLogFile(), ECHO_UNIQUE_TEXT, build, true); + TestUtils.assertConsoleContainsMatch(ECHO_UNIQUE_TEXT, rule, build, true); rule.assertBuildStatus(Result.NOT_BUILT, build); } @Test public void notFoundInConsole() throws Exception { - WorkflowJob project = rule.createProject(WorkflowJob.class, "pipeline"); + WorkflowJob project = rule.createProject(WorkflowJob.class); project.setDefinition( new CpsFlowDefinition( - "node {findText regexp: '" + "node {\n" + + " findText regexp: '" + UNIQUE_TEXT - + "', alsoCheckConsoleOutput: true}\n")); - WorkflowRun build = project.scheduleBuild2(0).get(); - rule.waitForCompletion(build); + + "', alsoCheckConsoleOutput: true\n" + + "}\n", + true)); + WorkflowRun build = rule.buildAndAssertSuccess(project); rule.assertLogContains("[Text Finder] Scanning console output...", build); rule.assertLogContains( "[Text Finder] Finished looking for pattern '" + UNIQUE_TEXT + "' in the console output", build); - rule.assertBuildStatus(Result.SUCCESS, build); } }