|
| 1 | +package hudson.plugins.git; |
| 2 | + |
| 3 | +import com.cloudbees.plugins.credentials.*; |
| 4 | +import com.cloudbees.plugins.credentials.common.StandardCredentials; |
| 5 | +import com.cloudbees.plugins.credentials.domains.Domain; |
| 6 | +import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; |
| 7 | +import jenkins.model.Jenkins; |
| 8 | +import jenkins.plugins.git.GitSampleRepoRule; |
| 9 | +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
| 10 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 11 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Rule; |
| 14 | +import org.junit.Test; |
| 15 | +import org.jvnet.hudson.test.Issue; |
| 16 | +import org.jvnet.hudson.test.JenkinsRule; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | + |
| 20 | +import static org.hamcrest.Matchers.notNullValue; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | + |
| 24 | +public class CredentialsUserRemoteConfigTest { |
| 25 | + |
| 26 | + @Rule |
| 27 | + public JenkinsRule r = new JenkinsRule(); |
| 28 | + |
| 29 | + @Rule |
| 30 | + public GitSampleRepoRule sampleRepo = new GitSampleRepoRule(); |
| 31 | + |
| 32 | + private CredentialsStore store = null; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void enableSystemCredentialsProvider() { |
| 36 | + SystemCredentialsProvider.getInstance().setDomainCredentialsMap( |
| 37 | + Collections.singletonMap(Domain.global(), Collections.<Credentials>emptyList())); |
| 38 | + for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.getInstance())) { |
| 39 | + if (s.getProvider() instanceof SystemCredentialsProvider.ProviderImpl) { |
| 40 | + store = s; |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + assertThat("The system credentials provider is enabled", store, notNullValue()); |
| 45 | + } |
| 46 | + |
| 47 | + @Issue("JENKINS-30515") |
| 48 | + @Test |
| 49 | + public void checkoutWithValidCredentials() throws Exception { |
| 50 | + sampleRepo.init(); |
| 51 | + store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "github")); |
| 52 | + store.save(); |
| 53 | + |
| 54 | + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); |
| 55 | + p.setDefinition(new CpsFlowDefinition( |
| 56 | + "node {\n" |
| 57 | + + " checkout(\n" |
| 58 | + + " [$class: 'GitSCM', \n" |
| 59 | + + " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n" |
| 60 | + + " )" |
| 61 | + + "}")); |
| 62 | + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); |
| 63 | + r.assertLogContains("using credential github", b); |
| 64 | + } |
| 65 | + |
| 66 | + @Issue("JENKINS-30515") |
| 67 | + @Test |
| 68 | + public void checkoutWithDifferentCredentials() throws Exception { |
| 69 | + sampleRepo.init(); |
| 70 | + store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "other")); |
| 71 | + store.save(); |
| 72 | + |
| 73 | + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); |
| 74 | + p.setDefinition(new CpsFlowDefinition( |
| 75 | + "node {\n" |
| 76 | + + " checkout(\n" |
| 77 | + + " [$class: 'GitSCM', \n" |
| 78 | + + " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n" |
| 79 | + + " )" |
| 80 | + + "}")); |
| 81 | + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); |
| 82 | + System.out.println(JenkinsRule.getLog(b)); |
| 83 | + r.assertLogContains("Warning: CredentialId \"github\" could not be found", b); |
| 84 | + } |
| 85 | + |
| 86 | + @Issue("JENKINS-30515") |
| 87 | + @Test |
| 88 | + public void checkoutWithInvalidCredentials() throws Exception { |
| 89 | + sampleRepo.init(); |
| 90 | + store.addCredentials(Domain.global(), createCredential(CredentialsScope.SYSTEM, "github")); |
| 91 | + store.save(); |
| 92 | + |
| 93 | + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); |
| 94 | + p.setDefinition(new CpsFlowDefinition( |
| 95 | + "node {\n" |
| 96 | + + " checkout(\n" |
| 97 | + + " [$class: 'GitSCM', \n" |
| 98 | + + " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n" |
| 99 | + + " )" |
| 100 | + + "}")); |
| 101 | + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); |
| 102 | + r.assertLogContains("Warning: CredentialId \"github\" could not be found", b); |
| 103 | + } |
| 104 | + |
| 105 | + @Issue("JENKINS-30515") |
| 106 | + @Test |
| 107 | + public void checkoutWithNoCredentialsStoredButUsed() throws Exception { |
| 108 | + sampleRepo.init(); |
| 109 | + |
| 110 | + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); |
| 111 | + p.setDefinition(new CpsFlowDefinition( |
| 112 | + "node {\n" |
| 113 | + + " checkout(\n" |
| 114 | + + " [$class: 'GitSCM', \n" |
| 115 | + + " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n" |
| 116 | + + " )" |
| 117 | + + "}")); |
| 118 | + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); |
| 119 | + System.out.println(JenkinsRule.getLog(b)); |
| 120 | + r.assertLogContains("Warning: CredentialId \"github\" could not be found", b); |
| 121 | + } |
| 122 | + |
| 123 | + @Issue("JENKINS-30515") |
| 124 | + @Test |
| 125 | + public void checkoutWithNoCredentialsSpecified() throws Exception { |
| 126 | + sampleRepo.init(); |
| 127 | + |
| 128 | + WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); |
| 129 | + p.setDefinition(new CpsFlowDefinition( |
| 130 | + "node {\n" |
| 131 | + + " checkout(\n" |
| 132 | + + " [$class: 'GitSCM', \n" |
| 133 | + + " userRemoteConfigs: [[url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n" |
| 134 | + + " )" |
| 135 | + + "}")); |
| 136 | + WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0)); |
| 137 | + System.out.println(JenkinsRule.getLog(b)); |
| 138 | + r.assertLogContains("No credentials specified", b); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + private StandardCredentials createCredential(CredentialsScope scope, String id) { |
| 143 | + return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password"); |
| 144 | + } |
| 145 | +} |
0 commit comments