PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/jenkins/plugins/git/GitStepTest.java

https://gitlab.com/vectorci/git-plugin
Java | 257 lines | 215 code | 13 blank | 29 comment | 0 complexity | c161855a37538c4cab821b5a3be14b56 MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright 2014 Jesse Glick.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package jenkins.plugins.git;
  25. import com.cloudbees.plugins.credentials.CredentialsProvider;
  26. import com.cloudbees.plugins.credentials.CredentialsScope;
  27. import com.cloudbees.plugins.credentials.common.IdCredentials;
  28. import com.cloudbees.plugins.credentials.domains.Domain;
  29. import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
  30. import hudson.model.Label;
  31. import hudson.plugins.git.GitSCM;
  32. import hudson.plugins.git.GitTagAction;
  33. import hudson.plugins.git.util.BuildData;
  34. import hudson.scm.ChangeLogSet;
  35. import hudson.scm.SCM;
  36. import hudson.triggers.SCMTrigger;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import jenkins.util.VirtualFile;
  40. import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
  41. import org.jenkinsci.plugins.workflow.job.WorkflowJob;
  42. import org.jenkinsci.plugins.workflow.job.WorkflowRun;
  43. import org.jenkinsci.plugins.workflow.steps.Step;
  44. import org.jenkinsci.plugins.workflow.steps.StepConfigTester;
  45. import static org.junit.Assert.*;
  46. import org.junit.ClassRule;
  47. import org.junit.Rule;
  48. import org.junit.Test;
  49. import org.jvnet.hudson.test.BuildWatcher;
  50. import org.jvnet.hudson.test.Issue;
  51. import org.jvnet.hudson.test.JenkinsRule;
  52. /**
  53. * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
  54. */
  55. public class GitStepTest {
  56. // Output build log to stderr
  57. // @ClassRule
  58. // public static BuildWatcher buildWatcher = new BuildWatcher();
  59. @Rule
  60. public JenkinsRule r = new JenkinsRule();
  61. @Rule
  62. public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
  63. @Rule
  64. public GitSampleRepoRule otherRepo = new GitSampleRepoRule();
  65. @Test
  66. public void roundtrip() throws Exception {
  67. GitStep step = new GitStep("git@github.com:jenkinsci/workflow-plugin.git");
  68. Step roundtrip = new StepConfigTester(r).configRoundTrip(step);
  69. r.assertEqualDataBoundBeans(step, roundtrip);
  70. }
  71. @Test
  72. public void roundtrip_withcredentials() throws Exception {
  73. IdCredentials c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, null, "user", "pass");
  74. CredentialsProvider.lookupStores(r.jenkins).iterator().next()
  75. .addCredentials(Domain.global(), c);
  76. GitStep step = new GitStep("git@github.com:jenkinsci/workflow-plugin.git");
  77. step.setCredentialsId(c.getId());
  78. Step roundtrip = new StepConfigTester(r).configRoundTrip(step);
  79. r.assertEqualDataBoundBeans(step, roundtrip);
  80. }
  81. @Test
  82. public void basicCloneAndUpdate() throws Exception {
  83. sampleRepo.init();
  84. WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "demo");
  85. r.createOnlineSlave(Label.get("remote"));
  86. p.setDefinition(new CpsFlowDefinition(
  87. "node('remote') {\n" +
  88. " ws {\n" +
  89. " git(url: $/" + sampleRepo + "/$, poll: false, changelog: false)\n" +
  90. " archive '**'\n" +
  91. " }\n" +
  92. "}"));
  93. WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  94. r.assertLogContains("Cloning the remote Git repository", b); // GitSCM.retrieveChanges
  95. assertTrue(b.getArtifactManager().root().child("file").isFile());
  96. sampleRepo.write("nextfile", "");
  97. sampleRepo.git("add", "nextfile");
  98. sampleRepo.git("commit", "--message=next");
  99. b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  100. r.assertLogContains("Fetching changes from the remote Git repository", b); // GitSCM.retrieveChanges
  101. assertTrue(b.getArtifactManager().root().child("nextfile").isFile());
  102. }
  103. @Test
  104. public void changelogAndPolling() throws Exception {
  105. sampleRepo.init();
  106. WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "demo");
  107. p.addTrigger(new SCMTrigger("")); // no schedule, use notifyCommit only
  108. r.createOnlineSlave(Label.get("remote"));
  109. p.setDefinition(new CpsFlowDefinition(
  110. "node('remote') {\n" +
  111. " ws {\n" +
  112. " git($/" + sampleRepo + "/$)\n" +
  113. " }\n" +
  114. "}"));
  115. WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  116. r.assertLogContains("Cloning the remote Git repository", b);
  117. sampleRepo.write("nextfile", "");
  118. sampleRepo.git("add", "nextfile");
  119. sampleRepo.git("commit", "--message=next");
  120. sampleRepo.notifyCommit(r);
  121. b = p.getLastBuild();
  122. assertEquals(2, b.number);
  123. r.assertLogContains("Fetching changes from the remote Git repository", b);
  124. List<ChangeLogSet<? extends ChangeLogSet.Entry>> changeSets = b.getChangeSets();
  125. assertEquals(1, changeSets.size());
  126. ChangeLogSet<? extends ChangeLogSet.Entry> changeSet = changeSets.get(0);
  127. assertEquals(b, changeSet.getRun());
  128. assertEquals("git", changeSet.getKind());
  129. Iterator<? extends ChangeLogSet.Entry> iterator = changeSet.iterator();
  130. assertTrue(iterator.hasNext());
  131. ChangeLogSet.Entry entry = iterator.next();
  132. assertEquals("[nextfile]", entry.getAffectedPaths().toString());
  133. assertFalse(iterator.hasNext());
  134. }
  135. @Test
  136. public void multipleSCMs() throws Exception {
  137. sampleRepo.init();
  138. otherRepo.git("init");
  139. otherRepo.write("otherfile", "");
  140. otherRepo.git("add", "otherfile");
  141. otherRepo.git("commit", "--message=init");
  142. WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "demo");
  143. p.addTrigger(new SCMTrigger(""));
  144. p.setQuietPeriod(3); // so it only does one build
  145. p.setDefinition(new CpsFlowDefinition(
  146. "node {\n" +
  147. " ws {\n" +
  148. " dir('main') {\n" +
  149. " git($/" + sampleRepo + "/$)\n" +
  150. " }\n" +
  151. " dir('other') {\n" +
  152. " git($/" + otherRepo + "/$)\n" +
  153. " }\n" +
  154. " archive '**'\n" +
  155. " }\n" +
  156. "}"));
  157. WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  158. VirtualFile artifacts = b.getArtifactManager().root();
  159. assertTrue(artifacts.child("main/file").isFile());
  160. assertTrue(artifacts.child("other/otherfile").isFile());
  161. sampleRepo.write("file2", "");
  162. sampleRepo.git("add", "file2");
  163. sampleRepo.git("commit", "--message=file2");
  164. otherRepo.write("otherfile2", "");
  165. otherRepo.git("add", "otherfile2");
  166. otherRepo.git("commit", "--message=otherfile2");
  167. sampleRepo.notifyCommit(r);
  168. otherRepo.notifyCommit(r);
  169. b = p.getLastBuild();
  170. assertEquals(2, b.number);
  171. artifacts = b.getArtifactManager().root();
  172. assertTrue(artifacts.child("main/file2").isFile());
  173. assertTrue(artifacts.child("other/otherfile2").isFile());
  174. Iterator<? extends SCM> scms = p.getSCMs().iterator();
  175. assertTrue(scms.hasNext());
  176. assertEquals(sampleRepo.toString(), ((GitSCM) scms.next()).getRepositories().get(0).getURIs().get(0).toString());
  177. assertTrue(scms.hasNext());
  178. assertEquals(otherRepo.toString(), ((GitSCM) scms.next()).getRepositories().get(0).getURIs().get(0).toString());
  179. assertFalse(scms.hasNext());
  180. List<ChangeLogSet<? extends ChangeLogSet.Entry>> changeSets = b.getChangeSets();
  181. assertEquals(2, changeSets.size());
  182. ChangeLogSet<? extends ChangeLogSet.Entry> changeSet = changeSets.get(0);
  183. assertEquals(b, changeSet.getRun());
  184. assertEquals("git", changeSet.getKind());
  185. Iterator<? extends ChangeLogSet.Entry> iterator = changeSet.iterator();
  186. assertTrue(iterator.hasNext());
  187. ChangeLogSet.Entry entry = iterator.next();
  188. assertEquals("[file2]", entry.getAffectedPaths().toString());
  189. assertFalse(iterator.hasNext());
  190. changeSet = changeSets.get(1);
  191. iterator = changeSet.iterator();
  192. assertTrue(iterator.hasNext());
  193. entry = iterator.next();
  194. assertEquals("[otherfile2]", entry.getAffectedPaths().toString());
  195. assertFalse(iterator.hasNext());
  196. }
  197. @Issue("JENKINS-29326")
  198. @Test
  199. public void identicalGitSCMs() throws Exception {
  200. otherRepo.git("init");
  201. otherRepo.write("firstfile", "");
  202. otherRepo.git("add", "firstfile");
  203. otherRepo.git("commit", "--message=init");
  204. WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "demo");
  205. p.setDefinition(new CpsFlowDefinition(
  206. "node {\n" +
  207. " dir('main') {\n" +
  208. " git($/" + otherRepo + "/$)\n" +
  209. " }\n" +
  210. " dir('other') {\n" +
  211. " git($/" + otherRepo + "/$)\n" +
  212. " }\n" +
  213. "}"));
  214. WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  215. assertEquals(1, b.getActions(BuildData.class).size());
  216. assertEquals(1, b.getActions(GitTagAction.class).size());
  217. assertEquals(0, b.getChangeSets().size());
  218. assertEquals(1, p.getSCMs().size());
  219. otherRepo.write("secondfile", "");
  220. otherRepo.git("add", "secondfile");
  221. otherRepo.git("commit", "--message=second");
  222. WorkflowRun b2 = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  223. assertEquals(1, b2.getActions(BuildData.class).size());
  224. assertEquals(1, b2.getActions(GitTagAction.class).size());
  225. assertEquals(1, b2.getChangeSets().size());
  226. assertFalse(b2.getChangeSets().get(0).isEmptySet());
  227. assertEquals(1, p.getSCMs().size());
  228. }
  229. @Test
  230. public void commitToWorkspace() throws Exception {
  231. sampleRepo.init();
  232. WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
  233. p.setDefinition(new CpsFlowDefinition(
  234. "def rungit(cmd) {def gitcmd = \"git ${cmd}\"; if (isUnix()) {sh gitcmd} else {bat gitcmd}}\n" +
  235. "node {\n" +
  236. " git url: $/" + sampleRepo + "/$\n" +
  237. " writeFile file: 'file', text: 'edited by build'\n" +
  238. " rungit 'commit --all --message=edits'\n" +
  239. " rungit 'show master'\n" +
  240. "}"));
  241. WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
  242. r.assertLogContains("+edited by build", b);
  243. }
  244. }