From ef91d9a19579b8dcfea84ca008b536fb5ef77cc5 Mon Sep 17 00:00:00 2001 From: Dmitry Smirnov Date: Wed, 12 Feb 2025 19:35:52 +0200 Subject: [PATCH] fixed scripts --- src/tests/tasks/30_secrets.sh | 4 - src/tests/tasks/40_services.sh | 168 +++++++----------------- src/tests/tasks/50_graceful_shutdown.sh | 85 ++++++------ 3 files changed, 93 insertions(+), 164 deletions(-) diff --git a/src/tests/tasks/30_secrets.sh b/src/tests/tasks/30_secrets.sh index acbbd82..e0deae4 100644 --- a/src/tests/tasks/30_secrets.sh +++ b/src/tests/tasks/30_secrets.sh @@ -54,8 +54,4 @@ if test_verify_secrets; then else log_error "Secrets" "Secrets fetching tests failed" exit 1 -fi -else - echo "Secrets fetching tests failed." - exit 1 fi \ No newline at end of file diff --git a/src/tests/tasks/40_services.sh b/src/tests/tasks/40_services.sh index a308114..fe2a506 100644 --- a/src/tests/tasks/40_services.sh +++ b/src/tests/tasks/40_services.sh @@ -3,165 +3,89 @@ # shellcheck source=/usr/local/lib/utils.sh disable=SC1091 source /usr/local/lib/utils.sh -# Test service management functionality -test_service_management() { - local test_name="Service Management" +# Test service functionality +test_services() { + local test_name="Service Tests" - log_info "$test_name: Test supervisor service management functionality" + log_info "$test_name: Testing service management functionality" - # Test 1: Service Configuration - log_info "Testing service configuration loading..." - if ! supervisorctl status | grep -q "test_service"; then - log_error "$test_name" "test_service not found in supervisor configuration" + # Test 1: Check service configuration + log_info "Testing service configuration..." + if ! worker service config | grep -q "test_service"; then + log_error "$test_name" "test_service not found in configuration" return 1 fi + log_success "$test_name" "Service configuration verified" - # Test 2: Service Start - log_info "Testing service start..." - supervisorctl start test_service + # Test 2: Service lifecycle + log_info "Testing service lifecycle..." + + # Start service + worker service start test_service sleep 2 - if ! supervisorctl status test_service | grep -q "RUNNING"; then - log_error "$test_name" "Failed to start test_service" + if ! worker service status test_service | grep -q "RUNNING"; then + log_error "$test_name" "Failed to start service" return 1 fi log_success "$test_name" "Service started successfully" - # Test 3: Service Status - log_info "Testing service status..." - if ! supervisorctl status test_service | grep -q "RUNNING"; then - log_error "$test_name" "Service status check failed" - return 1 - fi - log_success "$test_name" "Service status check passed" - - # Test 4: Service Stop - log_info "Testing service stop..." - supervisorctl stop test_service - sleep 7 # Allow time for graceful shutdown - if supervisorctl status test_service | grep -q "RUNNING"; then - log_error "$test_name" "Failed to stop test_service" + # Stop service + worker service stop test_service + sleep 5 # Allow time for graceful shutdown + if worker service status test_service | grep -q "RUNNING"; then + log_error "$test_name" "Failed to stop service" return 1 fi log_success "$test_name" "Service stopped successfully" - # Test 5: Service Restart - log_info "Testing service restart..." - supervisorctl start test_service + # Restart service + worker service restart test_service sleep 2 - supervisorctl restart test_service - sleep 7 # Allow time for stop and start - if ! supervisorctl status test_service | grep -q "RUNNING"; then - log_error "$test_name" "Failed to restart test_service" + if ! worker service status test_service | grep -q "RUNNING"; then + log_error "$test_name" "Failed to restart service" return 1 fi log_success "$test_name" "Service restarted successfully" - # Test 6: Check Service Logs - log_info "Testing service logging..." - if ! grep -q "Service running..." /var/log/supervisor/test_service.out.log; then + # Test 3: Service logs + log_info "Testing service logs..." + if ! worker service logs test_service | grep -q "Service running..."; then log_error "$test_name" "Service logs not found or incorrect" return 1 fi - log_success "$test_name" "Service logs verified successfully" + log_success "$test_name" "Service logs verified" - # Test 7: Graceful Shutdown + # Test 4: Graceful shutdown log_info "Testing graceful shutdown..." - supervisorctl stop test_service + worker service stop test_service sleep 1 - # Check for cleanup initiation - if ! grep -q "Starting cleanup..." /var/log/supervisor/test_service.out.log; then - log_error "$test_name" "Service did not initiate cleanup on stop" + # Check logs for cleanup + if ! worker service logs test_service | grep -q "Starting cleanup..."; then + log_error "$test_name" "Service did not initiate cleanup" return 1 fi - log_success "$test_name" "Service cleanup initiated successfully" - - # Wait for cleanup to complete - sleep 6 - if ! grep -q "Cleanup completed" /var/log/supervisor/test_service.out.log; then + sleep 5 + if ! worker service logs test_service | grep -q "Cleanup completed"; then log_error "$test_name" "Service did not complete cleanup" return 1 fi - log_success "$test_name" "Service cleanup completed successfully" + log_success "$test_name" "Service cleanup verified" - # Test 8: Multiple Services - log_info "Testing multiple services..." - # Start both test and app services - supervisorctl start all - sleep 2 - - # Check if both services are running - if ! supervisorctl status | grep -q "RUNNING" | wc -l | grep -q "2"; then - log_error "$test_name" "Failed to run multiple services" - return 1 - fi - log_success "$test_name" "Multiple services running successfully" - - # Stop all services - supervisorctl stop all - sleep 7 - - # Verify all stopped - if supervisorctl status | grep -q "RUNNING"; then - log_error "$test_name" "Failed to stop all services" - return 1 - fi - log_success "$test_name" "All services stopped successfully" - - log_success "$test_name" "All service management tests passed" + log_success "$test_name" "All service tests passed" return 0 } -# Test service configuration -test_service_config() { - local test_name="Service Configuration" - - log_info "$test_name: Test service configuration parsing and validation" - - # Test 1: Check service.yaml parsing - log_info "Testing services.yaml parsing..." - if [ ! -f "/home/udx/services.yaml" ]; then - log_error "$test_name" "services.yaml not found" - return 1 - fi - log_success "$test_name" "services.yaml found and accessible" - - # Test 2: Verify service properties - log_info "Testing service properties..." - local service_conf="/etc/supervisor/conf.d/test_service.conf" - if [ ! -f "$service_conf" ]; then - log_error "$test_name" "Service configuration not generated" - return 1 - fi - log_success "$test_name" "Service configuration file exists" - - # Check required properties - if ! grep -q "program:test_service" "$service_conf"; then - log_error "$test_name" "Invalid service name configuration" - return 1 - fi - log_success "$test_name" "Service name configured correctly" - - if ! grep -q "stopsignal=TERM" "$service_conf"; then - log_error "$test_name" "Invalid stop signal configuration" - return 1 - fi - log_success "$test_name" "Stop signal configured correctly" +# Run tests +main() { + # Remove any existing status file + rm -f /tmp/service_tests_passed - if ! grep -q "stopwaitsecs=10" "$service_conf"; then - log_error "$test_name" "Invalid stop wait configuration" - return 1 - fi - log_success "$test_name" "Stop wait time configured correctly" + # Run tests + test_services || exit 1 - log_success "$test_name" "All service configuration tests passed" - return 0 -} - -# Run all tests -main() { - test_service_config || exit 1 - test_service_management || exit 1 + # Create status file to indicate success + touch /tmp/service_tests_passed } main diff --git a/src/tests/tasks/50_graceful_shutdown.sh b/src/tests/tasks/50_graceful_shutdown.sh index 92bb952..854242b 100644 --- a/src/tests/tasks/50_graceful_shutdown.sh +++ b/src/tests/tasks/50_graceful_shutdown.sh @@ -3,60 +3,69 @@ # shellcheck source=/usr/local/lib/utils.sh disable=SC1091 source /usr/local/lib/utils.sh +# Verify that service tests have passed +verify_service_tests() { + local test_name="Service Tests" + + # Check if service test status file exists and indicates success + if [ ! -f "/tmp/service_tests_passed" ]; then + log_error "$test_name" "Service tests (40_services.sh) must pass before running graceful shutdown tests" + return 1 + fi + log_success "$test_name" "Service tests verified" + return 0 +} + # Test graceful shutdown handling test_graceful_shutdown() { local test_name="Graceful Shutdown" - log_info "$test_name: Test graceful shutdown of supervisor processes" + # First verify service tests have passed + if ! verify_service_tests; then + return 1 + fi - # Start the long-running service defined in services.yaml - supervisorctl start test_service + log_info "$test_name: Test graceful shutdown of services" - # Wait for service to start + # Start the long-running service defined in services.yaml + worker service start test_service sleep 5 # Check if service is running - if ! supervisorctl status test_service | grep -q "RUNNING"; then + if ! worker service status test_service | grep -q "RUNNING"; then log_error "$test_name" "Service failed to start" return 1 fi log_success "$test_name" "Service started successfully" - # Send SIGTERM to the process - if [ -f /var/run/supervisord.pid ]; then - local supervisor_pid=$(cat /var/run/supervisord.pid) - log_info "$test_name: Sending SIGTERM to supervisord (PID: $supervisor_pid)" - kill -TERM "$supervisor_pid" - - # Wait for graceful shutdown (should take about 5 seconds based on our test service) - sleep 7 - - # Check if process has exited gracefully - if ps -p "$supervisor_pid" > /dev/null 2>&1; then - log_error "$test_name" "Process did not exit gracefully within timeout" - return 1 - fi - log_success "$test_name" "Process exited gracefully" - - # Check service logs for proper shutdown sequence - if ! grep -q "Starting cleanup..." /var/log/supervisor/test_service.out.log; then - log_error "$test_name" "Service did not initiate cleanup" - return 1 - fi - log_success "$test_name" "Service cleanup initiated" - - if ! grep -q "Cleanup completed" /var/log/supervisor/test_service.out.log; then - log_error "$test_name" "Service did not complete cleanup" - return 1 - fi - log_success "$test_name" "Service cleanup completed" - - log_success "$test_name" "Service shut down gracefully" - return 0 - else - log_error "$test_name" "Supervisor PID file not found" + # Stop the service + worker service stop test_service + + # Wait for graceful shutdown (should take about 5 seconds based on our test service) + sleep 7 + + # Check if service has stopped + if worker service status test_service | grep -q "RUNNING"; then + log_error "$test_name" "Service did not exit gracefully within timeout" + return 1 + fi + log_success "$test_name" "Service exited gracefully" + + # Check service logs for proper shutdown sequence + if ! worker service logs test_service | grep -q "Starting cleanup..."; then + log_error "$test_name" "Service did not initiate cleanup" return 1 fi + log_success "$test_name" "Service cleanup initiated" + + if ! worker service logs test_service | grep -q "Cleanup completed"; then + log_error "$test_name" "Service did not complete cleanup" + return 1 + fi + log_success "$test_name" "Service cleanup completed" + + log_success "$test_name" "Service shut down gracefully" + return 0 } # Run the test