Search in sources :

Example 1 with Content

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"));
}
Also used : BuildQueue(com.cloudbees.jenkins.support.impl.BuildQueue) Set(java.util.Set) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Container(com.cloudbees.jenkins.support.api.Container) SystemProperties(com.cloudbees.jenkins.support.impl.SystemProperties) ZipFile(java.util.zip.ZipFile) Content(com.cloudbees.jenkins.support.api.Content) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Component(com.cloudbees.jenkins.support.api.Component) AboutJenkins(com.cloudbees.jenkins.support.impl.AboutJenkins) ZipFile(java.util.zip.ZipFile) File(java.io.File) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 2 with Content

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;
}
Also used : Container(com.cloudbees.jenkins.support.api.Container) PrefilteredContent(com.cloudbees.jenkins.support.filter.PrefilteredContent) Content(com.cloudbees.jenkins.support.api.Content) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap)

Example 3 with Content

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;
}
Also used : Container(com.cloudbees.jenkins.support.api.Container) PrefilteredContent(com.cloudbees.jenkins.support.filter.PrefilteredContent) Content(com.cloudbees.jenkins.support.api.Content) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap)

Example 4 with Content

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));
}
Also used : StringContent(com.cloudbees.jenkins.support.api.StringContent) Content(com.cloudbees.jenkins.support.api.Content) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with Content

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();
                }
            }
        });
    }
}
Also used : Node(hudson.model.Node) StringContent(com.cloudbees.jenkins.support.api.StringContent) StringContent(com.cloudbees.jenkins.support.api.StringContent) Content(com.cloudbees.jenkins.support.api.Content) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Content (com.cloudbees.jenkins.support.api.Content)11 Container (com.cloudbees.jenkins.support.api.Container)8 IOException (java.io.IOException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrefilteredContent (com.cloudbees.jenkins.support.filter.PrefilteredContent)4 Test (org.junit.Test)4 StringContent (com.cloudbees.jenkins.support.api.StringContent)3 Component (com.cloudbees.jenkins.support.api.Component)2 BufferedOutputStream (java.io.BufferedOutputStream)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 TreeMap (java.util.TreeMap)2 TimeoutException (java.util.concurrent.TimeoutException)2 SupportProvider (com.cloudbees.jenkins.support.api.SupportProvider)1 UnfilteredStringContent (com.cloudbees.jenkins.support.api.UnfilteredStringContent)1 ContentFilter (com.cloudbees.jenkins.support.filter.ContentFilter)1 FilteredOutputStream (com.cloudbees.jenkins.support.filter.FilteredOutputStream)1 AboutJenkins (com.cloudbees.jenkins.support.impl.AboutJenkins)1