warpgate/tests/10-scheduled/test-cron-warmup-schedule.sh
grabbit faf9d80824 feat: fill implementation gaps — preset unification, cron, adaptive bw, update cmd, tests
Step 1 — Unify preset logic (eliminate dual implementation)
- src/cli/preset.rs: add missing fields (chunk_limit, multi_thread_streams,
  multi_thread_cutoff), fix Office buffer_size 64M→128M, implement FromStr
- src/web/api.rs: post_preset() now calls Preset::apply() — no more inlined
  params; Office write_back unified to 5s (was 3s in API)

Step 2 — Fix setup.rs connection test: warn→bail
- All 4 "Warning: Could not connect/resolve" prints replaced with anyhow::bail!
  matching deploy/setup.rs behavior

Step 3 — Web UI: add [web] and [notifications] edit sections
- templates/web/tabs/config.html: new collapsible Web UI (password) and
  Notifications (webhook_url, cache_threshold_pct, nas_offline_minutes,
  writeback_depth) sections, both tagged "No restart"
- Also adds [log] section (file path + level select, "Full restart")

Step 4 — Full cron expression support in warmup scheduler
- Cargo.toml: add cron = "0.12", chrono = "0.4"
- supervisor.rs: normalize_cron_schedule() converts 5-field standard cron to
  7-field cron crate format; replaces naive hour-only matching

Step 5 — Adaptive bandwidth algorithm
- supervisor.rs: extract compute_adaptive_limit() pure function; sliding
  window of 6 samples, cv>0.3→congested (−25%, floor 1MiB/s), stable
  near-limit→maintain, under-utilizing→+10% (capped at limit_up)

Step 6 — warpgate update command
- src/cli/update.rs: query GitHub Releases API, compare with CARGO_PKG_VERSION
- src/main.rs: add Update{apply}, SetupWifi, CloneMac{interface} commands
- src/cli/wifi.rs: TODO stub for WiFi AP setup

Unit tests (+35, total 188→223)
- cli/preset.rs: 10 tests — FromStr, all fields for each preset, idempotency,
  connection/share isolation, write_back consistency regression
- supervisor.rs: 14 tests — normalize_cron_schedule (5 cases),
  compute_adaptive_limit (9 cases: congestion, floor, stable, under-utilizing,
  cap, zero-current, zero-max, empty window)
- config.rs: 11 tests — WebConfig (3), NotificationsConfig (4), LogConfig (4)

Shell tests (+4 scripts)
- tests/09-cli/test-preset-cli.sh: preset CLI without daemon; checks all
  three presets write correct values including unified buffer_size/write_back
- tests/09-cli/test-update-command.sh: update command; skips on no-network
- tests/10-scheduled/test-cron-warmup-schedule.sh: "* * * * *" fires in <90s
- tests/10-scheduled/test-adaptive-bandwidth.sh: adaptive loop stability
- tests/harness/config-gen.sh: add warmup.warmup_schedule override support
- tests/run-all.sh: add 10-scheduled category

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-19 16:55:00 +08:00

65 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Test: warmup_schedule triggers warmup at the configured cron time.
#
# Strategy: set warmup_schedule to "* * * * *" (every minute) so the
# supervisor fires at the next 60-second boundary. We also set a short
# dir-cache-time so the mount comes up fast. After the mount is ready we
# wait up to 70 s for a "Scheduled warmup triggered" log line.
#
# Requires: root (for FUSE mounts), a real mock NAS for rclone to connect to.
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
# Seed a file in the mock NAS so warmup has something to do
start_mock_nas
mkdir -p "$NAS_ROOT/warmup-dir"
echo "test content" > "$NAS_ROOT/warmup-dir/file.txt"
# Generate config with:
# - a warmup rule pointing at the seeded directory
# - warmup_schedule = "* * * * *" (every minute — fires within 60 s)
# - warmup.auto = false (we rely on the cron schedule only)
gen_config \
"warmup_auto=false" \
"warmup_schedule=* * * * *" \
"warmup.rules=[[warmup.rules]]\nshare = \"data\"\npath = \"warmup-dir\""
# Start warpgate and wait for the mount to be ready
start_warpgate
wait_for_mount 60
wait_for_rc_api 30
# The cron expression "* * * * *" fires every minute.
# We allow up to 90 s for the trigger log line to appear.
TIMEOUT=90
DEADLINE=$((SECONDS + TIMEOUT))
triggered=0
while [[ $SECONDS -lt $DEADLINE ]]; do
if grep -q "Scheduled warmup triggered" "$TEST_DIR/warpgate.log" 2>/dev/null; then
triggered=1
break
fi
sleep 2
done
if [[ $triggered -eq 0 ]]; then
echo "FAIL: 'Scheduled warmup triggered' not found in log within ${TIMEOUT}s"
echo "--- warpgate.log tail ---"
tail -30 "$TEST_DIR/warpgate.log" 2>/dev/null || true
exit 1
fi
# Verify the schedule string appears in the trigger log line
if ! grep "Scheduled warmup triggered" "$TEST_DIR/warpgate.log" | grep -q "schedule"; then
echo "FAIL: trigger log line should mention the schedule expression"
exit 1
fi
echo "PASS: $(basename "$0" .sh)"