diff --git a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java index 2de5aaef..ae3fa733 100644 --- a/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java +++ b/bootstrap/src/main/java/io/jenkins/jenkinsfile/runner/bootstrap/Util.java @@ -56,8 +56,12 @@ public static String readJenkinsPomProperty(String key) throws IOException { } } - public static File explodeWar(String jarPath) throws IOException { - try (JarFile jarfile = new JarFile(new File(jarPath))) { + public static File explodeWar(String warPath) throws IOException { + return explodeWar(new File(warPath)); + } + + public static File explodeWar(File warFile) throws IOException { + try (JarFile jarfile = new JarFile(warFile)) { Enumeration enu = jarfile.entries(); // Get current working directory path diff --git a/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/JFRTestUtil.java b/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/JFRTestUtil.java index 21423998..30066b4c 100644 --- a/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/JFRTestUtil.java +++ b/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/JFRTestUtil.java @@ -3,11 +3,14 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.CheckReturnValue; import io.jenkins.jenkinsfile.runner.bootstrap.Bootstrap; +import io.jenkins.jenkinsfile.runner.bootstrap.Util; import io.jenkins.jenkinsfile.runner.bootstrap.commands.JenkinsLauncherCommand; import io.jenkins.jenkinsfile.runner.bootstrap.commands.JenkinsLauncherOptions; import io.jenkins.jenkinsfile.runner.bootstrap.commands.PipelineRunOptions; import io.jenkins.jenkinsfile.runner.bootstrap.commands.RunJenkinsfileCommand; import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -23,16 +26,48 @@ */ public class JFRTestUtil { + + private boolean enableHttp = false; + + /** + * Enables HTTP Server + */ + public JFRTestUtil withEnableHttp(boolean enableHttp) { + this.enableHttp = enableHttp; + return this; + } + + /** + * Checks whether the test actually needs the exploded WAR directory + */ + public boolean isExplodeWar() { + return enableHttp; + } + + private File getWar() throws IOException { + File warDir = new File("target/war"); + if (!isExplodeWar()) { + return warDir; + } + + File war = new File(warDir, "jenkins.war"); + if (!war.exists()) { + throw new FileNotFoundException("No jenkins.war in " + war); + } + + return Util.explodeWar(war); + } + /** * Runs JFR using the low-level methods, {@link JenkinsLauncherCommand#postConstruct()} is skipped. */ @CheckReturnValue - public static int run(File jenkinsfile) throws Throwable { + public int run(File jenkinsfile) throws Throwable { RunJenkinsfileCommand jfr = new RunJenkinsfileCommand(); File vanillaTarget = new File("target"); jfr.launcherOptions = new JenkinsLauncherOptions(); jfr.pipelineRunOptions = new PipelineRunOptions(); - jfr.launcherOptions.warDir = new File(vanillaTarget, "war"); + jfr.launcherOptions.warDir = getWar(); jfr.launcherOptions.pluginsDir = new File(vanillaTarget, "plugins"); jfr.pipelineRunOptions.jenkinsfile = jenkinsfile; @@ -44,7 +79,7 @@ public static int run(File jenkinsfile) throws Throwable { * Runs JFR using the CLI routines */ @CheckReturnValue - public static int runAsCLI(File jenkinsfile) throws Throwable { + public int runAsCLI(File jenkinsfile) throws Throwable { return runAsCLI(jenkinsfile, null); } @@ -52,7 +87,7 @@ public static int runAsCLI(File jenkinsfile) throws Throwable { * Executes JFR "lint" using the CLI routines */ @CheckReturnValue - public static int lintAsCLI(File jenkinsfile) throws Throwable { + public int lintAsCLI(File jenkinsfile) throws Throwable { return executeAsCLI(jenkinsfile, "lint", null); } @@ -60,7 +95,7 @@ public static int lintAsCLI(File jenkinsfile) throws Throwable { * Executes JFR "lint" using the CLI routines */ @CheckReturnValue - public static int lintAsCLI(File jenkinsfile, @CheckForNull Collection additionalArgs) throws Throwable { + public int lintAsCLI(File jenkinsfile, @CheckForNull Collection additionalArgs) throws Throwable { return executeAsCLI(jenkinsfile, "lint", additionalArgs); } @@ -68,7 +103,7 @@ public static int lintAsCLI(File jenkinsfile, @CheckForNull Collection a * Executes JFR "run" using the CLI routines */ @CheckReturnValue - public static int runAsCLI(File jenkinsfile, @CheckForNull Collection additionalArgs) throws Throwable { + public int runAsCLI(File jenkinsfile, @CheckForNull Collection additionalArgs) throws Throwable { return executeAsCLI(jenkinsfile, "run", additionalArgs); } @@ -76,9 +111,9 @@ public static int runAsCLI(File jenkinsfile, @CheckForNull Collection ad * Executes JFR using the CLI routines */ @CheckReturnValue - private static int executeAsCLI(File jenkinsfile, String command, @CheckForNull Collection additionalArgs) throws Throwable { + private int executeAsCLI(File jenkinsfile, String command, @CheckForNull Collection additionalArgs) throws Throwable { File vanillaTarget = new File("target"); - File warDir = new File(vanillaTarget, "war"); + File warDir = getWar(); File pluginsDir = new File(vanillaTarget, "plugins"); assertTrue("Jenkins WAR directory must exist when running tests", warDir.exists()); assertTrue("Plugins directory must exist when running tests", pluginsDir.exists()); @@ -89,6 +124,9 @@ private static int executeAsCLI(File jenkinsfile, String command, @CheckForNull "-p", pluginsDir.getAbsolutePath(), "-f", jenkinsfile.getAbsolutePath()); List cmd = new ArrayList<>(basicArgs); + if (enableHttp) { + basicArgs.add("--httpPort=8080"); + } if (additionalArgs != null) { cmd.addAll(additionalArgs); } diff --git a/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/ParametersTest.java b/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/ParametersTest.java index bb1794ec..e2071117 100644 --- a/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/ParametersTest.java +++ b/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/ParametersTest.java @@ -51,7 +51,7 @@ public void scriptedPipeline() throws Throwable { " echo \"Value for param2: ${params.param2}\"\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile, Arrays.asList("-a", "param1=Hello", "-a", "param2=value2")); + int result = new JFRTestUtil().runAsCLI(jenkinsfile, Arrays.asList("-a", "param1=Hello", "-a", "param2=value2")); assertThat("JFR should be executed successfully", result, equalTo(0)); assertThat(systemOut.getLog(), containsString("Hello, world!")); assertThat(systemOut.getLog(), containsString("Value for param1: Hello")); @@ -78,7 +78,7 @@ public void declarativePipeline() throws Throwable { " }\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile, Arrays.asList("-a", "param1=Hello", "-a", "param2=value2")); + int result = new JFRTestUtil().runAsCLI(jenkinsfile, Arrays.asList("-a", "param1=Hello", "-a", "param2=value2")); assertThat("JFR should be executed successfully", result, equalTo(0)); assertThat(systemOut.getLog(), containsString("Hello, world!")); assertThat(systemOut.getLog(), containsString("Value for param1: Hello")); diff --git a/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/SmokeTest.java b/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/SmokeTest.java index 8cf1fc79..ff6f9f80 100644 --- a/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/SmokeTest.java +++ b/vanilla-package/src/test/java/io/jenkins/jenkinsfile/runner/vanilla/SmokeTest.java @@ -48,7 +48,18 @@ public void helloWorld() throws Throwable { File jenkinsfile = tmp.newFile("Jenkinsfile"); FileUtils.writeStringToFile(jenkinsfile, "echo 'Hello, world!'", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); + assertThat("JFR should be executed successfully", result, equalTo(0)); + assertThat(systemOut.getLog(), containsString("Hello, world!")); + } + + @Test(timeout = Integer.MAX_VALUE) + @Ignore + public void helloWorldWithUI() throws Throwable { + File jenkinsfile = tmp.newFile("Jenkinsfile"); + FileUtils.writeStringToFile(jenkinsfile, "echo 'Hello, world!'; sleep 100500", Charset.defaultCharset()); + + int result = new JFRTestUtil().withEnableHttp(true).runAsCLI(jenkinsfile); assertThat("JFR should be executed successfully", result, equalTo(0)); assertThat(systemOut.getLog(), containsString("Hello, world!")); } @@ -67,7 +78,7 @@ public void lintSuccess() throws Throwable { + " }\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.lintAsCLI(jenkinsfile); + int result = new JFRTestUtil().lintAsCLI(jenkinsfile); assertThat("JFR should not execute successfully", result, equalTo(0)); assertThat(systemOut.getLog(), containsString("Done")); } @@ -87,7 +98,7 @@ public void lintFailure() throws Throwable { + " }\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.lintAsCLI(jenkinsfile); + int result = new JFRTestUtil().lintAsCLI(jenkinsfile); assertThat("JFR should not execute successfully", result, equalTo(1)); assertThat(systemOut.getLog(), containsString("Not a valid section definition: \"agent\"")); } @@ -96,7 +107,7 @@ public void lintFailure() throws Throwable { public void shouldFailWithWrongJenkinsfile() throws Throwable { File jenkinsfile = new File(tmp.getRoot(), "Jenkinsfile"); - int result = JFRTestUtil.run(jenkinsfile); + int result = new JFRTestUtil().run(jenkinsfile); assertThat("JFR should fail when there is no Jenkinsfile", result, not(equalTo(0))); assertThat(systemOut.getLog(), containsString("does not exist")); } @@ -114,7 +125,7 @@ public void shouldHangWhenPipelineHangs() throws Throwable { " }\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.run(jenkinsfile); + int result = new JFRTestUtil().run(jenkinsfile); assertThat("JFR should fail when there is no Jenkinsfile", result, not(equalTo(0))); } @@ -125,7 +136,7 @@ public void pipelineExecutionFails() throws Throwable { " currentBuild.result = 'FAILED'\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); assertThat("JFR should fail when there is no Jenkinsfile", result, not(equalTo(0))); assertThat(systemOut.getLog(), containsString("Finished: FAILURE")); } @@ -137,7 +148,7 @@ public void pipelineExecutionUnstable() throws Throwable { " currentBuild.result = 'UNSTABLE'\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); assertThat("JFR should fail when there is no Jenkinsfile", result, not(equalTo(0))); assertThat(systemOut.getLog(), containsString("Finished: UNSTABLE")); } @@ -161,7 +172,7 @@ public void pipelineBadSyntax() throws Throwable { " }\n" + "}", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); assertThat("JFR should fail when there is no Jenkinsfile", result, not(equalTo(0))); assertThat(systemOut.getLog(), not(containsString("[Pipeline] End of Pipeline"))); assertThat(systemOut.getLog(), containsString("Finished: FAILURE")); @@ -175,7 +186,7 @@ public void shouldSupportDataboundMethods() throws Throwable { " checkout scm\n" + "}\n", Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); assertThat("JFR should be executed successfully", result, equalTo(0)); } @@ -192,7 +203,7 @@ public void helloWorldAsYaml() throws Throwable { " - echo \"Hello, world!\"" , Charset.defaultCharset()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); assertThat("JFR should be executed successfully", result, equalTo(0)); assertThat(systemOut.getLog(), containsString("Hello, world!")); } @@ -206,7 +217,7 @@ public void shouldFailWithABrokenConfig() throws Throwable { jcasc.toPath()); try { System.setProperty(ConfigurationAsCode.CASC_JENKINS_CONFIG_PROPERTY, jcasc.getAbsolutePath()); - int result = JFRTestUtil.runAsCLI(jenkinsfile); + int result = new JFRTestUtil().runAsCLI(jenkinsfile); assertThat("Jenkinsfile Runner execution should have failed", result, not(equalTo(0))); assertThat(systemErr.getLog(), containsString("No configurator for the following root elements globalNodeProperties")); } finally { @@ -225,7 +236,7 @@ public void checkoutSCM() throws Throwable { String scmConfigPath = createTestRepoWithContentAndSCMConfigYAML(filesAndContents, "master"); - int result = JFRTestUtil.runAsCLI(jenkinsfile, Arrays.asList("--scm", scmConfigPath)); + int result = new JFRTestUtil().runAsCLI(jenkinsfile, Arrays.asList("--scm", scmConfigPath)); assertThat("JFR should be executed successfully", result, equalTo(0)); assertThat(systemOut.getLog(), containsString("README.md exists with content 'Test repository'")); assertThat(systemOut.getLog(), containsString("using credential user1"));