From 8eaaca1da34fa64d4073279da36b1c427d9a306d Mon Sep 17 00:00:00 2001 From: recena Date: Tue, 18 Aug 2015 22:15:22 +0200 Subject: [PATCH 1/2] Added and reviewed tests from @kazesberger --- .../envinject/GlobalPropertiesTest.java | 74 +++++++++++++++++-- 1 file changed, 68 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java b/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java index 4f72c7ca..d34fc8cf 100644 --- a/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java +++ b/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java @@ -1,17 +1,18 @@ package org.jenkinsci.plugins.envinject; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.model.Hudson; +import hudson.model.*; 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 +34,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 +70,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 +95,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); + Hudson.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)); + } } From e48b3339ebb0c48c636d1190c91e1ee87c8ae3e8 Mon Sep 17 00:00:00 2001 From: recena Date: Tue, 18 Aug 2015 22:36:30 +0200 Subject: [PATCH 2/2] Reviewed imports --- .../jenkinsci/plugins/envinject/GlobalPropertiesTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java b/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java index d34fc8cf..93323ac6 100644 --- a/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java +++ b/src/test/java/org/jenkinsci/plugins/envinject/GlobalPropertiesTest.java @@ -1,6 +1,9 @@ package org.jenkinsci.plugins.envinject; -import hudson.model.*; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Node; +import hudson.model.Result; import hudson.slaves.EnvironmentVariablesNodeProperty; import hudson.slaves.NodeProperty; import hudson.slaves.NodePropertyDescriptor; @@ -134,7 +137,7 @@ public void testChangeOfGlobalPropertyGetsRecognizedWhenWithoutJobPropertyAndRun // now we change the global property variable value... testVarEntry = new EnvironmentVariablesNodeProperty.Entry(testVariableName, testVariableValueAfterChange); - Hudson.getInstance().getGlobalNodeProperties().add(new EnvironmentVariablesNodeProperty(testVarEntry)); + jenkins.getInstance().getGlobalNodeProperties().add(new EnvironmentVariablesNodeProperty(testVarEntry)); Set> afterChange = jenkins.getInstance().getComputer(slaveNode.getNodeName()).getEnvironment() .entrySet();