Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim the environment variables keys and values #10024

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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 @@ -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;
Expand Down Expand Up @@ -118,16 +119,20 @@ private Entry(Map.Entry<String, String> 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<Entry> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, String> envVars = executeBuild(j.jenkins);

assertEquals("globalValue", envVars.get("KEY"));
}

@Test
public void testExtraSpacesForAgent() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry(" KEY ", " agentValue "));
Map<String, String> 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<String, String> envVars = executeBuild(j.jenkins);

assertNull(envVars.get("KEY"));
}

@Test
public void testEmptyForAgent() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", ""));
Map<String, String> 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<String, String> envVars = executeBuild(j.jenkins);

assertNull(envVars.get("KEY"));
}

@Test
public void testOnlySpacesForAgent() throws Exception {
setVariables(agent, new EnvironmentVariablesNodeProperty.Entry("KEY", " "));
Map<String, String> 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
Expand Down
Loading