Skip to content

Commit d529421

Browse files
committed
SCMSourceRetrieverTest: checkDefaultVersion_inline_allowVersionEnvvar(): un-block test with injected envvars [JENKINS-69731]
1 parent f4c53c7 commit d529421

File tree

1 file changed

+77
-80
lines changed

1 file changed

+77
-80
lines changed

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

Lines changed: 77 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,89 +1043,86 @@ public class SCMSourceRetrieverTest {
10431043
r.assertLogContains("Loading library branchylib@stable", b1);
10441044
r.assertLogContains("something reliable", b1);
10451045

1046-
// TODO: similar trick with build-agent settings
1047-
// to check they override global server settings?
1048-
//p1.setAssignedNode(r.createSlave());
1049-
//p1.getEnvironment(r.jenkins.getNode("built-in"), null).put("TEST_VAR_NAME", "feature");
1050-
1051-
// This part of the test is optionally fenced away -
1052-
// so developers tinkering on the fix can do so and
1053-
// run it, but for default case it is so far ignored.
1054-
// $ ENABLE_TEST_VAR_NAME_JOBLEVEL=true mvn test -Dtest='SCMSourceRetrieverTest#checkDefaultVersion_inline_allowVersionEnvvar'
1055-
if ("true".equals(System.getenv("ENABLE_TEST_VAR_NAME_JOBLEVEL"))) {
1056-
// Try a more direct way to inject environment
1057-
// variables into a Job/Run without extra plugins:
1058-
1059-
// See research commented at
1060-
// https://github.com/jenkinsci/pipeline-groovy-lib-plugin/pull/19#discussion_r990781686
1061-
1062-
// General override idea was lifted from
1063-
// https://github.com/jenkinsci/subversion-plugin/blob/master/src/test/java/hudson/scm/SubversionSCMTest.java#L1383
1064-
// test-case recursiveEnvironmentVariables()
1065-
1066-
// Per https://github.com/jenkinsci/jenkins/blob/031f40c50899ec4e5fa4d886a1c006a5330f2627/core/src/main/java/hudson/ExtensionList.java#L296
1067-
// in implementation of `add(index, T)` the index is ignored.
1068-
// So we save a copy in same order, drop the list, add our
1069-
// override as the only entry (hence highest priority)
1070-
// and re-add the original list contents.
1071-
ExtensionList<EnvironmentContributor> ecList = EnvironmentContributor.all();
1072-
List<EnvironmentContributor> ecOrig = new ArrayList();
1073-
for (EnvironmentContributor ec : ecList) {
1074-
ecOrig.add(ec);
1046+
// Now try a more direct way to inject environment
1047+
// variables into a Job/Run without extra plugins:
1048+
1049+
// See research commented at
1050+
// https://github.com/jenkinsci/pipeline-groovy-lib-plugin/pull/19#discussion_r990781686
1051+
1052+
// General override idea was lifted from
1053+
// https://github.com/jenkinsci/subversion-plugin/blob/master/src/test/java/hudson/scm/SubversionSCMTest.java#L1383
1054+
// test-case recursiveEnvironmentVariables()
1055+
1056+
// Per https://github.com/jenkinsci/jenkins/blob/031f40c50899ec4e5fa4d886a1c006a5330f2627/core/src/main/java/hudson/ExtensionList.java#L296
1057+
// in implementation of `add(index, T)` the index is ignored.
1058+
// So we save a copy in same order, drop the list, add our
1059+
// override as the only entry (hence highest priority)
1060+
// and re-add the original list contents.
1061+
ExtensionList<EnvironmentContributor> ecList = EnvironmentContributor.all();
1062+
List<EnvironmentContributor> ecOrig = new ArrayList();
1063+
for (EnvironmentContributor ec : ecList) {
1064+
ecOrig.add(ec);
1065+
}
1066+
ecList.removeAll(ecOrig);
1067+
assumeFalse("EnvironmentContributor.all() should be empty now", !ecList.isEmpty());
1068+
1069+
ecList.add(new EnvironmentContributor() {
1070+
@Override public void buildEnvironmentFor(Run run, EnvVars ev, TaskListener tl) throws IOException, InterruptedException {
1071+
if (tl != null)
1072+
tl.getLogger().println("[DEBUG:RUN] Injecting TEST_VAR_NAME='feature' to EnvVars");
1073+
ev.put("TEST_VAR_NAME", "feature");
10751074
}
1076-
ecList.removeAll(ecOrig);
1077-
assumeFalse("EnvironmentContributor.all() should be empty now", !ecList.isEmpty());
1078-
1079-
ecList.add(new EnvironmentContributor() {
1080-
@Override public void buildEnvironmentFor(Run run, EnvVars ev, TaskListener tl) throws IOException, InterruptedException {
1081-
if (tl != null)
1082-
tl.getLogger().println("[DEBUG:RUN] Injecting TEST_VAR_NAME='feature' to EnvVars");
1083-
ev.put("TEST_VAR_NAME", "feature");
1084-
}
1085-
@Override public void buildEnvironmentFor(Job run, EnvVars ev, TaskListener tl) throws IOException, InterruptedException {
1086-
if (tl != null)
1087-
tl.getLogger().println("[DEBUG:JOB] Injecting TEST_VAR_NAME='feature' to EnvVars");
1088-
ev.put("TEST_VAR_NAME", "feature");
1089-
}
1090-
});
1091-
for (EnvironmentContributor ec : ecOrig) {
1092-
ecList.add(ec);
1075+
@Override public void buildEnvironmentFor(Job run, EnvVars ev, TaskListener tl) throws IOException, InterruptedException {
1076+
if (tl != null)
1077+
tl.getLogger().println("[DEBUG:JOB] Injecting TEST_VAR_NAME='feature' to EnvVars");
1078+
ev.put("TEST_VAR_NAME", "feature");
10931079
}
1094-
1095-
p1.scheduleBuild2(0);
1096-
r.waitUntilNoActivity();
1097-
WorkflowRun b2 = p1.getLastBuild();
1098-
r.waitForCompletion(b2);
1099-
assertFalse(p1.isBuilding());
1100-
r.assertBuildStatusSuccess(b2);
1101-
1102-
System.out.println("[DEBUG:EXT] wfJob env: " + p1.getEnvironment(null, null));
1103-
System.out.println("[DEBUG:EXT] wfRun env: " + b2.getEnvironment());
1104-
System.out.println("[DEBUG:EXT] wfRun envContribActions: " + b2.getActions(EnvironmentContributingAction.class));
1105-
1106-
// Our first try is expected to fail currently, since
1107-
// WorkflowRun::buildEnvironmentFor() takes "env" from
1108-
// super-class, and overlays with envvars from global
1109-
// configuration. However, if in the future behavior
1110-
// of workflow changes, it is not consequential - just
1111-
// something to adjust "correct" expectations for.
1112-
r.assertLogContains("Loading library branchylib@stable", b2);
1113-
r.assertLogContains("something reliable", b2);
1114-
1115-
// For the next try, however, we remove global config
1116-
// part and expect the injected envvar to take hold:
1117-
envVars.remove("TEST_VAR_NAME");
1118-
r.jenkins.save();
1119-
1120-
WorkflowRun b3 = r.buildAndAssertSuccess(p1);
1121-
1122-
System.out.println("[DEBUG:EXT] wfJob env: " + p1.getEnvironment(null, null));
1123-
System.out.println("[DEBUG:EXT] wfRun env: " + b3.getEnvironment());
1124-
System.out.println("[DEBUG:EXT] wfRun envContribActions: " + b3.getActions(EnvironmentContributingAction.class));
1125-
1126-
r.assertLogContains("Loading library branchylib@feature", b3);
1127-
r.assertLogContains("something very special", b3);
1080+
});
1081+
for (EnvironmentContributor ec : ecOrig) {
1082+
ecList.add(ec);
11281083
}
1084+
1085+
p1.scheduleBuild2(0);
1086+
r.waitUntilNoActivity();
1087+
WorkflowRun b2 = p1.getLastBuild();
1088+
r.waitForCompletion(b2);
1089+
assertFalse(p1.isBuilding());
1090+
r.assertBuildStatusSuccess(b2);
1091+
1092+
System.out.println("[DEBUG:EXT] wfJob env: " + p1.getEnvironment(null, null));
1093+
System.out.println("[DEBUG:EXT] wfRun env: " + b2.getEnvironment());
1094+
System.out.println("[DEBUG:EXT] wfRun envContribActions: " + b2.getActions(EnvironmentContributingAction.class));
1095+
1096+
// Our first try is expected to fail currently, since
1097+
// WorkflowRun::getEnvironment() takes "env" from
1098+
// super-class, and overlays with envvars from global
1099+
// configuration. However, if in the future behavior
1100+
// of workflow changes, it is not consequential - just
1101+
// something to adjust "correct" expectations for.
1102+
r.assertLogContains("Loading library branchylib@stable", b2);
1103+
r.assertLogContains("something reliable", b2);
1104+
1105+
// For the next try, however, we remove global config
1106+
// part and expect the injected envvar to take hold:
1107+
envVars.remove("TEST_VAR_NAME");
1108+
r.jenkins.save();
1109+
1110+
WorkflowRun b3 = r.buildAndAssertSuccess(p1);
1111+
1112+
System.out.println("[DEBUG:EXT] wfJob env: " + p1.getEnvironment(null, null));
1113+
System.out.println("[DEBUG:EXT] wfRun env: " + b3.getEnvironment());
1114+
System.out.println("[DEBUG:EXT] wfRun envContribActions: " + b3.getActions(EnvironmentContributingAction.class));
1115+
1116+
r.assertLogContains("Loading library branchylib@feature", b3);
1117+
r.assertLogContains("something very special", b3);
1118+
1119+
// TODO: similar trick with built-in agent settings
1120+
// which has lowest priority behind global and injected
1121+
// envvars (see Run::getEnvironment()). Check with the
1122+
// override above, and with ecList contents restored to
1123+
// ecOrig state only.
1124+
//p1.setAssignedNode(r.createSlave());
1125+
//p1.getEnvironment(r.jenkins.getNode("built-in"), null).put("TEST_VAR_NAME", "feature");
11291126
}
11301127

11311128
@Issue("JENKINS-43802")

0 commit comments

Comments
 (0)