Search in sources :

Example 11 with Exec

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());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) IntConsumer(java.util.function.IntConsumer) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) Platform(com.aws.greengrass.util.platforms.Platform) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(com.aws.greengrass.dependency.Context) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Path(java.nio.file.Path) Exec(com.aws.greengrass.util.Exec) GGServiceTestUtil(com.aws.greengrass.testcommons.testutilities.GGServiceTestUtil) SERVICE_UNIQUE_ID_KEY(com.aws.greengrass.ipc.AuthenticationHandler.SERVICE_UNIQUE_ID_KEY) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Topic(com.aws.greengrass.config.Topic) Topics(com.aws.greengrass.config.Topics) NucleusPaths(com.aws.greengrass.util.NucleusPaths) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TempDir(org.junit.jupiter.api.io.TempDir) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Exec(com.aws.greengrass.util.Exec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) IntConsumer(java.util.function.IntConsumer) Test(org.junit.jupiter.api.Test)

Example 12 with Exec

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());
    }
}
Also used : Exec(com.aws.greengrass.util.Exec) Test(org.junit.jupiter.api.Test)

Example 13 with Exec

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());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) IntConsumer(java.util.function.IntConsumer) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration) Mockito.lenient(org.mockito.Mockito.lenient) Platform(com.aws.greengrass.util.platforms.Platform) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Context(com.aws.greengrass.dependency.Context) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Path(java.nio.file.Path) Exec(com.aws.greengrass.util.Exec) GGServiceTestUtil(com.aws.greengrass.testcommons.testutilities.GGServiceTestUtil) SERVICE_UNIQUE_ID_KEY(com.aws.greengrass.ipc.AuthenticationHandler.SERVICE_UNIQUE_ID_KEY) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Topic(com.aws.greengrass.config.Topic) Topics(com.aws.greengrass.config.Topics) NucleusPaths(com.aws.greengrass.util.NucleusPaths) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TempDir(org.junit.jupiter.api.io.TempDir) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Exec(com.aws.greengrass.util.Exec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) IntConsumer(java.util.function.IntConsumer) Test(org.junit.jupiter.api.Test)

Example 14 with Exec

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());
    }
}
Also used : Exec(com.aws.greengrass.util.Exec) Test(org.junit.jupiter.api.Test)

Example 15 with Exec

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);
    }
}
Also used : Exec(com.aws.greengrass.util.Exec) IOException(java.io.IOException)

Aggregations

Exec (com.aws.greengrass.util.Exec)20 Test (org.junit.jupiter.api.Test)11 IOException (java.io.IOException)10 Platform (com.aws.greengrass.util.platforms.Platform)5 Path (java.nio.file.Path)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Topic (com.aws.greengrass.config.Topic)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Topics (com.aws.greengrass.config.Topics)2 Context (com.aws.greengrass.dependency.Context)2 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)2 SERVICE_UNIQUE_ID_KEY (com.aws.greengrass.ipc.AuthenticationHandler.SERVICE_UNIQUE_ID_KEY)2 LogEventBuilder (com.aws.greengrass.logging.api.LogEventBuilder)2 Logger (com.aws.greengrass.logging.api.Logger)2 GGServiceTestUtil (com.aws.greengrass.testcommons.testutilities.GGServiceTestUtil)2 NucleusPaths (com.aws.greengrass.util.NucleusPaths)2 Pair (com.aws.greengrass.util.Pair)2 File (java.io.File)2 Arrays (java.util.Arrays)2 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)2