HostsXmlProvisioner implements HostProvisioner { @Override public HostSpec allocateHost(String alias) { if (alias.equals(IMPLICIT_ADMIN_HOSTALIAS)) { if (hosts.asCollection().size() > 1) { throw new IllegalArgumentException("More than 1 host specified (" + hosts.asCollection().size() + ") and <admin> not specified"); } else { return host2HostSpec(getFirstHost()); } } else if (alias.equals(Container.SINGLENODE_CONTAINER_SERVICESPEC)) { return host2HostSpec(getFirstHost()); } for (Host host : hosts.asCollection()) { if (host.aliases().contains(alias)) { return new HostSpec(host.hostname(), host.aliases(), Optional.empty()); } } throw new IllegalArgumentException("Unable to find host for alias '" + alias + "'"); } HostsXmlProvisioner(Reader hosts); @Override HostSpec allocateHost(String alias); @Override List<HostSpec> prepare(ClusterSpec cluster, Capacity quantity, ProvisionLogger logger); static final String IMPLICIT_ADMIN_HOSTALIAS; }
@Test(expected = IllegalArgumentException.class) public void require_exception_when_unknown_hosts_alias() { HostsXmlProvisioner hostProvisioner = createProvisioner(oneHost); hostProvisioner.allocateHost("unknown"); }
@Test public void require_singlenode_HostAlias_is_used_if_hosts_xml() { HostsXmlProvisioner hostProvisioner = createProvisioner(oneHost); HostSpec hostSpec = hostProvisioner.allocateHost(Container.SINGLENODE_CONTAINER_SERVICESPEC); assertThat(hostSpec.hostname(), is("test1.yahoo.com")); }