Skip to content

Commit 4ce1164

Browse files
committed
SCMSourceRetrieverTest: complete the checkDefaultVersion_MBPsingleBranch_*() cases [JENKINS-69731]
1 parent 85e96e3 commit 4ce1164

File tree

1 file changed

+99
-51
lines changed

1 file changed

+99
-51
lines changed

src/test/java/org/jenkinsci/plugins/workflow/libs/SCMSourceRetrieverTest.java

Lines changed: 99 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
import java.util.List;
5353
import java.util.TreeMap;
5454
import hudson.util.DescribableList;
55+
import jenkins.branch.BranchProperty;
5556
import jenkins.branch.BranchSource;
57+
import jenkins.branch.DefaultBranchPropertyStrategy;
5658
import jenkins.plugins.git.GitSCMSource;
5759
import jenkins.plugins.git.GitSampleRepoRule;
5860
import jenkins.scm.api.SCMHead;
@@ -569,14 +571,10 @@ public class SCMSourceRetrieverTest {
569571
}
570572

571573
@Issue("JENKINS-69731")
572-
//@Ignore("Need help setting up MBP+SingleSCMSource")
573574
@Test public void checkDefaultVersion_MBPsingleBranch_staticStrings() throws Exception {
574-
// FAILS: Setup below does not instantiate any jobs!
575-
// (expected one for "feature")
576-
577575
// Test that lc.setAllowBRANCH_NAME(false) does not
578576
// preclude fixed branch names (they should work),
579-
// like @Library('branchylib@master') when used
577+
// like @Library('branchylib@feature') when used
580578
// for MBP with "Single repository and branch" as
581579
// the SCM source.
582580

@@ -612,41 +610,45 @@ public class SCMSourceRetrieverTest {
612610
sampleRepo2.git("add", "Jenkinsfile");
613611
sampleRepo2.git("commit", "--message=bogus");
614612

615-
// TODO: Test also another MBP where we would
616-
// set options "List of branches to build" and
617-
// "Branch Specifier" to a different value than
618-
// the "name" of branchSource. While the "name"
619-
// becomes "BRANCH_NAME" envvar (and MBP job name
620-
// generated for the branch), the specifier is
621-
// what gets placed into SCMs list.
622-
623-
// FAILS: Setup below does not instantiate any jobs!
624-
// (expected one for "feature")
625613
WorkflowMultiBranchProject mbp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "mbp");
626-
//GitSCM gitSCM = new GitSCM(sampleRepo2.toString());
627614
GitSCM gitSCM = new GitSCM(
628615
GitSCM.createRepoList(sampleRepo2.toString(), null),
629616
Collections.singletonList(new BranchSpec("*/feature")),
630617
null, null, Collections.emptyList());
631-
BranchSource branchSource = new BranchSource(new SingleSCMSource("feature-id", "feature", gitSCM));
632-
mbp.getSourcesList().add(branchSource);
618+
// We test an MBP with two leaf jobs where we
619+
// set options "List of branches to build"/
620+
// "Branch Specifier" to a same and a different
621+
// value than the "name" of branchSource.
622+
// While the "name" becomes "BRANCH_NAME" envvar
623+
// (and MBP job name generated for the branch),
624+
// the specifier is what gets placed into SCMs list.
625+
BranchSource branchSource1 = new BranchSource(
626+
new SingleSCMSource("feature-id1", "feature", gitSCM),
627+
new DefaultBranchPropertyStrategy(new BranchProperty[0]));
628+
BranchSource branchSource2 = new BranchSource(
629+
new SingleSCMSource("feature-id2", "featurette", gitSCM),
630+
new DefaultBranchPropertyStrategy(new BranchProperty[0]));
631+
mbp.getSourcesList().add(branchSource1);
632+
mbp.getSourcesList().add(branchSource2);
633633
mbp.save();
634-
// Note: this notification causes discovery of branches,
635-
// definition of MBP "leaf" jobs, and launch of builds,
636-
// so below we just make sure they complete and analyze
637-
// the outcomes.
634+
// Rescan to actually define leaf jobs:
635+
mbp.scheduleBuild(0);
638636
sampleRepo2.notifyCommit(r);
639637
r.waitUntilNoActivity();
638+
System.out.println("All Jenkins items: " + r.jenkins.getItems().toString());
639+
System.out.println("MBP sources: " + mbp.getSourcesList().toString());
640+
System.out.println("MBP source 0: " + mbp.getSourcesList().get(0).getSource().toString());
641+
System.out.println("MBP source 1: " + mbp.getSourcesList().get(1).getSource().toString());
640642
System.out.println("Jobs generated by MBP: " + mbp.getItems().toString());
641-
assumeFalse("MBP should have generated 'feature' pipeline job", mbp.getItems().isEmpty());
643+
assumeFalse("MBP should have generated 'feature' and 'featurette' pipeline job", mbp.getItems().size() != 2);
642644

643645
// In case of MBP with "Single repository and branch"
644-
// it only defines one job, so those for other branches
645-
// should be null:
646+
// it only defines one job (per single-branch source),
647+
// so those for other known branches should be null:
646648
WorkflowJob p1 = mbp.getItem("master");
647649
assertNull(p1);
648650

649-
WorkflowJob p2 = mbp.getItem("feature-id");
651+
WorkflowJob p2 = mbp.getItem("feature");
650652
assertNotNull(p2);
651653
WorkflowRun b2 = p2.getLastBuild();
652654
r.waitForCompletion(b2);
@@ -657,14 +659,28 @@ public class SCMSourceRetrieverTest {
657659

658660
WorkflowJob p3 = mbp.getItem("bogus");
659661
assertNull(p3);
662+
663+
// For fixed branch in @Library spec, we see the
664+
// SingleSCMSource checkout out the "*/feature"
665+
// specified in its GitSCM and so request the
666+
// @Library('branchylib@feature') spelled there.
667+
// And then MBP sets BRANCH_NAME='featurette'
668+
// (and leaf job) per SingleSCMSource "name".
669+
WorkflowJob p4 = mbp.getItem("featurette");
670+
assertNotNull(p4);
671+
WorkflowRun b4 = p4.getLastBuild();
672+
r.waitForCompletion(b4);
673+
assertFalse(p4.isBuilding());
674+
r.assertBuildStatusSuccess(b4);
675+
System.out.println("Envvar BRANCH_NAME set into 'featurette' job: " + b4.getEnvironment().get("BRANCH_NAME"));
676+
// We use same gitSCM source, and so same static
677+
// version of the library:
678+
r.assertLogContains("Loading library branchylib@feature", b4);
679+
r.assertLogContains("something very special", b4);
660680
}
661681

662682
@Issue("JENKINS-69731")
663-
//@Ignore("Need help setting up MBP+SingleSCMSource")
664683
@Test public void checkDefaultVersion_MBPsingleBranch_BRANCH_NAME() throws Exception {
665-
// FAILS: Setup below does not instantiate any jobs!
666-
// (expected one for "feature")
667-
668684
// Test that @Library('branchylib@${BRANCH_NAME}') works
669685
// also for MBP with "Single repository and branch" as
670686
// the SCM source.
@@ -682,7 +698,7 @@ public class SCMSourceRetrieverTest {
682698
lc.setDefaultVersion("master");
683699
lc.setIncludeInChangesets(false);
684700
lc.setAllowVersionOverride(true);
685-
lc.setAllowBRANCH_NAME(false);
701+
lc.setAllowBRANCH_NAME(true);
686702
lc.setTraceDefaultedVersion(true);
687703
GlobalLibraries.get().setLibraries(Collections.singletonList(lc));
688704

@@ -695,37 +711,41 @@ public class SCMSourceRetrieverTest {
695711
sampleRepo2.git("branch", "feature");
696712
sampleRepo2.git("branch", "bogus");
697713

698-
// TODO: Test also another MBP where we would
699-
// set options "List of branches to build" and
700-
// "Branch Specifier" to a different value than
701-
// the "name" of branchSource. While the "name"
702-
// becomes "BRANCH_NAME" envvar (and MBP job name
703-
// generated for the branch), the specifier is
704-
// what gets placed into SCMs list.
705-
706-
// FAILS: Setup below does not instantiate any jobs!
707-
// (expected one for "feature")
708714
WorkflowMultiBranchProject mbp = r.jenkins.createProject(WorkflowMultiBranchProject.class, "mbp");
709-
//GitSCM gitSCM = new GitSCM(sampleRepo2.toString());
710715
GitSCM gitSCM = new GitSCM(
711716
GitSCM.createRepoList(sampleRepo2.toString(), null),
712717
Collections.singletonList(new BranchSpec("*/feature")),
713718
null, null, Collections.emptyList());
714-
BranchSource branchSource = new BranchSource(new SingleSCMSource("feature", "feature", gitSCM));
715-
mbp.getSourcesList().add(branchSource);
719+
// We test an MBP with two leaf jobs where we
720+
// set options "List of branches to build"/
721+
// "Branch Specifier" to a same and a different
722+
// value than the "name" of branchSource.
723+
// While the "name" becomes "BRANCH_NAME" envvar
724+
// (and MBP job name generated for the branch),
725+
// the specifier is what gets placed into SCMs list.
726+
BranchSource branchSource1 = new BranchSource(
727+
new SingleSCMSource("feature-id1", "feature", gitSCM),
728+
new DefaultBranchPropertyStrategy(new BranchProperty[0]));
729+
BranchSource branchSource2 = new BranchSource(
730+
new SingleSCMSource("feature-id2", "featurette", gitSCM),
731+
new DefaultBranchPropertyStrategy(new BranchProperty[0]));
732+
mbp.getSourcesList().add(branchSource1);
733+
mbp.getSourcesList().add(branchSource2);
716734
mbp.save();
717-
// Note: this notification causes discovery of branches,
718-
// definition of MBP "leaf" jobs, and launch of builds,
719-
// so below we just make sure they complete and analyze
720-
// the outcomes.
735+
// Rescan to actually define leaf jobs:
736+
mbp.scheduleBuild(0);
721737
sampleRepo2.notifyCommit(r);
722738
r.waitUntilNoActivity();
739+
System.out.println("All Jenkins items: " + r.jenkins.getItems().toString());
740+
System.out.println("MBP sources: " + mbp.getSourcesList().toString());
741+
System.out.println("MBP source 0: " + mbp.getSourcesList().get(0).getSource().toString());
742+
System.out.println("MBP source 1: " + mbp.getSourcesList().get(1).getSource().toString());
723743
System.out.println("Jobs generated by MBP: " + mbp.getItems().toString());
724-
assumeFalse("MBP should have generated 'feature' pipeline job", mbp.getItems().isEmpty());
744+
assumeFalse("MBP should have generated 'feature' and 'featurette' pipeline job", mbp.getItems().size() != 2);
725745

726746
// In case of MBP with "Single repository and branch"
727-
// it only defines one job, so those for other branches
728-
// should be null:
747+
// it only defines one job (per single-branch source),
748+
// so those for other known branches should be null:
729749
WorkflowJob p1 = mbp.getItem("master");
730750
assertNull(p1);
731751

@@ -740,6 +760,34 @@ public class SCMSourceRetrieverTest {
740760

741761
WorkflowJob p3 = mbp.getItem("bogus");
742762
assertNull(p3);
763+
764+
// For fixed branch in @Library spec, we see the
765+
// SingleSCMSource checkout out the "*/feature"
766+
// specified in its GitSCM and so evaluate the
767+
// @Library('branchylib@${BRANCH_NAME}')...
768+
// And then MBP sets BRANCH_NAME='featurette'
769+
// (and leaf job) per SingleSCMSource "name".
770+
WorkflowJob p4 = mbp.getItem("featurette");
771+
assertNotNull(p4);
772+
WorkflowRun b4 = p4.getLastBuild();
773+
r.waitForCompletion(b4);
774+
assertFalse(p4.isBuilding());
775+
r.assertBuildStatusSuccess(b4);
776+
System.out.println("Envvar BRANCH_NAME set into 'featurette' job: " + b4.getEnvironment().get("BRANCH_NAME"));
777+
// Library does not have a "featurette" branch,
778+
// so if that source were tried according to the
779+
// single-branch source name, should fall back
780+
// to "master". But if it were tried according
781+
// to the actual branch of pipeline script, it
782+
// should use "feature".
783+
// For now, I've sided with the MBP plugin which
784+
// goes to great lengths to make-believe that
785+
// the "name" specified in config is the branch
786+
// name (also setting it into the WorkflowJob
787+
// BranchJobProperty), even if it does not exist
788+
// in actual SCM.
789+
r.assertLogContains("Loading library branchylib@master", b4);
790+
r.assertLogContains("something special", b4);
743791
}
744792

745793
@Issue("JENKINS-69731")

0 commit comments

Comments
 (0)