Skip to content

Commit

Permalink
Proper parsing of empty dnsHosts string
Browse files Browse the repository at this point in the history
When DNS hosts are empty, plugin sends DNS override to docker container
("Dns":[""]).  With this patch, correctly declines to override dns when
the cloud configuration does not ask for it.
  • Loading branch information
mgius committed Jun 25, 2014
1 parent 234baad commit c55fcca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,23 @@ public DockerTemplate(String image, String labelString,
this.instanceCap = Integer.parseInt(instanceCapStr);
}

this.dnsHosts = dnsString.split(" ");
//remove all empty volume entries
List<String> temp = new ArrayList<String>();
for(String vol:volumesString.split(" ")) {
if(!vol.isEmpty()) temp.add(vol);
}
this.volumes = temp.toArray(new String[temp.size()]);
this.dnsHosts = splitAndFilterEmpty(dnsString);
this.volumes = splitAndFilterEmpty(volumesString);

readResolve();
}

private String[] splitAndFilterEmpty(String s) {
List<String> temp = new ArrayList<String>();
for (String item : s.split(" ")) {
if (!item.isEmpty())
temp.add(item);
}

return temp.toArray(new String[temp.size()]);

}

public String getInstanceCapStr() {
if (instanceCap==Integer.MAX_VALUE) {
return "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.nirima.jenkins.plugins.docker;

import org.junit.Test;
import static org.junit.Assert.*;

public class DockerTemplateTest {

private DockerTemplate getDockerTemplateInstanceWithDNSHost(String dnsString) {
DockerTemplate instance = new DockerTemplate("image", null, "remoteFs", "credentialsId", " jvmOptions", " javaPath", "prefixStartSlaveCmd", " suffixStartSlaveCmd", "", dnsString, "dockerCommand", "volumes", "hostname", false);
return instance;
}

@Test
public void testDnsHosts() {
DockerTemplate instance;
String[] expected;

instance = getDockerTemplateInstanceWithDNSHost("");
assertEquals(0, instance.dnsHosts.length);

instance = getDockerTemplateInstanceWithDNSHost("8.8.8.8");
expected = new String[]{"8.8.8.8"};

assertEquals(1, instance.dnsHosts.length);
assertArrayEquals(expected, instance.dnsHosts);

instance = getDockerTemplateInstanceWithDNSHost("8.8.8.8 8.8.4.4");
expected = new String[]{"8.8.8.8", "8.8.4.4"};

assertEquals(2, instance.dnsHosts.length);
assertArrayEquals(expected, instance.dnsHosts);

}

}

0 comments on commit c55fcca

Please sign in to comment.