Search in sources :

Example 1 with RingBufferLogHandler

use of hudson.util.RingBufferLogHandler in project support-core-plugin by jenkinsci.

the class SupportActionTest method takeSnapshotAndMakeSureSomethingHappens.

/*
     * Integration test that simulates the user action of clicking the button to generate the bundle.
     * <p>
     * If any warning is reported to j.u.l logger, treat that as a sign of failure, because
     * support-core plugin works darn hard to try to generate something in the presence of failing
     * {@link Component} impls.
     */
@Test
public void takeSnapshotAndMakeSureSomethingHappens() throws Exception {
    j.createSlave("agent1", "test", null).getComputer().connect(false).get();
    j.createSlave("agent2", "test", null).getComputer().connect(false).get();
    RingBufferLogHandler checker = new RingBufferLogHandler();
    Logger logger = Logger.getLogger(SupportPlugin.class.getPackage().getName());
    logger.addHandler(checker);
    try {
        WebClient wc = j.createWebClient();
        HtmlPage p = wc.goTo(root.getUrlName());
        HtmlForm form = p.getFormByName("bundle-contents");
        HtmlButton submit = (HtmlButton) form.getElementsByTagName("button").get(0);
        Page zip = submit.click();
        File zipFile = File.createTempFile("test", "zip");
        IOUtils.copy(zip.getWebResponse().getContentAsStream(), Files.newOutputStream(zipFile.toPath()));
        ZipFile z = new ZipFile(zipFile);
        // check the presence of files
        // TODO: emit some log entries and see if it gets captured here
        assertNotNull(z.getEntry("about.md"));
        assertNotNull(z.getEntry("nodes.md"));
        assertNotNull(z.getEntry("nodes/master/thread-dump.txt"));
        if (SystemPlatform.LINUX == SystemPlatform.current()) {
            List<String> files = Arrays.asList("proc/swaps.txt", "proc/cpuinfo.txt", "proc/mounts.txt", "proc/system-uptime.txt", "proc/net/rpc/nfs.txt", "proc/net/rpc/nfsd.txt", "proc/meminfo.txt", "proc/self/status.txt", "proc/self/cmdline", "proc/self/environ", "proc/self/limits.txt", "proc/self/mountstats.txt", "sysctl.txt", "dmesg.txt", "userid.txt", "dmi.txt");
            for (String file : files) {
                assertNotNull(file + " was not found in the bundle", z.getEntry("nodes/master/" + file));
            }
        }
    } finally {
        logger.removeHandler(checker);
        for (LogRecord r : checker.getView()) {
            if (r.getLevel().intValue() >= Level.WARNING.intValue()) {
                Throwable thrown = r.getThrown();
                if (thrown != null)
                    thrown.printStackTrace(System.err);
                fail(r.getMessage());
            }
        }
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Matchers.containsString(org.hamcrest.Matchers.containsString) RingBufferLogHandler(hudson.util.RingBufferLogHandler) Logger(java.util.logging.Logger) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) ZipFile(java.util.zip.ZipFile) LogRecord(java.util.logging.LogRecord) ZipFile(java.util.zip.ZipFile) File(java.io.File) Test(org.junit.Test)

Example 2 with RingBufferLogHandler

use of hudson.util.RingBufferLogHandler in project support-core-plugin by jenkinsci.

the class SupportTestUtils method generateBundleWithoutWarnings.

/**
 * Generate a support bundle via HTTPS using the action passed in.
 *
 * <p>
 * If any warning is reported to j.u.l logger, treat that as a sign of failure, because
 * support-core plugin works darn hard to try to generate something in the presence of failing
 * {@link Component} implementations.
 *
 * @param baseUrl     the base url of the action. "" for root actions.
 * @param action      the {@link Action}
 * @param loggerClass the class package to inspect loggers from
 * @param webClient   a {@link org.jvnet.hudson.test.JenkinsRule.WebClient}
 * @return the {@link ZipFile} generated
 */
public static ZipFile generateBundleWithoutWarnings(String baseUrl, Action action, Class<?> loggerClass, JenkinsRule.WebClient webClient) throws Exception {
    RingBufferLogHandler checker = new RingBufferLogHandler(256);
    Logger logger = Logger.getLogger(loggerClass.getPackage().getName());
    logger.addHandler(checker);
    try {
        return SupportTestUtils.generateBundleFromAction(baseUrl, action, webClient);
    } finally {
        logger.removeHandler(checker);
        for (LogRecord r : checker.getView()) {
            if (r.getLevel().intValue() >= Level.WARNING.intValue()) {
                Throwable thrown = r.getThrown();
                if (thrown != null)
                    thrown.printStackTrace(System.err);
                fail(r.getMessage());
            }
        }
    }
}
Also used : LogRecord(java.util.logging.LogRecord) RingBufferLogHandler(hudson.util.RingBufferLogHandler) Logger(java.util.logging.Logger)

Aggregations

RingBufferLogHandler (hudson.util.RingBufferLogHandler)2 LogRecord (java.util.logging.LogRecord)2 Logger (java.util.logging.Logger)2 Page (com.gargoylesoftware.htmlunit.Page)1 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)1 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 File (java.io.File)1 ZipFile (java.util.zip.ZipFile)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 WebClient (org.jvnet.hudson.test.JenkinsRule.WebClient)1