Skip to content

Commit

Permalink
Merge pull request #445 from bitwiseman/test/realjenkins-once
Browse files Browse the repository at this point in the history
[JENKINS-66247] Add a RealJenkinsRule test
  • Loading branch information
bitwiseman authored Jul 30, 2021
2 parents 2ef9593 + 634d290 commit 015754e
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.jenkinsci.plugins.pipeline.modeldefinition;

import groovy.lang.DelegatingMetaClass;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RealJenkinsRule;

public class DeclarativePipelineTest {
@Rule
public RealJenkinsRule r = new RealJenkinsRule();

@Test
public void smokes() throws Throwable {
r.then(DeclarativePipelineTest::smokes_run);
}

private static void smokes_run(JenkinsRule j) throws Throwable {
final WorkflowRun run = runPipeline(j, m(
"pipeline {",
" agent none",
" stages {",
" stage('Example') {",
" steps {",
" echo 'Ran on RealJenkins right here!'",
" }",
" }",
" }",
"}"));

j.assertBuildStatusSuccess(run);
j.assertLogContains("Ran on RealJenkins right here!", run);
}

/**
* Run a pipeline job synchronously.
*
* @param definition the pipeline job definition
* @return the started job
*/
private static WorkflowRun runPipeline(JenkinsRule j, String definition) throws Exception {
final WorkflowJob project = j.createProject(WorkflowJob.class, "example");
project.setDefinition(new CpsFlowDefinition(definition, true));
final WorkflowRun workflowRun = project.scheduleBuild2(0).waitForStart();
j.waitForCompletion(workflowRun);
return workflowRun;
}

/**
* Approximates a multiline string in Java.
*
* @param lines the lines to concatenate with a newline separator
* @return the concatenated multiline string
*/
private static String m(String... lines) {
return String.join("\n", lines);
}
}

0 comments on commit 015754e

Please sign in to comment.