Search in sources :

Example 1 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class CopyArtifactTest method testPermission.

/**
 * Test that a user is prevented from bypassing permissions on other jobs
 */
@Test
public void testPermission() throws Exception {
    // any users can be authenticated with the password same to the user id.
    rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
    MockAuthorizationStrategy auth = new MockAuthorizationStrategy().grant(Jenkins.READ, Item.BUILD, Computer.BUILD).everywhere().toEveryone();
    rule.jenkins.setAuthorizationStrategy(auth);
    // only joe can access project "src"
    FreeStyleProject src = rule.createFreeStyleProject();
    src.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
    src.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
    auth.grant(Item.READ).onItems(src).to("joe");
    rule.assertBuildStatusSuccess(src.scheduleBuild2(0));
    // test access from anonymous
    {
        FreeStyleProject dest = rule.createFreeStyleProject();
        dest.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(src.getName(), "", new StatusBuildSelector(true), "", "", false, false, true));
        auth.grant(Item.READ, Item.CONFIGURE).onItems(dest).toEveryone();
        WebClient wc = rule.createWebClient();
        try {
            wc.getPage(src);
            fail("Job should not be accessible to anonymous");
        } catch (FailingHttpStatusCodeException e) {
            assertEquals("Job should not be accessible to anonymous", 404, e.getStatusCode());
        }
        rule.submit(wc.getPage(dest, "configure").getFormByName("config"));
        dest = rule.jenkins.getItemByFullName(dest.getFullName(), FreeStyleProject.class);
        CopyArtifact ca = dest.getBuildersList().getAll(CopyArtifact.class).get(0);
        // Preserves the configuration as-is in Production mode.
        assertEquals(src.getName(), ca.getProjectName());
        // Instead, the build will fail.
        rule.assertBuildStatus(Result.FAILURE, dest.scheduleBuild2(0));
    }
    // test access from joe
    {
        FreeStyleProject dest = rule.createFreeStyleProject();
        dest.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(src.getName(), "", new StatusBuildSelector(true), "", "", false, false, true));
        auth.grant(Item.READ, Item.CONFIGURE).onItems(dest).to("joe");
        WebClient wc = rule.createWebClient();
        wc.login("joe", "joe");
        assertNotNull(wc.getPage(src));
        rule.submit(wc.getPage(dest, "configure").getFormByName("config"));
        dest = rule.jenkins.getItemByFullName(dest.getFullName(), FreeStyleProject.class);
        CopyArtifact ca = dest.getBuildersList().getAll(CopyArtifact.class).get(0);
        assertEquals(src.getName(), ca.getProjectName());
        // Build should succeed when run as joe.
        Map<String, Authentication> authMap = new HashMap<>();
        authMap.put(dest.getFullName(), User.getById("joe", true).impersonate());
        QueueItemAuthenticatorConfiguration.get().getAuthenticators().clear();
        QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(authMap));
        rule.assertBuildStatusSuccess(dest.scheduleBuild2(0));
    }
}
Also used : MockQueueItemAuthenticator(org.jvnet.hudson.test.MockQueueItemAuthenticator) ArtifactArchiver(hudson.tasks.ArtifactArchiver) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) MockAuthorizationStrategy(org.jvnet.hudson.test.MockAuthorizationStrategy) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class CopyArtifactTest method testEnvDataWrapped.

@Issue("JENKINS-18762")
@Test
public void testEnvDataWrapped() throws Exception {
    FreeStyleProject upstream = rule.createFreeStyleProject("upstream");
    upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
    upstream.getPublishersList().add(new ArtifactArchiver("**/*", "", false, false));
    FreeStyleBuild upstreamBuild = upstream.scheduleBuild2(0).get();
    rule.assertBuildStatusSuccess(upstreamBuild);
    FreeStyleProject downstream = rule.createFreeStyleProject();
    downstream.getBuildersList().add(new WrapperBuilder(CopyArtifactUtil.createCopyArtifact("upstream", "", new SpecificBuildSelector(Integer.toString(upstreamBuild.getNumber())), "**/*", "", "", false, false, false)));
    CaptureEnvironmentBuilder envStep = new CaptureEnvironmentBuilder();
    downstream.getBuildersList().add(envStep);
    FreeStyleBuild downstreamBuild = downstream.scheduleBuild2(0).get();
    rule.assertBuildStatusSuccess(downstreamBuild);
    assertFile(true, "artifact.txt", downstreamBuild);
    assertEquals(Integer.toString(upstreamBuild.getNumber()), envStep.getEnvVars().get("COPYARTIFACT_BUILD_NUMBER_UPSTREAM"));
}
Also used : WrapperBuilder(hudson.plugins.copyartifact.testutils.WrapperBuilder) ArtifactArchiver(hudson.tasks.ArtifactArchiver) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) CaptureEnvironmentBuilder(org.jvnet.hudson.test.CaptureEnvironmentBuilder) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 3 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class CopyArtifactWorkflowTest method testWorkspaceBuildSelector.

@Test
public void testWorkspaceBuildSelector() throws Exception {
    // workspace can copy only from `AbstractProject`s
    FreeStyleProject copiee = jenkinsRule.createFreeStyleProject("copiee");
    copiee.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
    copiee.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
    jenkinsRule.assertBuildStatusSuccess(copiee.scheduleBuild2(0));
    WorkflowJob copier = jenkinsRule.createWorkflow("copier", "copyArtifacts(projectName: 'copiee', selector: workspace());" + "echo readFile('artifact.txt');");
    jenkinsRule.assertLogContains("foobar", jenkinsRule.assertBuildStatusSuccess(copier.scheduleBuild2(0)));
}
Also used : ArtifactArchiver(hudson.tasks.ArtifactArchiver) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) FreeStyleProject(hudson.model.FreeStyleProject) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) Test(org.junit.Test)

Example 4 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class DownstreamBuildSelectorTest method testUpstreamIsWorkflow.

@Test
public void testUpstreamIsWorkflow() throws Exception {
    WorkflowJob upstream = j.jenkins.createProject(WorkflowJob.class, "upstream");
    upstream.setDefinition(new CpsFlowDefinition("node {" + "writeFile text: \"${env.BUILD_TAG}\", file: 'upstream_artifact.txt'; " + "step([$class: 'ArtifactArchiver', artifacts: 'upstream_artifact.txt'])" + "}", true));
    WorkflowRun upstreamBuild = j.assertBuildStatusSuccess(upstream.scheduleBuild2(0));
    FreeStyleProject downstream = j.createFreeStyleProject();
    CopyArtifact ca = new CopyArtifact(upstream.getFullName());
    ca.setFingerprintArtifacts(true);
    ca.setFilter("upstream_artifact.txt");
    downstream.getBuildersList().add(ca);
    downstream.getBuildersList().add(new FileWriteBuilder("downstream_artifact.txt", "${BUILD_TAG}"));
    ArtifactArchiver aa = new ArtifactArchiver("downstream_artifact.txt");
    aa.setAllowEmptyArchive(false);
    aa.setFingerprint(true);
    downstream.getPublishersList().add(aa);
    FreeStyleBuild downstreamBuild = j.assertBuildStatusSuccess(downstream.scheduleBuild2(0));
    FreeStyleProject p = j.createFreeStyleProject();
    p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(downstream.getFullName(), "", new DownstreamBuildSelector(upstream.getFullName(), Integer.toString(upstreamBuild.getNumber())), "**/*", "", "", false, false, true));
    // fail as DownstreamBuildSelector doesn't support workflow upstream.
    FreeStyleBuild b = j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0).get());
// to see expected log is recorded.
// System.out.println(b.getLog());
}
Also used : ArtifactArchiver(hudson.tasks.ArtifactArchiver) CpsFlowDefinition(org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) FreeStyleBuild(hudson.model.FreeStyleBuild) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FreeStyleProject(hudson.model.FreeStyleProject) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) Test(org.junit.Test)

Example 5 with FileWriteBuilder

use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.

the class DownstreamBuildSelectorTest method testPerformSuccess.

@Test
public void testPerformSuccess() throws Exception {
    FreeStyleProject upstream = j.createFreeStyleProject();
    FreeStyleProject downstream = j.createFreeStyleProject();
    upstream.getBuildersList().add(new FileWriteBuilder("artifact.txt", "${BUILD_TAG}"));
    upstream.getPublishersList().add(new ArtifactArchiver("artifact.txt", "", false, false));
    upstream.getPublishersList().add(new Fingerprinter("", true));
    upstream.getPublishersList().add(new BuildTrigger(downstream.getFullName(), Result.SUCCESS.toString()));
    downstream.getBuildersList().add(new FileWriteBuilder("artifact2.txt", "${BUILD_ID}"));
    downstream.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(upstream.getFullName(), "", new TriggeredBuildSelector(false, TriggeredBuildSelector.UpstreamFilterStrategy.UseNewest, false), "**/*", "", "", false, false, // important! required to have Jenkins track builds.
    true));
    downstream.getPublishersList().add(new ArtifactArchiver("artifact2.txt", "", false, false));
    upstream.save();
    downstream.save();
    j.jenkins.rebuildDependencyGraph();
    ;
    // upstreamBuild1 -> downstreamBuild1
    // upstreamBuild2 -> downstreamBuild2
    // upstreamBuild3 -> downstreamBuild3
    FreeStyleBuild upstreamBuild1 = upstream.scheduleBuild2(0).get();
    upstreamBuild1.setDisplayName("upstreamBuild1");
    j.waitUntilNoActivity();
    FreeStyleBuild downstreamBuild1 = downstream.getLastBuild();
    assertEquals(upstreamBuild1, downstreamBuild1.getUpstreamRelationshipBuild(upstream));
    j.assertBuildStatusSuccess(upstreamBuild1);
    j.assertBuildStatusSuccess(downstreamBuild1);
    FreeStyleBuild upstreamBuild2 = upstream.scheduleBuild2(0).get();
    upstreamBuild2.setDisplayName("upstreamBuild2");
    j.waitUntilNoActivity();
    FreeStyleBuild downstreamBuild2 = downstream.getLastBuild();
    assertEquals(upstreamBuild2, downstreamBuild2.getUpstreamRelationshipBuild(upstream));
    j.assertBuildStatusSuccess(upstreamBuild2);
    j.assertBuildStatusSuccess(downstreamBuild2);
    FreeStyleBuild upstreamBuild3 = upstream.scheduleBuild2(0).get();
    upstreamBuild3.setDisplayName("upstreamBuild3");
    j.waitUntilNoActivity();
    FreeStyleBuild downstreamBuild3 = downstream.getLastBuild();
    assertEquals(upstreamBuild3, downstreamBuild3.getUpstreamRelationshipBuild(upstream));
    j.assertBuildStatusSuccess(upstreamBuild3);
    j.assertBuildStatusSuccess(downstreamBuild3);
    // copies from downstream2, which is a downstream of upstreamBuild2.
    // specify with a build number.
    // not use variables.
    {
        FreeStyleProject p = j.createFreeStyleProject();
        p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(downstream.getFullName(), "", new DownstreamBuildSelector(upstream.getFullName(), Integer.toString(upstreamBuild2.getNumber())), "**/*", "", "", false, false, true));
        FreeStyleBuild b = p.scheduleBuild2(0).get();
        j.assertBuildStatusSuccess(b);
        FilePath artifact = b.getWorkspace().child("artifact2.txt");
        assertTrue(artifact.exists());
        assertEquals(downstreamBuild2.getId(), artifact.readToString());
    }
    // copies from downstream1, which is a downstream of upstreamBuild1.
    // specify with a build id.
    // use variables.
    {
        FreeStyleProject p = j.createFreeStyleProject();
        p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("UPSTREAM_PROJECT_NAME", ""), new StringParameterDefinition("UPSTREAM_BUILD_NUMBER", "")));
        p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(downstream.getFullName(), "", new DownstreamBuildSelector("${UPSTREAM_PROJECT_NAME}", "${UPSTREAM_BUILD_NUMBER}"), "**/*", "", "", false, false, true));
        FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("UPSTREAM_PROJECT_NAME", upstream.getFullName()), new StringParameterValue("UPSTREAM_BUILD_NUMBER", upstreamBuild1.getId()))).get();
        j.assertBuildStatusSuccess(b);
        FilePath artifact = b.getWorkspace().child("artifact2.txt");
        assertTrue(artifact.exists());
        assertEquals(downstreamBuild1.getId(), artifact.readToString());
    }
    // copies from downstream3, which is a downstream of upstreamBuild3.
    // specify with a display name.
    // use variables.
    {
        FreeStyleProject p = j.createFreeStyleProject();
        p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("UPSTREAM_PROJECT_NAME", ""), new StringParameterDefinition("UPSTREAM_BUILD_NUMBER", "")));
        p.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(downstream.getFullName(), "", new DownstreamBuildSelector("${UPSTREAM_PROJECT_NAME}", "${UPSTREAM_BUILD_NUMBER}"), "**/*", "", "", false, false, true));
        FreeStyleBuild b = p.scheduleBuild2(0, new Cause.UserCause(), new ParametersAction(new StringParameterValue("UPSTREAM_PROJECT_NAME", upstream.getFullName()), new StringParameterValue("UPSTREAM_BUILD_NUMBER", "upstreamBuild3"))).get();
        j.assertBuildStatusSuccess(b);
        FilePath artifact = b.getWorkspace().child("artifact2.txt");
        assertTrue(artifact.exists());
        assertEquals(downstreamBuild3.getId(), artifact.readToString());
    }
}
Also used : FilePath(hudson.FilePath) ArtifactArchiver(hudson.tasks.ArtifactArchiver) StringParameterDefinition(hudson.model.StringParameterDefinition) StringParameterValue(hudson.model.StringParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) ParametersAction(hudson.model.ParametersAction) Fingerprinter(hudson.tasks.Fingerprinter) ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) FileWriteBuilder(hudson.plugins.copyartifact.testutils.FileWriteBuilder) Cause(hudson.model.Cause) BuildTrigger(hudson.tasks.BuildTrigger) Test(org.junit.Test)

Aggregations

FileWriteBuilder (hudson.plugins.copyartifact.testutils.FileWriteBuilder)37 ArtifactArchiver (hudson.tasks.ArtifactArchiver)37 Test (org.junit.Test)32 FreeStyleProject (hudson.model.FreeStyleProject)26 FreeStyleBuild (hudson.model.FreeStyleBuild)17 ParametersDefinitionProperty (hudson.model.ParametersDefinitionProperty)15 StringParameterDefinition (hudson.model.StringParameterDefinition)15 ParametersAction (hudson.model.ParametersAction)13 StringParameterValue (hudson.model.StringParameterValue)13 ParameterDefinition (hudson.model.ParameterDefinition)11 BuildTrigger (hudson.tasks.BuildTrigger)11 MockAuthorizationStrategy (org.jvnet.hudson.test.MockAuthorizationStrategy)11 Cause (hudson.model.Cause)10 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)9 CpsFlowDefinition (org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition)6 CopyArtifact (hudson.plugins.copyartifact.CopyArtifact)4 File (java.io.File)4 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)4 Axis (hudson.matrix.Axis)3 AxisList (hudson.matrix.AxisList)3