Skip to content
Open
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 @@ -32,31 +32,42 @@
import jenkins.branch.NoTriggerOrganizationFolderProperty;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.plugins.git.junit.jupiter.WithGitSampleRepo;
import org.jenkinsci.plugins.workflow.flow.DurabilityHintProvider;
import org.jenkinsci.plugins.workflow.flow.FlowDurabilityHint;
import org.jenkinsci.plugins.workflow.flow.GlobalDefaultFlowDurabilityLevel;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.job.properties.DurabilityHintJobProperty;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProjectTest.scheduleAndFindBranchProject;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Integration test for {@link NoTriggerBranchProperty} and {@link NoTriggerOrganizationFolderProperty}.
*/
@Issue("JENKINS-32396")
public class DurabilityHintBranchPropertyWorkflowTest {
@WithJenkins
@WithGitSampleRepo
class DurabilityHintBranchPropertyWorkflowTest {

@Rule public JenkinsRule r = new JenkinsRule();
@Rule public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
private JenkinsRule r;
private GitSampleRepoRule sampleRepo;

@BeforeEach
void setUp(JenkinsRule rule, GitSampleRepoRule repo) {
r = rule;
sampleRepo = repo;
}

@Test
public void configRoundtrip() throws Exception {
void configRoundtrip() throws Exception {
WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
BranchSource bs = new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false));
mp.getSourcesList().add(bs);
Expand All @@ -70,56 +81,55 @@ public void configRoundtrip() throws Exception {
break;
}
}
Assert.assertNotNull(prop);
Assert.assertEquals(FlowDurabilityHint.SURVIVABLE_NONATOMIC, prop.getHint());
assertNotNull(prop);
assertEquals(FlowDurabilityHint.SURVIVABLE_NONATOMIC, prop.getHint());
}

@Test public void durabilityHintByPropertyStep() throws Exception {
@Test
void durabilityHintByPropertyStep() throws Exception {
sampleRepo.init();
sampleRepo.write("Jenkinsfile",
"properties([durabilityHint('" + FlowDurabilityHint.SURVIVABLE_NONATOMIC.getName()+"')])\n"+
"echo 'whynot'");
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "--all", "--message=flow");


WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false)));
WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
r.waitUntilNoActivity();

WorkflowRun b1 = p.getLastBuild();
Assert.assertEquals(Result.SUCCESS, b1.getResult());
assertEquals(Result.SUCCESS, b1.getResult());
DurabilityHintJobProperty prop = p.getProperty(DurabilityHintJobProperty.class);
Assert.assertEquals(FlowDurabilityHint.SURVIVABLE_NONATOMIC, prop.getHint());
assertEquals(FlowDurabilityHint.SURVIVABLE_NONATOMIC, prop.getHint());
}

@Test
@Issue("JENKINS-48826")
public void durabilityHintByBranchProperty() throws Exception {
void durabilityHintByBranchProperty() throws Exception {
sampleRepo.init();
sampleRepo.write("Jenkinsfile",
"echo 'whynot'");
sampleRepo.git("add", "Jenkinsfile");
sampleRepo.git("commit", "--all", "--message=flow");


WorkflowMultiBranchProject mp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "p");
BranchSource bs = new BranchSource(new GitSCMSource(null, sampleRepo.toString(), "", "*", "", false));
mp.getSourcesList().add(bs);
bs.setStrategy(new DefaultBranchPropertyStrategy(new BranchProperty[]{new DurabilityHintBranchProperty(FlowDurabilityHint.SURVIVABLE_NONATOMIC)}));
WorkflowJob p = scheduleAndFindBranchProject(mp, "master");
r.waitUntilNoActivity();

Assert.assertEquals(FlowDurabilityHint.SURVIVABLE_NONATOMIC, DurabilityHintProvider.suggestedFor(p));
assertEquals(FlowDurabilityHint.SURVIVABLE_NONATOMIC, DurabilityHintProvider.suggestedFor(p));
WorkflowRun b1 = p.getLastBuild();
Assert.assertEquals(Result.SUCCESS, b1.getResult());
assertEquals(Result.SUCCESS, b1.getResult());

// Ensure when we remove the property, branches see that on the next build
bs.setStrategy(new DefaultBranchPropertyStrategy(new BranchProperty[]{}));
p = scheduleAndFindBranchProject(mp, "master");
r.waitUntilNoActivity();

Assert.assertEquals(GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint(), DurabilityHintProvider.suggestedFor(mp.getItems().iterator().next()));
assertEquals(GlobalDefaultFlowDurabilityLevel.getDefaultDurabilityHint(), DurabilityHintProvider.suggestedFor(mp.getItems().iterator().next()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,29 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jenkins.plugins.git.GitSCMSource;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMNavigatorDescriptor;
import jenkins.scm.api.SCMSourceObserver;
import org.apache.commons.io.IOUtils;
import static org.junit.Assert.assertFalse;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* Sample provider which scans a directory for Git checkouts.
*/
public class GitDirectorySCMNavigator extends SCMNavigator {

private final String directory;

@DataBoundConstructor public GitDirectorySCMNavigator(String directory) {
@DataBoundConstructor
public GitDirectorySCMNavigator(String directory) {
this.directory = directory;
}

Expand All @@ -66,7 +69,8 @@ protected String id() {
return directory;
}

@Override public void visitSources(SCMSourceObserver observer) throws IOException, InterruptedException {
@Override
public void visitSources(SCMSourceObserver observer) throws IOException, InterruptedException {
TaskListener listener = observer.getListener();
File[] kids = new File(directory).listFiles();
if (kids == null) {
Expand All @@ -89,7 +93,7 @@ protected String id() {
continue;
}
String origin = kid.getAbsolutePath(); // fallback
for (String line : IOUtils.readLines(new ByteArrayInputStream(baos.toByteArray()))) {
for (String line : IOUtils.readLines(new ByteArrayInputStream(baos.toByteArray()), StandardCharsets.UTF_8)) {
Matcher m = ORIGIN.matcher(line);
if (m.matches()) {
origin = m.group(1);
Expand All @@ -109,11 +113,13 @@ protected String id() {
public static class DescriptorImpl extends SCMNavigatorDescriptor {

@NonNull
@Override public String getDisplayName() {
@Override
public String getDisplayName() {
return "Directory of Git checkouts";
}

@Override public SCMNavigator newInstance(String name) {
@Override
public SCMNavigator newInstance(String name) {
return null;
}

Expand Down
Loading