Extract execCmdAsserts
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/ea87091c Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/ea87091c Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/ea87091c Branch: refs/heads/master Commit: ea87091c1e7aba6ae21d572740cbccffce3f3e15 Parents: ddb6acc Author: Aled Sage <[email protected]> Authored: Wed Aug 10 21:14:58 2016 +0100 Committer: Aled Sage <[email protected]> Committed: Thu Aug 11 20:37:25 2016 +0100 ---------------------------------------------------------------------- .../util/core/internal/ssh/ExecCmdAsserts.java | 97 ++++++++++++++++++++ .../base/VanillaSoftwareProcessTest.java | 82 +++-------------- 2 files changed, 108 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ea87091c/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/ExecCmdAsserts.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/ExecCmdAsserts.java b/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/ExecCmdAsserts.java new file mode 100644 index 0000000..f5fcab5 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/util/core/internal/ssh/ExecCmdAsserts.java @@ -0,0 +1,97 @@ +/* + * 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.ssh; + +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.util.List; + +import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd; + +import com.google.common.annotations.Beta; +import com.google.common.base.Predicate; + +@Beta +public class ExecCmdAsserts { + + public static void assertExecsContain(List<ExecCmd> 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(ExecCmd actual, String expectedCmdRegex) { + assertExecContains(actual, expectedCmdRegex, null); + } + + public static void assertExecContains(ExecCmd 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 ExecCmd> actuals, List<String> expectedNotCmdRegexs) { + for (ExecCmd actual : actuals) { + assertExecNotContains(actual, expectedNotCmdRegexs); + } + } + + public static void assertExecNotContains(ExecCmd 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<ExecCmd> actuals, List<? extends Predicate<? super ExecCmd>> 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(ExecCmd actual, Predicate<? super ExecCmd> expected) { + assertExecSatisfies(actual, expected, null); + } + + public static void assertExecSatisfies(ExecCmd actual, Predicate<? super ExecCmd> expected, String errMsg) { + if (!expected.apply(actual)) { + fail(expected + " not matched by " + actual + (errMsg != null ? "; "+errMsg : "")); + } + } + + public static ExecCmd findExecContaining(List<ExecCmd> actuals, String cmdRegex) { + for (ExecCmd 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"); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/ea87091c/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java index 3b30328..45b7cc6 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java @@ -18,10 +18,6 @@ */ package org.apache.brooklyn.entity.software.base; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -33,15 +29,14 @@ import org.apache.brooklyn.core.entity.Entities; import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; import org.apache.brooklyn.location.byon.FixedListMachineProvisioningLocation; import org.apache.brooklyn.location.ssh.SshMachineLocation; +import org.apache.brooklyn.util.core.internal.ssh.ExecCmdAsserts; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.CustomResponse; -import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmdPredicates; import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecParams; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -85,7 +80,7 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { .configure(VanillaSoftwareProcess.STOP_COMMAND, "stopCommand")); app.start(ImmutableList.of(loc)); - assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "preInstallCommand", "installCommand", "postInstallCommand", "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", "preLaunchCommand", "launchCommand", "postLaunchCommand", @@ -93,7 +88,7 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { app.stop(); - assertExecContains(RecordingSshTool.getLastExecCmd(), "stopCommand"); + ExecCmdAsserts.assertExecContains(RecordingSshTool.getLastExecCmd(), "stopCommand"); } // See https://issues.apache.org/jira/browse/BROOKLYN-273 @@ -127,7 +122,7 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { VanillaSoftwareProcess.RestartSoftwareParameters.RESTART_MACHINE.getName(), VanillaSoftwareProcess.RestartSoftwareParameters.RestartMachineMode.FALSE)) .get(); - assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "checkRunningCommand", "stopCommand", "preLaunchCommand", "launchCommand", "postLaunchCommand", "checkRunningCommand")); @@ -151,12 +146,12 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { .configure(VanillaSoftwareProcess.STOP_COMMAND, "stopCommand")); app.start(ImmutableList.of(loc)); - assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", "preLaunchCommand", "launchCommand", "postLaunchCommand", "checkRunningCommand")); - assertExecsNotContains(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsNotContains(RecordingSshTool.getExecCmds(), ImmutableList.of( "preInstallCommand", "installCommand", "postInstallCommand")); } @@ -177,10 +172,10 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { .configure(VanillaSoftwareProcess.STOP_COMMAND, "stopCommand")); app.start(ImmutableList.of(loc)); - assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "checkRunningCommand")); - assertExecsNotContains(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsNotContains(RecordingSshTool.getExecCmds(), ImmutableList.of( "launchCommand")); } @@ -219,7 +214,7 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { .configure(VanillaSoftwareProcess.STOP_COMMAND, "stopCommand")); app.start(ImmutableList.of(loc)); - assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsContain(RecordingSshTool.getExecCmds(), ImmutableList.of( "checkRunningCommand", "preInstallCommand", "installCommand", "postInstallCommand", "preCustomizeCommand", "customizeCommand", "postCustomizeCommand", @@ -246,7 +241,7 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { Map<String, String> expectedEnv = ImmutableMap.of("KEY1", "VAL1"); - assertExecsSatisfy(RecordingSshTool.getExecCmds(), ImmutableList.of( + ExecCmdAsserts.assertExecsSatisfy(RecordingSshTool.getExecCmds(), ImmutableList.of( Predicates.and(ExecCmdPredicates.containsCmd("preInstallCommand"), ExecCmdPredicates.containsEnv(expectedEnv)), Predicates.and(ExecCmdPredicates.containsCmd("installCommand"), ExecCmdPredicates.containsEnv(expectedEnv)), Predicates.and(ExecCmdPredicates.containsCmd("postInstallCommand"), ExecCmdPredicates.containsEnv(expectedEnv)), @@ -260,63 +255,8 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport { app.stop(); - assertExecSatisfies( + ExecCmdAsserts.assertExecSatisfies( RecordingSshTool.getLastExecCmd(), Predicates.and(ExecCmdPredicates.containsCmd("stopCommand"), ExecCmdPredicates.containsEnv(expectedEnv))); } - - protected void assertExecsContain(List<ExecCmd> 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); - } - } - - protected void assertExecContains(ExecCmd actual, String expectedCmdRegex) { - assertExecContains(actual, expectedCmdRegex, null); - } - - protected void assertExecContains(ExecCmd 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 : "")); - } - - protected void assertExecsNotContains(List<? extends ExecCmd> actuals, List<String> expectedNotCmdRegexs) { - for (ExecCmd actual : actuals) { - assertExecContains(actual, expectedNotCmdRegexs); - } - } - - protected void assertExecContains(ExecCmd 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); - } - } - } - } - - protected void assertExecsSatisfy(List<ExecCmd> actuals, List<? extends Predicate<? super ExecCmd>> 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); - } - } - - protected void assertExecSatisfies(ExecCmd actual, Predicate<? super ExecCmd> expected) { - assertExecSatisfies(actual, expected, null); - } - - protected void assertExecSatisfies(ExecCmd actual, Predicate<? super ExecCmd> expected, String errMsg) { - if (!expected.apply(actual)) { - fail(expected + " not matched by " + actual + (errMsg != null ? "; "+errMsg : "")); - } - } }
