Skip to content

Commit

Permalink
InetAddress.getLocalHost considered evil - can fail unit tests just b…
Browse files Browse the repository at this point in the history
…ecause network/localhost setup is set screwy by wifi points; use Networking.getLocalHost instead
  • Loading branch information
ahgittin committed Mar 4, 2014
1 parent 6a052d7 commit c31b15c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package brooklyn.location.basic;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
Expand All @@ -13,8 +12,8 @@
import brooklyn.location.PortRange;
import brooklyn.location.PortSupplier;
import brooklyn.util.collections.MutableMap;
import brooklyn.util.net.Networking;

import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;

Expand All @@ -28,15 +27,11 @@ public class SimulatedLocation extends AbstractLocation implements MachineProvis

private static final InetAddress address;
static {
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
throw Throwables.propagate(e);
}
address = Networking.getLocalHost();
}

Iterable permittedPorts = PortRanges.fromString("1+");
Set usedPorts = Sets.newLinkedHashSet();
Iterable<Integer> permittedPorts = PortRanges.fromString("1+");
Set<Integer> usedPorts = Sets.newLinkedHashSet();

public SimulatedLocation() {
this(MutableMap.<String,Object>of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import org.testng.annotations.Test
import brooklyn.location.Location
import brooklyn.location.NoMachinesAvailableException
import brooklyn.location.PortRange
import brooklyn.util.net.Networking;

public class LocalhostMachineProvisioningLocationTest {
@Test
public void defaultInvocationCanProvisionALocalhostInstance() {
LocalhostMachineProvisioningLocation provisioner = new LocalhostMachineProvisioningLocation()
SshMachineLocation machine = provisioner.obtain()
assertNotNull machine
assertEquals machine.address, InetAddress.localHost
assertEquals machine.address, Networking.localHost
}

@Test
Expand All @@ -37,12 +38,12 @@ public class LocalhostMachineProvisioningLocationTest {
// first machine
SshMachineLocation first = provisioner.obtain()
assertNotNull first
assertEquals first.address, InetAddress.localHost
assertEquals first.address, Networking.localHost

// second machine
SshMachineLocation second = provisioner.obtain()
assertNotNull second
assertEquals second.address, InetAddress.localHost
assertEquals second.address, Networking.localHost

// third machine
SshMachineLocation third = provisioner.obtain()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.ByteArrayOutputStream;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -20,6 +19,7 @@

import brooklyn.util.collections.MutableMap;
import brooklyn.util.internal.ssh.SshTool;
import brooklyn.util.net.Networking;
import brooklyn.util.stream.Streams;
import brooklyn.util.text.Identifiers;
import brooklyn.util.time.Time;
Expand Down Expand Up @@ -96,7 +96,7 @@ public void testConsecutiveBigStdinCommands() throws Exception {
public void testConsecutiveSmallCommandsWithDifferentProperties() throws Exception {
final Map<String, ?> emptyProperties = Collections.emptyMap();
final Map<String, ?> customProperties = MutableMap.of(
"address", InetAddress.getLocalHost(),
"address", Networking.getLocalHost(),
SshTool.PROP_SESSION_TIMEOUT.getName(), 20000,
SshTool.PROP_CONNECT_TIMEOUT.getName(), 50000,
SshTool.PROP_SCRIPT_HEADER.getName(), "#!/bin/bash");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import brooklyn.util.collections.MutableMap;
import brooklyn.util.internal.ssh.SshTool;
import brooklyn.util.internal.ssh.sshj.SshjTool;
import brooklyn.util.net.Networking;
import brooklyn.util.stream.Streams;
import brooklyn.util.time.Duration;

Expand Down Expand Up @@ -61,7 +62,7 @@ public static void reset() {
public void setUp() throws Exception {
managementContext = new LocalManagementContext();
host = managementContext.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class)
.configure("address", InetAddress.getLocalHost())
.configure("address", Networking.getLocalHost())
.configure(SshTool.PROP_TOOL_CLASS, RecordingSshjTool.class.getName()));
}

Expand Down Expand Up @@ -122,7 +123,7 @@ public void testSshCacheExpiresEvenIfNotUsed() throws Exception {

public Map<String, Object> customSshConfigKeys() throws UnknownHostException {
return MutableMap.<String, Object>of(
"address", InetAddress.getLocalHost(),
"address", Networking.getLocalHost(),
SshTool.PROP_SESSION_TIMEOUT.getName(), 20000,
SshTool.PROP_CONNECT_TIMEOUT.getName(), 50000,
SshTool.PROP_SCRIPT_HEADER.getName(), "#!/bin/bash");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SshMachineLocationTest {

@BeforeMethod(alwaysRun=true)
public void setUp() throws Exception {
host = new SshMachineLocation(MutableMap.of("address", InetAddress.getLocalHost()));
host = new SshMachineLocation(MutableMap.of("address", Networking.getLocalHost()));
}

@AfterMethod(alwaysRun=true)
Expand Down Expand Up @@ -67,7 +67,7 @@ public void testSshExecCommands() throws Exception {
// For issue #230
@Test(groups = "Integration")
public void testOverridingPropertyOnExec() throws Exception {
SshMachineLocation host = new SshMachineLocation(MutableMap.of("address", InetAddress.getLocalHost(), "sshPrivateKeyData", "wrongdata"));
SshMachineLocation host = new SshMachineLocation(MutableMap.of("address", Networking.getLocalHost(), "sshPrivateKeyData", "wrongdata"));

OutputStream outStream = new ByteArrayOutputStream();
String expectedName = Os.user();
Expand All @@ -79,7 +79,7 @@ public void testOverridingPropertyOnExec() throws Exception {

@Test(groups = "Integration", expectedExceptions={IllegalStateException.class, SshException.class})
public void testSshRunWithInvalidUserFails() throws Exception {
SshMachineLocation badHost = new SshMachineLocation(MutableMap.of("user", "doesnotexist", "address", InetAddress.getLocalHost()));
SshMachineLocation badHost = new SshMachineLocation(MutableMap.of("user", "doesnotexist", "address", Networking.getLocalHost()));
badHost.execScript("mysummary", ImmutableList.of("whoami; exit"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import brooklyn.location.basic.LocalhostMachineProvisioningLocation
import brooklyn.test.entity.TestApplicationImpl
import brooklyn.util.internal.Repeater
import brooklyn.util.internal.TimeExtras
import brooklyn.util.net.Networking

class Infinispan5ServerIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(Infinispan5ServerIntegrationTest.class)
Expand Down Expand Up @@ -45,7 +46,7 @@ class Infinispan5ServerIntegrationTest {
boolean socketClosed = new Repeater("Checking Infinispan has shut down")
.repeat {
if (shutdownSocket) shutdownSocket.close();
try { shutdownSocket = new Socket(InetAddress.localHost, DEFAULT_PORT); }
try { shutdownSocket = new Socket(Networking.localHost, DEFAULT_PORT); }
catch (SocketException e) { gotException = e; return; }
gotException = null
}
Expand All @@ -61,7 +62,7 @@ class Infinispan5ServerIntegrationTest {
}

public void ensureIsUp() {
Socket socket = new Socket(InetAddress.localHost, DEFAULT_PORT);
Socket socket = new Socket(Networking.localHost, DEFAULT_PORT);
socket.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import brooklyn.management.ManagementContext
import brooklyn.management.internal.LocalManagementContext
import brooklyn.test.entity.TestApplication
import brooklyn.util.collections.MutableMap
import brooklyn.util.net.Networking;
import brooklyn.util.text.Strings

/**
Expand Down Expand Up @@ -48,7 +49,7 @@ public class MySqlIntegrationTest {
}

// can start in AWS by running this -- or use brooklyn CLI/REST for most clouds, or programmatic/config for set of fixed IP machines
static String hostname = java.net.InetAddress.getLocalHost().getHostName()
static String hostname = Networking.getLocalHost().getHostName()

//from http://www.vogella.de/articles/MySQLJava/article.html
public static final String CREATION_SCRIPT = """
Expand Down

0 comments on commit c31b15c

Please sign in to comment.