@Override public PortRange apply(String x) { return fromString(x); } });
protected void assertFromStringThrowsIllegalArgumentException(String range) { try { PortRanges.fromString(range); Assert.fail(); } catch (IllegalArgumentException e) { // success } }
@Test public void testFromStringWithSpacesToString() { PortRange r = PortRanges.fromString(" 80 , 8080 , 8000 , 8080 - 8099 "); Assert.assertEquals(r.toString(), "80,8080,8000,8080-8099"); }
@Test public void testFromString() { PortRange r = PortRanges.fromString("80,8080,8000,8080-8099"); assertContents(r, 80, 8080, 8000, 8080, 8081, 8082, 8083, 8084, 8085, 8086, 8087, 8088, 8089, 8090, 8091, 8092, 8093, 8094, 8095, 8096, 8097, 8098, 8099); }
@Test public void testFromStringWithSpaces() { PortRange r = PortRanges.fromString(" 80 , 8080 , 8000 , 8080 - 8099 "); assertContents(r, 80, 8080, 8000, 8080, 8081, 8082, 8083, 8084, 8085, 8086, 8087, 8088, 8089, 8090, 8091, 8092, 8093, 8094, 8095, 8096, 8097, 8098, 8099); }
private static void urlContainsPort(NginxController nginx, PortAttributeSensorAndConfigKey sensor, String portRange) { Integer port = nginx.getAttribute(sensor); Assert.assertTrue(Iterables.contains(PortRanges.fromString(portRange), port), "Port "+port+" not in range "+portRange); String url = Preconditions.checkNotNull(nginx.getAttribute(LoadBalancer.MAIN_URI), "main uri").toString(); Assert.assertTrue(url.contains(":"+port), "URL does not contain expected port; port "+port+", url "+url); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+"), false).start(); httpsService = new HttpService(PortRanges.fromString("9000+"), true).start(); }
@Test public void testObtainPortDoesNotUsePreReservedPorts() { host = new SshMachineLocation(MutableMap.of("address", Networking.getReachableLocalHost(), "usedPorts", ImmutableSet.of(8000))); assertEquals(host.obtainPort(PortRanges.fromString("8000")), -1); assertEquals(host.obtainPort(PortRanges.fromString("8000+")), 8001); }
@Test public void testSimulatedRestrictedPermitted() throws Exception { loc.setPermittedPorts(PortRanges.fromString("1240+")); ConfigToAttributes.apply(entity, ps); int p = entity.getAttribute(ps); assertEquals(p, 1240); }
@Test(groups={"Integration", "Broken"}) public void testSetsBrooklynWebConsolePort() throws Exception { BrooklynNode brooklynNode = app.createAndManageChild(newBrooklynNodeSpecForTest() .configure(BrooklynNode.HTTP_PORT, PortRanges.fromString("45000+"))); app.start(locs); log.info("started "+app+" containing "+brooklynNode+" for "+JavaClassNames.niceClassAndMethod()); Integer httpPort = brooklynNode.getAttribute(BrooklynNode.HTTP_PORT); URI webConsoleUri = brooklynNode.getAttribute(BrooklynNode.WEB_CONSOLE_URI); assertTrue(httpPort >= 45000 && httpPort < 54100, "httpPort="+httpPort); assertEquals((Integer)webConsoleUri.getPort(), httpPort); HttpTestUtils.assertHttpStatusCodeEquals(webConsoleUri.toString(), 200, 401); }
@Override @DataProvider(name = "basicEntities") public Object[][] basicEntities() { TestApplication jettyApp = newTestApplication(); Jetty6Server jetty = jettyApp.createAndManageChild(EntitySpec.create(Jetty6Server.class) .configure(Jetty6Server.HTTP_PORT, PortRanges.fromString(DEFAULT_HTTP_PORT))); return new JavaWebAppSoftwareProcess[][] { new JavaWebAppSoftwareProcess[] {jetty} }; }
protected PortRange getPortRange() { if (portRange == null) { portRange = subnetTier.getConfig(PORT_RANGE); if (portRange == null && jcloudsLocation != null) { portRange = jcloudsLocation.getConfig(PORT_RANGE); } if (portRange == null) { portRange = managementContext.getConfig().getConfig(PORT_RANGE); } if (portRange == null) { int startingPort = managementContext.getConfig().getConfig(PortForwardManager.PORT_FORWARD_MANAGER_STARTING_PORT); portRange = PortRanges.fromString(startingPort+"+"); } } return portRange; } }
@Test(groups="Live") public void testPostgresScriptAndAccess() throws Exception { ChefLiveTestSupport.installBrooklynChefHostedConfig(app); PortRange randomPort = PortRanges.fromString(String.format("%d+", 5420 + new Random().nextInt(10))); psql = app.createAndManageChild(PostgreSqlSpecs.specChef() .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, PostgreSqlIntegrationTest.CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, randomPort) .configure(PostgreSqlNode.SHARED_MEMORY, "8MB") ); app.start(ImmutableList.of(targetLocation)); String url = psql.getAttribute(DatastoreCommon.DATASTORE_URL); log.info("Trying to connect to "+psql+" at "+url); Assert.assertNotNull(url); Assert.assertTrue(url.contains("542")); new VogellaExampleAccess("org.postgresql.Driver", url).readModifyAndRevertDataBase(); }
@Test(groups = "Integration") public void testConfiguresNginxInstancesWithInheritedPortConfig() { loadBalancerCluster = app.createAndManageChild(EntitySpec.create(LoadBalancerCluster.class) .configure(LoadBalancerCluster.MEMBER_SPEC, nginxSpec) .configure("initialSize", 1) .configure(NginxController.DOMAIN_NAME, "localhost") .configure(NginxController.PROXY_HTTP_PORT, PortRanges.fromString("8765+"))); app.start(ImmutableList.of(localhostProvisioningLoc)); NginxController nginx1 = Iterables.getOnlyElement(findNginxs()); loadBalancerCluster.resize(2); NginxController nginx2 = Iterables.getOnlyElement(Iterables.filter(findNginxs(), Predicates.not(Predicates.in(ImmutableList.of(nginx1))))); assertEquals((int) nginx1.getAttribute(NginxController.PROXY_HTTP_PORT), 8765); assertEquals((int) nginx2.getAttribute(NginxController.PROXY_HTTP_PORT), 8766); }
@Override public String openPortForwarding(String endpoint, String vDC, String identity, String credential, String protocol, String original, String originalPortRange, String translated) { LOG.info("creating nat rule {} {} -> {}, on {} @ {} (vDC {})", new Object[]{protocol, original, translated, identity, endpoint, vDC}); HostAndPort originalHostAndPort = HostAndPort.fromString(original); HostAndPort translatedHostAndPort = HostAndPort.fromString(translated); Preconditions.checkArgument(translatedHostAndPort.hasPort(), "translated %s must include port", translated); try { HostAndPort result = dispatcher().openPortForwarding(endpoint, vDC, identity, credential, new PortForwardingConfig() .protocol(Protocol.valueOf(protocol.toUpperCase())) .publicEndpoint(originalHostAndPort) .publicPortRange(Strings.isBlank(originalPortRange) ? null : PortRanges.fromString(originalPortRange)) .targetEndpoint(translatedHostAndPort)); return result.toString(); } catch (Exception e) { throw Exceptions.propagate(e); } }
@Test(groups = "Integration") public void testOpenHttpConsoleWebPort() throws Exception { BrooklynNodeWithOpenedPorts brooklynNode = app.createAndManageChild( EntitySpec.create(BrooklynNodeWithOpenedPorts.class) .configure(BrooklynNode.ON_EXISTING_PROPERTIES_FILE, BrooklynNode.ExistingFileBehaviour.DO_NOT_USE) .configure(BrooklynNode.NO_WEB_CONSOLE_AUTHENTICATION, true) .configure(BrooklynNode.HTTP_PORT, PortRanges.fromString("8082+"))); app.start(ImmutableList.of(loc)); assertNotNull(app); log.info("started " + app + " containing " + brooklynNode + " for " + JavaClassNames.niceClassAndMethod()); EntityAsserts.assertAttributeEqualsEventually(brooklynNode, BrooklynNode.SERVICE_UP, true); assertTrue(brooklynNode.getRequiredOpenPorts().contains(8082)); brooklynNode.stop(); EntityAsserts.assertAttributeEquals(brooklynNode, BrooklynNode.SERVICE_UP, false); }
@Test public void testGetRequiredOpenPortsGetsDynamicallyAddedPortBasedKeys() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); PortAttributeSensorAndConfigKey newTestConfigKeyPort = ConfigKeys.newPortSensorAndConfigKey("new.test.config.port.string.first", "port", "7777+"); PortAttributeSensorAndConfigKey newTestConfigKeyPort2 = ConfigKeys.newPortSensorAndConfigKey("new.test.config.port.string.second", "port"); ConfigKey<Object> newTestConfigKeyObject = ConfigKeys.newConfigKey(Object.class, "new.test.config.object"); ConfigKey<String> newTestConfigKeyString = ConfigKeys.newStringConfigKey("new.test.config.key.string"); entity.config().set(newTestConfigKeyPort, PortRanges.fromString("8888+")); entity.config().set(newTestConfigKeyPort2, PortRanges.fromInteger(9999)); entity.config().set(newTestConfigKeyObject, PortRanges.fromInteger(2222)); entity.config().set(newTestConfigKeyString, "foo.bar"); Collection<Integer> dynamicRequiredOpenPorts = InboundPortsUtils.getRequiredOpenPorts(entity, ImmutableSet.<ConfigKey<?>>of(), true, null); Assert.assertTrue(dynamicRequiredOpenPorts.contains(8888)); Assert.assertTrue(dynamicRequiredOpenPorts.contains(9999)); Assert.assertTrue(dynamicRequiredOpenPorts.contains(2222)); }
@Override @DataProvider(name = "basicEntities") public Object[][] basicEntities() { TestApplication jboss6App = newTestApplication(); JBoss6Server jboss6 = jboss6App.createAndManageChild(EntitySpec.create(JBoss6Server.class) .configure(JBoss6Server.PORT_INCREMENT, PORT_INCREMENT)); TestApplication jboss7App = newTestApplication(); JBoss7Server jboss7 = jboss7App.createAndManageChild(EntitySpec.create(JBoss7Server.class) .configure(JBoss7Server.HTTP_PORT, PortRanges.fromString(DEFAULT_HTTP_PORT))); return new JavaWebAppSoftwareProcess[][] { new JavaWebAppSoftwareProcess[] {jboss6}, new JavaWebAppSoftwareProcess[] {jboss7} }; }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { app = TestApplication.Factory.newManagedInstanceForTests(); mgmt = app.getManagementContext(); loc = app.newLocalhostProvisioningLocation(); entity = app.createAndManageChild(EntitySpec.create(NodeJsWebAppService.class) .configure(NodeJsWebAppService.HTTP_PORT, PortRanges.fromString(DEFAULT_HTTP_PORT)) .configure("gitRepoUrl", GIT_REPO_URL) .configure("appFileName", APP_FILE) .configure("appName", APP_NAME)); }
@Override public void init() { sensors().set(Attributes.HOSTNAME, JmxHelperTest.LOCALHOST_NAME); sensors().set(UsesJmx.JMX_PORT, LocalhostMachineProvisioningLocation.obtainPort(PortRanges.fromString( // just doing "40123+" was not enough to avoid collisions (on 40125), // observed on jenkins, not sure why but // maybe something else had a UDP connection we weren't detected, // or the static lock our localhost uses was being bypassed; // this should improve things (2016-01) NetworkingTestUtils.randomPortAround(40000)+"+"))); // only supports no-agent, at the moment config().set(UsesJmx.JMX_AGENT_MODE, JmxAgentModes.NONE); sensors().set(UsesJmx.RMI_REGISTRY_PORT, -1); // -1 means to use the JMX_PORT only ConfigToAttributes.apply(this, UsesJmx.JMX_CONTEXT); } }