diff --git a/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java b/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java index 4f72c7ca..93323ac6 100644 --- a/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java +++ b/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java @@ -2,16 +2,20 @@ import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; -import hudson.model.Hudson; +import hudson.model.Node; +import hudson.model.Result; import hudson.slaves.EnvironmentVariablesNodeProperty; import hudson.slaves.NodeProperty; import hudson.slaves.NodePropertyDescriptor; +import hudson.tasks.Shell; import hudson.util.DescribableList; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; @@ -33,7 +37,8 @@ public void testGlobalPropertiesWithWORKSPACE() throws Exception { final String workspaceName = "WORKSPACE"; //A global node property TEST_WORKSPACE - DescribableList, NodePropertyDescriptor> globalNodeProperties = Hudson.getInstance().getGlobalNodeProperties(); + DescribableList, NodePropertyDescriptor> globalNodeProperties = jenkins.getInstance() + .getGlobalNodeProperties(); globalNodeProperties.add(new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry(testWorkspaceVariableName, testWorkspaceVariableValue))); EnvInjectJobProperty jobProperty = new EnvInjectJobProperty(); @@ -68,8 +73,10 @@ public void testGlobalPropertiesSetWORKSPACE() throws Exception { final String testJobVariableName = "TEST_JOB_WORKSPACE"; final String testJobVariableExprValue = "${WORKSPACE}"; - DescribableList, NodePropertyDescriptor> globalNodeProperties = Hudson.getInstance().getGlobalNodeProperties(); - globalNodeProperties.add(new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry(testGlobalVariableName, testGlobalVariableValue))); + DescribableList, NodePropertyDescriptor> globalNodeProperties = jenkins.getInstance() + .getGlobalNodeProperties(); + globalNodeProperties.add(new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry + (testGlobalVariableName, testGlobalVariableValue))); StringBuffer propertiesContent = new StringBuffer(); propertiesContent.append(testJobVariableName).append("=").append(testJobVariableExprValue); @@ -91,4 +98,62 @@ public void testGlobalPropertiesSetWORKSPACE() throws Exception { assertEquals(result_testGlobalVariableName, result_testJobVariableName); } + + @Test + public void testChangeOfGlobalPropertyGetsRecognizedWhenWithoutJobPropertyAndRunOnSlaves() throws Exception { + FreeStyleProject project = jenkins.createFreeStyleProject(); + + final String testVariableName = "TESTVAR"; + final String testVariableValue = "value1"; + final String testVariableValueAfterChange = "value2"; + + //A global node property TESTVAR + DescribableList, NodePropertyDescriptor> globalNodeProperties = jenkins.getInstance() + .getGlobalNodeProperties(); + EnvironmentVariablesNodeProperty.Entry testVarEntry = new EnvironmentVariablesNodeProperty.Entry(testVariableName, testVariableValue); + EnvironmentVariablesNodeProperty testVarNodePropertyItem = new EnvironmentVariablesNodeProperty(testVarEntry); + globalNodeProperties.add(testVarNodePropertyItem); + + Node slaveNode = jenkins.createOnlineSlave(); + project.setAssignedNode(slaveNode); + project.getBuildersList().add(new Shell("echo \"TESTVAR=$TESTVAR\"")); + + // we do NOT add a jobProperty - we want to test "default" behaviour of jenkins with installed envinject plugin + // so we run the build right away + FreeStyleBuild build = project.scheduleBuild2(0).get(); + assertEquals(Result.SUCCESS, build.getResult()); + assertEquals(slaveNode.getNodeName(), build.getBuiltOn().getNodeName()); + + // assert correct injection of testVariable #1 + org.jenkinsci.lib.envinject.EnvInjectAction action = build.getAction(org.jenkinsci.lib.envinject.EnvInjectAction.class); + Map envVars = action.getEnvMap(); + String actualTestVariableValueInBuild = envVars.get(testVariableName); + assertNotNull("actual testVariableValue is null", actualTestVariableValueInBuild); + assertEquals(testVariableValue, actualTestVariableValueInBuild); + + Set> beforeChange = jenkins.getInstance().getComputer(slaveNode.getNodeName()) + .getEnvironment() + .entrySet(); + + // now we change the global property variable value... + testVarEntry = new EnvironmentVariablesNodeProperty.Entry(testVariableName, testVariableValueAfterChange); + jenkins.getInstance().getGlobalNodeProperties().add(new EnvironmentVariablesNodeProperty(testVarEntry)); + + Set> afterChange = jenkins.getInstance().getComputer(slaveNode.getNodeName()).getEnvironment() + .entrySet(); + // environment of the slave does not change without restarting it. assert it to make test fail if there will be + // some kind of auto-restart to reload config. + assertEquals(beforeChange, afterChange); + assertEquals(beforeChange.toString(), afterChange.toString()); + + //...run the job again... + FreeStyleBuild secondBuild = project.scheduleBuild2(0).get(); + assertEquals(Result.SUCCESS, secondBuild.getResult()); + assertEquals(slaveNode.getNodeName(), secondBuild.getBuiltOn().getNodeName()); + + //...and expect the testvariable to have the changed value + org.jenkinsci.lib.envinject.EnvInjectAction action2 = secondBuild.getAction(org.jenkinsci.lib.envinject.EnvInjectAction.class); + assertNotNull("actual testVariableValue is null", action2.getEnvMap().get(testVariableName)); + assertEquals(testVariableValueAfterChange, action2.getEnvMap().get(testVariableName)); + } }