Repository: brooklyn-server Updated Branches: refs/heads/master 7f11b4b3c -> e7cd82357
Adds WindowsYamlTest (unit test) Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/7f3eda61 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/7f3eda61 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/7f3eda61 Branch: refs/heads/master Commit: 7f3eda61b3e58b0675bb0074ecd23bd7d127e38a Parents: 0fe535e Author: Aled Sage <[email protected]> Authored: Fri May 26 12:14:40 2017 +0100 Committer: Aled Sage <[email protected]> Committed: Mon May 29 11:55:16 2017 +0100 ---------------------------------------------------------------------- .../camp/brooklyn/AbstractWindowsYamlTest.java | 138 ++++++++++++ .../camp/brooklyn/WindowsYamlLiveTest.java | 89 +------- .../brooklyn/camp/brooklyn/WindowsYamlTest.java | 222 +++++++++++++++++++ .../core/internal/winrm/ExecCmdAsserts.java | 120 ++++++++++ 4 files changed, 481 insertions(+), 88 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/7f3eda61/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractWindowsYamlTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractWindowsYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractWindowsYamlTest.java new file mode 100644 index 0000000..f23bec4 --- /dev/null +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractWindowsYamlTest.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.camp.brooklyn; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.brooklyn.api.mgmt.HasTaskChildren; +import org.apache.brooklyn.api.mgmt.Task; +import org.apache.brooklyn.core.mgmt.BrooklynTaskTags; +import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; +import org.apache.brooklyn.entity.software.base.SoftwareProcess; +import org.apache.brooklyn.util.core.task.TaskPredicates; +import org.apache.brooklyn.util.text.StringPredicates; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.collect.Lists; + +/** + * Tests Windows YAML blueprint features. + */ +public abstract class AbstractWindowsYamlTest extends AbstractYamlTest { + + // TODO Remove duplication of assertStreams and VanillaWindowsProcessWinrmStreamsLiveTest.assertStreams + + private static final Logger log = LoggerFactory.getLogger(AbstractWindowsYamlTest.class); + + @Override + protected ManagementContextInternal mgmt() { + return (ManagementContextInternal) super.mgmt(); + } + + protected void assertStreams(SoftwareProcess entity, Map<String, List<String>> stdouts) { + Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity); + + for (Map.Entry<String, List<String>> entry : stdouts.entrySet()) { + String taskNameRegex = entry.getKey(); + List<String> expectedOuts = entry.getValue(); + + Task<?> subTask = findTaskOrSubTask(tasks, TaskPredicates.displayNameSatisfies(StringPredicates.matchesRegex(taskNameRegex))).get(); + + String stdin = getStreamOrFail(subTask, BrooklynTaskTags.STREAM_STDIN); + String stdout = getStreamOrFail(subTask, BrooklynTaskTags.STREAM_STDOUT); + String stderr = getStreamOrFail(subTask, BrooklynTaskTags.STREAM_STDERR); + String env = getStream(subTask, BrooklynTaskTags.STREAM_ENV); + String msg = "stdin="+stdin+"; stdout="+stdout+"; stderr="+stderr+"; env="+env; + + for (String expectedOut : expectedOuts) { + assertTrue(stdout.contains(expectedOut), msg); + } + } + } + + protected void assertSubTaskFailures(SoftwareProcess entity, Map<String, Predicate<CharSequence>> taskErrs) throws Exception { + Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity); + + for (Map.Entry<String, Predicate<CharSequence>> entry : taskErrs.entrySet()) { + String taskNameRegex = entry.getKey(); + Predicate<? super String> errChecker = entry.getValue(); + Task<?> subTask = findTaskOrSubTask(tasks, TaskPredicates.displayNameSatisfies(StringPredicates.matchesRegex(taskNameRegex))).get(); + String msg = "regex="+taskNameRegex+"; task="+subTask; + assertNotNull(subTask, msg); + assertTrue(subTask.isDone(), msg); + assertTrue(subTask.isError(), msg); + try { + subTask.get(); + fail(); + } catch (Exception e) { + if (!errChecker.apply(e.toString())) { + throw e; + } + } + } + } + + public static String getStreamOrFail(Task<?> task, String streamType) { + String msg = "task="+task+"; stream="+streamType; + BrooklynTaskTags.WrappedStream stream = checkNotNull(BrooklynTaskTags.stream(task, streamType), "Stream null: " + msg); + return checkNotNull(stream.streamContents.get(), "Contents null: "+msg); + } + + public static String getStream(Task<?> task, String streamType) { + BrooklynTaskTags.WrappedStream stream = BrooklynTaskTags.stream(task, streamType); + return (stream != null) ? stream.streamContents.get() : null; + } + + protected Optional<Task<?>> findTaskOrSubTask(Iterable<? extends Task<?>> tasks, Predicate<? super Task<?>> matcher) { + List<String> taskNames = Lists.newArrayList(); + Optional<Task<?>> result = findTaskOrSubTaskImpl(tasks, matcher, taskNames); + if (!result.isPresent() && log.isDebugEnabled()) { + log.debug("Task not found matching "+matcher+"; contender names were "+taskNames); + } + return result; + } + + protected Optional<Task<?>> findTaskOrSubTaskImpl(Iterable<? extends Task<?>> tasks, Predicate<? super Task<?>> matcher, List<String> taskNames) { + for (Task<?> task : tasks) { + if (matcher.apply(task)) return Optional.<Task<?>>of(task); + + if (task instanceof HasTaskChildren) { + Optional<Task<?>> subResult = findTaskOrSubTask(((HasTaskChildren) task).getChildren(), matcher); + if (subResult.isPresent()) return subResult; + } + } + + return Optional.<Task<?>>absent(); + } + + @Override + protected Logger getLogger() { + return log; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/7f3eda61/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlLiveTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlLiveTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlLiveTest.java index cba6a61..6a34738 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlLiveTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlLiveTest.java @@ -18,29 +18,20 @@ */ package org.apache.brooklyn.camp.brooklyn; -import static com.google.common.base.Preconditions.checkNotNull; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.util.List; import java.util.Map; -import java.util.Set; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.location.MachineProvisioningLocation; -import org.apache.brooklyn.api.mgmt.HasTaskChildren; -import org.apache.brooklyn.api.mgmt.Task; import org.apache.brooklyn.core.entity.Attributes; import org.apache.brooklyn.core.entity.Entities; import org.apache.brooklyn.core.entity.EntityAsserts; -import org.apache.brooklyn.core.mgmt.BrooklynTaskTags; import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; -import org.apache.brooklyn.entity.software.base.SoftwareProcess; import org.apache.brooklyn.entity.software.base.VanillaWindowsProcess; import org.apache.brooklyn.entity.software.base.test.location.WindowsTestFixture; import org.apache.brooklyn.location.winrm.WinRmMachineLocation; -import org.apache.brooklyn.util.core.task.TaskPredicates; import org.apache.brooklyn.util.text.StringPredicates; import org.apache.brooklyn.util.text.Strings; import org.slf4j.Logger; @@ -52,8 +43,6 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.base.Joiner; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; @@ -62,7 +51,7 @@ import com.google.common.collect.Lists; * Tests Windows YAML blueprint features. */ @Test -public class WindowsYamlLiveTest extends AbstractYamlTest { +public class WindowsYamlLiveTest extends AbstractWindowsYamlTest { // TODO Remove duplication of assertStreams and VanillaWindowsProcessWinrmStreamsLiveTest.assertStreams @@ -328,82 +317,6 @@ public class WindowsYamlLiveTest extends AbstractYamlTest { } } - protected void assertStreams(SoftwareProcess entity, Map<String, List<String>> stdouts) { - Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity); - - for (Map.Entry<String, List<String>> entry : stdouts.entrySet()) { - String taskNameRegex = entry.getKey(); - List<String> expectedOuts = entry.getValue(); - - Task<?> subTask = findTaskOrSubTask(tasks, TaskPredicates.displayNameSatisfies(StringPredicates.matchesRegex(taskNameRegex))).get(); - - String stdin = getStreamOrFail(subTask, BrooklynTaskTags.STREAM_STDIN); - String stdout = getStreamOrFail(subTask, BrooklynTaskTags.STREAM_STDOUT); - String stderr = getStreamOrFail(subTask, BrooklynTaskTags.STREAM_STDERR); - String env = getStream(subTask, BrooklynTaskTags.STREAM_ENV); - String msg = "stdin="+stdin+"; stdout="+stdout+"; stderr="+stderr+"; env="+env; - - for (String expectedOut : expectedOuts) { - assertTrue(stdout.contains(expectedOut), msg); - } - } - } - - protected void assertSubTaskFailures(SoftwareProcess entity, Map<String, Predicate<CharSequence>> taskErrs) throws Exception { - Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), entity); - - for (Map.Entry<String, Predicate<CharSequence>> entry : taskErrs.entrySet()) { - String taskNameRegex = entry.getKey(); - Predicate<? super String> errChecker = entry.getValue(); - Task<?> subTask = findTaskOrSubTask(tasks, TaskPredicates.displayNameSatisfies(StringPredicates.matchesRegex(taskNameRegex))).get(); - String msg = "regex="+taskNameRegex+"; task="+subTask; - assertNotNull(subTask, msg); - assertTrue(subTask.isDone(), msg); - assertTrue(subTask.isError(), msg); - try { - subTask.get(); - fail(); - } catch (Exception e) { - if (!errChecker.apply(e.toString())) { - throw e; - } - } - } - } - - public static String getStreamOrFail(Task<?> task, String streamType) { - String msg = "task="+task+"; stream="+streamType; - BrooklynTaskTags.WrappedStream stream = checkNotNull(BrooklynTaskTags.stream(task, streamType), "Stream null: " + msg); - return checkNotNull(stream.streamContents.get(), "Contents null: "+msg); - } - - public static String getStream(Task<?> task, String streamType) { - BrooklynTaskTags.WrappedStream stream = BrooklynTaskTags.stream(task, streamType); - return (stream != null) ? stream.streamContents.get() : null; - } - - protected Optional<Task<?>> findTaskOrSubTask(Iterable<? extends Task<?>> tasks, Predicate<? super Task<?>> matcher) { - List<String> taskNames = Lists.newArrayList(); - Optional<Task<?>> result = findTaskOrSubTaskImpl(tasks, matcher, taskNames); - if (!result.isPresent() && log.isDebugEnabled()) { - log.debug("Task not found matching "+matcher+"; contender names were "+taskNames); - } - return result; - } - - protected Optional<Task<?>> findTaskOrSubTaskImpl(Iterable<? extends Task<?>> tasks, Predicate<? super Task<?>> matcher, List<String> taskNames) { - for (Task<?> task : tasks) { - if (matcher.apply(task)) return Optional.<Task<?>>of(task); - - if (task instanceof HasTaskChildren) { - Optional<Task<?>> subResult = findTaskOrSubTask(((HasTaskChildren) task).getChildren(), matcher); - if (subResult.isPresent()) return subResult; - } - } - - return Optional.<Task<?>>absent(); - } - @Override protected Logger getLogger() { return log; http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/7f3eda61/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlTest.java new file mode 100644 index 0000000..5b573ae --- /dev/null +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/WindowsYamlTest.java @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.camp.brooklyn; + +import static org.testng.Assert.assertEquals; + +import java.util.List; +import java.util.Map; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.entity.software.base.VanillaWindowsProcess; +import org.apache.brooklyn.util.core.internal.winrm.ExecCmdAsserts; +import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool; +import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.CustomResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; + +/** + * Tests Windows YAML blueprint features. + */ +@Test +public class WindowsYamlTest extends AbstractWindowsYamlTest { + + private static final Logger log = LoggerFactory.getLogger(WindowsYamlTest.class); + + private static final String LOCATION_CATALOG_ID = "byonWindowsLoc"; + + protected Entity app; + protected VanillaWindowsProcess entity; + + protected boolean useDefaultProperties() { + return true; + } + + @BeforeMethod(alwaysRun = true) + public void setUpClass() throws Exception { + super.setUp(); + + RecordingWinRmTool.clear(); + + addCatalogItems( + "brooklyn.catalog:", + " id: " + LOCATION_CATALOG_ID, + " version: 1.0.0", + " itemType: location", + " item:", + " type: byon", + " brooklyn.config:", + " hosts:", + " - winrm: 1.2.3.4", + " user: admin", + " brooklyn.winrm.config.winrmToolClass: "+RecordingWinRmTool.class.getName(), + " password: pa55w0rd", + " osFamily: windows"); + } + + @AfterMethod(alwaysRun = true) + @Override + public void tearDown() throws Exception { + try { + super.tearDown(); + } catch (Throwable t) { + RecordingWinRmTool.clear(); + } + } + + @Test + public void testPassesThroughUseHttps() throws Exception { + super.createAndStartApplication( + "location: "+LOCATION_CATALOG_ID, + "services:", + "- type: "+VanillaWindowsProcess.class.getName(), + " brooklyn.config:", + " launch.powershell.command: myLaunch", + " checkRunning.powershell.command: myCheckRunning", + " provisioning.properties:", + " winrm.useHttps: true"); + + Map<?, ?> constructorProps = RecordingWinRmTool.getLastConstructorProps(); + assertEquals(constructorProps.get("winrm.useHttps"), Boolean.TRUE, "props="+constructorProps); + } + + @Test + public void testPowershellMinimal() throws Exception { + createAndStartApplication( + "location: "+LOCATION_CATALOG_ID, + "services:", + "- type: "+VanillaWindowsProcess.class.getName(), + " brooklyn.config:", + " launch.powershell.command: myLaunch", + " checkRunning.powershell.command: myCheckRunning"); + + RecordingWinRmTool.getExecs(); + ExecCmdAsserts.assertExecHasOnlyOnce(RecordingWinRmTool.getExecs(), "myLaunch"); + ExecCmdAsserts.assertExecContains(RecordingWinRmTool.getLastExec(), "myCheckRunning"); + } + + @Test + public void testPowershell() throws Exception { + RecordingWinRmTool.setCustomResponse("myPreInstall", new CustomResponse(0, "myPreInstallStdout", "")); + RecordingWinRmTool.setCustomResponse("myInstall", new CustomResponse(0, "myInstallStdout", "")); + RecordingWinRmTool.setCustomResponse("myPostInstall", new CustomResponse(0, "myPostInstallStdout", "")); + RecordingWinRmTool.setCustomResponse("myCustomize", new CustomResponse(0, "myCustomizeStdout", "")); + RecordingWinRmTool.setCustomResponse("myPreLaunch", new CustomResponse(0, "myPreLaunchStdout", "")); + RecordingWinRmTool.setCustomResponse("myLaunch", new CustomResponse(0, "myLaunchStdout", "")); + RecordingWinRmTool.setCustomResponse("myPostLaunch", new CustomResponse(0, "myPostLaunchStdout", "")); + RecordingWinRmTool.setCustomResponse("myStop", new CustomResponse(0, "myStopStdout", "")); + + app = createAndStartApplication( + "location: "+LOCATION_CATALOG_ID, + "services:", + "- type: "+VanillaWindowsProcess.class.getName(), + " brooklyn.config:", + " pre.install.powershell.command: myPreInstall", + " install.powershell.command: myInstall", + " post.install.powershell.command: myPostInstall", + " customize.powershell.command: myCustomize", + " pre.launch.powershell.command: myPreLaunch", + " launch.powershell.command: myLaunch", + " post.launch.powershell.command: myPostLaunch", + " checkRunning.powershell.command: myCheckRunning", + " stop.powershell.command: myStop"); + entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, VanillaWindowsProcess.class)); + + Map<String, List<String>> stdouts = ImmutableMap.<String, List<String>>builder() + .put("winrm: pre-install.*", ImmutableList.of("myPreInstallStdout")) + .put("winrm: install.*", ImmutableList.of("myInstallStdout")) + .put("winrm: post-install.*", ImmutableList.of("myPostInstallStdout")) + .put("winrm: customize.*", ImmutableList.of("myCustomizeStdout")) + .put("winrm: pre-launch.*", ImmutableList.of("myPreLaunchStdout")) + .put("winrm: launch.*", ImmutableList.of("myLaunchStdout")) + .put("winrm: post-launch.*", ImmutableList.of("myPostLaunchStdout")) + .build(); + + assertStreams(entity, stdouts); + + entity.stop(); + + Map<String, List<String>> stopStdouts = ImmutableMap.<String, List<String>>builder() + .put("winrm: stop.*", ImmutableList.of("myStopStdout")) + .build(); + + assertStreams(entity, stopStdouts); + } + + @Test + public void testCommands() throws Exception { + RecordingWinRmTool.setCustomResponse("myPreInstall", new CustomResponse(0, "myPreInstallStdout", "")); + RecordingWinRmTool.setCustomResponse("myInstall", new CustomResponse(0, "myInstallStdout", "")); + RecordingWinRmTool.setCustomResponse("myPostInstall", new CustomResponse(0, "myPostInstallStdout", "")); + RecordingWinRmTool.setCustomResponse("myCustomize", new CustomResponse(0, "myCustomizeStdout", "")); + RecordingWinRmTool.setCustomResponse("myPreLaunch", new CustomResponse(0, "myPreLaunchStdout", "")); + RecordingWinRmTool.setCustomResponse("myLaunch", new CustomResponse(0, "myLaunchStdout", "")); + RecordingWinRmTool.setCustomResponse("myPostLaunch", new CustomResponse(0, "myPostLaunchStdout", "")); + RecordingWinRmTool.setCustomResponse("myStop", new CustomResponse(0, "myStopStdout", "")); + + app = createAndStartApplication( + "location: "+LOCATION_CATALOG_ID, + "services:", + "- type: "+VanillaWindowsProcess.class.getName(), + " brooklyn.config:", + " pre.install.command: myPreInstall", + " install.command: myInstall", + " post.install.command: myPostInstall", + " customize.command: myCustomize", + " pre.launch.command: myPreLaunch", + " launch.command: myLaunch", + " post.launch.command: myPostLaunch", + " checkRunning.command: myCheckRunning", + " stop.command: myStop"); + entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, VanillaWindowsProcess.class)); + + Map<String, List<String>> stdouts = ImmutableMap.<String, List<String>>builder() + .put("winrm: pre-install.*", ImmutableList.of("myPreInstallStdout")) + .put("winrm: install.*", ImmutableList.of("myInstallStdout")) + .put("winrm: post-install.*", ImmutableList.of("myPostInstallStdout")) + .put("winrm: customize.*", ImmutableList.of("myCustomizeStdout")) + .put("winrm: pre-launch.*", ImmutableList.of("myPreLaunchStdout")) + .put("winrm: launch.*", ImmutableList.of("myLaunchStdout")) + .put("winrm: post-launch.*", ImmutableList.of("myPostLaunchStdout")) + .build(); + + assertStreams(entity, stdouts); + + entity.stop(); + + Map<String, List<String>> stopStdouts = ImmutableMap.<String, List<String>>builder() + .put("winrm: stop.*", ImmutableList.of("myStopStdout")) + .build(); + + assertStreams(entity, stopStdouts); + } + + @Override + protected Logger getLogger() { + return log; + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/7f3eda61/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/ExecCmdAsserts.java ---------------------------------------------------------------------- diff --git a/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/ExecCmdAsserts.java b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/ExecCmdAsserts.java new file mode 100644 index 0000000..730f36c --- /dev/null +++ b/software/winrm/src/test/java/org/apache/brooklyn/util/core/internal/winrm/ExecCmdAsserts.java @@ -0,0 +1,120 @@ +/* + * Copyright 2016 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.brooklyn.util.core.internal.winrm; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.util.List; + +import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.ExecParams; + +import com.google.common.annotations.Beta; +import com.google.common.base.Predicate; + +@Beta +public class ExecCmdAsserts { + + public static void assertExecsContain(List<ExecParams> actuals, List<String> expectedCmds) { + String errMsg = "actuals="+actuals+"; expected="+expectedCmds; + assertTrue(actuals.size() >= expectedCmds.size(), "actualSize="+actuals.size()+"; expectedSize="+expectedCmds.size()+"; "+errMsg); + for (int i = 0; i < expectedCmds.size(); i++) { + assertExecContains(actuals.get(i), expectedCmds.get(i), errMsg); + } + } + + public static void assertExecContains(ExecParams actual, String expectedCmdRegex) { + assertExecContains(actual, expectedCmdRegex, null); + } + + public static void assertExecContains(ExecParams actual, String expectedCmdRegex, String errMsg) { + for (String cmd : actual.commands) { + if (cmd.matches(expectedCmdRegex)) { + return; + } + } + fail(expectedCmdRegex + " not matched by any commands in " + actual+(errMsg != null ? "; "+errMsg : "")); + } + + public static void assertExecsNotContains(List<? extends ExecParams> actuals, List<String> expectedNotCmdRegexs) { + for (ExecParams actual : actuals) { + assertExecNotContains(actual, expectedNotCmdRegexs); + } + } + + public static void assertExecNotContains(ExecParams actual, List<String> expectedNotCmdRegexs) { + for (String cmdRegex : expectedNotCmdRegexs) { + for (String subActual : actual.commands) { + if (subActual.matches(cmdRegex)) { + fail("Exec should not contain " + cmdRegex + ", but matched by " + actual); + } + } + } + } + + public static void assertExecsSatisfy(List<ExecParams> actuals, List<? extends Predicate<? super ExecParams>> expectedCmds) { + String errMsg = "actuals="+actuals+"; expected="+expectedCmds; + assertTrue(actuals.size() >= expectedCmds.size(), "actualSize="+actuals.size()+"; expectedSize="+expectedCmds.size()+"; "+errMsg); + for (int i = 0; i < expectedCmds.size(); i++) { + assertExecSatisfies(actuals.get(i), expectedCmds.get(i), errMsg); + } + } + + public static void assertExecSatisfies(ExecParams actual, Predicate<? super ExecParams> expected) { + assertExecSatisfies(actual, expected, null); + } + + public static void assertExecSatisfies(ExecParams actual, Predicate<? super ExecParams> expected, String errMsg) { + if (!expected.apply(actual)) { + fail(expected + " not matched by " + actual + (errMsg != null ? "; "+errMsg : "")); + } + } + + public static void assertExecHasNever(List<ExecParams> actuals, String expectedCmd) { + assertExecHasExactly(actuals, expectedCmd, 0); + } + + public static void assertExecHasOnlyOnce(List<ExecParams> actuals, String expectedCmd) { + assertExecHasExactly(actuals, expectedCmd, 1); + } + + public static void assertExecHasExactly(List<ExecParams> actuals, String expectedCmd, int expectedCount) { + String errMsg = "actuals="+actuals+"; expected="+expectedCmd; + int count = 0; + for (ExecParams actual : actuals) { + for (String subActual : actual.commands) { + if (subActual.matches(expectedCmd)) { + count++; + break; + } + } + } + assertEquals(count, expectedCount, errMsg); + } + + public static ExecParams findExecContaining(List<ExecParams> actuals, String cmdRegex) { + for (ExecParams actual : actuals) { + for (String subActual : actual.commands) { + if (subActual.matches(cmdRegex)) { + return actual; + } + } + } + fail("No match for '"+cmdRegex+"' in "+actuals); + throw new IllegalStateException("unreachable code"); + } +}
