Skip to content
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 @@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
* The MIT License
*
* Copyright (c) 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.envinject;

import static org.junit.Assert.assertEquals;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.BuildListener;
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 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;

public class EnvInjectActionTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void doNotOverrideWrapperEnvVar() throws Exception {
FreeStyleProject p = setupProjectWithDefaultEnvValue();

p.getBuildWrappersList().add(new ContributingWrapper("DISPLAY", "BUILD_VAL"));

assertValueInjected(p);
}

@Test
public void doNotOverrideContributorEnvVar() throws Exception {
FreeStyleProject p = setupProjectWithDefaultEnvValue();

p.getBuildersList().add(new ContributingBuilder("DISPLAY", "BUILD_VAL"));

assertValueInjected(p);
}

@Test
public void doNotOverrideWithBuildStep() throws Exception {
FreeStyleProject p = setupProjectWithDefaultEnvValue();
p.getBuildersList().add(new EnvInjectBuilder(null, "IRRELEVANT_VAR=true"));

p.getBuildWrappersList().add(new ContributingWrapper("DISPLAY", "BUILD_VAL"));

assertValueInjected(p);
}

@Test
public void doNotOverrideWithBuildWrapper() throws Exception {
FreeStyleProject p = setupProjectWithDefaultEnvValue();
final EnvInjectBuildWrapper wrapper = new EnvInjectBuildWrapper();
p.getBuildWrappersList().add(wrapper);
wrapper.setInfo(new EnvInjectJobPropertyInfo(
null, "IRRELEVANT_VAR=true", null, null, null, false));

p.getBuildWrappersList().add(new ContributingWrapper("DISPLAY", "BUILD_VAL"));

assertValueInjected(p);
}

@Test
public void doNotOverrideWithPasswordWrapper() throws Exception {
FreeStyleProject p = setupProjectWithDefaultEnvValue();
final EnvInjectPasswordWrapper wrapper = new EnvInjectPasswordWrapper();
wrapper.setPasswordEntries(new EnvInjectPasswordEntry[] {
new EnvInjectPasswordEntry("IRRELEVANT", "value")
});
p.getBuildWrappersList().add(wrapper);

p.getBuildWrappersList().add(new ContributingWrapper("DISPLAY", "BUILD_VAL"));

assertValueInjected(p);
}

private void assertValueInjected(FreeStyleProject p) throws Exception {
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 {
DumbSlave slave = slaveContributing("DISPLAY", "SLAVE_VAL");
FreeStyleProject p = j.jenkins.createProject(FreeStyleProject.class, "project");
p.setAssignedNode(slave);
return p;
}

private DumbSlave slaveContributing(String key, String value) throws Exception {
return j.createOnlineSlave(null, new EnvVars(key, value));
}

private static final class ContributingWrapper extends BuildWrapper {
private final String value;
private final String key;

private ContributingWrapper(String key, String value) {
this.value = value;
this.key = key;
}

@Override
public Environment setUp(
AbstractBuild build, Launcher launcher, BuildListener listener
) throws IOException, InterruptedException {
return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.put(key, value);
}
};
}

@Extension
public static class Descriptor extends hudson.model.Descriptor<BuildWrapper> {
@Override
public String getDisplayName() {
return null;
}
}
}

private static final class ContributingBuilder extends TestBuilder {
private final String value;
private final String key;

private ContributingBuilder(String key, String value) {
this.value = value;
this.key = key;
}

@Override
public boolean perform(
AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener
) throws InterruptedException, IOException {
// Start serving envvar from EnvironmentContributor
contributor.values(key, value);
return true;
}
}

@TestExtension
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 void values(String k, String v) {
value = v;
key = k;
}

@SuppressWarnings("rawtypes")
@Override
public void buildEnvironmentFor(
Run r, EnvVars envs, TaskListener listener
) throws IOException, InterruptedException {
if (key != null && value != null) {
envs.put(key, value);
}
}
}
}