test-task-macos.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/bin/bash
  2. # macOS Task Management Test Script
  3. # Tests DDNS task functionality on macOS systems
  4. # Exits with non-zero status on verification failure
  5. # Usage: test-task-macos.sh [DDNS_COMMAND]
  6. # Examples:
  7. # test-task-macos.sh (uses default: python3 -m ddns)
  8. # test-task-macos.sh ddns (uses ddns command)
  9. # test-task-macos.sh ./dist/ddns (uses binary executable)
  10. # test-task-macos.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 macOS ==="
  20. echo "DDNS Command: $DDNS_CMD"
  21. echo ""
  22. # Function to check launchd services
  23. check_launchd_service() {
  24. local expected_state=$1 # "exists" or "not_exists"
  25. if command -v launchctl >/dev/null 2>&1; then
  26. echo "Checking launchd services..."
  27. if launchctl list 2>/dev/null | grep -q "ddns"; then
  28. echo "✅ DDNS launchd service found"
  29. echo "Service details:"
  30. launchctl list | grep ddns || true
  31. if [[ "$expected_state" == "not_exists" ]]; then
  32. echo "❌ VERIFICATION FAILED: launchd service should not exist but was found"
  33. return 1
  34. fi
  35. else
  36. echo "ℹ️ No DDNS launchd service found"
  37. if [[ "$expected_state" == "exists" ]]; then
  38. echo "❌ VERIFICATION FAILED: launchd service should exist but was not found"
  39. return 1
  40. fi
  41. fi
  42. else
  43. echo "❌ VERIFICATION FAILED: launchctl not available on macOS"
  44. return 1
  45. fi
  46. return 0
  47. }
  48. # Function to check LaunchAgents directory
  49. check_launch_agents() {
  50. local expected_state=$1 # "exists" or "not_exists"
  51. local user_agents_dir="$HOME/Library/LaunchAgents"
  52. local ddns_plist="$user_agents_dir/cc.newfuture.ddns.plist"
  53. echo "Checking LaunchAgents directory..."
  54. if [[ -f "$ddns_plist" ]]; then
  55. echo "✅ DDNS plist file found: $ddns_plist"
  56. echo "Plist content preview:"
  57. head -10 "$ddns_plist" 2>/dev/null || true
  58. if [[ "$expected_state" == "not_exists" ]]; then
  59. echo "❌ VERIFICATION FAILED: plist file should not exist but was found"
  60. return 1
  61. fi
  62. else
  63. echo "ℹ️ No DDNS plist file found in $user_agents_dir"
  64. if [[ "$expected_state" == "exists" ]]; then
  65. echo "❌ VERIFICATION FAILED: plist file should exist but was not found"
  66. return 1
  67. fi
  68. fi
  69. return 0
  70. }
  71. # Function to check crontab (fallback on macOS)
  72. check_crontab() {
  73. local expected_state=$1 # "exists" or "not_exists"
  74. if command -v crontab >/dev/null 2>&1; then
  75. echo "Checking crontab (fallback scheduling)..."
  76. if crontab -l 2>/dev/null | grep -q "ddns\|DDNS"; then
  77. echo "✅ DDNS crontab entry found"
  78. echo "Crontab entries:"
  79. crontab -l 2>/dev/null | grep -i ddns || true
  80. if [[ "$expected_state" == "not_exists" ]]; then
  81. echo "❌ VERIFICATION FAILED: Crontab entry should not exist but was found"
  82. return 1
  83. fi
  84. else
  85. echo "ℹ️ No DDNS crontab entry found"
  86. if [[ "$expected_state" == "exists" ]]; then
  87. echo "❌ VERIFICATION FAILED: Crontab entry should exist but was not found"
  88. return 1
  89. fi
  90. fi
  91. else
  92. echo "⚠️ crontab not available"
  93. fi
  94. return 0
  95. }
  96. check_ddns_status() {
  97. local expected_status=$1 # "Yes" or "No"
  98. local status_output
  99. echo "Checking DDNS status..."
  100. status_output=$($DDNS_CMD task --status | grep "Installed:" | head -1 || echo "Installed: Unknown")
  101. echo "Status: $status_output"
  102. if echo "$status_output" | grep -q "Installed.*$expected_status"; then
  103. echo "✅ DDNS status verification passed (Expected: $expected_status)"
  104. return 0
  105. else
  106. echo "❌ VERIFICATION FAILED: Expected 'Installed: $expected_status', got '$status_output'"
  107. return 1
  108. fi
  109. }
  110. # Check if we're actually on macOS
  111. if [[ "$(uname)" != "Darwin" ]]; then
  112. echo "❌ VERIFICATION FAILED: This script is designed for macOS (Darwin), detected: $(uname)"
  113. exit 1
  114. fi
  115. echo "✅ Confirmed running on macOS ($(sw_vers -productName) $(sw_vers -productVersion))"
  116. # Test Step 1: Initial state check
  117. echo ""
  118. echo "=== Step 1: Initial state verification ==="
  119. $DDNS_CMD task -h
  120. $DDNS_CMD task --status
  121. initial_status=$($DDNS_CMD task --status | grep "Installed:" | head -1 || echo "Installed: Unknown")
  122. echo "Initial status: $initial_status"
  123. # Check initial system state
  124. echo ""
  125. echo "=== Step 2: Initial system state verification ==="
  126. check_launchd_service "not_exists" || exit 1
  127. check_launch_agents "not_exists" || exit 1
  128. check_crontab "not_exists" || exit 1
  129. # Test Step 3: Install task
  130. echo ""
  131. echo "=== Step 3: Installing DDNS task ==="
  132. if echo "$initial_status" | grep -q "Installed.*No"; then
  133. echo "Installing task with 15-minute interval..."
  134. $DDNS_CMD task --install 15 || {
  135. echo "❌ VERIFICATION FAILED: Task installation failed"
  136. exit 1
  137. }
  138. echo "✅ Task installation command completed"
  139. else
  140. echo "Task already installed, proceeding with verification..."
  141. fi
  142. # Test Step 4: Verify installation
  143. echo ""
  144. echo "=== Step 4: Verifying installation ==="
  145. check_ddns_status "Yes" || exit 1
  146. # Check system state after installation
  147. echo ""
  148. echo "=== Step 5: System verification after installation ==="
  149. check_launchd_service "exists" || {
  150. echo "ℹ️ Warning: launchd service not found (may use cron instead)"
  151. }
  152. check_launch_agents "exists" || {
  153. echo "ℹ️ Warning: LaunchAgent plist not found (may use cron instead)"
  154. }
  155. check_crontab "exists" || {
  156. echo "ℹ️ Warning: crontab entry not found (may use launchd instead)"
  157. }
  158. # Verify at least one scheduling method is active
  159. if ! (launchctl list 2>/dev/null | grep -q "ddns" || crontab -l 2>/dev/null | grep -q "ddns\|DDNS"); then
  160. echo "❌ VERIFICATION FAILED: No scheduling system (launchd or cron) has DDNS task"
  161. exit 1
  162. fi
  163. echo "✅ At least one scheduling system has DDNS task"
  164. # Test Step 6: uninstall task
  165. echo ""
  166. echo "=== Step 6: Uninstalling DDNS task ==="
  167. $DDNS_CMD task --uninstall || {
  168. echo "❌ VERIFICATION FAILED: Task uninstallation failed"
  169. exit 1
  170. }
  171. echo "✅ Task uninstallation command completed"
  172. # Test Step 7: Verify uninstallation
  173. echo ""
  174. echo "=== Step 7: Verifying uninstallation ==="
  175. check_ddns_status "No" || exit 1
  176. # Final system state verification
  177. echo ""
  178. echo "=== Step 8: Final system state verification ==="
  179. check_launchd_service "not_exists" || exit 1
  180. check_launch_agents "not_exists" || exit 1
  181. check_crontab "not_exists" || exit 1
  182. # Test help commands availability
  183. echo ""
  184. echo "=== Step 9: Help commands verification ==="
  185. if $DDNS_CMD task --help | grep -q "install\|uninstall\|enable\|disable\|status"; then
  186. echo "✅ Task commands found in help"
  187. else
  188. echo "❌ VERIFICATION FAILED: Task commands missing from help"
  189. exit 1
  190. fi
  191. echo ""
  192. echo "🍎 ============================================="
  193. echo "🍎 ALL TESTS PASSED - macOS task management OK"
  194. echo "🍎 ============================================="
  195. echo ""
  196. exit 0