|
| 1 | +{ |
| 2 | + pkgs, |
| 3 | + devshell, |
| 4 | + runTest, |
| 5 | +}: |
| 6 | +let |
| 7 | + hookBeforeStart = pkgs.writeShellScript "hook-before-start" '' |
| 8 | + echo "BEFORE_START_EXECUTED" > "$PRJ_DATA_DIR/hook_before_start.log" |
| 9 | + ''; |
| 10 | + hookAfterStart = pkgs.writeShellScript "hook-after-start" '' |
| 11 | + echo "AFTER_START_EXECUTED" > "$PRJ_DATA_DIR/hook_after_start.log" |
| 12 | + ''; |
| 13 | + hookBeforeStop = pkgs.writeShellScript "hook-before-stop" '' |
| 14 | + echo "BEFORE_STOP_EXECUTED" > "$PRJ_DATA_DIR/hook_before_stop.log" |
| 15 | + ''; |
| 16 | + hookAfterStop = pkgs.writeShellScript "hook-after-stop" '' |
| 17 | + echo "AFTER_STOP_EXECUTED" > "$PRJ_DATA_DIR/hook_after_stop.log" |
| 18 | + ''; |
| 19 | + dummyService = pkgs.writeShellScript "dummy-service" '' |
| 20 | + while true; do sleep 1; done |
| 21 | + ''; |
| 22 | + |
| 23 | + shell = devshell.mkShell { |
| 24 | + devshell.name = "service-groups-hooks-test"; |
| 25 | + serviceGroups.test = { |
| 26 | + description = "Test service group"; |
| 27 | + beforeStart = "${hookBeforeStart}"; |
| 28 | + afterStart = "${hookAfterStart}"; |
| 29 | + beforeStop = "${hookBeforeStop}"; |
| 30 | + afterStop = "${hookAfterStop}"; |
| 31 | + services.dummy = { command = "${dummyService}"; }; |
| 32 | + }; |
| 33 | + }; |
| 34 | +in |
| 35 | +{ |
| 36 | + service-groups-hooks-test = runTest "service-groups-hooks" { } '' |
| 37 | + # Load the devshell |
| 38 | + source ${shell}/env.bash |
| 39 | +
|
| 40 | + # Start the service group |
| 41 | + test:start & |
| 42 | + START_PID=$! |
| 43 | +
|
| 44 | + # Give it a moment to start |
| 45 | + sleep 2 |
| 46 | +
|
| 47 | + # Check that beforeStart hook was executed |
| 48 | + if [ ! -f "$PRJ_DATA_DIR/hook_before_start.log" ]; then |
| 49 | + echo "ERROR: beforeStart hook was not executed" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +
|
| 53 | + if [ "$(cat "$PRJ_DATA_DIR/hook_before_start.log")" != "BEFORE_START_EXECUTED" ]; then |
| 54 | + echo "ERROR: beforeStart hook did not execute correctly" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +
|
| 58 | + # Check that afterStart hook was executed |
| 59 | + if [ ! -f "$PRJ_DATA_DIR/hook_after_start.log" ]; then |
| 60 | + echo "ERROR: afterStart hook was not executed" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +
|
| 64 | + if [ "$(cat "$PRJ_DATA_DIR/hook_after_start.log")" != "AFTER_START_EXECUTED" ]; then |
| 65 | + echo "ERROR: afterStart hook did not execute correctly" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +
|
| 69 | + # Stop the service group |
| 70 | + test:stop |
| 71 | +
|
| 72 | + # Give it a moment to stop |
| 73 | + sleep 2 |
| 74 | +
|
| 75 | + # Check that beforeStop hook was executed |
| 76 | + if [ ! -f "$PRJ_DATA_DIR/hook_before_stop.log" ]; then |
| 77 | + echo "ERROR: beforeStop hook was not executed" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +
|
| 81 | + if [ "$(cat "$PRJ_DATA_DIR/hook_before_stop.log")" != "BEFORE_STOP_EXECUTED" ]; then |
| 82 | + echo "ERROR: beforeStop hook did not execute correctly" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | +
|
| 86 | + # Check that afterStop hook was executed |
| 87 | + if [ ! -f "$PRJ_DATA_DIR/hook_after_stop.log" ]; then |
| 88 | + echo "ERROR: afterStop hook was not executed" |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | +
|
| 92 | + if [ "$(cat "$PRJ_DATA_DIR/hook_after_stop.log")" != "AFTER_STOP_EXECUTED" ]; then |
| 93 | + echo "ERROR: afterStop hook did not execute correctly" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | +
|
| 97 | + # Clean up |
| 98 | + kill $START_PID 2>/dev/null || true |
| 99 | + wait $START_PID 2>/dev/null || true |
| 100 | + ''; |
| 101 | +} |
0 commit comments