Skip to content

Commit

Permalink
[JENKINS-25108] Added a test
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Oct 10, 2014
1 parent 1c33376 commit b5f5b10
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/main/java/com/cloudbees/jenkins/support/impl/JenkinsLogs.java
Expand Up @@ -179,6 +179,9 @@ protected void printTo(PrintWriter out) throws IOException {
*/
private void addSlaveLaunchLog(Container result) {
class Slave implements Comparable<Slave> {
/**
* Launch log directory of the slave: logs/slaves/NAME
*/
File dir;
long time;

Expand Down Expand Up @@ -215,13 +218,16 @@ public boolean isTooOld() {

{// find all the slave launch log files and sort them newer ones first

File slaveLogs = new File(Jenkins.getInstance().getRootDir(), "logs/slaves");
for (File dir : slaveLogs.listFiles()) {
File lastLog = new File(dir, "slave.log");
if (lastLog.exists()) {
Slave s = new Slave(dir, lastLog);
if (s.isTooOld()) continue; // we don't care
all.add(s);
File slaveLogsDir = new File(Jenkins.getInstance().getRootDir(), "logs/slaves");
File[] logs = slaveLogsDir.listFiles();
if (logs!=null) {
for (File dir : logs) {
File lastLog = new File(dir, "slave.log");
if (lastLog.exists()) {
Slave s = new Slave(dir, lastLog);
if (s.isTooOld()) continue; // we don't care
all.add(s);
}
}
}

Expand All @@ -237,9 +243,11 @@ public boolean isTooOld() {

// now add them all
for (Slave s : all) {
for (File f : Jenkins.getInstance().getRootDir().listFiles(ROTATED_LOGFILE_FILTER)) {
result.add(new FileContent("nodes/slave/" + s.getName() + "/launchLogs/"+f.getName() , f));
}
File[] files = s.dir.listFiles(ROTATED_LOGFILE_FILTER);
if (files!=null)
for (File f : files) {
result.add(new FileContent("nodes/slave/" + s.getName() + "/launchLogs/"+f.getName() , f));
}
}
}

Expand Down
83 changes: 83 additions & 0 deletions src/test/java/com/cloudbees/jenkins/support/SupportActionTest.java
@@ -0,0 +1,83 @@
package com.cloudbees.jenkins.support;

import com.cloudbees.jenkins.support.api.Component;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.util.IOUtils;
import hudson.util.RingBufferLogHandler;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import javax.inject.Inject;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.zip.ZipFile;

/**
* @author Kohsuke Kawaguchi
*/
public class SupportActionTest extends Assert {
@Rule
public JenkinsRule rule = new JenkinsRule();

@Inject
SupportAction root;

@Before
public void setUp() {
rule.jenkins.getInjector().injectMembers(this);
}

/**
* 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 {
rule.createSlave("slave1","test",null).getComputer().connect(false).get();
rule.createSlave("slave2","test",null).getComputer().connect(false).get();

RingBufferLogHandler checker = new RingBufferLogHandler();
Logger logger = Logger.getLogger(SupportPlugin.class.getPackage().getName());

logger.addHandler(checker);

try {
WebClient wc = rule.createWebClient();
HtmlPage p = wc.goTo(root.getUrlName());

HtmlForm form = p.getFormByName("bundle-contents");
Page zip = form.submit((HtmlButton) form.getHtmlElementsByTagName("button").get(0));
File zipFile = File.createTempFile("test", "zip");
IOUtils.copy(zip.getWebResponse().getContentAsStream(), zipFile);

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/slave/slave1/system.properties"));
assertNotNull(z.getEntry("nodes/master/thread-dump.txt"));
assertNotNull(z.getEntry("nodes/slave/slave2/launchLogs/slave.log"));
} finally {
logger.removeHandler(checker);
for (LogRecord r : checker.getView()) {
if (r.getLevel().intValue() >= Level.WARNING.intValue())
fail(r.getMessage());
}
}
}
}

0 comments on commit b5f5b10

Please sign in to comment.