test-task-windows.bat 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. @echo off
  2. REM Windows Task Management Test Script
  3. REM Tests DDNS task functionality on Windows systems
  4. REM Exits with non-zero status on verification failure
  5. REM Usage: test-task-windows.bat [DDNS_COMMAND]
  6. REM Examples:
  7. REM test-task-windows.bat (uses default: python3 -m ddns)
  8. REM test-task-windows.bat ddns (uses ddns command)
  9. REM test-task-windows.bat ./dist/ddns.exe (uses binary executable)
  10. REM test-task-windows.bat "python -m ddns" (uses custom python command)
  11. setlocal enabledelayedexpansion
  12. set "PYTHON_CMD=python3"
  13. REM Check if DDNS command is provided as argument
  14. if "%~1"=="" (
  15. set "DDNS_CMD=%PYTHON_CMD% -m ddns"
  16. ) else (
  17. set "DDNS_CMD=%~1"
  18. )
  19. echo === DDNS Task Management Test for Windows ===
  20. echo DDNS Command: %DDNS_CMD%
  21. echo.
  22. REM Check if we're actually on Windows
  23. ver | findstr /i "Windows" >nul
  24. if !ERRORLEVEL! neq 0 (
  25. echo ERROR: This script is designed for Windows
  26. exit /b 1
  27. )
  28. for /f "tokens=*" %%i in ('ver') do echo Confirmed running on %%i
  29. REM Test Step 1: Initial state check
  30. echo.
  31. echo === Step 1: Initial state verification ===
  32. %DDNS_CMD% task -h
  33. if !ERRORLEVEL! neq 0 (
  34. echo ERROR: Task help command failed
  35. exit /b 1
  36. )
  37. %DDNS_CMD% task --status
  38. for /f "tokens=*" %%i in ('%DDNS_CMD% task --status ^| findstr "Installed:"') do set "initial_status=%%i"
  39. if not defined initial_status set "initial_status=Installed: Unknown"
  40. echo Initial status: !initial_status!
  41. REM Check initial system state - should not exist
  42. echo.
  43. echo === Step 2: Initial system state verification ===
  44. echo Checking Windows Task Scheduler...
  45. schtasks /query /tn "DDNS" >nul 2>&1
  46. if !ERRORLEVEL! == 0 (
  47. echo ERROR: DDNS scheduled task should not exist initially but was found
  48. exit /b 1
  49. ) else (
  50. echo OK: No DDNS scheduled task found initially
  51. )
  52. REM Test Step 3: Install task
  53. echo.
  54. echo === Step 3: Installing DDNS task ===
  55. echo !initial_status! | findstr /i "Installed.*No" >nul
  56. if !ERRORLEVEL! == 0 (
  57. echo Installing task with 12-minute interval...
  58. %DDNS_CMD% task --install 12
  59. if !ERRORLEVEL! neq 0 (
  60. echo ERROR: Task installation failed
  61. exit /b 1
  62. )
  63. echo OK: Task installation command completed
  64. ) else (
  65. echo Task already installed, proceeding with verification...
  66. )
  67. REM Test Step 4: Verify installation
  68. echo.
  69. echo === Step 4: Verifying installation ===
  70. for /f "tokens=*" %%i in ('%DDNS_CMD% task --status ^| findstr "Installed:"') do set "install_status=%%i"
  71. echo Status: !install_status!
  72. echo !install_status! | findstr /i "Installed.*Yes" >nul
  73. if !ERRORLEVEL! == 0 (
  74. echo OK: DDNS status verification passed
  75. ) else (
  76. echo ERROR: Expected 'Installed: Yes', got '!install_status!'
  77. exit /b 1
  78. )
  79. REM Check system state after installation
  80. echo.
  81. echo === Step 5: System verification after installation ===
  82. echo Checking Windows Task Scheduler...
  83. schtasks /query /tn "DDNS" >nul 2>&1
  84. if !ERRORLEVEL! == 0 (
  85. echo OK: DDNS scheduled task found
  86. echo Task details:
  87. schtasks /query /tn "DDNS" /fo list 2>nul | findstr /i "TaskName State"
  88. ) else (
  89. echo ERROR: Scheduled task should exist but was not found
  90. exit /b 1
  91. )
  92. REM Test Step 6: Delete task
  93. echo.
  94. echo === Step 6: Deleting DDNS task ===
  95. %DDNS_CMD% task --uninstall
  96. if !ERRORLEVEL! neq 0 (
  97. echo ERROR: Task deletion failed
  98. exit /b 1
  99. )
  100. echo OK: Task deletion command completed
  101. REM Test Step 7: Verify deletion
  102. echo.
  103. echo === Step 7: Verifying deletion ===
  104. for /f "tokens=*" %%i in ('%DDNS_CMD% task --status ^| findstr "Installed:"') do set "final_status=%%i"
  105. echo Status: !final_status!
  106. echo !final_status! | findstr /i "Installed.*No" >nul
  107. if !ERRORLEVEL! == 0 (
  108. echo OK: DDNS status verification passed
  109. ) else (
  110. echo ERROR: Expected 'Installed: No', got '!final_status!'
  111. exit /b 1
  112. )
  113. REM Final system state verification
  114. echo.
  115. echo === Step 8: Final system state verification ===
  116. echo Checking Windows Task Scheduler...
  117. schtasks /query /tn "DDNS" >nul 2>&1
  118. if !ERRORLEVEL! == 0 (
  119. echo ERROR: Scheduled task should not exist but was found
  120. exit /b 1
  121. ) else (
  122. echo OK: DDNS scheduled task successfully removed
  123. )
  124. REM Test help commands availability
  125. echo.
  126. echo === Step 9: Help commands verification ===
  127. %DDNS_CMD% task --help | findstr /i "install uninstall enable disable status" >nul
  128. if !ERRORLEVEL! == 0 (
  129. echo OK: Task commands found in help
  130. ) else (
  131. echo ERROR: Task commands missing from help
  132. exit /b 1
  133. )
  134. echo.
  135. echo ===============================================
  136. echo ALL TESTS PASSED - Windows task management OK
  137. echo ===============================================
  138. echo.
  139. exit /b 0