mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lifecycle): add support for startup and shutdown initialization hooks (#128)
* feat(lifecycle): add support for startup and shutdown initialization hooks * test(lifecycle): add unit tests for initialization hooks * test(lifecycle): add integration tests for initialization hooks * docs: add documentation and configuration for initialization hooks
This commit is contained in:
1 parent
823770e463
commit
7b2576fb42
14 files changed
+872
-34
No files matched your search
+181
@@ -0,0 +1,181 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HookScriptExecutorTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private EmulatorConfig emulatorConfigMock;
|
||||
|
||||
@InjectMocks
|
||||
private HookScriptExecutor hookScriptExecutor;
|
||||
|
||||
@Test
|
||||
@DisplayName("Should complete when process exits successfully")
|
||||
void shouldCompleteWhenProcessExitsSuccessfully() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenReturn(true);
|
||||
Mockito.when(process.exitValue()).thenReturn(0);
|
||||
Mockito.when(process.isAlive()).thenReturn(false);
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Mockito.verify(process).waitFor(30L, TimeUnit.SECONDS);
|
||||
Mockito.verify(process).exitValue();
|
||||
Mockito.verify(process, Mockito.never()).destroy();
|
||||
Mockito.verify(process, Mockito.never()).destroyForcibly();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw when process exits with a non-zero code")
|
||||
void shouldThrowWhenProcessExitsWithNonZeroCode() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenReturn(true);
|
||||
Mockito.when(process.exitValue()).thenReturn(42);
|
||||
Mockito.when(process.isAlive()).thenReturn(false);
|
||||
|
||||
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Mockito.verify(process).exitValue();
|
||||
Mockito.verify(process, Mockito.never()).destroy();
|
||||
|
||||
Assertions.assertAll(
|
||||
() -> Assertions.assertEquals("Hook script failed: script.sh exited with code 42", exception.getMessage()),
|
||||
() -> Assertions.assertNull(exception.getCause())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should terminate process and throw when process times out")
|
||||
void shouldTerminateProcessAndThrowWhenProcessTimesOut() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
Mockito.when(emulatorConfigMock.initHooks().shutdownGracePeriodSeconds()).thenReturn(2L);
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenReturn(false);
|
||||
Mockito.when(process.isAlive()).thenReturn(true, false);
|
||||
Mockito.when(process.waitFor(2L, TimeUnit.SECONDS)).thenReturn(false);
|
||||
|
||||
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Mockito.verify(process).destroy();
|
||||
Mockito.verify(process, times(2)).waitFor(2L, TimeUnit.SECONDS);
|
||||
Mockito.verify(process).destroyForcibly();
|
||||
|
||||
Assertions.assertAll(
|
||||
() -> Assertions.assertEquals("Hook script timed out after 30 seconds: script.sh", exception.getMessage()),
|
||||
() -> Assertions.assertNull(exception.getCause())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should not force kill when process terminates during grace period")
|
||||
void shouldNotForceKillWhenProcessTerminatesDuringGracePeriod() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
Mockito.when(emulatorConfigMock.initHooks().shutdownGracePeriodSeconds()).thenReturn(2L);
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenReturn(false);
|
||||
Mockito.when(process.isAlive()).thenReturn(true, false);
|
||||
Mockito.when(process.waitFor(2L, TimeUnit.SECONDS)).thenReturn(true);
|
||||
|
||||
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Mockito.verify(process).destroy();
|
||||
Mockito.verify(process, Mockito.never()).destroyForcibly();
|
||||
|
||||
Assertions.assertAll(
|
||||
() -> Assertions.assertEquals("Hook script timed out after 30 seconds: script.sh", exception.getMessage()),
|
||||
() -> Assertions.assertNull(exception.getCause())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should force cleanup when interrupted while waiting")
|
||||
void shouldForceCleanupWhenInterruptedWhileWaiting() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
InterruptedException exception = new InterruptedException("boom");
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenThrow(exception);
|
||||
Mockito.when(process.isAlive()).thenReturn(true);
|
||||
|
||||
InterruptedException thrown = Assertions.assertThrows(InterruptedException.class, () -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Assertions.assertSame(exception, thrown);
|
||||
Mockito.verify(process).destroyForcibly();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should force cleanup when process is still alive in finally")
|
||||
void shouldForceCleanupWhenProcessIsStillAliveInFinally() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenReturn(true);
|
||||
Mockito.when(process.exitValue()).thenReturn(0);
|
||||
Mockito.when(process.isAlive()).thenReturn(true);
|
||||
|
||||
Assertions.assertDoesNotThrow(() -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Mockito.verify(process).destroyForcibly();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should not wait for grace period when process stops immediately after destroy")
|
||||
void shouldNotWaitForGracePeriodWhenProcessStopsImmediatelyAfterDestroy() throws InterruptedException {
|
||||
Process process = Mockito.mock(Process.class);
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().timeoutSeconds()).thenReturn(30L);
|
||||
Mockito.when(process.waitFor(30L, TimeUnit.SECONDS)).thenReturn(false);
|
||||
Mockito.when(process.isAlive()).thenReturn(false, false);
|
||||
|
||||
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> hookScriptExecutor.run(process, "script.sh"));
|
||||
|
||||
Mockito.verify(process).destroy();
|
||||
Mockito.verify(process, Mockito.never()).waitFor(2L, TimeUnit.SECONDS);
|
||||
Mockito.verify(process, Mockito.never()).destroyForcibly();
|
||||
|
||||
Assertions.assertAll(
|
||||
() -> Assertions.assertEquals("Hook script timed out after 30 seconds: script.sh", exception.getMessage()),
|
||||
() -> Assertions.assertNull(exception.getCause())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should throw IOException when shell executable does not exist")
|
||||
void shouldThrowIOExceptionWhenShellExecutableDoesNotExist() {
|
||||
File hookDirectory = new File(".");
|
||||
|
||||
Mockito.when(emulatorConfigMock.initHooks().shellExecutable()).thenReturn("/definitely/missing/bash");
|
||||
|
||||
IOException exception = Assertions.assertThrows(IOException.class, () -> hookScriptExecutor.run(hookDirectory, "script.sh"));
|
||||
|
||||
Assertions.assertAll(
|
||||
() -> Assertions.assertNotNull(exception.getMessage()),
|
||||
() -> Assertions.assertTrue(exception.getMessage().startsWith("Cannot run program")),
|
||||
() -> Assertions.assertTrue(exception.getMessage().contains("\"/definitely/missing/bash\""))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
@QuarkusTest
|
||||
class InitializationHooksRunnerIntegrationTest {
|
||||
|
||||
@Inject
|
||||
private InitializationHooksRunner initializationHooksRunner;
|
||||
|
||||
@TempDir
|
||||
private Path hookScriptsDirectory;
|
||||
|
||||
@Test
|
||||
@DisplayName("Should execute startup hook scripts in lexicographical order")
|
||||
void shouldExecuteRealScriptsInLexicographicalOrder() throws IOException, InterruptedException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
File hookDirectory = hookScriptsDirectory.toFile();
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Path absoluteOutputFile = outputFile.toAbsolutePath();
|
||||
String bootstrapScript = """
|
||||
#!/bin/bash
|
||||
echo bootstrap >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
String seedResourcesScript = """
|
||||
#!/bin/bash
|
||||
echo seed-resources >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
|
||||
Files.writeString(hookScriptsDirectory.resolve("20-seed-resources.sh"), seedResourcesScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("10-bootstrap.sh"), bootstrapScript);
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
List<String> expectedLines = List.of("bootstrap", "seed-resources");
|
||||
List<String> lines = Files.readAllLines(outputFile);
|
||||
Assertions.assertEquals(expectedLines, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should ignore non-shell files in the hook directory")
|
||||
void shouldIgnoreNonShellScriptFiles() throws IOException, InterruptedException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
File hookDirectory = hookScriptsDirectory.toFile();
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Path absoluteOutputFile = outputFile.toAbsolutePath();
|
||||
String bootstrapScript = """
|
||||
#!/bin/bash
|
||||
echo bootstrap >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
String seedResourcesScript = """
|
||||
#!/bin/bash
|
||||
echo seed-resources >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
|
||||
Files.writeString(hookScriptsDirectory.resolve("10-bootstrap.sh"), bootstrapScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("15-notes.txt"), "ignored");
|
||||
Files.writeString(hookScriptsDirectory.resolve("20-seed-resources.sh"), seedResourcesScript);
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
List<String> expectedLines = List.of("bootstrap", "seed-resources");
|
||||
List<String> lines = Files.readAllLines(outputFile);
|
||||
Assertions.assertEquals(expectedLines, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should use lexicographical ordering for numbered hook file names")
|
||||
void shouldExecuteScriptsUsingLexicographicalFileNameOrder() throws IOException, InterruptedException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
File hookDirectory = hookScriptsDirectory.toFile();
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Path absoluteOutputFile = outputFile.toAbsolutePath();
|
||||
String configureDomainScript = """
|
||||
#!/bin/bash
|
||||
echo configure-domain >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
String bootstrapScript = """
|
||||
#!/bin/bash
|
||||
echo bootstrap >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
String createBucketsScript = """
|
||||
#!/bin/bash
|
||||
echo create-buckets >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
|
||||
Files.writeString(hookScriptsDirectory.resolve("10-configure-domain.sh"), configureDomainScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("01-bootstrap.sh"), bootstrapScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("02-create-buckets.sh"), createBucketsScript);
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
List<String> expectedLines = List.of("bootstrap", "create-buckets", "configure-domain");
|
||||
List<String> lines = Files.readAllLines(outputFile);
|
||||
Assertions.assertEquals(expectedLines, lines);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should do nothing when the hook directory contains no shell scripts")
|
||||
void shouldDoNothingWhenDirectoryContainsNoShellScripts() throws IOException, InterruptedException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
File hookDirectory = hookScriptsDirectory.toFile();
|
||||
Files.writeString(hookScriptsDirectory.resolve("README.txt"), "hook documentation");
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Assertions.assertFalse(Files.exists(outputFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should do nothing when the hook directory does not exist")
|
||||
void shouldDoNothingWhenHookDirectoryDoesNotExist() throws IOException, InterruptedException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
Path missingHookDirectory = hookScriptsDirectory.resolve("missing");
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
|
||||
initializationHooksRunner.run("startup", missingHookDirectory.toFile());
|
||||
|
||||
Assertions.assertFalse(Files.exists(outputFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should do nothing when the hook path is not a directory")
|
||||
void shouldDoNothingWhenHookPathIsNotADirectory() throws IOException, InterruptedException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
Path hookPathFile = hookScriptsDirectory.resolve("hook-file.txt");
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Files.writeString(hookPathFile, "ignored");
|
||||
|
||||
initializationHooksRunner.run("startup", hookPathFile.toFile());
|
||||
|
||||
Assertions.assertFalse(Files.exists(outputFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should stop executing startup hooks after the first failing script")
|
||||
void shouldStopAtFirstFailingScript() throws IOException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
File hookDirectory = hookScriptsDirectory.toFile();
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Path absoluteOutputFile = outputFile.toAbsolutePath();
|
||||
String bootstrapScript = """
|
||||
#!/bin/bash
|
||||
echo bootstrap >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
String failingScript = """
|
||||
#!/bin/bash
|
||||
exit 42
|
||||
""";
|
||||
String seedDataScript = """
|
||||
#!/bin/bash
|
||||
echo seed-data >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
|
||||
Files.writeString(hookScriptsDirectory.resolve("10-bootstrap.sh"), bootstrapScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("20-create-queue.sh"), failingScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("30-seed-data.sh"), seedDataScript);
|
||||
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> initializationHooksRunner.run("startup", hookDirectory));
|
||||
Assertions.assertEquals(List.of("bootstrap"), Files.readAllLines(outputFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should stop executing startup hooks after the first timed out script")
|
||||
void shouldStopAtFirstTimedOutScript() throws IOException {
|
||||
Path bashExecutable = Path.of("/bin/bash");
|
||||
Assumptions.assumeTrue(Files.isExecutable(bashExecutable));
|
||||
|
||||
File hookDirectory = hookScriptsDirectory.toFile();
|
||||
Path outputFile = hookScriptsDirectory.resolve("output.txt");
|
||||
Path absoluteOutputFile = outputFile.toAbsolutePath();
|
||||
String bootstrapScript = """
|
||||
#!/bin/bash
|
||||
echo bootstrap >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
String timeoutScript = """
|
||||
#!/bin/bash
|
||||
sleep 6
|
||||
""";
|
||||
String publishEventsScript = """
|
||||
#!/bin/bash
|
||||
echo publish-events >> "%s"
|
||||
""".formatted(absoluteOutputFile);
|
||||
|
||||
Files.writeString(hookScriptsDirectory.resolve("10-bootstrap.sh"), bootstrapScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("20-seed-fixtures.sh"), timeoutScript);
|
||||
Files.writeString(hookScriptsDirectory.resolve("30-publish-events.sh"), publishEventsScript);
|
||||
|
||||
Assertions.assertThrows(IllegalStateException.class, () -> initializationHooksRunner.run("startup", hookDirectory));
|
||||
|
||||
List<String> expectedLines = List.of("bootstrap");
|
||||
List<String> lines = Files.readAllLines(outputFile);
|
||||
Assertions.assertEquals(expectedLines, lines);
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class InitializationHooksRunnerTest {
|
||||
|
||||
@Mock
|
||||
private HookScriptExecutor hookScriptExecutorMock;
|
||||
|
||||
@InjectMocks
|
||||
private InitializationHooksRunner initializationHooksRunner;
|
||||
|
||||
@Test
|
||||
@DisplayName("Should execute shell scripts in sorted order")
|
||||
void shouldExecuteShellScriptsInSortedOrder() throws IOException, InterruptedException {
|
||||
File hookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(hookDirectory.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookDirectory.exists()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.isDirectory()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.list(ArgumentMatchers.any())).thenReturn(new String[]{"20-third.sh", "10-first.sh", "15-second.sh"});
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
var inOrder = Mockito.inOrder(hookScriptExecutorMock);
|
||||
inOrder.verify(hookScriptExecutorMock).run(hookDirectory, "10-first.sh");
|
||||
inOrder.verify(hookScriptExecutorMock).run(hookDirectory, "15-second.sh");
|
||||
inOrder.verify(hookScriptExecutorMock).run(hookDirectory, "20-third.sh");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should ignore directories without shell scripts")
|
||||
void shouldIgnoreDirectoriesWithoutShellScripts() throws IOException, InterruptedException {
|
||||
File hookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(hookDirectory.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookDirectory.exists()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.isDirectory()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.list(ArgumentMatchers.any())).thenReturn(new String[0]);
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
Mockito.verifyNoInteractions(hookScriptExecutorMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should ignore hook directory when listing scripts returns null")
|
||||
void shouldIgnoreHookDirectoryWhenListingScriptsReturnsNull() throws IOException, InterruptedException {
|
||||
final File hookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(hookDirectory.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookDirectory.exists()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.isDirectory()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.list(ArgumentMatchers.any())).thenReturn(null);
|
||||
|
||||
initializationHooksRunner.run("startup", hookDirectory);
|
||||
|
||||
Mockito.verifyNoInteractions(hookScriptExecutorMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should ignore missing hook directory")
|
||||
void shouldIgnoreMissingHookDirectory() throws IOException, InterruptedException {
|
||||
File missingHookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(missingHookDirectory.getAbsolutePath()).thenReturn("/hooks/missing");
|
||||
Mockito.when(missingHookDirectory.exists()).thenReturn(false);
|
||||
|
||||
initializationHooksRunner.run("startup", missingHookDirectory);
|
||||
|
||||
Mockito.verifyNoInteractions(hookScriptExecutorMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should ignore hook path that is not a directory")
|
||||
void shouldIgnoreHookPathThatIsNotADirectory() throws IOException, InterruptedException {
|
||||
File hookPath = Mockito.mock(File.class);
|
||||
Mockito.when(hookPath.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookPath.exists()).thenReturn(true);
|
||||
Mockito.when(hookPath.isDirectory()).thenReturn(false);
|
||||
|
||||
initializationHooksRunner.run("startup", hookPath);
|
||||
|
||||
Mockito.verifyNoInteractions(hookScriptExecutorMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should stop at first script failure")
|
||||
void shouldStopAtFirstScriptFailure() throws IOException, InterruptedException {
|
||||
File hookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(hookDirectory.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookDirectory.exists()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.isDirectory()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.list(ArgumentMatchers.any())).thenReturn(new String[]{"10-first.sh", "20-failing.sh", "30-never-runs.sh"});
|
||||
|
||||
Mockito.doNothing().when(hookScriptExecutorMock).run(hookDirectory, "10-first.sh");
|
||||
|
||||
IllegalStateException illegalStateException = new IllegalStateException("Hook script failed: 20-failing.sh exited with code 127");
|
||||
Mockito.doThrow(illegalStateException).when(hookScriptExecutorMock).run(hookDirectory, "20-failing.sh");
|
||||
|
||||
IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> initializationHooksRunner.run("startup", hookDirectory));
|
||||
Assertions.assertSame(illegalStateException, exception);
|
||||
|
||||
var inOrder = Mockito.inOrder(hookScriptExecutorMock);
|
||||
inOrder.verify(hookScriptExecutorMock).run(hookDirectory, "10-first.sh");
|
||||
inOrder.verify(hookScriptExecutorMock).run(hookDirectory, "20-failing.sh");
|
||||
inOrder.verifyNoMoreInteractions();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should propagate IOException from hook script executor")
|
||||
void shouldPropagateIOExceptionFromHookScriptExecutor() throws IOException, InterruptedException {
|
||||
File hookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(hookDirectory.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookDirectory.exists()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.isDirectory()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.list(ArgumentMatchers.any())).thenReturn(new String[]{"10-bootstrap.sh"});
|
||||
|
||||
IOException ioException = new IOException("Cannot execute hook script");
|
||||
Mockito.doThrow(ioException).when(hookScriptExecutorMock).run(hookDirectory, "10-bootstrap.sh");
|
||||
|
||||
IOException exception = Assertions.assertThrows(IOException.class, () -> initializationHooksRunner.run("startup", hookDirectory));
|
||||
|
||||
Mockito.verify(hookScriptExecutorMock).run(hookDirectory, "10-bootstrap.sh");
|
||||
Assertions.assertSame(ioException, exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should propagate InterruptedException from hook script executor")
|
||||
void shouldPropagateInterruptedExceptionFromHookScriptExecutor() throws IOException, InterruptedException {
|
||||
File hookDirectory = Mockito.mock(File.class);
|
||||
Mockito.when(hookDirectory.getAbsolutePath()).thenReturn("/hooks/startup");
|
||||
Mockito.when(hookDirectory.exists()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.isDirectory()).thenReturn(true);
|
||||
Mockito.when(hookDirectory.list(ArgumentMatchers.any())).thenReturn(new String[]{"10-bootstrap.sh"});
|
||||
|
||||
InterruptedException interruptedException = new InterruptedException("Hook execution interrupted");
|
||||
Mockito.doThrow(interruptedException).when(hookScriptExecutorMock).run(hookDirectory, "10-bootstrap.sh");
|
||||
|
||||
InterruptedException exception = Assertions.assertThrows(InterruptedException.class, () -> initializationHooksRunner.run("startup", hookDirectory));
|
||||
|
||||
Mockito.verify(hookScriptExecutorMock).run(hookDirectory, "10-bootstrap.sh");
|
||||
Assertions.assertSame(interruptedException, exception);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user