Search in sources :

Example 1 with WebClient

use of org.jvnet.hudson.test.JenkinsRule.WebClient in project promoted-builds-plugin by jenkinsci.

the class ManualConditionTest method testManualPromotionPermissionsViaWebClient.

@Test
// TODO figure out a good way to test this with SECURITY-2293
@Ignore
public void testManualPromotionPermissionsViaWebClient() throws Exception {
    enableSecurity(j);
    FreeStyleProject p = j.createFreeStyleProject();
    PromotionProcess pp = addPromotionProcess(p, "foo");
    WebClient wc = j.createWebClient();
    FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
    ManualCondition cond = new ManualCondition();
    pp.conditions.add(cond);
    j.assertBuildStatusSuccess(cond.approve(b, pp, Collections.EMPTY_LIST));
    assertThat(b.getAction(ManualApproval.class), notNullValue());
    {
        // Re-execute promotion as user without Promotion/Promote when no users are specified
        wc.login("non-promoter", "non-promoter");
        // Status#doBuild does a bare `return;` without scheduling the build in this case, which is why we use goTo with "" for the MIME type.
        wc.goTo(String.format("job/%s/%d/promotion/%s/build?json={}", p.getName(), b.getNumber(), pp.getName()), "");
        assertThat(pp.getBuildByNumber(2), nullValue());
    }
    {
        // Re-execute promotion as user with Promotion/Promote when no users are specified
        wc.login("promoter", "promoter");
        try {
            wc.getPage(b, String.format("promotion/%s/build?json={}", pp.getName()));
            fail();
        } catch (FailingHttpStatusCodeException e) {
            // Redirect after the build is broken.
            assertThat(e.getStatusCode(), equalTo(404));
        }
        assertThat(waitForBuildByNumber(pp, 2).getResult(), equalTo(Result.SUCCESS));
    }
    {
        // Re-execute promotion as specified user without Promotion/Promote
        cond.setUsers("non-promoter");
        wc.login("non-promoter", "non-promoter");
        try {
            wc.getPage(b, String.format("promotion/%s/build?json={}", pp.getName()));
            fail();
        } catch (FailingHttpStatusCodeException e) {
            // Redirect after the build is broken.
            assertThat(e.getStatusCode(), equalTo(404));
        }
        assertThat(waitForBuildByNumber(pp, 3).getResult(), equalTo(Result.SUCCESS));
    }
    {
        // Re-execute promotion as unspecified user with Promotion/Promote
        cond.setUsers("non-promoter");
        wc.login("promoter", "promoter");
        // Status#doBuild does a bare `return;` without scheduling the build in this case, which is why we use goTo with "" for the MIME type.
        wc.goTo(String.format("job/%s/%d/promotion/%s/build?json={}", p.getName(), b.getNumber(), pp.getName()), "");
        assertThat(pp.getBuildByNumber(4), nullValue());
    }
}
Also used : ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with WebClient

use of org.jvnet.hudson.test.JenkinsRule.WebClient in project support-core-plugin by jenkinsci.

the class SupportActionTest method doBundle.

private WebResponse doBundle(String action, String bundle, String user, String extraBundle) throws IOException {
    j.jenkins.setCrumbIssuer(null);
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
    j.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin").grant(Jenkins.READ).everywhere().to("user"));
    WebClient wc = j.createWebClient().withBasicCredentials(user).withThrowExceptionOnFailingStatusCode(false);
    String json = "?json={%22bundles%22:[{%22selected%22:+true,%22name%22:+%22" + bundle + "%22}]}";
    if (extraBundle != null) {
        json = "?json={%22bundles%22:[{%22selected%22:+true,%22name%22:+%22" + bundle + "%22},{%22selected%22:+true,%22name%22:+%22" + extraBundle + "%22}]}";
    }
    WebRequest request = new WebRequest(new URL(j.getURL() + root.getUrlName() + "/" + action + json), HttpMethod.POST);
    return wc.getPage(request).getWebResponse();
}
Also used : WebRequest(com.gargoylesoftware.htmlunit.WebRequest) MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) URL(java.net.URL)

Example 3 with WebClient

use of org.jvnet.hudson.test.JenkinsRule.WebClient 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)

Aggregations

WebClient (org.jvnet.hudson.test.JenkinsRule.WebClient)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 Page (com.gargoylesoftware.htmlunit.Page)1 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)1 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 FreeStyleBuild (hudson.model.FreeStyleBuild)1 FreeStyleProject (hudson.model.FreeStyleProject)1 PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)1 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)1 RingBufferLogHandler (hudson.util.RingBufferLogHandler)1 File (java.io.File)1 URL (java.net.URL)1 LogRecord (java.util.logging.LogRecord)1 Logger (java.util.logging.Logger)1 ZipFile (java.util.zip.ZipFile)1 Ignore (org.junit.Ignore)1