use of com.cloudbees.jenkins.support.api.Content in project support-core-plugin by jenkinsci.
the class SupportPluginTest method testGenerateBundleExceptionHandler.
@Test
@Issue("JENKINS-58393")
public void testGenerateBundleExceptionHandler() throws Exception {
List<Component> componentsToCreate = Arrays.asList(new Component() {
@NonNull
@Override
public Set<Permission> getRequiredPermissions() {
return Collections.singleton(Jenkins.ADMINISTER);
}
@NonNull
@Override
public String getDisplayName() {
return "JENKINS-58393 Test";
}
@Override
public void addContents(@NonNull Container container) {
container.add(new Content("test/testGenerateBundleExceptionHandler.md") {
@Override
public void writeTo(OutputStream os) throws IOException {
os.write("test".getBytes(StandardCharsets.UTF_8));
}
@Override
public long getTime() throws IOException {
throw new IOException("JENKINS-58393: Exception should not fail the generation");
}
});
}
}, ExtensionList.lookup(Component.class).get(AboutJenkins.class), ExtensionList.lookup(Component.class).get(BuildQueue.class), ExtensionList.lookup(Component.class).get(SystemProperties.class));
File bundleFile = temp.newFile();
try (OutputStream os = Files.newOutputStream(bundleFile.toPath())) {
SupportPlugin.writeBundle(os, componentsToCreate);
}
ZipFile zip = new ZipFile(bundleFile);
assertNull(zip.getEntry("test/testGenerateBundleExceptionHandler.md"));
assertNotNull(zip.getEntry("manifest.md"));
assertNotNull(zip.getEntry("manifest/errors.txt"));
assertNotNull(zip.getEntry("buildqueue.md"));
assertNotNull(zip.getEntry("nodes/master/system.properties"));
assertNotNull(zip.getEntry("about.md"));
assertNotNull(zip.getEntry("nodes.md"));
}
use of com.cloudbees.jenkins.support.api.Content in project support-core-plugin by jenkinsci.
the class SupportTestUtils method invokeComponentToMap.
/**
* Invoke an object component, and return the component contents as a Map<String,String> where the key is the
* zip key and the value is the string content.
*/
public static <T extends AbstractModelObject> Map<String, String> invokeComponentToMap(final ObjectComponent<T> component, T object) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Map<String, String> contents = new TreeMap<>();
component.addContents(new Container() {
@Override
public void add(@CheckForNull Content content) {
try {
Objects.requireNonNull(content).writeTo(baos);
contents.put(SupportPlugin.getNameFiltered(SupportPlugin.getContentFilter(), content.getName(), content.getFilterableParameters()), baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
baos.reset();
}
}
}, object);
return contents;
}
use of com.cloudbees.jenkins.support.api.Content in project support-core-plugin by jenkinsci.
the class SupportTestUtils method invokeComponentToMap.
/**
* Invoke a component, and return the component contents as a Map<String,String> where the key is the
* zip key and the value is the string content.
*/
public static Map<String, String> invokeComponentToMap(final Component component) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Map<String, String> contents = new TreeMap<>();
component.addContents(new Container() {
@Override
public void add(@CheckForNull Content content) {
try {
Objects.requireNonNull(content).writeTo(baos);
contents.put(SupportPlugin.getNameFiltered(SupportPlugin.getContentFilter(), content.getName(), content.getFilterableParameters()), baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
baos.reset();
}
}
});
return contents;
}
use of com.cloudbees.jenkins.support.api.Content in project support-core-plugin by jenkinsci.
the class ThreadDumps method addContents.
@Override
public void addContents(@NonNull Container result) {
result.add(new Content("nodes/master/thread-dump.txt") {
@Override
public void writeTo(OutputStream os) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, "utf-8")));
try {
out.println("Master");
out.println("======");
out.println();
} finally {
out.flush();
}
try {
Timer.get().submit(new Runnable() {
@Override
public void run() {
/* OK */
}
}).get(10, TimeUnit.SECONDS);
} catch (ExecutionException | InterruptedException x) {
logger.log(Level.WARNING, null, x);
} catch (TimeoutException x) {
out.println("*WARNING*: jenkins.util.Timer is unresponsive");
}
try {
threadDump(os);
} finally {
os.flush();
}
}
});
Jenkins.get().getNodes().stream().filter(node -> node.toComputer() != null).map(Node::toComputer).forEach(computer -> addContents(result, computer));
}
use of com.cloudbees.jenkins.support.api.Content in project support-core-plugin by jenkinsci.
the class ThreadDumps method addContents.
@Override
public void addContents(@NonNull Container container, @NonNull Computer item) {
Node node = item.getNode();
if (node == null) {
return;
}
// let's start collecting thread dumps now... this gives us until the end of the bundle to finish
final Future<String> threadDump;
try {
threadDump = getThreadDump(node);
} catch (IOException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
final StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
Functions.printStackTrace(e, out);
out.close();
container.add(new StringContent("nodes/slave/{0}/thread-dump.txt", new String[] { node.getNodeName() }, sw.toString()));
return;
}
if (threadDump == null) {
StringBuilder buf = new StringBuilder();
buf.append(node.getNodeName()).append("\n");
buf.append("======\n");
buf.append("\n");
buf.append("N/A: No connection to node.\n");
container.add(new StringContent("nodes/slave/{0}/thread-dump.txt", new String[] { node.getNodeName() }, buf.toString()));
} else {
container.add(new Content("nodes/slave/{0}/thread-dump.txt", node.getNodeName()) {
@Override
public void writeTo(OutputStream os) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, "utf-8")));
try {
out.println(node.getNodeName());
out.println("======");
out.println();
String content = null;
try {
// We want to wait here a bit longer than normal
// as we will not fall back to a cache
content = threadDump.get(Math.min(SupportPlugin.REMOTE_OPERATION_TIMEOUT_MS * 8, TimeUnit.SECONDS.toMillis(SupportPlugin.REMOTE_OPERATION_CACHE_TIMEOUT_SEC)), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
Functions.printStackTrace(e, out);
} catch (ExecutionException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
Functions.printStackTrace(e, out);
} catch (TimeoutException e) {
logger.log(Level.WARNING, "Could not record thread dump for " + node.getNodeName(), e);
Functions.printStackTrace(e, out);
threadDump.cancel(true);
}
if (content != null) {
out.println(content);
}
} finally {
out.flush();
}
}
});
}
}
Aggregations