@Test public void testAddsPolicy() throws Exception { MyPolicy policy = new MyPolicy(); entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(policy)); assertEquals(Iterables.getOnlyElement(entity.policies()), policy); }
@Test public void testAddsPolicySpec() throws Exception { entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(MyPolicy.class) .displayName("mypolicyname") .configure(MyPolicy.CONF1, "myconf1val") .configure("myfield", "myfieldval"))); Policy policy = Iterables.getOnlyElement(entity.policies()); assertTrue(policy instanceof MyPolicy, "policy="+policy); assertEquals(policy.getDisplayName(), "mypolicyname"); assertEquals(policy.getConfig(MyPolicy.CONF1), "myconf1val"); }
.policy(PolicySpec.create(CreateUserPolicy.class) .configure(CreateUserPolicy.GRANT_SUDO, true) .configure(CreateUserPolicy.VM_USERNAME, newUsername)));
@Test public void testScheduledEffectorFiresImmediately() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(ScheduledEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test public void testAdvertisesMachineLoginDetails() throws Exception { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(AdvertiseWinrmLoginPolicy.class))); WinRmMachineLocation machine = mgmt.getLocationManager().createLocation(LocationSpec.create(WinRmMachineLocation.class) .configure("address", "1.2.3.4") .configure("user", "myuser") .configure("port", 5678) .configure("password", "mypassword")); app.start(ImmutableList.of(machine)); String expected = "myuser : mypassword @ 1.2.3.4:5678"; EntityAsserts.assertAttributeEqualsEventually(entity, AdvertiseWinrmLoginPolicy.VM_USER_CREDENTIALS, expected); } }
@Test public void testEffectorMaintainsPreviousCollectionThroughRebind() throws Exception { final Set<Integer> input1 = ImmutableSet.of(1, 2); final Set<Integer> input2 = ImmutableSet.of(2, 3); final Set<Integer> input3 = ImmutableSet.of(3, 4); Entity testEntity = app().createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(InvokeEffectorOnCollectionSensorChange.class) .configure(InvokeEffectorOnCollectionSensorChange.TRIGGER_SENSOR, SENSOR) .configure(InvokeEffectorOnCollectionSensorChange.ON_REMOVED_EFFECTOR_NAME, "on-removed-effector")) .addInitializer(new AddEffector(Effectors.effector(Void.class, "on-removed-effector") .impl(new PublishingEffector()) .build()))); testEntity.sensors().set(SENSOR, input1); testEntity.sensors().set(SENSOR, input2); EntityAsserts.assertAttributeEqualsEventually(testEntity, REMOVED_EFFECTOR_VALUES, ImmutableSet.<Object>of(1)); newApp = rebind(); testEntity = Iterables.getOnlyElement(newApp.getChildren()); testEntity.sensors().set(SENSOR, input3); EntityAsserts.assertAttributeEqualsEventually(testEntity, REMOVED_EFFECTOR_VALUES, ImmutableSet.<Object>of(1, 2)); }
@Test public void testPeriodicEffectorFires() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.ONE_MILLISECOND) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(PeriodicEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, PeriodicEffectorPolicy.RUNNING, true); assertCallHistoryEventually(entity, "myEffector", 2); }
@Test(groups="Integration") public void testSuspendsAndResumes() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.WAIT, Duration.FIVE_SECONDS) .configure(ScheduledEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); policy.suspend(); policy.resume(); Time.sleep(Duration.seconds(5)); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test public void testSuspendsAndResumeBeforeTriggered() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.ONE_MILLISECOND) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); policy.suspend(); policy.resume(); assertCallHistoryNeverContinually(entity, "myEffector"); entity.sensors().set(START, Boolean.TRUE); assertCallHistoryContainsEventually(entity, "myEffector"); } }
@Test public void testScheduledEffectorFiresOnSensor() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(ScheduledEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); assertCallHistoryNeverContinually(entity, "myEffector"); entity.sensors().set(ScheduledEffectorPolicy.INVOKE_IMMEDIATELY, Boolean.TRUE); assertCallHistoryContainsEventually(entity, "myEffector"); } }
@Test public void testSuspendsAndResumes() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.ONE_MILLISECOND) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); entity.sensors().set(START, Boolean.TRUE); assertCallHistoryContainsEventually(entity, "myEffector"); policy.suspend(); assertCallsStopEventually(entity, "myEffector"); entity.clearCallHistory(); policy.resume(); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test public void testShortPeriodicEffectorFiresAfterRebind() throws Exception { TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.millis(1)) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); origEntity.sensors().set(START, Boolean.TRUE); assertCallHistoryContainsEventually(origEntity, "myEffector"); newApp = rebind(RebindOptions.create().terminateOrigManagementContext(true)); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); Policy newPolicy = Iterables.find(newEntity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)); assertConfigEqualsEventually(newPolicy, PeriodicEffectorPolicy.RUNNING, true); int calls = newEntity.getCallHistory().size(); assertCallHistoryEventually(newEntity, "myEffector", calls + 2); }
@Test(groups="Integration") public void testScheduledEffectorFiresAfterDelay() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(ScheduledEffectorPolicy.class) .configure(ScheduledEffectorPolicy.EFFECTOR, "myEffector") .configure(ScheduledEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(ScheduledEffectorPolicy.WAIT, Duration.FIVE_SECONDS) .configure(ScheduledEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(ScheduledEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(ScheduledEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, ScheduledEffectorPolicy.RUNNING, true); assertCallHistoryNeverContinually(entity, "myEffector"); Time.sleep(Duration.seconds(5)); assertCallHistoryContainsEventually(entity, "myEffector"); }
@Test(groups="Integration") public void testPeriodicEffectorFiresAfterDelay() { TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.ONE_MILLISECOND) .configure(PeriodicEffectorPolicy.WAIT, Duration.FIVE_SECONDS) .configure(PeriodicEffectorPolicy.START_SENSOR, START))); Policy policy = Iterables.tryFind(entity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)).orNull(); Asserts.assertNotNull(policy); Asserts.assertTrue(entity.getCallHistory().isEmpty()); Asserts.assertFalse(policy.config().get(PeriodicEffectorPolicy.RUNNING)); entity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(policy, PeriodicEffectorPolicy.RUNNING, true); assertCallHistoryNeverContinually(entity, "myEffector"); Time.sleep(Duration.seconds(5)); assertCallHistoryEventually(entity, "myEffector", 2); }
@Test public void testLongPeriodicEffectorFiresAfterRebind() throws Exception { TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.millis(100)) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); origEntity.sensors().set(START, Boolean.TRUE); assertCallHistoryContainsEventually(origEntity, "myEffector"); newApp = rebind(RebindOptions.create().terminateOrigManagementContext(true)); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); Policy newPolicy = Iterables.find(newEntity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)); assertConfigEqualsEventually(newPolicy, PeriodicEffectorPolicy.RUNNING, true); assertCallHistoryContainsEventually(newEntity, "myEffector"); }
.policy(PolicySpec.create(CreateUserPolicy.class) .configure(CreateUserPolicy.GRANT_SUDO, true) .configure(CreateUserPolicy.RESET_LOGIN_USER, false)
.tags(ImmutableList.<Object>of("tag1", "tag2")) .enricher(EnricherSpec.create(TestEnricher.class)) .policy(PolicySpec.create(TestPolicy.class)));
@Test public void testPeriodicEffectorStartsAfterRebind() throws Exception { TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class) .policy(PolicySpec.create(PeriodicEffectorPolicy.class) .configure(PeriodicEffectorPolicy.EFFECTOR, "myEffector") .configure(PeriodicEffectorPolicy.EFFECTOR_ARGUMENTS, ImmutableMap.of()) .configure(PeriodicEffectorPolicy.PERIOD, Duration.millis(1)) .configure(PeriodicEffectorPolicy.TIME, "immediately") .configure(PeriodicEffectorPolicy.START_SENSOR, START))); newApp = rebind(RebindOptions.create().terminateOrigManagementContext(true)); TestEntity newEntity = (TestEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(TestEntity.class)); Policy newPolicy = Iterables.find(newEntity.policies(), Predicates.instanceOf(PeriodicEffectorPolicy.class)); Asserts.assertFalse(newPolicy.config().get(PeriodicEffectorPolicy.RUNNING)); Asserts.assertFalse(newEntity.getCallHistory().contains("myEffector")); newEntity.sensors().set(START, Boolean.TRUE); assertConfigEqualsEventually(newPolicy, PeriodicEffectorPolicy.RUNNING, true); assertCallHistoryEventually(newEntity, "myEffector", 2); }
.configure(ControlledDynamicWebAppCluster.CONTROLLER_SPEC, EntitySpec.create(TrackingAbstractController.class)) .configure(ControlledDynamicWebAppCluster.MEMBER_SPEC, EntitySpec.create(TestJavaWebAppEntity.class)) .policy(PolicySpec.create(AutoScalerPolicy.class) .configure(AutoScalerPolicy.METRIC, scalingMetric) .configure(AutoScalerPolicy.MIN_POOL_SIZE, 1)
.configure(DockerContainer.DOCKER_INFRASTRUCTURE, getInfrastructure()); if (config().get(DockerInfrastructure.HA_POLICY_ENABLE)) { dockerContainerSpec.policy(PolicySpec.create(ServiceRestarter.class) .configure(ServiceRestarter.FAILURE_SENSOR_TO_MONITOR, ServiceFailureDetector.ENTITY_FAILED));