@Override public String addRegion(String location) { Preconditions.checkNotNull(location, "location"); Location l = getManagementContext().getLocationRegistry().getLocationManaged(location); addLocations(Arrays.asList(l)); Entity e = addCluster(l); ((EntityInternal)e).addLocations(Arrays.asList(l)); if (e instanceof Startable) { Task<?> task = e.invoke(Startable.START, ImmutableMap.of("locations", ImmutableList.of(l))); task.getUnchecked(); } return e.getId(); }
private Object invokeEffectorNamed(Entity target, String effectorName, ConfigBag params) { LOG.debug("{} forwarding effector invocation on {} to entity={}, effector={}, parameters={}", new Object[]{this, entity(), target, effectorName, params}); Effector<?> effector = EffectorUtils.findEffectorDeclared(target, effectorName).get(); return target.invoke(effector, params.getAllConfig()).getUnchecked(); } }
/** the composed effector should allow us to inspect its children */ @Test public void testComposedEffector() throws Exception { Entity txp1 = app.createAndManageChild(EntitySpec.create(Entity.class, Txp1Entity.class)); Task<Integer> e = txp1.invoke(TWO_X_PLUS_ONE, MutableMap.of("numberToStartWith", 3)); Assert.assertTrue(e instanceof DynamicSequentialTask); Assert.assertEquals(e.get(), (Integer)7); Assert.assertEquals( Iterables.size( ((HasTaskChildren)e).getChildren() ), 1); Task<?> child = ((HasTaskChildren)e).getChildren().iterator().next(); Assert.assertEquals( Iterables.size( ((HasTaskChildren)child).getChildren() ), 1); }
@Test public void testEffectorWithoutBodyFails() throws Exception { Entity doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); boolean failed = false; try { doubler.invoke(DOUBLE_BODYLESS, MutableMap.of("numberToDouble", 3)); } catch (Exception e) { failed = true; } if (!failed) Assert.fail("doubling should have failed because it had no body"); }
/** the composed effector should allow us to inspect its children */ @Test public void testComposedEffectorBasic() throws Exception { Entity txp1 = app.createAndManageChild(EntitySpec.create(Entity.class, Txp1Entity.class)); Task<Integer> e = txp1.invoke(TWO_X_PLUS_ONE_BASIC, MutableMap.of("numberToStartWith", 3)); Assert.assertTrue(e instanceof DynamicSequentialTask); Assert.assertEquals(e.get(), (Integer)7); Assert.assertEquals( Iterables.size( ((HasTaskChildren)e).getChildren() ), 2); }
@Test public void testTestJavaWebAppEntityStarts() throws Exception { Entity test = app.createAndManageChild(EntitySpec.create(TestJavaWebAppEntity.class)); test.invoke(Startable.START, ImmutableMap.of("locations", ImmutableList.of(loc))).get(); EntityAsserts.assertAttributeEqualsEventually(test, Attributes.SERVICE_UP, true); }
@Test // also assert it works when the effector is defined on an entity public void testSimpleEffectorOnEntity() throws Exception { Entity doubler = app.createAndManageChild(EntitySpec.create(Entity.class, DoublingEntity.class)); Assert.assertEquals(doubler.invoke(DOUBLE_1, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
public static String doEffectorCallBrooklyn(Entity me) { return me.invoke(Effectors.effector(String.class, "sayHI").buildAbstract(), ImmutableMap.of("name", "brooklyn")).getUnchecked(); }
@Test // also assert it works when the entity is defined on an entity public void testOverriddenEffectorOnEntity() throws Exception { Entity doubler = app.createAndManageChild(EntitySpec.create(Entity.class, BadDoublingEntity.class)); Assert.assertEquals(doubler.invoke(DoublingEntity.DOUBLE, MutableMap.of("numberToDouble", 3, "numberToStartWith", 3)).get(), (Integer)7); }
@Test public void testEffectorWithBodyWorksEvenIfNotOnEntity() throws Exception { Entity doubler = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Assert.assertEquals(doubler.invoke(DOUBLE_1, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@Override protected JcloudsMachineLocation obtainMachine(JcloudsLocation jcloudsLoc, Map<?,?> props) throws Exception { final MachineEntity entity = (MachineEntity) Iterables.getOnlyElement(origApp.getChildren()); origApp.invoke(Startable.START, ImmutableMap.<String, Object>of()).get(); // Execute ssh (with RecordingSshTool), and confirm was given resolved password entity.execCommand("mycmd"); Map<?, ?> constructorProps = RecordingSshTool.getLastConstructorProps(); ExecCmd execCmd = RecordingSshTool.getLastExecCmd(); assertEquals(constructorProps.get("password"), "myYamlPassword", "constructorProps: "+constructorProps+"; execProps: "+execCmd.props); return Machines.findUniqueMachineLocation(entity.getLocations(), JcloudsMachineLocation.class).get(); } }
@Test // also assert it works when an abstract effector name is passed in to the entity public void testSimpleEffectorNameMatching() throws Exception { Entity doubler = app.createAndManageChild(EntitySpec.create(Entity.class, DoublingEntity.class)); Assert.assertEquals(doubler.invoke(Effectors.effector(Integer.class, "double").buildAbstract(), MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@Test public void testThrowsIfTargetDoesNotResolve() throws Exception { final String effectorName = "proxy-effector"; Entity app = createAndStartApplication( "location: localhost", "services:", "- type: " + BasicEntity.class.getName(), " brooklyn.initializers:", " - type: org.apache.brooklyn.core.effector.ProxyEffector", " brooklyn.config:", " name: " + effectorName, " targetEntity: $brooklyn:entity(\"skhbfskdbf\")", " targetEffector: identityEffector" ); waitForApplicationTasks(app); Entity basicEntity = app.getChildren().iterator().next(); Effector<?> effector = basicEntity.getEntityType().getEffectorByName(effectorName).get(); try { basicEntity.invoke(effector, ImmutableMap.<String, Object>of()).get(); Asserts.shouldHaveFailedPreviously("expected exception when invoking effector that does not exist"); } catch (Exception e) { Asserts.expectedFailure(e); } }
@Override protected JcloudsLocation newJcloudsLocation(ComputeServiceRegistry computeServiceRegistry) throws Exception { ByonComputeServiceStaticRef.setInstance(computeServiceRegistry); String yaml = Joiner.on("\n").join( "location:", " "+LOCATION_CATALOG_ID+":", " identity: $brooklyn:external(\"creds\", \"test-identity\")", " credential: $brooklyn:external(\"creds\", \"test-credential\")", "services:\n"+ "- type: org.apache.brooklyn.entity.stock.BasicApplication"); EntitySpec<?> spec = mgmt().getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, yaml, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class); final Entity app = mgmt().getEntityManager().createEntity(spec); app.invoke(Startable.START, ImmutableMap.<String, Object>of()).get(); JcloudsLocation result = (JcloudsLocation) Iterables.getOnlyElement(app.getLocations()); assertEquals(result.getIdentity(), "myidentity"); assertEquals(result.getCredential(), "mycredential"); return result; } }
@Test public void testCatalogBomLoadsFileInBundle() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); // add a file in the bundle String customText = "Sample data "+Identifiers.makeRandomId(4); jf = bm.copyAdding(jf, MutableMap.of( new ZipEntry("sample.txt"), (InputStream) new ByteArrayInputStream(customText.getBytes()))); installBundle(jf); String yaml = Strings.lines("name: simple-app-yaml", "services:", "- type: " + "basic1", " brooklyn.initializers:", " - type: "+GetFileContentsEffector.class.getName()); Entity app = createAndStartApplication(yaml); Entity basic1 = Iterables.getOnlyElement( app.getChildren() ); // check the file put in the bundle gets loaded without needing to do anything special String contents = basic1.invoke(GetFileContentsEffector.GET_FILE_CONTENTS, MutableMap.of(GetFileContentsEffector.FILENAME.getName(), "classpath://sample.txt")).get(); Asserts.assertEquals(contents, customText); }
protected Entity createAndStartApplication(String input, Map<String,?> startParameters) throws Exception { EntitySpec<?> spec = mgmt().getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, input, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class); final Entity app = mgmt().getEntityManager().createEntity(spec); getLogger().info("Test created app, and will now start " + app); // start the app (happens automatically if we use camp to instantiate, but not if we use crate spec approach) app.invoke(Startable.START, startParameters).get(); return app; }
protected Entity createAndStartApplicationAsync(String yaml, Map<String,?> startParameters) throws Exception { EntitySpec<?> spec = mgmt().getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, yaml, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class); final Entity app = brooklynMgmt.getEntityManager().createEntity(spec); // start the app (happens automatically if we use camp to instantiate, but not if we use create spec approach). // Note calling .get() on task, so this is non-blocking. app.invoke(Startable.START, startParameters); return app; }
protected Entity createAndStartApplication(String input, Map<String,?> startParameters) throws Exception { EntitySpec<?> spec = mgmt().getTypeRegistry().createSpecFromPlan(CampTypePlanTransformer.FORMAT, input, RegisteredTypeLoadingContexts.spec(Application.class), EntitySpec.class); final Entity app = brooklynMgmt.getEntityManager().createEntity(spec); // start the app (happens automatically if we use camp to instantiate, but not if we use crate spec approach) app.invoke(Startable.START, startParameters).get(); return app; }
@Test public void testRebindsClusterWithEntitySpecWrappingOsgi() throws Exception { RegisteredType ci = installWrappedMoreEntity(); EntitySpec<DynamicCluster> clusterSpec = EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.INITIAL_SIZE, 1) .configure(DynamicCluster.MEMBER_SPEC, origManagementContext.getTypeRegistry().createSpec(ci, null, EntitySpec.class)); final Entity app = mgmt().getEntityManager().createEntity(EntitySpec.create(BasicApplication.class).child(clusterSpec)); app.invoke(Startable.START, MutableMap.of()).get(); rebind(); }
@Test public void testClusterWithEntitySpecFromOsgi() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); // install dependencies ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_OSGI_ENTITIES_URL) ).checkNoError(); ((ManagementContextInternal)mgmt()).getOsgiManager().get().install( new ResourceUtils(getClass()).getResourceFromUrl(BROOKLYN_TEST_MORE_ENTITIES_V2_URL) ).get(); RegisteredType ci = Preconditions.checkNotNull( mgmt().getTypeRegistry().get(BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY) ); EntitySpec<DynamicCluster> clusterSpec = EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.INITIAL_SIZE, 1) .configure(DynamicCluster.MEMBER_SPEC, origManagementContext.getTypeRegistry().createSpec(ci, null, EntitySpec.class)); final Entity app = mgmt().getEntityManager().createEntity(EntitySpec.create(BasicApplication.class).child(clusterSpec)); app.invoke(Startable.START, MutableMap.of()).get(); rebind(); }