@Override public PortRange apply(Integer x) { return fromInteger(x); } });
@Override public void openFirewallPortAsync(EntityAndAttribute<String> publicIp, int port, Protocol protocol, Cidr accessingCidr) { openFirewallPortRangeAsync(publicIp, PortRanges.fromInteger(port), protocol, accessingCidr); }
@Override public void openFirewallPort(Entity entity, int port, Protocol protocol, Cidr accessingCidr) { openFirewallPortRange(entity, PortRanges.fromInteger(port), protocol, accessingCidr); }
@Test public void testSetFromFlagCoercesDefaultToPortRange() { MyLocation loc = newLocation(); assertEquals(loc.portRange1, PortRanges.fromInteger(1234)); }
@Test public void testSetFromFlagCoercesDefaultToPortRange() { MyPolicy policy = newPolicy(); assertEquals(policy.portRange1, PortRanges.fromInteger(1234)); }
@Test public void testSingleRange() { PortRange r = PortRanges.fromInteger(1234); assertContents(r, 1234); }
public static PortRange fromIterable(Iterable<?> c) { List<PortRange> l = new ArrayList<PortRange>(); for (Object o: c) { if (o instanceof Integer) l.add(fromInteger((Integer)o)); else if (o instanceof String) for (String string : JavaStringEscapes.unwrapJsonishListIfPossible((String)o)) l.add(fromString(string)); else if (o instanceof Iterable) l.add(fromIterable((Iterable<?>)o)); else if (o instanceof int[]) l.add(fromIterable(Ints.asList((int[])o))); else if (o instanceof String[]) for (String string : (String[])o) l.add(fromString(string)); else if (o instanceof Object[]) for (Object object : (Object[])o) if (object instanceof Integer) l.add(fromInteger((Integer)object)); else if (object instanceof String) l.add(fromString((String)object)); else throw new IllegalArgumentException("'" + object + "' must be of type Integer or String"); else l.add(TypeCoercions.coerce(o, PortRange.class)); } return new AggregatePortRange(l); }
public static Set<Integer> getContainerPorts(Entity entity) { List<Integer> entityOpenPorts = MutableList.of(); List<Integer> openPorts = entity.config().get(DockerAttributes.DOCKER_OPEN_PORTS); if (openPorts != null) entityOpenPorts.addAll(openPorts); List<PortAttributeSensorAndConfigKey> entityPortConfig = entity.config().get(DockerAttributes.DOCKER_OPEN_PORT_CONFIG); if (entityPortConfig != null) { for (PortAttributeSensorAndConfigKey key : entityPortConfig) { PortRange range = entity.config().get(key); if (range != null && !range.isEmpty()) { Integer port = range.iterator().next(); if (port != null) { openPorts.add(port); } } } } Map<Integer, Integer> portBindings = entity.sensors().get(DockerAttributes.DOCKER_CONTAINER_PORT_BINDINGS); if (portBindings != null) entityOpenPorts.addAll(portBindings.values()); if (entityOpenPorts.size() > 0) { // Create config and sensor for these ports for (int i = 0; i < entityOpenPorts.size(); i++) { Integer port = entityOpenPorts.get(i); String name = String.format("docker.port.%d", port); entity.sensors().set(Sensors.newIntegerSensor(name), port); entity.config().set(ConfigKeys.newConfigKey(PortRange.class, name), PortRanges.fromInteger(port)); } } return ImmutableSet.copyOf(entityOpenPorts); }
@Test public void obtainLowNumberedPortsAutomatically() throws Exception { LocalhostMachineProvisioningLocation p = newLocalhostProvisioner(); SshMachineLocation m = p.obtain(); int start = 983; //random rarely used port, not that it matters try { int actual = m.obtainPort(PortRanges.fromInteger(start)); Assert.assertEquals(actual, start); } finally { m.releasePort(start); } }
@Test(groups={"Integration"}) public void testStartsWithJmxPortSpecifiedInConfig() throws Exception { int port = 53405; String main = MAIN_CLASS.getCanonicalName(); VanillaJavaApp javaProcess = app.createAndManageChild(EntitySpec.create(VanillaJavaApp.class) .configure("main", main).configure("classpath", ImmutableList.of(BROOKLYN_THIS_CLASSPATH)) .configure("args", ImmutableList.of())); javaProcess.config().set(UsesJmx.JMX_PORT, PortRanges.fromInteger(port)); app.start(ImmutableList.of(loc)); assertEquals(javaProcess.getAttribute(UsesJmx.JMX_PORT), (Integer)port); }
@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)); }
@Test public void testPortSetAsAttributeOnSoftwareProcess() throws Exception { addCatalogItems( "brooklyn.catalog:", " itemType: entity", " items:", " - id: entity-with-keys", " item:", " type: "+EmptySoftwareProcess.class.getName(), " brooklyn.parameters:", " - name: my.param.key", " type: port", " default: 1234"); String yaml = Joiner.on("\n").join( "location:", " localhost:", " " + SshMachineLocation.SSH_TOOL_CLASS.getName() + ": " + RecordingSshTool.class.getName(), "services:", "- type: entity-with-keys"); Entity app = createStartWaitAndLogApplication(yaml); EmptySoftwareProcess entity = (EmptySoftwareProcess) Iterables.getOnlyElement(app.getChildren()); assertEquals(entity.config().get(ConfigKeys.newConfigKey(Object.class, "my.param.key")), PortRanges.fromInteger(1234)); assertEquals(entity.sensors().get(Sensors.newSensor(Object.class, "my.param.key")), 1234); }
/** * Altered to use a single postgresql server to avoid issues with shared memory limits */ @Test(groups = {"Integration","Broken"}) public void test_localhost_postgres() throws Exception { String createTwoDbsScript = PostgreSqlIntegrationTest.CREATION_SCRIPT + PostgreSqlIntegrationTest.CREATION_SCRIPT.replaceAll("CREATE USER.*", "").replaceAll(" feedback", " feedback1"); PostgreSqlNode db1 = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, createTwoDbsScript) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(9113)) .configure(PostgreSqlNode.MAX_CONNECTIONS, 10) .configure(PostgreSqlNode.SHARED_MEMORY, "512kB")); // Very low so kernel configuration not needed startInLocation(app, db1, "feedback", db1, "feedback1", new LocalhostMachineProvisioningLocation()); testReplication(db1, "feedback", db1, "feedback1"); }
@Override protected void doTest(Location loc) throws Exception { PostgreSqlNode db1 = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, PostgreSqlIntegrationTest.CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(9111))); PostgreSqlNode db2 = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, PostgreSqlIntegrationTest.CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(9111))); RubyRepIntegrationTest.startInLocation(app, db1, db2, loc); RubyRepIntegrationTest.testReplication(db1, db2); }
.configure("memberSpec", clusterSpec) .configure("customChildFlags", ImmutableMap.of("fromFabric", "passed to cluster but not base entity")) .configure(Attributes.HTTP_PORT, PortRanges.fromInteger(1234))); // for inheritance by children (as a port range) DynamicCluster child = (DynamicCluster) getChild(fabric, 0); assertEquals(child.getMembers().size(), 1); assertEquals(getMember(child, 0).getConfig(Attributes.HTTP_PORT.getConfigKey()), PortRanges.fromInteger(1234)); assertEquals(((TestEntity)getMember(child, 0)).getConfigureProperties().get("a"), null); assertEquals(((TestEntity)getMember(child, 0)).getConfigureProperties().get("b"), "avail"); assertEquals(getGrandchild(fabric, 0, 1).getConfig(Attributes.HTTP_PORT.getConfigKey()), PortRanges.fromInteger(1234)); assertEquals(((TestEntity)getMember(child, 1)).getConfigureProperties().get("a"), null); assertEquals(((TestEntity)getMember(child, 1)).getConfigureProperties().get("b"), "avail");
@Test(enabled = false, groups = "Integration") // TODO this doesn't appear to be supported by RubyRep public void test_localhost_postgres_mysql() throws Exception { PostgreSqlNode db1 = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, PostgreSqlIntegrationTest.CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(9115)) .configure(PostgreSqlNode.MAX_CONNECTIONS, 10) .configure(PostgreSqlNode.SHARED_MEMORY, "512kB")); // Very low so kernel configuration not needed MySqlNode db2 = app.createAndManageChild(EntitySpec.create(MySqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, MySqlIntegrationTest.CREATION_SCRIPT) .configure(MySqlNode.MYSQL_PORT, PortRanges.fromInteger(9116))); startInLocation(app, db1, db2, new LocalhostMachineProvisioningLocation()); testReplication(db1, db2); }
@Test(groups = {"Integration","Broken"}) public void test_localhost_mysql() throws Exception { MySqlNode db1 = app.createAndManageChild(EntitySpec.create(MySqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, MySqlIntegrationTest.CREATION_SCRIPT) .configure("test.table.name", "COMMENTS") .configure(MySqlNode.MYSQL_PORT, PortRanges.fromInteger(9111))); MySqlNode db2 = app.createAndManageChild(EntitySpec.create(MySqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, MySqlIntegrationTest.CREATION_SCRIPT) .configure("test.table.name", "COMMENTS") .configure(MySqlNode.MYSQL_PORT, PortRanges.fromInteger(9112))); startInLocation(app, db1, db2, new LocalhostMachineProvisioningLocation()); testReplication(db1, db2); }
@Test public void testMainUriSensorsCorrectlyComputedWithoutDomain() throws Exception { // The MachineLocation needs to implement HasSubnetHostname for the Attributes.SUBNET_HOSTNAME // to be set with the subnet addresss (otherwise it will fall back to using machine.getAddress()). // See Machines.getSubnetHostname. TrackingAbstractController controller2 = app.addChild(EntitySpec.create(TrackingAbstractController.class) .configure(TrackingAbstractController.SERVER_POOL, cluster) .configure(TrackingAbstractController.PROXY_HTTP_PORT, PortRanges.fromInteger(8081)) .location(LocationSpec.create(SshMachineLocationWithSubnetHostname.class) .configure("address", Inet4Address.getByName("1.1.1.1")) .configure(SshMachineLocation.PRIVATE_ADDRESSES, ImmutableList.of("2.2.2.2")))); controller2.start(ImmutableList.<Location>of()); EntityAsserts.assertAttributeEquals(controller2, Attributes.ADDRESS, "1.1.1.1"); EntityAsserts.assertAttributeEquals(controller2, Attributes.SUBNET_ADDRESS, "2.2.2.2"); EntityAsserts.assertAttributeEquals(controller2, Attributes.MAIN_URI, URI.create("http://2.2.2.2:8081/")); EntityAsserts.assertAttributeEquals(controller2, Attributes.MAIN_URI_MAPPED_PUBLIC, URI.create("http://1.1.1.1:8081/")); EntityAsserts.assertAttributeEquals(controller2, Attributes.MAIN_URI_MAPPED_SUBNET, URI.create("http://2.2.2.2:8081/")); } public static class SshMachineLocationWithSubnetHostname extends SshMachineLocation implements HasSubnetHostname {
public void test(String osRegex) throws Exception { PostgreSqlNode db1 = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, PostgreSqlIntegrationTest.CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(9111))); PostgreSqlNode db2 = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, PostgreSqlIntegrationTest.CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(9111))); BrooklynProperties brooklynProperties = mgmt.getBrooklynProperties(); brooklynProperties.put("brooklyn.location.jclouds.rackspace-cloudservers-uk.imageNameRegex", osRegex); brooklynProperties.remove("brooklyn.location.jclouds.rackspace-cloudservers-uk.image-id"); brooklynProperties.remove("brooklyn.location.jclouds.rackspace-cloudservers-uk.imageId"); brooklynProperties.put("brooklyn.location.jclouds.rackspace-cloudservers-uk.inboundPorts", Arrays.asList(22, 9111)); Location loc = mgmt.getLocationRegistry().getLocationManaged("jclouds:rackspace-cloudservers-uk"); startInLocation(app, db1, db2, loc); //hack to get the port for mysql open; is the inbounds property not respected on rackspace?? for (DatastoreCommon node : ImmutableSet.of(db1, db2)) { SshMachineLocation l = (SshMachineLocation) node.getLocations().iterator().next(); l.execCommands("add iptables rule", ImmutableList.of(IptablesCommands.insertIptablesRule(Chain.INPUT, Protocol.TCP, 9111, Policy.ACCEPT))); } testReplication(db1, db2); }
public void test(String osRegex) throws Exception { PostgreSqlNode psql = app.createAndManageChild(EntitySpec.create(PostgreSqlNode.class) .configure(DatastoreCommon.CREATION_SCRIPT_CONTENTS, CREATION_SCRIPT) .configure(PostgreSqlNode.POSTGRESQL_PORT, PortRanges.fromInteger(5432)) .configure(PostgreSqlNode.SHARED_MEMORY, "32MB")); mgmt.getBrooklynProperties().put("brooklyn.location.jclouds.rackspace-cloudservers-uk.imageNameRegex", osRegex); mgmt.getBrooklynProperties().remove("brooklyn.location.jclouds.rackspace-cloudservers-uk.image-id"); mgmt.getBrooklynProperties().remove("brooklyn.location.jclouds.rackspace-cloudservers-uk.imageId"); mgmt.getBrooklynProperties().put("brooklyn.location.jclouds.rackspace-cloudservers-uk.inboundPorts", Arrays.asList(22, 5432)); JcloudsLocation jcloudsLocation = (JcloudsLocation) mgmt.getLocationRegistry().getLocationManaged("jclouds:rackspace-cloudservers-uk"); app.start(ImmutableList.of(jcloudsLocation)); SshMachineLocation l = (SshMachineLocation) psql.getLocations().iterator().next(); l.execCommands("add iptables rule", ImmutableList.of(IptablesCommands.insertIptablesRule(Chain.INPUT, Protocol.TCP, 5432, Policy.ACCEPT))); String url = psql.getAttribute(DatastoreCommon.DATASTORE_URL); new VogellaExampleAccess("org.postgresql.Driver", url).readModifyAndRevertDataBase(); } }