test-task-cron.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/sh
  2. # Cron Task Management Test Script
  3. # Tests DDNS task functionality with cron on Linux systems
  4. # Exits with non-zero status on verification failure
  5. # Usage: test-task-cron.sh [DDNS_COMMAND]
  6. # Examples:
  7. # test-task-cron.sh (uses default: python3 -m ddns)
  8. # test-task-cron.sh ddns (uses ddns command)
  9. # test-task-cron.sh ./dist/ddns (uses binary executable)
  10. # test-task-cron.sh "python -m ddns" (uses custom python command)
  11. set -e # Exit on any error
  12. PYTHON_CMD=${PYTHON_CMD:-python3}
  13. # Check if DDNS command is provided as argument
  14. if [ -z "$1" ]; then
  15. DDNS_CMD="$PYTHON_CMD -m ddns"
  16. else
  17. DDNS_CMD="$1"
  18. fi
  19. echo "=== DDNS Task Management Test for Linux/Cron ==="
  20. echo "DDNS Command: $DDNS_CMD"
  21. echo ""
  22. # Check if cron is available
  23. if ! command -v crontab >/dev/null 2>&1; then
  24. echo "❌ crontab not available - skipping cron tests"
  25. exit 1
  26. fi
  27. # Function to check crontab and validate task existence
  28. check_crontab() {
  29. expected_state=$1 # "exists" or "not_exists"
  30. echo "Checking crontab..."
  31. if crontab -l 2>/dev/null | grep -q "ddns\|DDNS"; then
  32. echo "✅ DDNS crontab entry found"
  33. echo "Crontab entries:"
  34. crontab -l 2>/dev/null | grep -i ddns || true
  35. if [ "$expected_state" = "not_exists" ]; then
  36. echo "❌ VERIFICATION FAILED: Crontab entry should not exist but was found"
  37. return 1
  38. fi
  39. else
  40. echo "ℹ️ No DDNS crontab entry found"
  41. if [ "$expected_state" = "exists" ]; then
  42. echo "❌ VERIFICATION FAILED: Crontab entry should exist but was not found"
  43. return 1
  44. fi
  45. fi
  46. return 0
  47. }
  48. check_ddns_status() {
  49. expected_status=$1 # "Yes" or "No"
  50. echo "Checking DDNS status..."
  51. status_output=$($DDNS_CMD task --status | grep "Installed:" | head -1 || echo "Installed: Unknown")
  52. echo "Status: $status_output"
  53. if echo "$status_output" | grep -q "Installed.*$expected_status"; then
  54. echo "✅ DDNS status verification passed (Expected: $expected_status)"
  55. return 0
  56. else
  57. echo "❌ VERIFICATION FAILED: Expected 'Installed: $expected_status', got '$status_output'"
  58. return 1
  59. fi
  60. }
  61. # Disable systemd if available to force cron usage
  62. if command -v systemctl >/dev/null 2>&1; then
  63. echo "ℹ️ Systemd detected - this test will verify cron fallback behavior"
  64. fi
  65. # Test Step 1: Initial state check
  66. echo "=== Step 1: Initial state verification ==="
  67. $DDNS_CMD task -h
  68. $DDNS_CMD task --status
  69. initial_status=$($DDNS_CMD task --status | grep "Installed:" | head -1 || echo "Installed: Unknown")
  70. echo "Initial status: $initial_status"
  71. # Check initial cron state
  72. echo ""
  73. echo "=== Step 2: Initial cron state verification ==="
  74. check_crontab "not_exists" || exit 1
  75. # Test Step 3: Install task with cron
  76. echo ""
  77. echo "=== Step 3: Installing DDNS task ==="
  78. if echo "$initial_status" | grep -q "Installed.*No"; then
  79. echo "Installing task with 10-minute interval..."
  80. # Set environment to prefer cron over systemd
  81. export DDNS_TASK_PREFER_CRON=1
  82. $DDNS_CMD task --install 10 || {
  83. echo "❌ VERIFICATION FAILED: Task installation failed"
  84. exit 1
  85. }
  86. echo "✅ Task installation command completed"
  87. else
  88. echo "Task already installed, proceeding with verification..."
  89. fi
  90. # Test Step 4: Verify installation
  91. echo ""
  92. echo "=== Step 4: Verifying installation ==="
  93. check_ddns_status "Yes" || exit 1
  94. # Check cron state after installation
  95. echo ""
  96. echo "=== Step 5: Cron verification after installation ==="
  97. check_crontab "exists" || exit 1
  98. # Verify crontab entry format
  99. echo "Verifying crontab entry format..."
  100. cron_entry=$(crontab -l 2>/dev/null | grep -i ddns | head -1)
  101. if [ -n "$cron_entry" ]; then
  102. echo "Cron entry: $cron_entry"
  103. if echo "$cron_entry" | grep -q "\*/10"; then
  104. echo "✅ Cron entry has correct 10-minute interval"
  105. else
  106. echo "⚠️ Warning: Cron entry interval may not match expected 10 minutes"
  107. fi
  108. else
  109. echo "❌ VERIFICATION FAILED: No cron entry found after installation"
  110. exit 1
  111. fi
  112. # Test Step 6: uninstall task
  113. echo ""
  114. echo "=== Step 6: Uninstalling DDNS task ==="
  115. $DDNS_CMD task --uninstall || {
  116. echo "❌ VERIFICATION FAILED: Task uninstallation failed"
  117. exit 1
  118. }
  119. echo "✅ Task uninstallation command completed"
  120. # Test Step 7: Verify deletion
  121. echo ""
  122. echo "=== Step 7: Verifying deletion ==="
  123. check_ddns_status "No" || exit 1
  124. # Final cron state verification
  125. echo ""
  126. echo "=== Step 8: Final cron state verification ==="
  127. check_crontab "not_exists" || exit 1
  128. # Test help commands availability
  129. echo ""
  130. echo "=== Step 9: Help commands verification ==="
  131. if $DDNS_CMD task --help | grep -q "install\|uninstall\|enable\|disable\|status"; then
  132. echo "✅ Task commands found in help"
  133. else
  134. echo "❌ VERIFICATION FAILED: Task commands missing from help"
  135. exit 1
  136. fi
  137. echo ""
  138. echo "🎉 ============================================"
  139. echo "🎉 ALL TESTS PASSED - Cron task management OK"
  140. echo "🎉 ============================================"
  141. echo ""
  142. exit 0