From 62315261f7e6b011edccacb72992de61196d11da Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Fri, 21 Apr 2017 12:16:56 +0100 Subject: [PATCH] Adds BashCommands.waitForFileExists Also changes waitForFileContents to `cat file` if the check fails --- .../core/ssh/BashCommandsIntegrationTest.java | 52 ++++++++++++++----- .../brooklyn/util/ssh/BashCommands.java | 19 +++++++ 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/core/src/test/java/org/apache/brooklyn/util/core/ssh/BashCommandsIntegrationTest.java b/core/src/test/java/org/apache/brooklyn/util/core/ssh/BashCommandsIntegrationTest.java index 92be83dad3..3f99ee3ac2 100644 --- a/core/src/test/java/org/apache/brooklyn/util/core/ssh/BashCommandsIntegrationTest.java +++ b/core/src/test/java/org/apache/brooklyn/util/core/ssh/BashCommandsIntegrationTest.java @@ -33,8 +33,7 @@ import java.util.Collection; import java.util.List; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.test.BrooklynMgmtUnitTestSupport; import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests; import org.apache.brooklyn.util.core.ResourceUtils; import org.apache.brooklyn.util.core.task.BasicExecutionContext; @@ -62,11 +61,10 @@ import com.google.common.collect.ImmutableMap; import com.google.common.io.Files; -public class BashCommandsIntegrationTest { +public class BashCommandsIntegrationTest extends BrooklynMgmtUnitTestSupport { private static final Logger log = LoggerFactory.getLogger(BashCommandsIntegrationTest.class); - private ManagementContext mgmt; private BasicExecutionContext exec; private File destFile; @@ -87,7 +85,8 @@ public class BashCommandsIntegrationTest { @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { - mgmt = new LocalManagementContextForTests(); + super.setUp(); + exec = new BasicExecutionContext(mgmt.getExecutionManager()); destFile = Os.newTempFile(getClass(), "commoncommands-test-dest.txt"); @@ -120,14 +119,17 @@ public void setUp() throws Exception { @AfterMethod(alwaysRun=true) public void tearDown() throws Exception { - if (sourceFile1 != null) sourceFile1.delete(); - if (sourceFile2 != null) sourceFile2.delete(); - if (destFile != null) destFile.delete(); - if (localRepoEntityFile != null) localRepoEntityFile.delete(); - if (tmpSudoersFile != null) tmpSudoersFile.delete(); - if (localRepoEntityBasePath != null) FileUtils.deleteDirectory(localRepoEntityBasePath); - if (loc != null) loc.close(); - if (mgmt != null) Entities.destroyAll(mgmt); + try { + if (sourceFile1 != null) sourceFile1.delete(); + if (sourceFile2 != null) sourceFile2.delete(); + if (destFile != null) destFile.delete(); + if (localRepoEntityFile != null) localRepoEntityFile.delete(); + if (tmpSudoersFile != null) tmpSudoersFile.delete(); + if (localRepoEntityBasePath != null) FileUtils.deleteDirectory(localRepoEntityBasePath); + if (loc != null) loc.close(); + } finally { + super.tearDown(); + } } @Test(groups="Integration") @@ -332,6 +334,30 @@ public void testWaitForFileContentsWhenContentsAppearAfterStart() throws Excepti assertFalse(output.contains("Couldn't find"), "output="+output); } + @Test(groups="Integration") + public void testWaitForFileExistsWhenAbortingOnFail() throws Exception { + String cmd = BashCommands.waitForFileExists(destFile.getAbsolutePath(), Duration.ONE_SECOND, true); + + int exitcode = loc.execCommands("test", ImmutableList.of(cmd)); + assertEquals(exitcode, 0); + + destFile.delete(); + int exitcode2 = loc.execCommands("test", ImmutableList.of(cmd)); + assertEquals(exitcode2, 1); + } + + @Test(groups="Integration") + public void testWaitForFileExistsWhenNotAbortingOnFail() throws Exception { + String cmd = BashCommands.waitForFileExists(destFile.getAbsolutePath(), Duration.ONE_SECOND, false); + + String output = execRequiringZeroAndReturningStdout(loc, cmd).get(); + assertFalse(output.contains("Couldn't find"), "output="+output); + + destFile.delete(); + String output2 = execRequiringZeroAndReturningStdout(loc, cmd).get(); + assertTrue(output2.contains("Couldn't find"), "output="+output2); + } + @Test(groups="Integration", dependsOnMethods="testSudo") public void testWaitForPortFreeWhenAbortingOnTimeout() throws Exception { ServerSocket serverSocket = openServerSocket(); diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/ssh/BashCommands.java b/utils/common/src/main/java/org/apache/brooklyn/util/ssh/BashCommands.java index 414abc582e..fe7c939821 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/ssh/BashCommands.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/ssh/BashCommands.java @@ -493,6 +493,8 @@ public static String waitForFileContents(String file, String desiredContent, Dur " sleep 1", "done", "if test \"$result\" -ne 0; then", + " ls -l "+file+" || true", + " cat "+file+" || true", " "+ (failOnTimeout ? "echo \"Couldn't find "+desiredContent+" in "+file+"; aborting\" && exit 1" : "echo \"Couldn't find "+desiredContent+" in "+file+"; continuing\""), @@ -500,6 +502,23 @@ public static String waitForFileContents(String file, String desiredContent, Dur return Joiner.on("\n").join(commands); } + public static String waitForFileExists(String file, Duration timeout, boolean failOnTimeout) { + long secs = Math.max(timeout.toSeconds(), 1); + + List commands = ImmutableList.of( + "for i in {1.."+secs+"}; do", + " [[ -f "+file+" ]] && result=0 || result=$?", + " [ \"$result\" == 0 ] && break", + " sleep 1", + "done", + "if test \"$result\" -ne 0; then", + " "+ (failOnTimeout ? + "echo \"Couldn't find file "+file+"; aborting\" && exit 1" : + "echo \"Couldn't find file "+file+"; continuing\""), + "fi"); + return Joiner.on("\n").join(commands); + } + public static String waitForPortFree(int port, Duration timeout, boolean failOnTimeout) { long secs = Math.max(timeout.toSeconds(), 1);