Search in sources :

Example 1 with GitStep

use of jenkins.plugins.git.GitStep in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinitionTest method emptyChangeLogEmptyChangeSets.

@Issue("JENKINS-29881")
@Test
public void emptyChangeLogEmptyChangeSets() throws Exception {
    sampleRepo.init();
    sampleRepo.write("flow.groovy", "echo 'version one'");
    sampleRepo.git("add", "flow.groovy");
    sampleRepo.git("commit", "--message=init");
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitStep(sampleRepo.toString()).createSCM(), "flow.groovy");
    def.setLightweight(false);
    p.setDefinition(def);
    WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
    r.assertLogContains("Cloning the remote Git repository", b);
    r.assertLogContains("version one", b);
    r.assertLogNotContains("Retrying after 10 seconds", b);
    b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
    assertEquals(2, b.number);
    List<ChangeLogSet<? extends ChangeLogSet.Entry>> changeSets = b.getChangeSets();
    assertEquals(Collections.emptyList(), changeSets);
}
Also used : ChangeLogSet(hudson.scm.ChangeLogSet) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) GitStep(jenkins.plugins.git.GitStep) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 2 with GitStep

use of jenkins.plugins.git.GitStep in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinitionTest method configRoundtrip.

@Test
public void configRoundtrip() throws Exception {
    sampleRepo.init();
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitStep(sampleRepo.toString()).createSCM(), "Jenkinsfile");
    def.setLightweight(true);
    p.setDefinition(def);
    r.configRoundtrip(p);
    def = (CpsScmFlowDefinition) p.getDefinition();
    assertEquals("Jenkinsfile", def.getScriptPath());
    assertTrue(def.isLightweight());
    assertEquals(GitSCM.class, def.getScm().getClass());
}
Also used : WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) GitStep(jenkins.plugins.git.GitStep) Test(org.junit.Test)

Example 3 with GitStep

use of jenkins.plugins.git.GitStep in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinitionTest method scriptPathSymlinksCannotEscapeCheckoutDirectory.

@Issue("SECURITY-2595")
@Test
public void scriptPathSymlinksCannotEscapeCheckoutDirectory() throws Exception {
    // On Windows, the symlink is treated as a regular file, so there is no vulnerability, but the error message is different.
    assumeFalse(Functions.isWindows());
    sampleRepo.init();
    Path secrets = Paths.get(sampleRepo.getRoot().getPath(), "Jenkinsfile");
    Files.createSymbolicLink(secrets, Paths.get(r.jenkins.getRootDir() + "/secrets/master.key"));
    sampleRepo.git("add", ".");
    sampleRepo.git("commit", "-m", "init");
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    GitStep step = new GitStep(sampleRepo.toString());
    p.setDefinition(new CpsScmFlowDefinition(step.createSCM(), "Jenkinsfile"));
    WorkflowRun b = r.buildAndAssertStatus(Result.FAILURE, p);
    assertThat(b.getExecution(), nullValue());
    r.assertLogContains("Jenkinsfile references a file that is not inside " + r.jenkins.getWorkspaceFor(p), b);
}
Also used : Path(java.nio.file.Path) WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) GitStep(jenkins.plugins.git.GitStep) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 4 with GitStep

use of jenkins.plugins.git.GitStep in project workflow-cps-plugin by jenkinsci.

the class CpsScmFlowDefinitionTest method retry.

@Issue("JENKINS-39194")
@Test
public void retry() throws Exception {
    // We use an un-initialized repo here to test retry
    WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
    GitStep step = new GitStep(invalidRepo.toString());
    CpsScmFlowDefinition def = new CpsScmFlowDefinition(step.createSCM(), "flow.groovy");
    def.setLightweight(false);
    p.setDefinition(def);
    r.jenkins.setScmCheckoutRetryCount(1);
    WorkflowRun b = r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0));
    r.assertLogContains("Could not read from remote repository", b);
    r.assertLogContains("Retrying after 10 seconds", b);
}
Also used : WorkflowJob(org.jenkinsci.plugins.workflow.job.WorkflowJob) WorkflowRun(org.jenkinsci.plugins.workflow.job.WorkflowRun) GitStep(jenkins.plugins.git.GitStep) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 5 with GitStep

use of jenkins.plugins.git.GitStep 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

GitStep (jenkins.plugins.git.GitStep)8 WorkflowJob (org.jenkinsci.plugins.workflow.job.WorkflowJob)8 Test (org.junit.Test)8 WorkflowRun (org.jenkinsci.plugins.workflow.job.WorkflowRun)7 Issue (org.jvnet.hudson.test.Issue)6 ChangeLogSet (hudson.scm.ChangeLogSet)2 Path (java.nio.file.Path)2 SubversionSCM (hudson.scm.SubversionSCM)1 SCMTrigger (hudson.triggers.SCMTrigger)1 File (java.io.File)1 FileMatchers.anExistingFile (org.hamcrest.io.FileMatchers.anExistingFile)1 TestDurabilityHintProvider (org.jenkinsci.plugins.workflow.TestDurabilityHintProvider)1