### Question:
SetHostnameCustomizer extends BasicMachineLocationCustomizer { protected String generateHostname(SshMachineLocation machine) { String hostnameTemplate = config.get(HOSTNAME_TEMPLATE); if (Strings.isNonBlank(hostnameTemplate)) { return TemplateProcessor.processTemplateContents(hostnameTemplate, machine, ImmutableMap.<String, Object>of()); } else { return null; } } SetHostnameCustomizer(ConfigBag config); @Override void customize(MachineLocation machine); String setLocalHostname(SshMachineLocation machine); String setLocalHostname(SshMachineLocation machine, String hostName, String domainFixed); static final Logger log; static final ConfigKey<String> FIXED_HOSTNAME; static final ConfigKey<String> FIXED_DOMAIN; static final ConfigKey<String> HOSTNAME_TEMPLATE; static final ConfigKey<String> LOCAL_HOSTNAME; static final ConfigKey<String> LOCAL_IP; static final ConfigKey<Predicate<? super MachineLocation>> MACHINE_FILTER; }### Answer:
@Test public void testGeneratedHostnameUsesPrivateIp() throws Exception { SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("privateAddresses", ImmutableList.of("1.2.3.4", "5.6.7.8")) .configure("address", "4.3.2.1")); assertEquals(customizer.generateHostname(machine), "ip-1-2-3-4-"+machine.getId()); }
@Test public void testGeneratedHostnameUsesPublicIpIfNoPrivate() throws Exception { SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("address", "4.3.2.1")); assertEquals(customizer.generateHostname(machine), "ip-4-3-2-1-"+machine.getId()); }
@Test public void testGeneratedHostnameUsesPublicIpIfEmptyListOfPrivate() throws Exception { SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocation.class) .configure("privateAddresses", ImmutableList.of()) .configure("address", "4.3.2.1")); assertEquals(customizer.generateHostname(machine), "ip-4-3-2-1-"+machine.getId()); }
@Test public void testGeneratedHostnameIfNoIps() throws Exception { SshMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(SshMachineLocationWithNoPublicIps.class) .configure("address", "4.3.2.1")); assertEquals(customizer.generateHostname(machine), "ip-none-"+machine.getId()); }