use of com.aws.greengrass.util.Exec in project aws-greengrass-nucleus by aws-greengrass.
the class ShellRunnerTest method GIVEN_shell_command_WHEN_run_in_background_THEN_succeeds.
@Test
void GIVEN_shell_command_WHEN_run_in_background_THEN_succeeds() throws Exception {
final AtomicInteger exitCode = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
IntConsumer background = (value) -> {
exitCode.set(value);
latch.countDown();
};
final ShellRunner shellRunner = context.get(ShellRunner.class);
try (Exec exec = shellRunner.setup("note", "echo 0", greengrassService)) {
boolean ok = shellRunner.successful(exec, "note", background, greengrassService);
assertTrue(ok);
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertEquals(0, exitCode.get());
assertFalse(exec.isRunning());
}
}
use of com.aws.greengrass.util.Exec in project aws-greengrass-nucleus by aws-greengrass.
the class ShellRunnerTest method GIVEN_shell_command_WHEN_run_in_foreground_THEN_succeeds.
@Test
void GIVEN_shell_command_WHEN_run_in_foreground_THEN_succeeds() throws Exception {
final ShellRunner shellRunner = context.get(ShellRunner.class);
try (Exec exec = shellRunner.setup("note", "echo hi", greengrassService)) {
boolean ok = shellRunner.successful(exec, "note", null, greengrassService);
assertTrue(ok);
assertFalse(exec.isRunning());
}
}
use of com.aws.greengrass.util.Exec in project aws-greengrass-nucleus by aws-greengrass.
the class ShellRunnerTest method GIVEN_shell_command_that_doesnt_exist_WHEN_run_in_background_THEN_fails.
@Test
void GIVEN_shell_command_that_doesnt_exist_WHEN_run_in_background_THEN_fails() throws Exception {
final AtomicInteger exitCode = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
IntConsumer background = (value) -> {
exitCode.set(value);
latch.countDown();
};
final ShellRunner shellRunner = context.get(ShellRunner.class);
try (Exec exec = shellRunner.setup("note", "there_is_no_such_program", greengrassService)) {
boolean ok = shellRunner.successful(exec, "note", background, greengrassService);
// when runs in background, always return true
assertTrue(ok);
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertEquals(Platform.getInstance().exitCodeWhenCommandDoesNotExist(), exitCode.get());
assertFalse(exec.isRunning());
}
}
use of com.aws.greengrass.util.Exec in project aws-greengrass-nucleus by aws-greengrass.
the class ShellRunnerTest method GIVEN_shell_command_that_doesnt_exist_WHEN_run_in_foreground_THEN_fails.
@Test
void GIVEN_shell_command_that_doesnt_exist_WHEN_run_in_foreground_THEN_fails() throws Exception {
final ShellRunner shellRunner = context.get(ShellRunner.class);
try (Exec exec = shellRunner.setup("note", "there_is_no_such_program", greengrassService)) {
boolean ok = shellRunner.successful(exec, "note", null, greengrassService);
assertFalse(ok);
assertFalse(exec.isRunning());
}
}
use of com.aws.greengrass.util.Exec in project aws-greengrass-nucleus by aws-greengrass.
the class UnixPlatform method runCmd.
/**
* Run a arbitrary command.
* @param cmdStr command string
* @param out output consumer
* @param msg error string
* @throws IOException IO exception
*/
public void runCmd(String cmdStr, Consumer<CharSequence> out, String msg) throws IOException {
try (Exec exec = getInstance().createNewProcessRunner()) {
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
Optional<Integer> exit = exec.withExec(cmdStr.split(" ")).withShell().withOut(o -> {
out.accept(o);
output.append(o);
}).withErr(error::append).exec();
if (!exit.isPresent() || exit.get() != 0) {
throw new IOException(String.format(String.format("%s - command: %s, output: %s , error: %s ", msg, cmdStr, output.toString(), error.toString())));
}
} catch (InterruptedException | IOException e) {
throw new IOException(String.format("%s , command : %s", msg, cmdStr), e);
}
}
Aggregations