warpgate/tests/08-crash-recovery/test-cache-corruption.sh
grabbit a2d49137f9 Add comprehensive test suite: 63 integration tests + 110 Rust unit tests
Integration tests (tests/):
- 9 categories covering config, lifecycle, signals, supervision,
  cache, writeback, network faults, crash recovery, and CLI
- Shell-based harness with mock NAS (network namespace + SFTP),
  fault injection (tc netem), and power loss simulation
- TAP format runner (run-all.sh) with proper SKIP detection

Rust unit tests (warpgate/src/):
- 110 tests across 14 modules, all passing in 0.01s
- Config parsing, defaults validation, RestartTracker logic,
  RC API response parsing, rclone arg generation, service
  config generation, CLI output formatting, warmup path logic

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-18 11:21:35 +08:00

87 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Test: behavior when a cached file is corrupted on disk
#
# Verifies what happens when a file in the VFS cache is corrupted between
# warpgate restarts. rclone may serve the corrupted data from cache or
# re-fetch from the remote — this test documents the observed behavior.
#
# Sequence:
# 1. Create a test file on the NAS with known content.
# 2. Start warpgate, read file through mount to cache it.
# 3. Stop warpgate gracefully.
# 4. Overwrite the cached file with garbage data.
# 5. Restart warpgate with a short dir_cache_time (1s).
# 6. Wait for dir cache to expire, then read through mount.
# 7. Compare result against expected content and document behavior.
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 a test file
start_mock_nas
nas_create_file_content "corrupt-test.txt" "original"
# Generate config with short dir_cache_time so rclone rechecks the remote
gen_config dir_cache_time=1s
# Start warpgate and wait for readiness
start_warpgate
wait_for_mount
wait_for_rc_api
# Read file through the FUSE mount to pull it into cache
content_before=$(cat "$TEST_MOUNT/corrupt-test.txt")
if [[ "$content_before" != "original" ]]; then
echo "FAIL: initial read returned unexpected content: $content_before" >&2
exit 1
fi
assert_cached "corrupt-test.txt"
echo "INFO: file cached successfully, content: '$content_before'"
# Stop warpgate gracefully
stop_warpgate
# Corrupt the cached file by overwriting with garbage
cache_file="$CACHE_DIR/vfs/nas/corrupt-test.txt"
if [[ ! -f "$cache_file" ]]; then
echo "FAIL: cache file not found at $cache_file" >&2
exit 1
fi
echo -n "CORRUPTED-GARBAGE-DATA" > "$cache_file"
echo "INFO: corrupted cache file at $cache_file"
# Restart warpgate
start_warpgate
wait_for_mount 60
wait_for_rc_api
# Wait for the dir cache to expire (1s dir_cache_time + margin)
sleep 3
# Read the file through the mount again
content_after=$(cat "$TEST_MOUNT/corrupt-test.txt")
echo "INFO: content after restart with corrupted cache: '$content_after'"
# The corrupted cache file has a different size than the NAS original
# ("CORRUPTED-GARBAGE-DATA" = 22 bytes vs "original" = 8 bytes).
# rclone VFS should detect the size mismatch and re-fetch from the remote.
# We MUST verify the file matches the NAS original — serving corrupted data
# is a data integrity failure.
nas_original=$(nas_read_file "corrupt-test.txt")
if [[ "$content_after" != "$nas_original" ]]; then
echo "FAIL: content after restart does not match NAS original" >&2
echo " NAS original: '$nas_original'" >&2
echo " mount returned: '$content_after'" >&2
exit 1
fi
echo "INFO: rclone correctly re-fetched file from remote (corruption detected)"
echo "PASS: $(basename "$0" .sh)"