feat(core): make the init hook directory configurable (#5801)

* feat(core): make the init hook directory configurable

Hook directories were fixed at /etc/floci/init/{boot,start,ready,stop,
shutdown}.d, which only the image can provide. floci.init-hooks.dir
(FLOCI_INIT_HOOKS_DIR) now sets that root, defaulting to /etc/floci/init so
the image is unchanged; the LocalStack-compat tree is still read as well.

Hooks also run on Windows, where there is no /bin/sh: .ps1 with
PowerShell, .cmd and .bat with cmd, and .py with python. A .sh hook there
is skipped with a warning unless another shell is configured; on Linux and
macOS the Windows script types are ignored.

Refs #5767

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(core): match Windows hook extensions in any case and warn about a skip once

Review follow-ups for the configurable hook directory:

- Windows file names are case-insensitive, so SETUP.BAT and Init.PS1 are
  now hooks there; elsewhere the extension must stay lower case, as before.
- The warning for a hook Windows cannot run is logged once per file
  instead of on every directory listing, and Windows script types are
  ignored silently on Linux and macOS.
- The environment variable reference and application.yml list shutdown.d
  with the other hook directories.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(core): always resolve hook directories from the configured root

findMergedScripts called getPrimaryPaths() for the default root and
getPrimaryPaths(hooksRoot) otherwise, though both return the same
directories; the no-argument form only survived because five tests stub
it on a mocked hook. The runner now always passes its root, the overload
is gone, and those tests stub getPrimaryPaths(anyString()).

The runner tests this branch added drop @DisplayName, per AGENTS.md.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-16 19:04:48 -04:00
1 parent 43ad214896
commit 050f93e600
11 files changed
+313 -39

No files matched your search

@@ -0,0 +1,50 @@
package io.github.hectorvent.floci.lifecycle.inithook;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
class HookScriptsTest {
@Test
void unixCommandsMatchTheImage() {
assertThat(HookScripts.command("01.sh", false, "/bin/sh"), equalTo(List.of("/bin/sh", "01.sh")));
assertThat(HookScripts.command("01.py", false, "/bin/sh"), equalTo(List.of("python3", "01.py")));
}
@Test
void windowsCommandsUseNativeInterpreters() {
assertThat(HookScripts.command("01.ps1", true, "/bin/sh"),
equalTo(List.of("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "01.ps1")));
assertThat(HookScripts.command("01.bat", true, "/bin/sh"), equalTo(List.of("cmd", "/c", "01.bat")));
assertThat(HookScripts.command("01.py", true, "/bin/sh"), equalTo(List.of("python", "01.py")));
}
@Test
void runnableDependsOnPlatformAndShell() {
assertThat(HookScripts.canRun("01.sh", false, "/bin/sh"), is(true));
assertThat(HookScripts.canRun("01.ps1", false, "/bin/sh"), is(false));
assertThat(HookScripts.canRun("01.sh", true, "/bin/sh"), is(false));
assertThat(HookScripts.canRun("01.sh", true, "bash.exe"), is(true));
assertThat(HookScripts.canRun("notes.txt", true, "/bin/sh"), is(false));
}
@Test
void windowsMatchesExtensionsInAnyCase() {
assertThat(HookScripts.command("SETUP.BAT", true, "/bin/sh"), equalTo(List.of("cmd", "/c", "SETUP.BAT")));
assertThat(HookScripts.canRun("Init.PS1", true, "/bin/sh"), is(true));
assertThat(HookScripts.isScript("SEED.SH", false), is(false));
assertThat(HookScripts.isScript("seed.sh", false), is(true));
}
@Test
void windowsIsDetectedFromOsName() {
assertThat(HookScripts.isWindows("Windows Server 2022"), is(true));
assertThat(HookScripts.isWindows("Mac OS X"), is(false));
assertThat(HookScripts.isWindows(null), is(false));
}
}
@@ -1,14 +1,15 @@
package io.github.hectorvent.floci.lifecycle.inithook;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.lifecycle.InitLifecycleState;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentMatchers;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@@ -30,9 +31,94 @@ class InitializationHooksRunnerTest {
@Mock
private InitLifecycleState initLifecycleStateMock;
@InjectMocks
private InitializationHooksRunner initializationHooksRunner;
@BeforeEach
void setUp() {
initializationHooksRunner = runner(InitializationHook.DEFAULT_ROOT, "Linux");
}
private InitializationHooksRunner runner(String root, String osName) {
return runner(root, osName, "/bin/sh");
}
private InitializationHooksRunner runner(String root, String osName, String shell) {
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.lenient().when(config.initHooks().dir()).thenReturn(root);
Mockito.lenient().when(config.initHooks().shellExecutable()).thenReturn(shell);
return new InitializationHooksRunner(hookScriptExecutorMock, initLifecycleStateMock, config, osName);
}
@Test
void configuredDirReplacesDefaultRoot(@TempDir Path root) throws IOException, InterruptedException {
Files.createDirectories(root.resolve("ready.d"));
Files.createFile(root.resolve("ready.d").resolve("01-seed.sh"));
runner(root.toString(), "Linux").run(InitializationHook.READY);
Mockito.verify(hookScriptExecutorMock).run(root.resolve("ready.d").resolve("01-seed.sh").toFile());
}
@Test
void configuredDirKeepsBothStopDirectories(@TempDir Path root) throws IOException, InterruptedException {
Files.createDirectories(root.resolve("stop.d"));
Files.createDirectories(root.resolve("shutdown.d"));
Files.createFile(root.resolve("stop.d").resolve("01-a.sh"));
Files.createFile(root.resolve("shutdown.d").resolve("02-b.sh"));
runner(root.toString(), "Linux").run(InitializationHook.STOP);
InOrder inOrder = Mockito.inOrder(hookScriptExecutorMock);
inOrder.verify(hookScriptExecutorMock).run(root.resolve("stop.d").resolve("01-a.sh").toFile());
inOrder.verify(hookScriptExecutorMock).run(root.resolve("shutdown.d").resolve("02-b.sh").toFile());
}
@Test
void windowsRunsNativeScriptsAndSkipsShell(@TempDir Path root) throws IOException, InterruptedException {
Path ready = Files.createDirectories(root.resolve("ready.d"));
Files.createFile(ready.resolve("01-seed.ps1"));
Files.createFile(ready.resolve("02-seed.cmd"));
Files.createFile(ready.resolve("03-seed.sh"));
runner(root.toString(), "Windows 11").run(InitializationHook.READY);
Mockito.verify(hookScriptExecutorMock).run(ready.resolve("01-seed.ps1").toFile());
Mockito.verify(hookScriptExecutorMock).run(ready.resolve("02-seed.cmd").toFile());
Mockito.verify(hookScriptExecutorMock, Mockito.never()).run(ready.resolve("03-seed.sh").toFile());
}
@Test
void windowsRunsUppercaseExtensions(@TempDir Path root) throws IOException, InterruptedException {
Path ready = Files.createDirectories(root.resolve("ready.d"));
Files.createFile(ready.resolve("01-SETUP.BAT"));
Files.createFile(ready.resolve("02-Init.PS1"));
runner(root.toString(), "Windows 11").run(InitializationHook.READY);
Mockito.verify(hookScriptExecutorMock).run(ready.resolve("01-SETUP.BAT").toFile());
Mockito.verify(hookScriptExecutorMock).run(ready.resolve("02-Init.PS1").toFile());
}
@Test
void windowsRunsShellScriptsWithAConfiguredShell(@TempDir Path root) throws IOException, InterruptedException {
Path ready = Files.createDirectories(root.resolve("ready.d"));
Files.createFile(ready.resolve("01-seed.sh"));
runner(root.toString(), "Windows 11", "C:/Program Files/Git/bin/bash.exe").run(InitializationHook.READY);
Mockito.verify(hookScriptExecutorMock).run(ready.resolve("01-seed.sh").toFile());
}
@Test
void unixIgnoresWindowsScripts(@TempDir Path root) throws IOException, InterruptedException {
Path ready = Files.createDirectories(root.resolve("ready.d"));
Files.createFile(ready.resolve("01-seed.ps1"));
runner(root.toString(), "Linux").run(InitializationHook.READY);
Mockito.verifyNoInteractions(hookScriptExecutorMock);
}
@Test
@DisplayName("Should execute shell scripts in sorted order")
void shouldExecuteShellScriptsInSortedOrder() throws IOException, InterruptedException {
@@ -169,7 +255,7 @@ class InitializationHooksRunnerTest {
Files.createFile(tempDir.resolve("01-setup.sh"));
InitializationHook hook = inlineMock(InitializationHook.class);
Mockito.when(hook.getName()).thenReturn("startup");
Mockito.when(hook.getPrimaryPaths()).thenReturn(List.of(tempDir.toFile()));
Mockito.when(hook.getPrimaryPaths(Mockito.anyString())).thenReturn(List.of(tempDir.toFile()));
Mockito.when(hook.getCompatPaths()).thenReturn(List.of());
Assertions.assertTrue(initializationHooksRunner.hasHooks(hook));
@@ -181,7 +267,7 @@ class InitializationHooksRunnerTest {
Files.createFile(tempDir.resolve("01-setup.sh"));
InitializationHook hook = inlineMock(InitializationHook.class);
Mockito.when(hook.getName()).thenReturn("startup");
Mockito.when(hook.getPrimaryPaths()).thenReturn(List.of());
Mockito.when(hook.getPrimaryPaths(Mockito.anyString())).thenReturn(List.of());
Mockito.when(hook.getCompatPaths()).thenReturn(List.of(tempDir.toFile()));
Assertions.assertTrue(initializationHooksRunner.hasHooks(hook));
@@ -192,7 +278,7 @@ class InitializationHooksRunnerTest {
void hasHooksShouldReturnFalseWhenDirectoryIsEmpty(@TempDir Path tempDir) {
InitializationHook hook = inlineMock(InitializationHook.class);
Mockito.when(hook.getName()).thenReturn("startup");
Mockito.when(hook.getPrimaryPaths()).thenReturn(List.of(tempDir.toFile()));
Mockito.when(hook.getPrimaryPaths(Mockito.anyString())).thenReturn(List.of(tempDir.toFile()));
Mockito.when(hook.getCompatPaths()).thenReturn(List.of());
Assertions.assertFalse(initializationHooksRunner.hasHooks(hook));
@@ -203,7 +289,7 @@ class InitializationHooksRunnerTest {
void hasHooksShouldReturnFalseWhenDirectoryDoesNotExist() {
InitializationHook hook = inlineMock(InitializationHook.class);
Mockito.when(hook.getName()).thenReturn("startup");
Mockito.when(hook.getPrimaryPaths()).thenReturn(List.of(new File("/nonexistent/path")));
Mockito.when(hook.getPrimaryPaths(Mockito.anyString())).thenReturn(List.of(new File("/nonexistent/path")));
Mockito.when(hook.getCompatPaths()).thenReturn(List.of());
Assertions.assertFalse(initializationHooksRunner.hasHooks(hook));
@@ -219,7 +305,7 @@ class InitializationHooksRunnerTest {
InitializationHook hook = inlineMock(InitializationHook.class);
Mockito.when(hook.getName()).thenReturn("ready");
Mockito.when(hook.getPrimaryPaths()).thenReturn(List.of(primaryDir.toFile()));
Mockito.when(hook.getPrimaryPaths(Mockito.anyString())).thenReturn(List.of(primaryDir.toFile()));
Mockito.when(hook.getCompatPaths()).thenReturn(List.of(compatDir.toFile()));
initializationHooksRunner.run(hook);