Skip to content

Commit

Permalink
Reproducing jenkinsci#1206
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 21, 2021
1 parent ee7d9dd commit 04323ea
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 19 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-multibranch</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
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));
}
}
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'
}

0 comments on commit 04323ea

Please sign in to comment.