Skip to content

Commit

Permalink
Merge pull request #520 from oleg-nenashev/tests-with-webUI
Browse files Browse the repository at this point in the history
Allow running JFR tests with Jenkins UI
  • Loading branch information
oleg-nenashev authored Jun 10, 2021
2 parents c947dbe + 6b5c7e7 commit a532218
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<JarEntry> enu = jarfile.entries();

// Get current working directory path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -44,41 +79,41 @@ 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);
}

/**
* 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);
}

/**
* Executes JFR "lint" using the CLI routines
*/
@CheckReturnValue
public static int lintAsCLI(File jenkinsfile, @CheckForNull Collection<String> additionalArgs) throws Throwable {
public int lintAsCLI(File jenkinsfile, @CheckForNull Collection<String> additionalArgs) throws Throwable {
return executeAsCLI(jenkinsfile, "lint", additionalArgs);
}

/**
* Executes JFR "run" using the CLI routines
*/
@CheckReturnValue
public static int runAsCLI(File jenkinsfile, @CheckForNull Collection<String> additionalArgs) throws Throwable {
public int runAsCLI(File jenkinsfile, @CheckForNull Collection<String> additionalArgs) throws Throwable {
return executeAsCLI(jenkinsfile, "run", additionalArgs);
}

/**
* Executes JFR using the CLI routines
*/
@CheckReturnValue
private static int executeAsCLI(File jenkinsfile, String command, @CheckForNull Collection<String> additionalArgs) throws Throwable {
private int executeAsCLI(File jenkinsfile, String command, @CheckForNull Collection<String> 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());
Expand All @@ -89,6 +124,9 @@ private static int executeAsCLI(File jenkinsfile, String command, @CheckForNull
"-p", pluginsDir.getAbsolutePath(),
"-f", jenkinsfile.getAbsolutePath());
List<String> cmd = new ArrayList<>(basicArgs);
if (enableHttp) {
basicArgs.add("--httpPort=8080");
}
if (additionalArgs != null) {
cmd.addAll(additionalArgs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"));
}
Expand All @@ -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"));
}
Expand All @@ -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\""));
}
Expand All @@ -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"));
}
Expand All @@ -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)));
}

Expand All @@ -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"));
}
Expand All @@ -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"));
}
Expand All @@ -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"));
Expand All @@ -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));
}

Expand All @@ -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!"));
}
Expand All @@ -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 {
Expand All @@ -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"));
Expand Down

0 comments on commit a532218

Please sign in to comment.