Skip to content

Commit

Permalink
[FIXED JENKINS-26583] Do not capture slave variables
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Jan 25, 2015
1 parent 1ab1de6 commit 2948c71
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
Expand Up @@ -10,6 +10,7 @@
import hudson.model.listeners.RunListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;

import org.jenkinsci.lib.envinject.EnvInjectException;
import org.jenkinsci.lib.envinject.EnvInjectLogger;
import org.jenkinsci.plugins.envinject.model.EnvInjectJobPropertyContributor;
Expand All @@ -33,19 +34,12 @@ public Environment setUpEnvironment(AbstractBuild build, Launcher launcher, Buil
EnvInjectLogger logger = new EnvInjectLogger(listener);
try {

//Process environment variables at node level
Node buildNode = build.getBuiltOn();
if (buildNode != null) {
loadEnvironmentVariablesNode(build, buildNode, logger);
}

//Load job envinject job property
if (isEnvInjectJobPropertyActive(build)) {
return setUpEnvironmentJobPropertyObject(build, launcher, listener, logger);
} else {
return setUpEnvironmentWithoutJobPropertyObject(build, launcher, listener);
}

} catch (Run.RunnerAbortedException rre) {
logger.info("Fail the build.");
throw new Run.RunnerAbortedException();
Expand Down Expand Up @@ -75,22 +69,6 @@ private boolean isEligibleJobType(AbstractBuild build) {

}

private void loadEnvironmentVariablesNode(AbstractBuild build, Node buildNode, EnvInjectLogger logger) throws EnvInjectException {

EnvironmentVariablesNodeLoader environmentVariablesNodeLoader = new EnvironmentVariablesNodeLoader();
Map<String, String> configNodeEnvVars = environmentVariablesNodeLoader.gatherEnvironmentVariablesNode(build, buildNode, logger);
EnvInjectActionSetter envInjectActionSetter = new EnvInjectActionSetter(buildNode.getRootPath());
try {
envInjectActionSetter.addEnvVarsToEnvInjectBuildAction(build, configNodeEnvVars);

} catch (IOException ioe) {
throw new EnvInjectException(ioe);
} catch (InterruptedException ie) {
throw new EnvInjectException(ie);
}
}


private boolean isEnvInjectJobPropertyActive(AbstractBuild build) {
EnvInjectVariableGetter variableGetter = new EnvInjectVariableGetter();
EnvInjectJobProperty envInjectJobProperty = variableGetter.getEnvInjectJobProperty(build);
Expand Down
Expand Up @@ -106,7 +106,6 @@ private void checkBuildCauses(FreeStyleBuild build, String expectedMainCauseValu
Assert.assertNotNull(envVars);

String causeValue = envVars.get("BUILD_CAUSE");
Assert.assertNotNull(causeValue);
Assert.assertEquals(expectedMainCauseValue, causeValue);

String rootCauseValue = envVars.get("ROOT_BUILD_CAUSE");
Expand Down
Expand Up @@ -24,26 +24,25 @@
package org.jenkinsci.plugins.envinject;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.EnvironmentContributor;
import hudson.model.FreeStyleProject;
import hudson.model.Run;
import hudson.slaves.DumbSlave;
import hudson.tasks.BuildWrapper;
import hudson.tasks.Shell;

import java.io.IOException;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;
import org.jvnet.hudson.test.TestExtension;
Expand Down Expand Up @@ -102,12 +101,12 @@ public class EnvInjectActionTest {
validate(p);
}

@SuppressWarnings("deprecation")
private void validate(FreeStyleProject p) throws Exception {
p.getBuildersList().add(new Shell("echo actual=$DISPLAY"));
FreeStyleBuild build = p.scheduleBuild2(0).get();
assertEquals("BUILD_VAL", build.getEnvironment().get("DISPLAY"));
assertTrue(build.getLog(), build.getLog().contains("actual=BUILD_VAL"));
CaptureEnvironmentBuilder capture = new CaptureEnvironmentBuilder();
p.getBuildersList().add(capture);

p.scheduleBuild2(0).get();
assertEquals("BUILD_VAL", capture.getEnvVars().get("DISPLAY"));
}

private FreeStyleProject setupProjectWithDefaultEnvValue()throws Exception, IOException {
Expand Down Expand Up @@ -165,17 +164,24 @@ public boolean perform(
AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener
) throws InterruptedException, IOException {
// Start serving envvar from EnvironmentContributor
ContributingExtension.values(key, value);
contributor.values(key, value);
return true;
}
}

@TestExtension
public static final class ContributingExtension extends EnvironmentContributor {
private static String value = null;
private static String key = null;
public static final Contributor contributor = new Contributor();

@Before
public void setUp() {
contributor.values(null, null);
}

private static class Contributor extends EnvironmentContributor {
private String value = null;
private String key = null;

private static void values(String k, String v) {
private void values(String k, String v) {
value = v;
key = k;
}
Expand Down

0 comments on commit 2948c71

Please sign in to comment.