From 646b3d58647012025e1f94081a7ce5be7ded65b3 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Sat, 21 Feb 2015 13:16:53 -0500 Subject: [PATCH] [JENKINS-26761] Verifying that polling still works after a restart. --- .../steps/scm/GitStepRestartTest.java | 104 ++++++++++++++++++ .../workflow/steps/scm/GitStepTest.java | 9 +- 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepRestartTest.java diff --git a/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepRestartTest.java b/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepRestartTest.java new file mode 100644 index 000000000..f81b441f7 --- /dev/null +++ b/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepRestartTest.java @@ -0,0 +1,104 @@ +/* + * The MIT License + * + * Copyright 2015 Jesse Glick. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jenkinsci.plugins.workflow.steps.scm; + +import hudson.model.Label; +import hudson.scm.ChangeLogSet; +import hudson.triggers.SCMTrigger; +import java.io.File; +import java.net.URLEncoder; +import java.util.Iterator; +import java.util.List; +import org.apache.commons.io.FileUtils; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import static org.jenkinsci.plugins.workflow.steps.scm.GitStepTest.createSampleRepo; +import static org.jenkinsci.plugins.workflow.steps.scm.GitStepTest.git; +import org.junit.Test; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TemporaryFolder; +import org.junit.runners.model.Statement; +import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.RestartableJenkinsRule; + +public class GitStepRestartTest { + + @Rule public RestartableJenkinsRule r = new RestartableJenkinsRule(); + @Rule public TemporaryFolder tmp = new TemporaryFolder(); + + private File sampleRepo; + + @Before public void sampleRepo() throws Exception { + sampleRepo = createSampleRepo(tmp); + } + + @Issue("JENKINS-26761") + @Test public void checkoutsRestored() throws Exception { + r.addStep(new Statement() { + @Override public void evaluate() throws Throwable { + WorkflowJob p = r.j.jenkins.createProject(WorkflowJob.class, "p"); + p.addTrigger(new SCMTrigger("")); + r.j.createOnlineSlave(Label.get("remote")); + p.setDefinition(new CpsFlowDefinition( + "node('remote') {\n" + + " ws {\n" + + " git '" + sampleRepo + "'\n" + + " }\n" + + "}")); + p.save(); + WorkflowRun b = r.j.assertBuildStatusSuccess(p.scheduleBuild2(0)); + r.j.assertLogContains("Cloning the remote Git repository", b); + } + }); + r.addStep(new Statement() { + @Override public void evaluate() throws Throwable { + WorkflowJob p = r.j.jenkins.getItemByFullName("p", WorkflowJob.class); + r.j.createOnlineSlave(Label.get("remote")); + FileUtils.touch(new File(sampleRepo, "nextfile")); + git(sampleRepo, "add", "nextfile"); + git(sampleRepo, "commit", "--message=next"); + System.out.println(r.j.createWebClient().goTo("git/notifyCommit?url=" + URLEncoder.encode(sampleRepo.getAbsolutePath(), "UTF-8"), "text/plain").getWebResponse().getContentAsString()); + r.j.waitUntilNoActivity(); + WorkflowRun b = p.getLastBuild(); + assertEquals(2, b.number); + r.j.assertLogContains("Cloning the remote Git repository", b); // new slave, new workspace + List> changeSets = b.getChangeSets(); + assertEquals(1, changeSets.size()); + ChangeLogSet changeSet = changeSets.get(0); + assertEquals(b, changeSet.getRun()); + assertEquals("git", changeSet.getKind()); + Iterator iterator = changeSet.iterator(); + assertTrue(iterator.hasNext()); + ChangeLogSet.Entry entry = iterator.next(); + assertEquals("[nextfile]", entry.getAffectedPaths().toString()); + assertFalse(iterator.hasNext()); + } + }); + } + +} diff --git a/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepTest.java b/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepTest.java index 806189fc0..26eb88469 100644 --- a/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepTest.java +++ b/aggregator/src/test/java/org/jenkinsci/plugins/workflow/steps/scm/GitStepTest.java @@ -53,7 +53,7 @@ public class GitStepTest { private File sampleRepo; - private static void git(File repo, String... cmds) throws Exception { + static void git(File repo, String... cmds) throws Exception { List args = new ArrayList(); args.add("git"); args.addAll(Arrays.asList(cmds)); @@ -66,11 +66,16 @@ private static void git(File repo, String... cmds) throws Exception { } @Before public void sampleRepo() throws Exception { - sampleRepo = tmp.newFolder(); + sampleRepo = createSampleRepo(tmp); + } + + static File createSampleRepo(TemporaryFolder tmp) throws Exception { + File sampleRepo = tmp.newFolder(); git(sampleRepo, "init"); FileUtils.touch(new File(sampleRepo, "file")); git(sampleRepo, "add", "file"); git(sampleRepo, "commit", "--message=init"); + return sampleRepo; } @Test public void basicCloneAndUpdate() throws Exception {