test-task-systemd.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash
  2. # Systemd Task Management Test Script
  3. # Tests DDNS task functionality with systemd on Linux systems
  4. # Exits with non-zero status on verification failure
  5. # Usage: test-task-systemd.sh [DDNS_COMMAND]
  6. # Examples:
  7. # test-task-systemd.sh (uses default: python3 -m ddns)
  8. # test-task-systemd.sh ddns (uses ddns command)
  9. # test-task-systemd.sh ./dist/ddns (uses binary executable)
  10. # test-task-systemd.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/Systemd ==="
  20. echo "DDNS Command: $DDNS_CMD"
  21. echo ""
  22. # Check if systemd is available
  23. if ! command -v systemctl >/dev/null 2>&1; then
  24. echo "❌ systemctl not available - skipping systemd tests"
  25. exit 1
  26. fi
  27. # Function to check systemd timer and validate task existence
  28. check_systemd_timer() {
  29. local expected_state=$1 # "exists" or "not_exists"
  30. echo "Checking systemd timer..."
  31. if systemctl list-timers --all 2>/dev/null | grep -q "ddns"; then
  32. echo "✅ DDNS systemd timer found"
  33. systemctl status ddns.timer 2>/dev/null | head -5 || true
  34. if [[ "$expected_state" == "not_exists" ]]; then
  35. echo "❌ VERIFICATION FAILED: Timer should not exist but was found"
  36. return 1
  37. fi
  38. else
  39. echo "ℹ️ No DDNS systemd timer found"
  40. if [[ "$expected_state" == "exists" ]]; then
  41. echo "❌ VERIFICATION FAILED: Timer should exist but was not found"
  42. return 1
  43. fi
  44. fi
  45. return 0
  46. }
  47. check_ddns_status() {
  48. local expected_status=$1 # "Yes" or "No"
  49. local status_output
  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. # Test Step 1: Initial state check
  62. echo "=== Step 1: Initial state verification ==="
  63. $DDNS_CMD task -h
  64. $DDNS_CMD task --status
  65. initial_status=$($DDNS_CMD task --status | grep "Installed:" | head -1 || echo "Installed: Unknown")
  66. echo "Initial status: $initial_status"
  67. # Check initial system state
  68. echo ""
  69. echo "=== Step 2: Initial systemd state verification ==="
  70. check_systemd_timer "not_exists" || exit 1
  71. # Test Step 3: Install task
  72. echo ""
  73. echo "=== Step 3: Installing DDNS task ==="
  74. if echo "$initial_status" | grep -q "Installed.*No"; then
  75. echo "Installing task with 10-minute interval..."
  76. sudo $DDNS_CMD task --install 10 || {
  77. echo "❌ VERIFICATION FAILED: Task installation failed"
  78. exit 1
  79. }
  80. echo "✅ Task installation command completed"
  81. else
  82. echo "Task already installed, proceeding with verification..."
  83. fi
  84. # Test Step 4: Verify installation
  85. echo ""
  86. echo "=== Step 4: Verifying installation ==="
  87. check_ddns_status "Yes" || exit 1
  88. # Check systemd state after installation
  89. echo ""
  90. echo "=== Step 5: Systemd verification after installation ==="
  91. check_systemd_timer "exists" || exit 1
  92. # Verify systemd service files exist
  93. echo "Checking systemd service files..."
  94. if systemctl cat ddns.service >/dev/null 2>&1; then
  95. echo "✅ DDNS systemd service found"
  96. else
  97. echo "❌ VERIFICATION FAILED: DDNS systemd service not found"
  98. exit 1
  99. fi
  100. if systemctl cat ddns.timer >/dev/null 2>&1; then
  101. echo "✅ DDNS systemd timer found"
  102. else
  103. echo "❌ VERIFICATION FAILED: DDNS systemd timer not found"
  104. exit 1
  105. fi
  106. # Test Step 6: Delete task
  107. echo ""
  108. echo "=== Step 6: Deleting DDNS task ==="
  109. sudo $DDNS_CMD task --uninstall || {
  110. echo "❌ VERIFICATION FAILED: Task deletion failed"
  111. exit 1
  112. }
  113. echo "✅ Task deletion command completed"
  114. # Test Step 7: Verify deletion
  115. echo ""
  116. echo "=== Step 7: Verifying deletion ==="
  117. check_ddns_status "No" || exit 1
  118. # Final systemd state verification
  119. echo ""
  120. echo "=== Step 8: Final systemd state verification ==="
  121. check_systemd_timer "not_exists" || exit 1
  122. # Verify systemd service files are removed
  123. echo "Checking systemd service files removal..."
  124. if systemctl cat ddns.service >/dev/null 2>&1; then
  125. echo "❌ VERIFICATION FAILED: DDNS systemd service still exists"
  126. exit 1
  127. else
  128. echo "✅ DDNS systemd service properly removed"
  129. fi
  130. if systemctl cat ddns.timer >/dev/null 2>&1; then
  131. echo "❌ VERIFICATION FAILED: DDNS systemd timer still exists"
  132. exit 1
  133. else
  134. echo "✅ DDNS systemd timer properly removed"
  135. fi
  136. # Test help commands availability
  137. echo ""
  138. echo "=== Step 9: Help commands verification ==="
  139. if $DDNS_CMD task --help | grep -q "install\|uninstall\|enable\|disable\|status"; then
  140. echo "✅ Task commands found in help"
  141. else
  142. echo "❌ VERIFICATION FAILED: Task commands missing from help"
  143. exit 1
  144. fi
  145. echo ""
  146. echo "🎉 ================================================="
  147. echo "🎉 ALL TESTS PASSED - Systemd task management OK"
  148. echo "🎉 ================================================="
  149. echo ""
  150. exit 0