diff --git a/core/src/main/java/hudson/slaves/EnvironmentVariablesNodeProperty.java b/core/src/main/java/hudson/slaves/EnvironmentVariablesNodeProperty.java index dd3ec84b131d..3986fbdd2c47 100644 --- a/core/src/main/java/hudson/slaves/EnvironmentVariablesNodeProperty.java +++ b/core/src/main/java/hudson/slaves/EnvironmentVariablesNodeProperty.java @@ -28,6 +28,7 @@ import hudson.EnvVars; import hudson.Extension; import hudson.Launcher; +import hudson.Util; import hudson.model.AbstractBuild; import hudson.model.BuildListener; import hudson.model.ComputerSet; @@ -118,16 +119,20 @@ private Entry(Map.Entry e) { @DataBoundConstructor public Entry(String key, String value) { - this.key = key; - this.value = value; + this.key = Util.fixEmptyAndTrim(key); + this.value = Util.fixEmptyAndTrim(value); } } private static EnvVars toMap(List entries) { EnvVars map = new EnvVars(); - if (entries != null) - for (Entry entry : entries) - map.put(entry.key, entry.value); + if (entries != null) { + for (Entry entry : entries) { + if(entry.key != null && entry.value != null) { + map.put(entry.key, entry.value); + } + } + } return map; } diff --git a/test/src/test/java/hudson/slaves/EnvironmentVariableNodePropertyTest.java b/test/src/test/java/hudson/slaves/EnvironmentVariableNodePropertyTest.java index 258c43868d90..fa5241710796 100644 --- a/test/src/test/java/hudson/slaves/EnvironmentVariableNodePropertyTest.java +++ b/test/src/test/java/hudson/slaves/EnvironmentVariableNodePropertyTest.java @@ -1,6 +1,7 @@ package hudson.slaves; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; @@ -141,6 +142,188 @@ public void testFormRoundTripForAgent() throws Exception { assertEquals("value", prop.getEnvVars().get("KEY")); } + @Test + public void testExtraSpacesForController() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry(" KEY ", " globalValue ")))); + + Map envVars = executeBuild(j.jenkins); + + assertEquals("globalValue", envVars.get("KEY")); + } + + @Test + public void testExtraSpacesForAgent() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry(" KEY ", " agentValue ")); + Map envVars = executeBuild(agent); + assertEquals("agentValue", envVars.get("KEY")); + } + + @Test + public void testEmptyForController() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry("KEY", "")))); + + Map envVars = executeBuild(j.jenkins); + + assertNull(envVars.get("KEY")); + } + + @Test + public void testEmptyForAgent() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", "")); + Map envVars = executeBuild(agent); + assertNull(envVars.get("KEY")); + } + + @Test + public void testOnlySpacesForController() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry("KEY", " ")))); + + Map envVars = executeBuild(j.jenkins); + + assertNull(envVars.get("KEY")); + } + + @Test + public void testOnlySpacesForAgent() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", " ")); + Map envVars = executeBuild(agent); + assertNull(envVars.get("KEY")); + } + + @Test + public void testFormRoundTripForControllerWithEmpty() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry("", "")))); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(j.jenkins, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForAgentWithEmpty() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("", "")); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(agent, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, agent.getNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForControllerWithSpaces() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry(" ", " ")))); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(j.jenkins, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForAgentWithSpaces() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry(" ", " ")); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(agent, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, agent.getNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForControllerWithSpacesAndKey() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry("KEY", " ")))); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(j.jenkins, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForAgentWithSpacesAndKey() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", " ")); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(agent, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, agent.getNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForControllerWithEmptyAndKey() throws Exception { + j.jenkins.getGlobalNodeProperties().replaceBy( + Set.of(new EnvironmentVariablesNodeProperty( + new EnvironmentVariablesNodeProperty.Entry("KEY", "")))); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(j.jenkins, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, j.jenkins.getGlobalNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = j.jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + + @Test + public void testFormRoundTripForAgentWithEmptyAndKey() throws Exception { + setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", "")); + + WebClient webClient = j.createWebClient(); + HtmlPage page = webClient.getPage(agent, "configure"); + HtmlForm form = page.getFormByName("config"); + j.submit(form); + + assertEquals(1, agent.getNodeProperties().toList().size()); + + EnvironmentVariablesNodeProperty prop = agent.getNodeProperties().get(EnvironmentVariablesNodeProperty.class); + assertEquals(0, prop.getEnvVars().size()); + } + // //////////////////////// setup ////////////////////////////////////////// @Before