Files
DDNS/tests/scripts/test-task-windows.bat
6c24557505 feat(cli): add task subcommand for automated scheduled task management (#541)
* Initial plan

* feat(cli): implement task subcommand with cross-platform scheduler support

Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com>

* docs(cli): add task subcommand documentation

Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com>

* fix(cli,task): address review feedback - move task handler to cli, remove defaults, improve fallback status checking

Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com>

* refactor(task): convert TaskManager class to functions and handle task commands entirely in CLI

Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com>

* fix(cli,tests): remove handle_task_command from exports and rename test file

Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com>

* feat(task): add enable and disable commands for scheduled tasks

* add task tests

* refactor(task): simplify task command handling and update status retrieval

* update task

* test(task): update subcommand tests to assert exit codes and rename delete to uninstall

* Add unit tests for DDNS task subcommand functionality and enhance existing tests

- Introduced comprehensive unit tests for the DDNS task subcommand, covering help, status, install, enable, disable, delete, and argument handling.
- Added tests for file I/O operations in the ddns.util.fileio module, ensuring proper directory creation, file reading/writing, and error handling.
- Enhanced test coverage for task scheduling functions, including systemd and cron installations, with additional checks for argument handling.
- Updated existing tests to improve compatibility and reliability across different operating systems, particularly for path handling on Windows.
- Refactored task command building tests to ensure correct command generation with various argument types, including lists and boolean flags.

* refactor(task): improve command execution and path handling for DDNS tasks

* remove DEVNULL

* fix(task): ensure proper task interval handling and add sudo for task commands in test script

* add and fix tests

* fix lint

* refactor(cli): organize DDNS arguments into groups and improve task subcommand handling

* fix tests

* +x

* fix py2 and types

* rm sudo

* fix(tests): use sudo for task installation and uninstallation in test script

* update test command

* always force

* fix format

* docs: add task management parameters and usage examples to CLI documentation

* update log

* docs: update CLI task management instructions and examples

* fix py2

* fix task in python2

* fix lint

* refactor tasks

* update tests

* add schtasks-based task scheduler implementation and unit tests

* Enhance scheduler functionality and improve error handling

- Added a 60-second timeout to subprocess calls in BaseScheduler to prevent hanging.
- Improved error logging in BaseScheduler to capture command failures.
- Refactored CronScheduler to remove unnecessary variable initialization.
- Updated SystemdScheduler to handle PermissionError and provide user feedback for permission issues.
- Enhanced file I/O functions in fileio.py to return default values on exceptions.
- Improved unit tests for scheduler classes to ensure robust testing of functionality and error handling.
- Added lifecycle operation tests for SystemdScheduler to validate install, enable, disable, and uninstall processes.
- Ensured consistent status reporting across multiple calls in SystemdScheduler.
- Cleaned up test code for better readability and maintainability.

* fix lint

* Refactor scheduler command execution and enhance error handling in BaseScheduler and LaunchdScheduler tests

* fix tests

* Add unittest configuration to settings and improve SchtasksScheduler error handling

* Refactor code for improved readability and maintainability in various test files and the DnspodProvider class

* Refactor LaunchdScheduler tests for improved readability and error handling

* fix py2

* fix mac launchd test

* fix lint

* fix macos tests

* fix windows test

* patch readme

* update doc

* Update tests/test_scheduler_cron.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com>
Co-authored-by: New Future <NewFuture@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-08-07 17:58:43 +00:00

159 lines
4.5 KiB
Batchfile
Executable File

@echo off
REM Windows Task Management Test Script
REM Tests DDNS task functionality on Windows systems
REM Exits with non-zero status on verification failure
REM Usage: test-task-windows.bat [DDNS_COMMAND]
REM Examples:
REM test-task-windows.bat (uses default: python3 -m ddns)
REM test-task-windows.bat ddns (uses ddns command)
REM test-task-windows.bat ./dist/ddns.exe (uses binary executable)
REM test-task-windows.bat "python -m ddns" (uses custom python command)
setlocal enabledelayedexpansion
set "PYTHON_CMD=python3"
REM Check if DDNS command is provided as argument
if "%~1"=="" (
set "DDNS_CMD=%PYTHON_CMD% -m ddns"
) else (
set "DDNS_CMD=%~1"
)
echo === DDNS Task Management Test for Windows ===
echo DDNS Command: %DDNS_CMD%
echo.
REM Check if we're actually on Windows
ver | findstr /i "Windows" >nul
if !ERRORLEVEL! neq 0 (
echo ERROR: This script is designed for Windows
exit /b 1
)
for /f "tokens=*" %%i in ('ver') do echo Confirmed running on %%i
REM Test Step 1: Initial state check
echo.
echo === Step 1: Initial state verification ===
%DDNS_CMD% task -h
if !ERRORLEVEL! neq 0 (
echo ERROR: Task help command failed
exit /b 1
)
%DDNS_CMD% task --status
for /f "tokens=*" %%i in ('%DDNS_CMD% task --status ^| findstr "Installed:"') do set "initial_status=%%i"
if not defined initial_status set "initial_status=Installed: Unknown"
echo Initial status: !initial_status!
REM Check initial system state - should not exist
echo.
echo === Step 2: Initial system state verification ===
echo Checking Windows Task Scheduler...
schtasks /query /tn "DDNS" >nul 2>&1
if !ERRORLEVEL! == 0 (
echo ERROR: DDNS scheduled task should not exist initially but was found
exit /b 1
) else (
echo OK: No DDNS scheduled task found initially
)
REM Test Step 3: Install task
echo.
echo === Step 3: Installing DDNS task ===
echo !initial_status! | findstr /i "Installed.*No" >nul
if !ERRORLEVEL! == 0 (
echo Installing task with 12-minute interval...
%DDNS_CMD% task --install 12
if !ERRORLEVEL! neq 0 (
echo ERROR: Task installation failed
exit /b 1
)
echo OK: Task installation command completed
) else (
echo Task already installed, proceeding with verification...
)
REM Test Step 4: Verify installation
echo.
echo === Step 4: Verifying installation ===
for /f "tokens=*" %%i in ('%DDNS_CMD% task --status ^| findstr "Installed:"') do set "install_status=%%i"
echo Status: !install_status!
echo !install_status! | findstr /i "Installed.*Yes" >nul
if !ERRORLEVEL! == 0 (
echo OK: DDNS status verification passed
) else (
echo ERROR: Expected 'Installed: Yes', got '!install_status!'
exit /b 1
)
REM Check system state after installation
echo.
echo === Step 5: System verification after installation ===
echo Checking Windows Task Scheduler...
schtasks /query /tn "DDNS" >nul 2>&1
if !ERRORLEVEL! == 0 (
echo OK: DDNS scheduled task found
echo Task details:
schtasks /query /tn "DDNS" /fo list 2>nul | findstr /i "TaskName State"
) else (
echo ERROR: Scheduled task should exist but was not found
exit /b 1
)
REM Test Step 6: Delete task
echo.
echo === Step 6: Deleting DDNS task ===
%DDNS_CMD% task --uninstall
if !ERRORLEVEL! neq 0 (
echo ERROR: Task deletion failed
exit /b 1
)
echo OK: Task deletion command completed
REM Test Step 7: Verify deletion
echo.
echo === Step 7: Verifying deletion ===
for /f "tokens=*" %%i in ('%DDNS_CMD% task --status ^| findstr "Installed:"') do set "final_status=%%i"
echo Status: !final_status!
echo !final_status! | findstr /i "Installed.*No" >nul
if !ERRORLEVEL! == 0 (
echo OK: DDNS status verification passed
) else (
echo ERROR: Expected 'Installed: No', got '!final_status!'
exit /b 1
)
REM Final system state verification
echo.
echo === Step 8: Final system state verification ===
echo Checking Windows Task Scheduler...
schtasks /query /tn "DDNS" >nul 2>&1
if !ERRORLEVEL! == 0 (
echo ERROR: Scheduled task should not exist but was found
exit /b 1
) else (
echo OK: DDNS scheduled task successfully removed
)
REM Test help commands availability
echo.
echo === Step 9: Help commands verification ===
%DDNS_CMD% task --help | findstr /i "install uninstall enable disable status" >nul
if !ERRORLEVEL! == 0 (
echo OK: Task commands found in help
) else (
echo ERROR: Task commands missing from help
exit /b 1
)
echo.
echo ===============================================
echo ALL TESTS PASSED - Windows task management OK
echo ===============================================
echo.
exit /b 0