#!/usr/bin/env bash # Test: cached files survive SIGKILL and remain readable after restart # # Verifies that read-through cached files persist on disk through a # simulated power loss (SIGKILL). After restarting warpgate, the files # should be readable through the mount with content matching the originals # on the NAS. # # Sequence: # 1. Create 3 test files (100 KB each) on the mock NAS. # 2. Start warpgate, read all files through the mount to cache them. # 3. Verify all files are cached. # 4. simulate_power_loss. # 5. Start a fresh warpgate instance. # 6. Read the cached files again through the mount. # 7. Verify content matches the NAS originals. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "$SCRIPT_DIR/../harness/helpers.sh" source "$SCRIPT_DIR/../harness/mock-nas.sh" require_root setup_test_env trap teardown_test_env EXIT # Start the mock NAS and create 3 test files start_mock_nas nas_create_file "photo-001.dat" 100 nas_create_file "photo-002.dat" 100 nas_create_file "photo-003.dat" 100 # Record checksums of the original NAS files cksum_001=$(nas_file_checksum "photo-001.dat") cksum_002=$(nas_file_checksum "photo-002.dat") cksum_003=$(nas_file_checksum "photo-003.dat") # Generate default config gen_config # Start warpgate and wait for readiness start_warpgate wait_for_mount wait_for_rc_api # Read all files through the FUSE mount to pull them into cache cat "$TEST_MOUNT/photo-001.dat" > /dev/null cat "$TEST_MOUNT/photo-002.dat" > /dev/null cat "$TEST_MOUNT/photo-003.dat" > /dev/null # Verify all files are cached assert_cached "photo-001.dat" assert_cached "photo-002.dat" assert_cached "photo-003.dat" echo "INFO: all 3 files cached successfully" # Simulate power loss simulate_power_loss # Verify cache files persist on disk for f in photo-001.dat photo-002.dat photo-003.dat; do if [[ ! -f "$CACHE_DIR/vfs/nas/$f" ]]; then echo "FAIL: cache file missing after power loss: $f" >&2 exit 1 fi done echo "INFO: all cache files persist on disk after SIGKILL" # Clean up any stale FUSE mount if mountpoint -q "$TEST_MOUNT" 2>/dev/null; then fusermount3 -uz "$TEST_MOUNT" 2>/dev/null || fusermount -uz "$TEST_MOUNT" 2>/dev/null || true fi # Start a fresh warpgate instance start_warpgate wait_for_mount 60 wait_for_rc_api # Read the files again through the mount and verify checksums actual_001=$(md5sum "$TEST_MOUNT/photo-001.dat" | awk '{print $1}') actual_002=$(md5sum "$TEST_MOUNT/photo-002.dat" | awk '{print $1}') actual_003=$(md5sum "$TEST_MOUNT/photo-003.dat" | awk '{print $1}') fail=0 if [[ "$actual_001" != "$cksum_001" ]]; then echo "FAIL: photo-001.dat checksum mismatch after restart" >&2 echo " expected: $cksum_001 actual: $actual_001" >&2 fail=1 fi if [[ "$actual_002" != "$cksum_002" ]]; then echo "FAIL: photo-002.dat checksum mismatch after restart" >&2 echo " expected: $cksum_002 actual: $actual_002" >&2 fail=1 fi if [[ "$actual_003" != "$cksum_003" ]]; then echo "FAIL: photo-003.dat checksum mismatch after restart" >&2 echo " expected: $cksum_003 actual: $actual_003" >&2 fail=1 fi if [[ "$fail" -ne 0 ]]; then exit 1 fi echo "INFO: all 3 files readable with correct content after crash + restart" echo "PASS: $(basename "$0" .sh)"