Adds test for WindowsPerformanceCounterFeed.SendPerfCountersToSensors.onSuccess
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/a85fb3c9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/a85fb3c9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/a85fb3c9 Branch: refs/heads/master Commit: a85fb3c9af4090ad8ec8729e870712f29d5b03ee Parents: 280baa7 Author: Martin Harris <[email protected]> Authored: Mon Jun 29 16:48:13 2015 +0100 Committer: Martin Harris <[email protected]> Committed: Mon Jun 29 16:48:13 2015 +0100 ---------------------------------------------------------------------- .../windows/WindowsPerformanceCounterFeed.java | 4 +- .../WindowsPerformanceCounterFeedTest.java | 83 +++++++++++++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a85fb3c9/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java b/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java index 8b6b007..5278ff9 100644 --- a/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java +++ b/core/src/main/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeed.java @@ -54,6 +54,7 @@ import brooklyn.util.exceptions.Exceptions; import brooklyn.util.flags.TypeCoercions; import brooklyn.util.time.Duration; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Strings; @@ -260,7 +261,8 @@ public class WindowsPerformanceCounterFeed extends AbstractFeed { } } - private static class SendPerfCountersToSensors implements PollHandler<WinRmToolResponse> { + @VisibleForTesting + static class SendPerfCountersToSensors implements PollHandler<WinRmToolResponse> { private final EntityLocal entity; private final List<WindowsPerformanceCounterPollConfig<?>> polls; private final Set<AttributeSensor<?>> failedAttributes = Sets.newLinkedHashSet(); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a85fb3c9/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java b/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java index 4b2ed91..f4ba43b 100644 --- a/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java +++ b/core/src/test/java/brooklyn/event/feed/windows/WindowsPerformanceCounterFeedTest.java @@ -18,15 +18,54 @@ */ package brooklyn.event.feed.windows; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import java.util.Collection; +import java.util.Iterator; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.util.Iterator; +import brooklyn.entity.BrooklynAppUnitTestSupport; +import brooklyn.entity.basic.EntityLocal; +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.event.AttributeSensor; +import brooklyn.event.basic.Sensors; +import brooklyn.location.Location; +import brooklyn.location.basic.LocalhostMachineProvisioningLocation; +import brooklyn.test.EntityTestUtils; +import brooklyn.test.entity.TestEntity; +import brooklyn.util.text.Strings; +import io.cloudsoft.winrm4j.winrm.WinRmToolResponse; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; -import static org.testng.Assert.*; +public class WindowsPerformanceCounterFeedTest extends BrooklynAppUnitTestSupport { -public class WindowsPerformanceCounterFeedTest { + private Location loc; + private EntityLocal entity; + + @BeforeMethod(alwaysRun=true) + @Override + public void setUp() throws Exception { + super.setUp(); + loc = new LocalhostMachineProvisioningLocation(); + entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + app.start(ImmutableList.of(loc)); + } + + @AfterMethod(alwaysRun=true) + @Override + public void tearDown() throws Exception { + super.tearDown(); + } private static final Logger log = LoggerFactory.getLogger(WindowsPerformanceCounterFeedTest.class); @@ -50,4 +89,42 @@ public class WindowsPerformanceCounterFeedTest { assertFalse(iterator.hasNext()); } + @Test + public void testSendPerfCountersToSensors() { + AttributeSensor<String> stringSensor = Sensors.newStringSensor("foo.bar"); + AttributeSensor<Integer> integerSensor = Sensors.newIntegerSensor("bar.baz"); + AttributeSensor<Double> doubleSensor = Sensors.newDoubleSensor("baz.quux"); + + Collection<WindowsPerformanceCounterPollConfig<?>> polls = ImmutableSet.<WindowsPerformanceCounterPollConfig<?>>of( + new WindowsPerformanceCounterPollConfig(stringSensor).performanceCounterName("\\processor information(_total)\\% processor time"), + new WindowsPerformanceCounterPollConfig(integerSensor).performanceCounterName("\\integer.sensor"), + new WindowsPerformanceCounterPollConfig(doubleSensor).performanceCounterName("\\double\\sensor\\with\\multiple\\sub\\paths") + ); + + WindowsPerformanceCounterFeed.SendPerfCountersToSensors sendPerfCountersToSensors = new WindowsPerformanceCounterFeed.SendPerfCountersToSensors(entity, polls); + + assertNull(entity.getAttribute(stringSensor)); + + StringBuilder responseBuilder = new StringBuilder(); + // NOTE: This builds the response in a different order to which they are passed to the SendPerfCountersToSensors constructor + // this tests that the values are applied correctly even if the (possibly non-deterministic) order in which + // they are returned by the Get-Counter scriptlet is different + addMockResponse(responseBuilder, "\\\\machine.name\\double\\sensor\\with\\multiple\\sub\\paths", "3.1415926"); + addMockResponse(responseBuilder, "\\\\win-lge7uj2blau\\processor information(_total)\\% processor time", "99.9"); + addMockResponse(responseBuilder, "\\\\machine.name\\integer.sensor", "15"); + + sendPerfCountersToSensors.onSuccess(new WinRmToolResponse(responseBuilder.toString(), "", 0)); + + EntityTestUtils.assertAttributeEquals(entity, stringSensor, "99.9"); + EntityTestUtils.assertAttributeEquals(entity, integerSensor, 15); + EntityTestUtils.assertAttributeEquals(entity, doubleSensor, 3.1415926); + } + + private void addMockResponse(StringBuilder responseBuilder, String path, String value) { + responseBuilder.append(path); + responseBuilder.append(Strings.repeat(" ", 200 - (path.length() + value.length()))); + responseBuilder.append(value); + responseBuilder.append("\r\n"); + } + }
