Search in sources :

Example 1 with SubversionSCM

use of hudson.scm.SubversionSCM in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinitionTest method checkoutDirectoriesAreNotReusedByDifferentScms.

@Issue("SECURITY-2463")
@Test
public void checkoutDirectoriesAreNotReusedByDifferentScms() throws Exception {
    // Checkout hook is not cross-platform.
    assumeFalse(Functions.isWindows());
    sampleRepo.init();
    sampleRepo.write("Jenkinsfile", "echo('git library')");
    sampleRepo.git("add", "Jenkinsfile");
    sampleRepo.git("commit", "--message=init");
    sampleRepoSvn.init();
    sampleRepoSvn.write("Jenkinsfile", "echo('subversion library')");
    // Copy .git folder from the Git repo into the SVN repo as data.
    File gitDirInSvnRepo = new File(sampleRepoSvn.wc(), ".git");
    FileUtils.copyDirectory(new File(sampleRepo.getRoot(), ".git"), gitDirInSvnRepo);
    String jenkinsRootDir = r.jenkins.getRootDir().toString();
    // Add a Git post-checkout hook to the .git folder in the SVN repo.
    Path postCheckoutHook = gitDirInSvnRepo.toPath().resolve("hooks/post-checkout");
    // Always create hooks directory for compatibility with https://github.com/jenkinsci/git-plugin/pull/1207.
    Files.createDirectories(postCheckoutHook.getParent());
    Files.write(postCheckoutHook, ("#!/bin/sh\ntouch '" + jenkinsRootDir + "/hook-executed'\n").getBytes(StandardCharsets.UTF_8));
    sampleRepoSvn.svnkit("add", sampleRepoSvn.wc() + "/Jenkinsfile");
    sampleRepoSvn.svnkit("add", sampleRepoSvn.wc() + "/.git");
    sampleRepoSvn.svnkit("propset", "svn:executable", "ON", sampleRepoSvn.wc() + "/.git/hooks/post-checkout");
    sampleRepoSvn.svnkit("commit", "--message=init", sampleRepoSvn.wc());
    // Run a build using the SVN repo.
    WorkflowJob p = r.createProject(WorkflowJob.class);
    p.setDefinition(new CpsScmFlowDefinition(new SubversionSCM(sampleRepoSvn.trunkUrl()), "Jenkinsfile"));
    r.buildAndAssertSuccess(p);
    // Run a build using the Git repo. It should be checked out to a different directory than the SVN repo.
    p.setDefinition(new CpsScmFlowDefinition(new GitStep(sampleRepo.toString()).createSCM(), "Jenkinsfile"));
    WorkflowRun b2 = r.buildAndAssertSuccess(p);
    assertThat(new File(r.jenkins.getRootDir(), "hook-executed"), not(anExistingFile()));
}
Also used : Path(java.nio.file.Path) SubversionSCM(hudson.scm.SubversionSCM) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) FileMatchers.anExistingFile(org.hamcrest.io.FileMatchers.anExistingFile) File(java.io.File) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) GitStep(jenkins.plugins.git.GitStep) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Aggregations

SubversionSCM (hudson.scm.SubversionSCM)1 File (java.io.File)1 Path (java.nio.file.Path)1 GitStep (jenkins.plugins.git.GitStep)1 FileMatchers.anExistingFile (org.hamcrest.io.FileMatchers.anExistingFile)1 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)1 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)1 Test (org.junit.Test)1 Issue (org.jvnet.hudson.test.Issue)1