Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,32 @@ 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\""),
"fi");
return Joiner.on("\n").join(commands);
}

public static String waitForFileExists(String file, Duration timeout, boolean failOnTimeout) {
long secs = Math.max(timeout.toSeconds(), 1);

List<String> 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);

Expand Down