public static <T> T assertAttributeEventuallyNonNull(final Entity entity, final AttributeSensor<T> attribute) { return assertAttributeEventuallyNonNull(Maps.newLinkedHashMap(), entity, attribute); }
@Override public void run() { EntityAsserts.assertAttributeEventuallyNonNull(entity, TestEntity.NAME); } });
protected void assertLatencyAttributesNonNull(Entity entity) { EntityAsserts.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT); EntityAsserts.assertAttributeEventuallyNonNull(entity, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW); log.info("Latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT)); log.info("Mean latency to "+entity.getAttribute(TEST_URL)+" is "+entity.getAttribute(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW)); } }
@Test(groups="Integration") public void testPropogateHttpPorts() { ControlledDynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(ControlledDynamicWebAppCluster.class) .configure("initialSize", 1)); app.start(locs); EntityAsserts.assertAttributeEventuallyNonNull(cluster, LoadBalancer.PROXY_HTTP_PORT); EntityAsserts.assertAttributeEventuallyNonNull(cluster, LoadBalancer.PROXY_HTTPS_PORT); }
@Test(groups = "Integration") public void testCanStartAndStop() throws Exception { CrateNode entity = app.createAndManageChild(EntitySpec.create(CrateNode.class)); app.start(ImmutableList.of(localhostProvisioningLocation)); EntityAsserts.assertAttributeEqualsEventually(entity, Startable.SERVICE_UP, true); EntityAsserts.assertAttributeEventuallyNonNull(entity, CrateNode.SERVER_NAME); entity.stop(); assertFalse(entity.getAttribute(Startable.SERVICE_UP)); } }
@Test(groups="Integration") public void testSshSensor() throws Exception { new SshCommandSensor<String>(ConfigBag.newInstance() .configure(SshCommandSensor.SENSOR_PERIOD, Duration.millis(100)) .configure(SshCommandSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(SshCommandSensor.SENSOR_COMMAND, "echo foo > "+tempFile.getAbsolutePath()+"\n" + "wc "+tempFile.getAbsolutePath())) .apply(entity); entity.sensors().set(Attributes.SERVICE_UP, true); String val = EntityAsserts.assertAttributeEventuallyNonNull(entity, SENSOR_STRING); assertTrue(val.contains("1"), "val="+val); String[] counts = val.trim().split("\\s+"); Assert.assertEquals(counts.length, 4, "val="+val); Assert.assertEquals(counts[0], "1", "val="+val); } }
@Test(groups="Integration") public void testEnrichersOnRebindJavaApp() throws Exception { VanillaJavaApp javaProcess = app.addChild(EntitySpec.create(VanillaJavaApp.class, EnrichedVanillaJavaAppImpl.class) .configure("main", MAIN_CLASS.getCanonicalName()).configure("classpath", ImmutableList.of(BROOKLYN_THIS_CLASSPATH))); app.start(ImmutableList.of(loc)); EntityAsserts.assertAttributeEventuallyNonNull(javaProcess, EnrichedVanillaJavaAppImpl.AVG1); EntityAsserts.assertAttributeEventuallyNonNull(javaProcess, EnrichedVanillaJavaAppImpl.AVG2); LOG.info("Got avg "+javaProcess.getAttribute(EnrichedVanillaJavaAppImpl.AVG1)); rebind(); VanillaJavaApp javaProcess2 = (VanillaJavaApp) Iterables.find(app.getChildren(), Predicates.instanceOf(VanillaJavaApp.class)); // check sensors working EntityAsserts.assertAttributeChangesEventually(javaProcess2, EnrichedVanillaJavaAppImpl.PROCESS_CPU_TIME); LOG.info("Avg now "+javaProcess2.getAttribute(EnrichedVanillaJavaAppImpl.AVG1)); // check enrichers are functioning EntityAsserts.assertAttributeChangesEventually(javaProcess2, EnrichedVanillaJavaAppImpl.AVG1); EntityAsserts.assertAttributeChangesEventually(javaProcess2, EnrichedVanillaJavaAppImpl.AVG2); LOG.info("Avg now "+javaProcess2.getAttribute(EnrichedVanillaJavaAppImpl.AVG1)); // and check we don't have too many Assert.assertEquals(javaProcess2.getEnrichers().size(), javaProcess.getEnrichers().size()); }
app.start(ImmutableList.of(machine)); String creds = EntityAsserts.assertAttributeEventuallyNonNull(entity, CreateUserPolicy.VM_USER_CREDENTIALS); Pattern pattern = Pattern.compile("(.*) : (.*) @ (.*):(.*)"); Matcher matcher = pattern.matcher(creds);
String creds = EntityAsserts.assertAttributeEventuallyNonNull(entity, CreateUserPolicy.VM_USER_CREDENTIALS); Pattern pattern = Pattern.compile("(.*) : (.*) @ (.*):(.*)"); Matcher matcher = pattern.matcher(creds);
/** this is one of the most common pattern */ @Test(groups="Integration") public void testReturnsSshStdoutAndInfersMachine() throws Exception { final TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class) // inject the machine location, because the app was started with a provisioning location // and TestEntity doesn't provision .location(machine)); feed = SshFeed.builder() .entity(entity2) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command("echo hello") .onSuccess(SshValueFunctions.stdout())) .build(); EntityAsserts.assertAttributeEventuallyNonNull(entity2, SENSOR_STRING); String val = entity2.getAttribute(SENSOR_STRING); Assert.assertTrue(val.contains("hello"), "val="+val); Assert.assertEquals(val.trim(), "hello"); }
/** this is one of the most common pattern */ @Test(groups="Live") public void testReturnsStdoutAndInfersMachine() throws Exception { final TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class) // inject the machine location, because the app was started with a provisioning location // and TestEntity doesn't provision .location(machine)); feed = CmdFeed.builder() .entity(entity2) .poll(new CommandPollConfig<String>(SENSOR_STRING) .command("echo hello") .onSuccess(SshValueFunctions.stdout())) .build(); EntityAsserts.assertAttributeEventuallyNonNull(entity2, SENSOR_STRING); String val = entity2.getAttribute(SENSOR_STRING); Assert.assertTrue(val.contains("hello"), "val="+val); Assert.assertEquals(val.trim(), "hello"); }
@Test(groups = {"Integration"}) public void testRebindMongoDb() throws Exception { MongoDBServer origEntity = origApp.createAndManageChild(EntitySpec.create(MongoDBServer.class) .configure("mongodbConfTemplateUrl", "classpath:///test-mongodb.conf")); origApp.start(ImmutableList.of(loc)); EntityAsserts.assertAttributeEventuallyNonNull(origEntity, MongoDBServer.STATUS_BSON); // rebind rebind(); final MongoDBServer newEntity = (MongoDBServer) Iterables.find(newApp.getChildren(), Predicates.instanceOf(MongoDBServer.class)); // confirm effectors still work on entity EntityAsserts.assertAttributeEqualsEventually(newEntity, MongoDBServer.SERVICE_UP, true); newEntity.stop(); EntityAsserts.assertAttributeEqualsEventually(newEntity, MongoDBServer.SERVICE_UP, false); } }
@Test(groups = "Integration", dependsOnMethods = { "testCanStartAndStop" }) public void testPollInsertCountSensor() throws Exception { MongoDBServer entity = app.createAndManageChild(EntitySpec.create(MongoDBServer.class) .configure("mongodbConfTemplateUrl", "classpath:///test-mongodb.conf")); app.start(ImmutableList.of(localhostProvisioningLocation)); EntityAsserts.assertAttributeEqualsEventually(entity, Startable.SERVICE_UP, true); EntityAsserts.assertAttributeEventuallyNonNull(entity, MongoDBServer.OPCOUNTERS_INSERTS); Long initialInserts = entity.getAttribute(MongoDBServer.OPCOUNTERS_INSERTS); MongoDBTestHelper.insert(entity, "a", Boolean.TRUE); MongoDBTestHelper.insert(entity, "b", Boolean.FALSE); EntityAsserts.assertAttributeEqualsEventually(entity, MongoDBServer.OPCOUNTERS_INSERTS, initialInserts + 2); }
@Test public void testAddsStaticSensorOfTypeString() { BasicEntity entity = app.createAndManageChild(EntitySpec.create(BasicEntity.class) .addInitializer(new MaxConcurrencySensor(ConfigBag.newInstance(ImmutableMap.of( MaxConcurrencySensor.SENSOR_NAME, "myname", MaxConcurrencySensor.MAX_CONCURRENCY, DependentConfiguration.formatString("%d", DependentConfiguration.attributeWhenReady(app, MAX_PERMITS))))))); int actualPermits = 10; app.sensors().set(MAX_PERMITS, actualPermits); EntityAsserts.assertAttributeEventuallyNonNull(entity, (AttributeSensor<?>)entity.getEntityType().getSensor("myname")); }
@Test(groups="Integration") public void testDisableRetrievalOfUsageMetrics() throws Exception { LocalhostMachineProvisioningLocation loc = app.newLocalhostProvisioningLocation(); final TomcatServer tc1 = app.createAndManageChild(EntitySpec.create(TomcatServer.class) .configure(SoftwareProcess.RETRIEVE_USAGE_METRICS, false)); final TomcatServer tc2 = app.createAndManageChild(EntitySpec.create(TomcatServer.class)); tc1.start(ImmutableList.of(loc)); tc2.start(ImmutableList.of(loc)); // tc2 uses defaults, so will include usage metrics Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertNotNull(tc2.getAttribute(TomcatServer.CONNECTOR_STATUS)); assertNotNull(tc2.getAttribute(TomcatServer.ERROR_COUNT)); assertNotNull(tc2.getAttribute(TomcatServer.REQUEST_COUNT)); assertNotNull(tc2.getAttribute(TomcatServer.TOTAL_PROCESSING_TIME)); }}); // tc1 should have status info, but not usage metrics EntityAsserts.assertAttributeEventuallyNonNull(tc1, TomcatServer.CONNECTOR_STATUS); EntityAsserts.assertAttributeEqualsContinually(tc1, TomcatServer.ERROR_COUNT, null); assertNull(tc1.getAttribute(TomcatServer.REQUEST_COUNT)); assertNull(tc1.getAttribute(TomcatServer.TOTAL_PROCESSING_TIME)); } }
.build(); EntityAsserts.assertAttributeEventuallyNonNull(entity2, SENSOR_STRING); final String val1 = assertDifferentOneInOutput(entity2);
.build(); EntityAsserts.assertAttributeEventuallyNonNull(entity2, SENSOR_STRING); final String val1 = assertDifferentOneInOutput(entity2);
app().start(ImmutableList.of(machine)); String creds = EntityAsserts.assertAttributeEventuallyNonNull(entity, CreateUserPolicy.VM_USER_CREDENTIALS); if (!isUseraddExecuted()) { fail("useradd not found in: "+RecordingSshTool.getExecCmds());
@Test public void testHttpLatencyDetectorEnricher() throws Exception { webServer = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 1000; i++) { webServer.enqueue(new MockResponse().setResponseCode(200).addHeader("content-type: application/json").setBody("{\"foo\":\"myfoo\"}")); } webServer.play(); URL baseUrl = webServer.getUrl("/"); origApp.enrichers().add(HttpLatencyDetector.builder() .rollup(Duration.of(50, TimeUnit.MILLISECONDS)) .period(Duration.of(10, TimeUnit.MILLISECONDS)) .url(baseUrl) .buildSpec()); origApp.sensors().set(Attributes.SERVICE_UP, true); TestApplication newApp = rebind(); newApp.sensors().set(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT, null); newApp.sensors().set(HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW, null); EntityAsserts.assertAttributeEventuallyNonNull(newApp, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_MOST_RECENT); EntityAsserts.assertAttributeEventuallyNonNull(newApp, HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW); }