public static <T> T assertAttributeEventually(final Entity entity, final AttributeSensor<T> attribute, Predicate<? super T> predicate) { return assertAttributeEventually(ImmutableMap.of(), entity, attribute, predicate); }
public static <T> T assertAttributeEventuallyNonNull(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute) { return assertAttributeEventually(flags, entity, attribute, Predicates.notNull()); }
@Override public void run() { EntityAsserts.assertAttributeEventually(entity, TestEntity.NAME, new Predicate<String>() { @Override public boolean apply(String input) { eventuallyEntered.countDown(); return input.matches(".*\\d+"); } }); } });
protected void assertNotUpIndicatorIncludesEventually(final Entity entity, final String expectedKey, final String expectedVal) throws Exception { EntityAsserts.assertAttributeEventually(entity, Attributes.SERVICE_NOT_UP_INDICATORS, new Predicate<Map<?,?>>() { @Override public boolean apply(Map<?, ?> input) { if (input == null) return false; for (Map.Entry<?, ?> entry : input.entrySet()) { boolean keyMatches = expectedKey.equals(entry.getKey()); boolean valueMatches = expectedVal.equals(entry.getValue()); if (keyMatches && valueMatches) return true; } LOG.info("entity "+entity+" not-up-indicators: "+input); return false; }}); }
protected void checkStartsRunning(Entity mysql) { // should be starting within a few seconds (and almost certainly won't complete in that time) EntityAsserts.assertAttributeEventually( mysql, Attributes.SERVICE_STATE_ACTUAL, Predicates.or(Predicates.equalTo(Lifecycle.STARTING), Predicates.equalTo(Lifecycle.RUNNING))); // should be up and running within 5m EntityAsserts.assertAttributeEqualsEventually(MutableMap.of("timeout", Duration.FIVE_MINUTES), mysql, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); }
@Test(groups={"Broken"}) public void testFiltersForRunningEntities() { app.start(ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname)); publishSensors(2, true, true, true); TestEntity problemChild = Iterables.get(Entities.descendantsAndSelf(app, TestEntity.class), 0); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(1)); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); }
@Test public void testGeoInfoOnParentLocation() { app.start( ImmutableList.of(westChild, eastChild) ); publishSensors(2, true, false, false); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); assertIsTarget("West child"); assertIsTarget("East child"); }
@Test public void testSubscribesToHostname() { geoDns.config().set(GeoDnsTestServiceImpl.ADD_ANYTHING, false); app.start( ImmutableList.of(westChild, eastChildWithLocationAndWithPrivateHostname) ); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 0); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 2); assertIsTarget("West child"); assertIsTarget("East child with location"); }
@Test public void testGeoInfoOnLocation() { app.start( ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname) ); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); assertIsTarget("West child with location"); assertIsTarget("East child with location"); }
@Test public void testFiltersEntirelyPrivate() { geoDns.config().set(GeoDnsTestServiceImpl.ADD_ANYTHING, false); app.start( ImmutableList.of(westChild, eastChildWithLocationAndWithPrivateHostname, northChildWithLocation) ); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 0); publishSensors(3, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); Assert.assertEquals(geoDns.getTargetHostsByName().size(), 2); assertIsTarget("West child"); assertIsTarget("East child with location"); assertIsNotTarget("North child"); }
@Test public void testCanDisableFilterForRunningEntities() throws Exception { geoDns.config().set(AbstractGeoDnsService.FILTER_FOR_RUNNING, false); app.start(ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname)); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); final Map<String, String> targets = ImmutableMap.copyOf(geoDns.sensors().get(AbstractGeoDnsService.TARGETS)); TestEntity problemChild = Iterables.get(Entities.descendantsAndSelf(app, TestEntity.class), 0); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); assertAttributeEqualsContinually(geoDns, AbstractGeoDnsService.TARGETS, targets); }
@Test(groups="Live") public void testReturnsStdout() throws Exception { feed = CmdFeed.builder() .entity(entity) .machine(machine) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command("echo hello") .onSuccess(SshValueFunctions.stdout())) .build(); EntityAsserts.assertAttributeEventually(entity, SENSOR_STRING, Predicates.compose(Predicates.equalTo("hello"), StringFunctions.trim())); }
private void assertUpAndRunningEventually(Entity entity) { try { EntityAsserts.assertAttributeEventually(entity, Attributes.SERVICE_NOT_UP_INDICATORS, CollectionFunctionals.<String>mapEmptyOrNull()); EntityAsserts.assertAttributeEventually(entity, ServiceStateLogic.SERVICE_PROBLEMS, CollectionFunctionals.<String>mapEmptyOrNull()); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, true); } catch (Throwable t) { Entities.dumpInfo(entity); String err = "(Dumped entity info - see log); entity=" + entity + "; " + "state=" + entity.sensors().get(Attributes.SERVICE_STATE_ACTUAL) + "; " + "up="+entity.sensors().get(Attributes.SERVICE_UP) + "; " + "notUpIndicators="+entity.sensors().get(Attributes.SERVICE_NOT_UP_INDICATORS) + "; " + "serviceProblems="+entity.sensors().get(Attributes.SERVICE_PROBLEMS); throw new AssertionError(err, t); } }
@Test(groups="Integration") public void testReturnsSshStdout() throws Exception { feed = SshFeed.builder() .entity(entity) .machine(machine) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command("echo hello") .onSuccess(SshValueFunctions.stdout())) .build(); EntityAsserts.assertAttributeEventually(entity, SENSOR_STRING, Predicates.compose(Predicates.equalTo("hello"), StringFunctions.trim())); }
@Test(groups="Integration") public void testReturnsSshStderr() throws Exception { final String cmd = "thiscommanddoesnotexist"; feed = SshFeed.builder() .entity(entity) .machine(machine) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command(cmd) .onFailure(SshValueFunctions.stderr())) .build(); EntityAsserts.assertAttributeEventually(entity, SENSOR_STRING, StringPredicates.containsLiteral(cmd)); }
@Test(groups="Live") public void testReturnsStderr() throws Exception { final String cmd = "thiscommanddoesnotexist"; feed = CmdFeed.builder() .entity(entity) .machine(machine) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command(cmd) .onFailure(SshValueFunctions.stderr())) .build(); EntityAsserts.assertAttributeEventually(entity, SENSOR_STRING, StringPredicates.containsLiteral(cmd)); }
@Test public void testAggregatingMembersEnricher() throws Exception { origApp.start(ImmutableList.of(origLoc)); origCluster.resize(2); origApp.enrichers().add(Enrichers.builder() .aggregating(METRIC1) .from(origCluster) .fromMembers() .computing(StringFunctions.joiner(",")) .publishing(METRIC2) .build()); TestApplication newApp = rebind(); DynamicCluster newCluster = (DynamicCluster) Iterables.find(newApp.getChildren(), Predicates.instanceOf(DynamicCluster.class)); int i = 1; for (Entity member : newCluster.getMembers()) { ((EntityInternal)member).sensors().set(METRIC1, "myval"+(i++)); } EntityAsserts.assertAttributeEventually(newApp, METRIC2, Predicates.or(Predicates.equalTo("myval1,myval2"), Predicates.equalTo("myval2,myval1"))); }
@SuppressWarnings("unchecked") @Test public void testCombiningEnricher() throws Exception { origApp.enrichers().add(Enrichers.builder() .combining(METRIC1, METRIC2) .from(origEntity) .computing(StringFunctions.joiner(",")) .publishing(METRIC2) .build()); TestApplication newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); newEntity.sensors().set(METRIC1, "myval"); newEntity.sensors().set(METRIC2, "myval2"); EntityAsserts.assertAttributeEventually(newApp, METRIC2, Predicates.or(Predicates.equalTo("myval,myval2"), Predicates.equalTo("myval2,myval"))); }
@Test(groups="Integration") public void testFailsOnNonZero() throws Exception { feed = SshFeed.builder() .entity(entity) .machine(machine) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command("exit 123") .onFailure(new Function<SshPollValue, String>() { @Override public String apply(SshPollValue input) { return "Exit status " + input.getExitStatus(); }})) .build(); EntityAsserts.assertAttributeEventually(entity, SENSOR_STRING, StringPredicates.containsLiteral("Exit status 123")); }
@Test(groups="Live") public void testFailsOnNonZero() throws Exception { feed = CmdFeed.builder() .entity(entity) .machine(machine) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command("exit 123") .onFailure(new Function<SshPollValue, String>() { @Override public String apply(SshPollValue input) { return "Exit status " + input.getExitStatus(); }})) .build(); EntityAsserts.assertAttributeEventually(entity, SENSOR_STRING, StringPredicates.containsLiteral("Exit status 123")); }