From 38d6d9bab63e95e8acd498fc1338609efcc40922 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Wed, 9 Dec 2015 14:23:03 -0500 Subject: [PATCH] Converting BootFailureTest from Groovy to Java. (cherry picked from commit 09722a982fc50b3271cc9fbcc05ef5154840b954) --- .../groovy/hudson/util/BootFailureTest.groovy | 115 --------------- .../java/hudson/util/BootFailureTest.java | 137 ++++++++++++++++++ 2 files changed, 137 insertions(+), 115 deletions(-) delete mode 100644 test/src/test/groovy/hudson/util/BootFailureTest.groovy create mode 100644 test/src/test/java/hudson/util/BootFailureTest.java diff --git a/test/src/test/groovy/hudson/util/BootFailureTest.groovy b/test/src/test/groovy/hudson/util/BootFailureTest.groovy deleted file mode 100644 index 389ad757e5..0000000000 --- a/test/src/test/groovy/hudson/util/BootFailureTest.groovy +++ /dev/null @@ -1,115 +0,0 @@ -package hudson.util - -import hudson.WebAppMain -import hudson.model.Hudson -import hudson.model.listeners.ItemListener -import jenkins.model.Jenkins -import org.junit.After -import org.junit.Rule -import org.junit.Test -import org.junit.rules.TemporaryFolder -import org.jvnet.hudson.test.HudsonHomeLoader -import org.jvnet.hudson.test.JenkinsRule -import org.jvnet.hudson.test.TestEnvironment -import org.jvnet.hudson.test.TestExtension -import org.kohsuke.stapler.WebApp - -import javax.servlet.ServletContextEvent - -/** - * - * - * @author Kohsuke Kawaguchi - */ -class BootFailureTest { - @Rule - public TemporaryFolder tmpDir = new TemporaryFolder(); - - static boolean makeBootFail = true; - - @Rule - public JenkinsRule j = new JenkinsRule() { - @Override - void before() throws Throwable { - env = new TestEnvironment(testDescription); - env.pin(); - // don't let Jenkins start automatically - } - - @Override - public Hudson newHudson() throws Exception { - def ws = createWebServer() - def wa = new WebAppMain() { - @Override - WebAppMain.FileAndDescription getHomeDir(ServletContextEvent event) { - return new WebAppMain.FileAndDescription(homeLoader.allocate(),"test"); - } - } - wa.contextInitialized(new ServletContextEvent(ws)); - wa.joinInit(); - - def a = WebApp.get(ws).app; - if (a instanceof Jenkins) - return a; - return null; // didn't boot - } - } - - @After - public void tearDown() { - Jenkins.getInstance()?.cleanUp() - } - - public static class SeriousError extends Error {} - - @TestExtension() - public static class InduceBootFailure extends ItemListener { - @Override - void onLoaded() { - if (makeBootFail) - throw new SeriousError(); - } - } - - @Test - void runBootFailureScript() { - final def home = tmpDir.newFolder() - j.with({ -> home} as HudsonHomeLoader) - - // creates a script - new File(home,"boot-failure.groovy").text = "hudson.util.BootFailureTest.problem = exception"; - def d = new File(home, "boot-failure.groovy.d") - d.mkdirs(); - new File(d,"1.groovy").text = "hudson.util.BootFailureTest.runRecord << '1'"; - new File(d,"2.groovy").text = "hudson.util.BootFailureTest.runRecord << '2'"; - - // first failed boot - makeBootFail = true; - assert j.newHudson()==null; - assert bootFailures(home)==1; - - // second failed boot - problem = null; - runRecord = []; - assert j.newHudson()==null; - assert bootFailures(home)==2; - assert runRecord==["1","2"] - - // make sure the script has actually run - assert problem.cause instanceof SeriousError - - // if it boots well, the failure record should be gone - makeBootFail = false; - assert j.newHudson()!=null; - assert !BootFailure.getBootFailureFile(home).exists() - } - - private static int bootFailures(File home) { - return BootFailure.getBootFailureFile(home).readLines().size() - } - - // to be set by the script - public static Exception problem; - public static def runRecord = []; - -} diff --git a/test/src/test/java/hudson/util/BootFailureTest.java b/test/src/test/java/hudson/util/BootFailureTest.java new file mode 100644 index 0000000000..1362c2c59e --- /dev/null +++ b/test/src/test/java/hudson/util/BootFailureTest.java @@ -0,0 +1,137 @@ +package hudson.util; + +import hudson.WebAppMain; +import hudson.model.Hudson; +import hudson.model.listeners.ItemListener; +import static hudson.util.BootFailureTest.makeBootFail; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import jenkins.model.Jenkins; +import org.apache.commons.io.FileUtils; +import org.junit.After; +import static org.junit.Assert.*; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.jvnet.hudson.test.HudsonHomeLoader; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.TestEnvironment; +import org.jvnet.hudson.test.TestExtension; +import org.kohsuke.stapler.WebApp; + +/** + * + * + * @author Kohsuke Kawaguchi + */ +public class BootFailureTest { + @Rule + public TemporaryFolder tmpDir = new TemporaryFolder(); + + static boolean makeBootFail = true; + + static class CustomRule extends JenkinsRule { + @Override + public void before() throws Throwable { + env = new TestEnvironment(testDescription); + env.pin(); + // don't let Jenkins start automatically + } + + @Override + public Hudson newHudson() throws Exception { + ServletContext ws = createWebServer(); + WebAppMain wa = new WebAppMain() { + @Override + public WebAppMain.FileAndDescription getHomeDir(ServletContextEvent event) { + try { + return new WebAppMain.FileAndDescription(homeLoader.allocate(), "test"); + } catch (Exception x) { + throw new AssertionError(x); + } + } + }; + wa.contextInitialized(new ServletContextEvent(ws)); + wa.joinInit(); + + Object a = WebApp.get(ws).getApp(); + if (a instanceof Hudson) { + return (Hudson) a; + } + return null; // didn't boot + } + } + @Rule + public CustomRule j = new CustomRule(); + + @After + public void tearDown() { + Jenkins j = Jenkins.getInstance(); + if (j != null) { + j.cleanUp(); + } + } + + public static class SeriousError extends Error {} + + @TestExtension("runBootFailureScript") + public static class InduceBootFailure extends ItemListener { + @Override + public void onLoaded() { + if (makeBootFail) + throw new SeriousError(); + } + } + + @Test + public void runBootFailureScript() throws Exception { + final File home = tmpDir.newFolder(); + j.with(new HudsonHomeLoader() { + @Override + public File allocate() throws Exception { + return home; + } + }); + + // creates a script + FileUtils.write(new File(home, "boot-failure.groovy"), "hudson.util.BootFailureTest.problem = exception"); + File d = new File(home, "boot-failure.groovy.d"); + d.mkdirs(); + FileUtils.write(new File(d, "1.groovy"), "hudson.util.BootFailureTest.runRecord << '1'"); + FileUtils.write(new File(d, "2.groovy"), "hudson.util.BootFailureTest.runRecord << '2'"); + + // first failed boot + makeBootFail = true; + assertNull(j.newHudson()); + assertEquals(1, bootFailures(home)); + + // second failed boot + problem = null; + runRecord = new ArrayList(); + assertNull(j.newHudson()); + assertEquals(2, bootFailures(home)); + assertEquals(Arrays.asList("1", "2"), runRecord); + + // make sure the script has actually run + assertEquals(SeriousError.class, problem.getCause().getClass()); + + // if it boots well, the failure record should be gone + makeBootFail = false; + assertNotNull(j.newHudson()); + assertFalse(BootFailure.getBootFailureFile(home).exists()); + } + + private static int bootFailures(File home) throws IOException { + return FileUtils.readLines(BootFailure.getBootFailureFile(home)).size(); + } + + // to be set by the script + public static Exception problem; + public static List runRecord = new ArrayList(); + +} -- GitLab