@Test public void testAddsEnricher() throws Exception { MyEnricher enricher = new MyEnricher(); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class, TestEntityNoEnrichersImpl.class) .enricher(enricher)); assertEquals(Iterables.getOnlyElement(entity.enrichers()), enricher); }
@Test public void testAddsEnricherSpec() throws Exception { entity = app.createAndManageChild(EntitySpec.create(TestEntity.class, TestEntityNoEnrichersImpl.class) .enricher(EnricherSpec.create(MyEnricher.class) .displayName("myenrichername") .configure(MyEnricher.CONF1, "myconf1val") .configure("myfield", "myfieldval"))); Enricher enricher = Iterables.getOnlyElement(entity.enrichers()); assertTrue(enricher instanceof MyEnricher, "enricher="+enricher); assertEquals(enricher.getDisplayName(), "myenrichername"); assertEquals(enricher.getConfig(MyEnricher.CONF1), "myconf1val"); }
@Override public void apply(EntitySpec<?> entitySpec, String nodeId, ToscaApplication toscaApplication) { // for tosca.capabilities.Endpoint and derivatives (and alien Compute) entitySpec.enricher(Enrichers.builder().transforming(Attributes.ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("ip_address")).build()); entitySpec.enricher(Enrichers.builder().transforming(Attributes.SUBNET_ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("private_address")).build()); entitySpec.enricher(Enrichers.builder().transforming(Attributes.ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("public_address")).build()); // alien specific entitySpec.enricher(Enrichers.builder().transforming(Attributes.ADDRESS) .computing(Functions.identity()) .publishing(Sensors.newStringSensor("public_ip_address")).build()); // open up any ports that end with "_port" in addition to the default ".port" entitySpec.configure(SoftwareProcess.INBOUND_PORTS_CONFIG_REGEX, "(.*[\\._])?port$"); } }
@Test(groups = "Live") public void testRestartLaunchesService() { String launchCmd = "nohup bash -c \"echo \\$\\$ > $PID_FILE; while true; do sleep 1000; done\" &"; EntitySpec<VanillaSoftwareProcess> procSpec = EntitySpec.create(VanillaSoftwareProcess.class) .configure(VanillaSoftwareProcess.LAUNCH_COMMAND, launchCmd) .enricher(EnricherSpec.create(SystemServiceEnricher.class)); VanillaSoftwareProcess proc = app.createAndManageChild(procSpec); app.start(ImmutableList.of(location)); waitHealthy(proc); SshMachineLocation machine = EffectorTasks.getSshMachine(proc); String pidFile = getPidFile(proc); String killCmd = "kill -9 `cat " + pidFile + "`"; machine.execCommands("kill process", ImmutableList.of(killCmd)); waitFailed(proc); int restartCode = machine.execCommands("restart machine", ImmutableList.of(BashCommands.sudo("/sbin/shutdown -r now"))); assertEquals(restartCode, 0); waitHealthy(proc); }
coreSpec.enricher(Enrichers.builder() .propagatingAllBut(Attributes.SERVICE_UP, Attributes.SERVICE_NOT_UP_INDICATORS, Attributes.SERVICE_STATE_ACTUAL, Attributes.SERVICE_STATE_EXPECTED,
@Test public void testUpdateMapEmitsEventOnChange() throws Exception { Entity entity = app.createAndManageChild(EntitySpec.create(BasicApplication.class) .enricher(EnricherSpec.create(UpdatingMap.class) .configure(UpdatingMap.SOURCE_SENSOR.getName(), mySensor.getName()) .configure(UpdatingMap.TARGET_SENSOR, mapSensor) .configure(UpdatingMap.KEY_IN_TARGET_SENSOR, "myKey") .configure(UpdatingMap.COMPUTING, Functions.forMap(MutableMap.of("v1", "valIsV1", "v2", "valIsV2"), "myDefault")))); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of("myKey", "myDefault")); RecordingSensorEventListener<Map<String, Object>> listener = new RecordingSensorEventListener<>(); app.subscriptions().subscribe(entity, mapSensor, listener); entity.sensors().set(mySensor, "v1"); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of("myKey", "valIsV1")); listener.assertHasEventEventually(Predicates.alwaysTrue()); assertEquals(Iterables.getOnlyElement(listener.getEventValues()), ImmutableMap.of("myKey", "valIsV1")); }
@Test public void testRemovingIfResultIsNull() throws Exception { Entity entity = app.createAndManageChild(EntitySpec.create(BasicApplication.class) .enricher(EnricherSpec.create(UpdatingMap.class) .configure(UpdatingMap.SOURCE_SENSOR.getName(), mySensor.getName()) .configure(UpdatingMap.TARGET_SENSOR, mapSensor) .configure(UpdatingMap.REMOVING_IF_RESULT_IS_NULL, true) .configure(UpdatingMap.COMPUTING, Functions.forMap(MutableMap.of("v1", "valIsV1"), null)))); entity.sensors().set(mySensor, "v1"); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of("mySensor", "valIsV1")); entity.sensors().set(mySensor, "different"); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of()); }
@Test public void testUpdateMapUsingDifferentProducer() throws Exception { Entity producer = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Entity entity = app.createAndManageChild(EntitySpec.create(BasicApplication.class) .enricher(EnricherSpec.create(UpdatingMap.class) .configure(UpdatingMap.PRODUCER, producer) .configure(UpdatingMap.SOURCE_SENSOR.getName(), mySensor.getName()) .configure(UpdatingMap.TARGET_SENSOR, mapSensor) .configure(UpdatingMap.KEY_IN_TARGET_SENSOR, "myKey") .configure(UpdatingMap.COMPUTING, Functions.forMap(MutableMap.of("v1", "valIsV1", "v2", "valIsV2"), "myDefault")))); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of("myKey", "myDefault")); producer.sensors().set(mySensor, "v1"); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of("myKey", "valIsV1")); }
@Test public void testUpdateServiceNotUpIndicator() throws Exception { AttributeSensor<Object> extraIsUpSensor = Sensors.newSensor(Object.class, "extraIsUp"); Entity entity = app.createAndManageChild(EntitySpec.create(BasicApplication.class) .enricher(EnricherSpec.create(UpdatingMap.class) .configure(UpdatingMap.SOURCE_SENSOR.getName(), extraIsUpSensor.getName()) .configure(UpdatingMap.TARGET_SENSOR, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS) .configure(UpdatingMap.COMPUTING, Functions.forMap(MutableMap.of(true, null, false, "valIsFalse"), "myDefault")))); assertMapSensorContainsEventually(entity, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS, ImmutableMap.of("extraIsUp", "myDefault")); entity.sensors().set(extraIsUpSensor, true); assertMapSensorNotContainsKeysEventually(entity, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS, ImmutableList.of("extraIsUp")); app.start(ImmutableList.<Location>of()); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsContinually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); entity.sensors().set(extraIsUpSensor, false); EntityAsserts.assertAttributeEqualsEventually(entity, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS, ImmutableMap.of("extraIsUp", "valIsFalse")); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, false); entity.sensors().set(extraIsUpSensor, true); EntityAsserts.assertAttributeEqualsEventually(entity, ServiceStateLogic.SERVICE_NOT_UP_INDICATORS, ImmutableMap.of()); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, true); }
.parametersAdd(params) .tags(ImmutableList.<Object>of("tag1", "tag2")) .enricher(EnricherSpec.create(TestEnricher.class)) .policy(PolicySpec.create(TestPolicy.class)));
@Test public void testNotRemovingIfResultIsNull() throws Exception { Entity entity = app.createAndManageChild(EntitySpec.create(BasicApplication.class) .enricher(EnricherSpec.create(UpdatingMap.class) .configure(UpdatingMap.SOURCE_SENSOR.getName(), mySensor.getName()) .configure(UpdatingMap.TARGET_SENSOR, mapSensor) .configure(UpdatingMap.REMOVING_IF_RESULT_IS_NULL, false) .configure(UpdatingMap.COMPUTING, Functions.forMap(MutableMap.of("v1", "valIsV1"), null)))); entity.sensors().set(mySensor, "v1"); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, ImmutableMap.of("mySensor", "valIsV1")); entity.sensors().set(mySensor, "different"); EntityAsserts.assertAttributeEqualsEventually(entity, mapSensor, MutableMap.of("mySensor", null)); }
.parametersAdd(params) .tags(ImmutableList.<Object>of("tag1", "tag2")) .enricher(EnricherSpec.create(TestEnricher.class)) .policy(PolicySpec.create(TestPolicy.class)));
public void testAbstractEnricherDeduplicationBroken() { final TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) .enricher(EnricherSpec.create(EmittingEnricher.class))); final AttributeSensor<String> TEST_SENSOR = Sensors.newStringSensor("test.sensor");
/** * Enricher sees state in the wrong order -> running, starting, running * * Indeterministic, fails a couple of times per 100 invocations when run with "mvn test" in the * brooklyn-itest docker container. */ @Test(groups="Broken") public void testWrongSensorInitValue() { TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) .impl(TestApplicationNoEnrichersImpl.class) .enricher(EnricherSpec.create(RecodingChildSensorEnricher.class)) .child(EntitySpec.create(TestEntity.class) .impl(TestEntityNoEnrichersImpl.class))); Entity child = Iterables.get(app.getChildren(), 0); child.sensors().set(RecodingChildSensorEnricher.RECORDED_SENSOR, "first"); child.sensors().set(RecodingChildSensorEnricher.RECORDED_SENSOR, "second"); final RecodingChildSensorEnricher enricher = getFirstEnricher(app, RecodingChildSensorEnricher.class); // setEntity -> onUpdate // CHILD_ADDED -> onUpdate // set RECORDED_SENSOR=first -> onUpdate // set RECORDED_SENSOR=second -> onUpdate Asserts.eventually(Suppliers.ofInstance(enricher.seenValues), CollectionFunctionals.sizeEquals(4)); boolean isOrdered = Ordering.explicit(MutableList.of("first", "second")) .nullsFirst() .isOrdered(enricher.seenValues); assertTrue(isOrdered, "Unexpected ordering for " + enricher.seenValues); }