forked from jenkinsci/gitlab-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 96 additions & 19 deletions
115
src/test/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,152 @@ | ||
package com.dabsquared.gitlabjenkins.workflow; | ||
|
||
import com.cloudbees.plugins.credentials.CredentialsProvider; | ||
import com.cloudbees.plugins.credentials.CredentialsScope; | ||
import com.cloudbees.plugins.credentials.CredentialsStore; | ||
import com.cloudbees.plugins.credentials.SystemCredentialsProvider; | ||
import com.cloudbees.plugins.credentials.domains.Domain; | ||
import com.dabsquared.gitlabjenkins.connection.GitLabConnection; | ||
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig; | ||
import com.dabsquared.gitlabjenkins.gitlab.api.impl.V4GitLabClientBuilder; | ||
import hudson.model.Run; | ||
import hudson.util.Secret; | ||
import java.util.List; | ||
import org.apache.commons.io.IOUtils; | ||
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.RealJenkinsRule; | ||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.junit.MockServerRule; | ||
|
||
public class GitLabCommitStatusStepTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
public RealJenkinsRule rr = new RealJenkinsRule(); | ||
|
||
@Rule | ||
public MockServerRule mockServer = new MockServerRule(new Object()); | ||
|
||
private MockServerClient mockServerClient; | ||
|
||
@Before | ||
public void setup() { | ||
mockServerClient = new MockServerClient("localhost", mockServer.getPort()); | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
mockServerClient.reset(); | ||
} | ||
|
||
@Test | ||
public void bare_gitlabCommitStatus() throws Exception { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
public void bare_gitlabCommitStatus() throws Throwable { | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/bare-gitlabCommitStatus-pipeline.groovy")); | ||
rr.then(j -> _bare_gitlabCommitStatus(j, pipelineText)); | ||
} | ||
private static void _bare_gitlabCommitStatus(JenkinsRule j, String pipelineText) throws Throwable { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is simple jenkins-build", build); | ||
} | ||
|
||
@Test | ||
public void named_simple_pipeline_builds_as_LString() throws Exception { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
public void named_simple_pipeline_builds_as_LString() throws Throwable { | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/named-simple-pipeline-builds-as-LString.groovy")); | ||
rr.then(j -> _named_simple_pipeline_builds_as_LString(j, pipelineText)); | ||
} | ||
private static void _named_simple_pipeline_builds_as_LString(JenkinsRule j, String pipelineText) throws Throwable { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is pre-build stage", build); | ||
} | ||
|
||
@Test | ||
public void named_simple_pipeline_builds_as_String() throws Exception { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
public void named_simple_pipeline_builds_as_String() throws Throwable { | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/named-simple-pipeline-builds-as-String.groovy")); | ||
rr.then(j -> _named_simple_pipeline_builds_as_String(j, pipelineText)); | ||
} | ||
private static void _named_simple_pipeline_builds_as_String(JenkinsRule j, String pipelineText) throws Throwable { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is pre-build stage", build); | ||
} | ||
|
||
@Test | ||
public void multisite() throws Exception { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
public void multisite() throws Throwable { | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/multisite-pipeline.groovy")); | ||
rr.then(j -> _multisite(j, pipelineText)); | ||
} | ||
private static void _multisite(JenkinsRule j, String pipelineText) throws Throwable { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is stage3", build); | ||
} | ||
|
||
@Test | ||
public void multiproject_specific_connection() throws Exception { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
public void multiproject_specific_connection() throws Throwable { | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/multiproject-specific-connection-pipeline.groovy")); | ||
rr.then(j -> _multiproject_specific_connection(j, pipelineText)); | ||
} | ||
private static void _multiproject_specific_connection(JenkinsRule j, String pipelineText) throws Throwable { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is pre-build stage", build); | ||
} | ||
|
||
@Test | ||
public void multiproject() throws Exception { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
public void multiproject() throws Throwable { | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/multiproject-pipeline.groovy")); | ||
rr.then(j -> _multiproject(j, pipelineText)); | ||
} | ||
private static void _multiproject(JenkinsRule j, String pipelineText) throws Throwable { | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is pre-build stage", build); | ||
} | ||
|
||
@Test | ||
public void updateGitlabCommitStatus() throws Throwable { | ||
int port = mockServer.getPort(); | ||
String pipelineText = IOUtils.toString(getClass().getResourceAsStream( | ||
"pipeline/updateGitlabCommitStatus.groovy")); | ||
rr.then(j -> _updateGitlabCommitStatus(j, port, pipelineText)); | ||
} | ||
private static void _updateGitlabCommitStatus(JenkinsRule j, int port, String pipelineText) throws Throwable { | ||
setupGitLabConnections(j, port); | ||
WorkflowJob project = j.createProject(WorkflowJob.class); | ||
project.setDefinition(new CpsFlowDefinition(pipelineText, false)); | ||
Run build = j.buildAndAssertSuccess(project); | ||
j.assertLogContains("this is simple jenkins-build", build); | ||
j.assertLogNotContains("No GitLab connection configured", build); | ||
} | ||
|
||
private static void setupGitLabConnections(JenkinsRule j, int port) throws Throwable { | ||
GitLabConnectionConfig connectionConfig = j.get(GitLabConnectionConfig.class); | ||
for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(j.jenkins)) { | ||
if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) { | ||
List<Domain> domains = credentialsStore.getDomains(); | ||
credentialsStore.addCredentials(domains.get(0), | ||
new StringCredentialsImpl(CredentialsScope.SYSTEM, "apiTokenId", "GitLab API Token", Secret.fromString("secret"))); | ||
} | ||
} | ||
connectionConfig.addConnection(new GitLabConnection("test-connection", "http://localhost:" + port + "/gitlab", "apiTokenId", new V4GitLabClientBuilder(), false, 10, 10)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
.../resources/com/dabsquared/gitlabjenkins/workflow/pipeline/updateGitlabCommitStatus.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.dabsquared.gitlabjenkins.workflow.pipeline | ||
|
||
properties([ | ||
gitLabConnection('test-connection') | ||
]) | ||
|
||
node('master') { | ||
updateGitlabCommitStatus name: 'build', state: 'pending' | ||
echo 'this is simple jenkins-build' | ||
updateGitlabCommitStatus name: 'build', state: 'success' | ||
} |