@Test public void testExternalRegistration(){ EurekaHttpResponse<Applications> response = generateMockResponse(Collections.<InstanceInfo>emptyList()); when(requestHandler.getApplications()).thenReturn(response); Config config = new XmlConfigBuilder().build(); DiscoveryConfig discoveryConfig = config.getNetworkConfig().getJoin().getDiscoveryConfig(); DiscoveryStrategyConfig strategyConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); strategyConfig.addProperty("self-registration", "false"); HazelcastInstance hz1 = factory.newHazelcastInstance(config); HazelcastInstance hz2 = factory.newHazelcastInstance(config); assertClusterSizeEventually(2, hz1); assertClusterSizeEventually(2, hz2); verify(requestHandler, after(5000).never()).register(any(InstanceInfo.class)); }
@Test public void testNamespaceRegistration(){ final String appName = "other"; final String namespace = "hz"; configure(namespace, appName); EurekaHttpResponse<Applications> response = generateMockResponse(Collections.<InstanceInfo>emptyList(), appName); when(requestHandler.getApplications()).thenReturn(response); Config config = new XmlConfigBuilder().build(); DiscoveryConfig discoveryConfig = config.getNetworkConfig().getJoin().getDiscoveryConfig(); DiscoveryStrategyConfig strategyConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); strategyConfig.addProperty("namespace", namespace); HazelcastInstance hz1 = factory.newHazelcastInstance(config); assertClusterSizeEventually(1, hz1); ArgumentCaptor<InstanceInfo> captor = ArgumentCaptor.forClass(InstanceInfo.class); verify(requestHandler, timeout(5000).atLeastOnce()).register(captor.capture()); String actual = captor.getValue().getAppName().toLowerCase(); assertThat(actual, is(appName)); }
@Test public void discoveryStrategyFactoryTest() { JCloudsDiscoveryStrategyFactory jCloudsDiscoveryStrategyFactory = new JCloudsDiscoveryStrategyFactory(); String xmlFileName = "test-jclouds-config.xml"; InputStream xmlResource = JCloudsDiscoveryFactoryTest.class.getClassLoader().getResourceAsStream(xmlFileName); Config config = new XmlConfigBuilder(xmlResource).build(); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); assertEquals(jCloudsDiscoveryStrategyFactory.getDiscoveryStrategyType(), JCloudsDiscoveryStrategy.class); assertEquals(JCloudsDiscoveryStrategy.class.getName(), providerConfig.getClassName()); assertEquals(jCloudsDiscoveryStrategyFactory.getConfigurationProperties().size(), providerConfig.getProperties().size()); assertTrue(jCloudsDiscoveryStrategyFactory. newDiscoveryStrategy(null, null, new HashMap<String, Comparable>()) instanceof DiscoveryStrategy); }
private void validateConfig(final Config config){ final DiscoveryConfig discoveryConfig = config.getNetworkConfig().getJoin().getDiscoveryConfig(); final DiscoveryServiceSettings settings = new DiscoveryServiceSettings().setDiscoveryConfig(discoveryConfig); final DefaultDiscoveryService service = new DefaultDiscoveryService(settings); final Iterator<DiscoveryStrategy> strategies = service.getDiscoveryStrategies().iterator(); assertTrue(strategies.hasNext()); final DiscoveryStrategy strategy = strategies.next(); assertTrue(strategy != null && strategy instanceof AwsDiscoveryStrategy); }
@Test public void parseDiscoveryStrategyConfigPasses() { final AwsDiscoveryStrategyFactory factory = new AwsDiscoveryStrategyFactory(); final Config config = createConfig("test-aws-config.xml"); final JoinConfig joinConfig = config.getNetworkConfig().getJoin(); assertFalse(joinConfig.getAwsConfig().isEnabled()); assertFalse(joinConfig.getTcpIpConfig().isEnabled()); assertFalse(joinConfig.getMulticastConfig().isEnabled()); final DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); assertTrue(discoveryConfig.isEnabled()); assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size()); final DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); final Map<String, Comparable> providerProperties = providerConfig.getProperties(); final Collection<PropertyDefinition> factoryConfigProperties = factory.getConfigurationProperties(); assertEquals(factory.getDiscoveryStrategyType(), AwsDiscoveryStrategy.class); assertEquals(AwsDiscoveryStrategy.class.getName(), providerConfig.getClassName()); assertEquals(factoryConfigProperties.size(), providerProperties.size()); for (AwsProperties prop : AwsProperties.values()) { assertTrue(factoryConfigProperties.contains(prop.getDefinition())); } assertEquals("test-access-key", providerProperties.get("access-key")); assertEquals("test-secret-key", providerProperties.get("secret-key")); assertEquals("test-region", providerProperties.get("region")); assertEquals("test-iam-role", providerProperties.get("iam-role")); assertEquals("ec2.test-host-header", providerProperties.get("host-header")); assertEquals("test-security-group-name", providerProperties.get("security-group-name")); assertEquals("test-tag-key", providerProperties.get("tag-key")); assertEquals("test-tag-value", providerProperties.get("tag-value")); assertEquals("10", providerProperties.get("connection-timeout-seconds")); assertEquals("5702", providerProperties.get("hz-port")); }
@Test public void testParsing() { String xmlFileName = "test-jclouds-config.xml"; InputStream xmlResource = JCloudsDiscoveryFactoryTest.class.getClassLoader().getResourceAsStream(xmlFileName); Config config = new XmlConfigBuilder(xmlResource).build(); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); AwsConfig awsConfig = joinConfig.getAwsConfig(); assertFalse(awsConfig.isEnabled()); TcpIpConfig tcpIpConfig = joinConfig.getTcpIpConfig(); assertFalse(tcpIpConfig.isEnabled()); MulticastConfig multicastConfig = joinConfig.getMulticastConfig(); assertFalse(multicastConfig.isEnabled()); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); assertTrue(discoveryConfig.isEnabled()); assertEquals(1, discoveryConfig.getDiscoveryStrategyConfigs().size()); DiscoveryStrategyConfig providerConfig = discoveryConfig.getDiscoveryStrategyConfigs().iterator().next(); assertEquals(12, providerConfig.getProperties().size()); assertEquals("aws-ec2", providerConfig.getProperties().get("provider")); assertEquals("test", providerConfig.getProperties().get("identity")); assertEquals("test", providerConfig.getProperties().get("credential")); assertEquals("zone1,zone2", providerConfig.getProperties().get("zones")); assertEquals("region1,region2", providerConfig.getProperties().get("regions")); assertEquals("zone1,zone2", providerConfig.getProperties().get("zones")); assertEquals("tag1,tag2", providerConfig.getProperties().get("tag-keys")); assertEquals("tagvalue1,tagvalue2", providerConfig.getProperties().get("tag-values")); assertEquals("group", providerConfig.getProperties().get("group")); assertEquals("5702", providerConfig.getProperties().get("hz-port")); assertEquals("myfile.json", providerConfig.getProperties().get("credentialPath")); assertEquals("myRole", providerConfig.getProperties().get("role-name")); assertEquals("http://foo/bar", providerConfig.getProperties().get("endpoint")); }
public HazelcastInstance newInstance() { String name = NAME.get(); String password = PASS.get(); if (StringUtils.isBlank(name)) { name = hazelcastDao.getGroupName(); } if (StringUtils.isBlank(password)) { password = hazelcastDao.getGroupPassword(); } Config config = new Config(); if (JMX.get()) { config.setProperty("hazelcast.jmx", "true"); } config.setProperty("hazelcast.logging.type", LOGGING.get()); config.setProperty("hazelcast.discovery.enabled", "true"); GroupConfig groupConfig = new GroupConfig(); groupConfig.setName(name); groupConfig.setPassword(password); config.setGroupConfig(groupConfig); DiscoveryStrategyConfig dsc = new DiscoveryStrategyConfig(dbDiscoveryFactory); DiscoveryConfig dc = new DiscoveryConfig(); dc.setDiscoveryStrategyConfigs(Arrays.asList(dsc)); JoinConfig joinConfig = new JoinConfig(); joinConfig.setDiscoveryConfig(dc); joinConfig.getTcpIpConfig().setEnabled(false); joinConfig.getMulticastConfig().setEnabled(false); joinConfig.getAwsConfig().setEnabled(false); NetworkConfig nc = new NetworkConfig(); nc.setPort(DBDiscovery.DEFAULT_PORT.get()); nc.setJoin(joinConfig); nc.setPublicAddress(dbDiscovery.getLocalConfig().getAdvertiseAddress()); config.setNetworkConfig(nc); HazelcastInstance hi = Hazelcast.newHazelcastInstance(config); while (!hi.getPartitionService().isClusterSafe()) { log.info("Waiting for cluster to be in a steady state"); try { Thread.sleep(2000L); } catch (InterruptedException e) { throw new IllegalStateException("Waiting for cluster to be in a steady state", e); } } return hi; }
@Test public void test() { Config config = new XmlConfigBuilder().build(); config.setProperty("hazelcast.discovery.enabled", "true"); JoinConfig joinConfig = config.getNetworkConfig().getJoin(); joinConfig.getMulticastConfig().setEnabled(false); joinConfig.getTcpIpConfig().setEnabled(false); DiscoveryConfig discoveryConfig = joinConfig.getDiscoveryConfig(); DiscoveryStrategyFactory discoveryStrategyFactory = new SomeRestServiceDiscoveryStrategyFactory(); DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(discoveryStrategyFactory); discoveryConfig.addDiscoveryStrategyConfig(discoveryStrategyConfig); config.getNetworkConfig().setPort(55667); HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config); config.getNetworkConfig().setPort(1122); HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, hazelcastInstance1.getCluster().getMembers().size()); assertEquals(2, hazelcastInstance2.getCluster().getMembers().size()); Hazelcast.shutdownAll(); }