Skip to content

Migrate from RestartableJenkinsRule to JenkinsSessionRule #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,45 @@
import static org.junit.Assert.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.JenkinsSessionRule;

public class SCMVarTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
@Rule public JenkinsSessionRule story = new JenkinsSessionRule();
@Rule public GitSampleRepoRule sampleGitRepo = new GitSampleRepoRule();

@Test public void scmPickle() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
@Test public void scmPickle() throws Throwable {
story.then(j -> {
sampleGitRepo.init();
sampleGitRepo.write("Jenkinsfile", "def _scm = scm; semaphore 'wait'; node {checkout _scm; echo readFile('file')}");
sampleGitRepo.write("file", "initial content");
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--all", "--message=flow");
WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
SemaphoreStep.waitForStart("wait/1", null);
WorkflowRun b1 = p.getLastBuild();
assertNotNull(b1);
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
story.then(j -> {
SemaphoreStep.success("wait/1", null);
WorkflowJob p = story.j.jenkins.getItemByFullName("p/master", WorkflowJob.class);
WorkflowJob p = j.jenkins.getItemByFullName("p/master", WorkflowJob.class);
assertNotNull(p);
WorkflowRun b1 = p.getLastBuild();
assertNotNull(b1);
assertEquals(1, b1.getNumber());
story.j.assertLogContains("initial content", story.j.waitForCompletion(b1));
j.assertLogContains("initial content", j.waitForCompletion(b1));
SCMBinderTest.assertRevisionAction(b1);
}
});
}

@Issue("JENKINS-30222")
@Test public void globalVariable() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
@Test public void globalVariable() throws Throwable {
story.then(j -> {
// Set up a standardJob definition:
File lib = new File(Jenkins.get().getRootDir(), "somelib");
LibraryConfiguration cfg = new LibraryConfiguration("somelib", new LocalRetriever(lib));
Expand Down Expand Up @@ -121,12 +115,11 @@ public class SCMVarTest {
sampleGitRepo.git("add", "resource");
sampleGitRepo.git("commit", "--all", "--message=flow");
// And run:
WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleGitRepo.toString(), "", "*", "", false), new DefaultBranchPropertyStrategy(new BranchProperty[0])));
WorkflowJob p = WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject(mp, "master");
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("loaded resource content", b);
}
WorkflowRun b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("loaded resource content", b);
});
}

Expand All @@ -145,19 +138,17 @@ private static final class LocalRetriever extends LibraryRetriever {
}

@Issue("JENKINS-31386")
@Test public void standaloneProject() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
@Test public void standaloneProject() throws Throwable {
story.then(j -> {
sampleGitRepo.init();
sampleGitRepo.write("Jenkinsfile", "node {checkout scm; echo readFile('file')}");
sampleGitRepo.write("file", "some content");
sampleGitRepo.git("add", "Jenkinsfile");
sampleGitRepo.git("commit", "--all", "--message=flow");
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsScmFlowDefinition(new GitStep(sampleGitRepo.toString()).createSCM(), "Jenkinsfile"));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("some content", b);
}
WorkflowRun b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
j.assertLogContains("some content", b);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,20 @@
import static org.junit.Assert.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsSessionRule;

public class WorkflowBranchProjectFactoryTest {

@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
@Rule public JenkinsSessionRule story = new JenkinsSessionRule();
@Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();

@Issue("JENKINS-30744")
@Test public void slashyBranches() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
@Test public void slashyBranches() throws Throwable {
story.then(j -> {
sampleRepo.init();
sampleRepo.git("checkout", "-b", "dev/main");
String script =
Expand All @@ -63,41 +62,38 @@ public class WorkflowBranchProjectFactoryTest {
sampleRepo.write("Jenkinsfile", script);
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "--all", "--message=flow");
WorkflowMultiBranchProject mp = story.j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
WorkflowMultiBranchProject mp = j.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
GitSCMSource source = new GitSCMSource(sampleRepo.toString());
source.setTraits(Collections.singletonList(new BranchDiscoveryTrait()));
mp.getSourcesList().add(new BranchSource(source));
WorkflowJob p = scheduleAndFindBranchProject(mp, "dev%2Fmain");
assertEquals(1, mp.getItems().size());
story.j.waitUntilNoActivity();
j.waitUntilNoActivity();
WorkflowRun b1 = p.getLastBuild();
assertEquals(1, b1.getNumber());
story.j.assertLogContains("branch=dev/main", b1);
story.j.assertLogContains("workspace=dev_main", b1);
verifyProject(p);
j.assertLogContains("branch=dev/main", b1);
j.assertLogContains("workspace=dev_main", b1);
verifyProject(j, p);
sampleRepo.write("Jenkinsfile", script.replace("branch=", "Branch="));
}
});
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.getItemByFullName("p/dev%2Fmain", WorkflowJob.class);
story.then(j -> {
WorkflowJob p = j.jenkins.getItemByFullName("p/dev%2Fmain", WorkflowJob.class);
assertNotNull(p);
sampleRepo.git("commit", "--all", "--message=Flow");
sampleRepo.notifyCommit(story.j);
sampleRepo.notifyCommit(j);
WorkflowRun b2 = p.getLastBuild();
assertEquals(2, b2.getNumber());
story.j.assertLogContains("Branch=dev/main", b2);
story.j.assertLogContains("workspace=dev_main", b2);
verifyProject(p);
}
j.assertLogContains("Branch=dev/main", b2);
j.assertLogContains("workspace=dev_main", b2);
verifyProject(j, p);
});
}
private void verifyProject(WorkflowJob p) throws Exception {
private static void verifyProject(JenkinsRule j, WorkflowJob p) throws Exception {
assertEquals("dev%2Fmain", p.getName());
assertEquals("dev/main", p.getDisplayName());
assertEquals("p/dev%2Fmain", p.getFullName());
assertEquals("p » dev/main", p.getFullDisplayName());
story.j.createWebClient().getPage(p);
j.createWebClient().getPage(p);
assertEquals(new File(new File(p.getParent().getRootDir(), "branches"), "dev-main.k31kdj"), p.getRootDir());
}

Expand Down