From f51fbb2828e500e459e44d64d62270069b26e5a4 Mon Sep 17 00:00:00 2001 From: Dmitry Smirnov Date: Wed, 12 Feb 2025 19:47:19 +0200 Subject: [PATCH] fix tests --- lib/auth.sh | 3 +-- lib/cleanup.sh | 2 +- lib/secrets.sh | 2 +- lib/utils.sh | 11 +++++++++-- lib/worker_config.sh | 4 ++-- src/tests/tasks/10_dependencies.sh | 2 +- src/tests/tasks/40_services.sh | 20 ++++++++++---------- src/tests/tasks/50_graceful_shutdown.sh | 12 ++++++------ 8 files changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/auth.sh b/lib/auth.sh index d76ef66..1ebc851 100644 --- a/lib/auth.sh +++ b/lib/auth.sh @@ -39,7 +39,7 @@ authenticate_actors() { if [[ -z "$creds" ]]; then continue else - log_info "Detected credentials for $type" + log_success "Authentication" "Detected credentials for $type" fi # Explicitly check for JSON format @@ -121,7 +121,6 @@ authenticate_provider() { # Set an environment variable to mark successful authorization export "${provider^^}_AUTHORIZED=true" - log_info "Authorization successful for provider $provider." return 0 } diff --git a/lib/cleanup.sh b/lib/cleanup.sh index 037fa0d..9a71693 100644 --- a/lib/cleanup.sh +++ b/lib/cleanup.sh @@ -51,7 +51,7 @@ cleanup_provider() { return 1 fi else - log_info "$name authentication cleaned up successfully." + log_success "Cleanup" "$name authentication cleaned up successfully." cleaned_up=true fi diff --git a/lib/secrets.sh b/lib/secrets.sh index f73a7e3..04153cb 100644 --- a/lib/secrets.sh +++ b/lib/secrets.sh @@ -94,7 +94,7 @@ fetch_secrets() { # Export the secret as an environment variable if [[ -n "$value" ]]; then echo "export $name=\"$value\"" >> "$secrets_env_file" - log_success "Resolved secret for $name from $provider." + log_success "Secrets" "Resolved secret for $name from $provider." else log_error "Secrets" "Failed to resolve secret for $name from $provider." fi diff --git a/lib/utils.sh b/lib/utils.sh index 21e6fa2..afa2aa0 100755 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -7,11 +7,18 @@ resolve_env_vars() { } # Logging functions with direct ANSI sequences +# log_info() { +# if [ $# -eq 1 ]; then +# printf "\033[0;34m[INFO] %s\033[0m\n" "$1" >&2 +# else +# printf "\033[0;34m[INFO] %s: %s\033[0m\n" "$1" "$2" >&2 +# fi +# } log_info() { if [ $# -eq 1 ]; then - printf "\033[0;34m[INFO] %s\033[0m\n" "$1" >&2 + printf "[INFO] %s\n" "$1" >&2 else - printf "\033[0;34m[INFO] %s: %s\033[0m\n" "$1" "$2" >&2 + printf "[INFO] %s: %s\n" "$1" "$2" >&2 fi } diff --git a/lib/worker_config.sh b/lib/worker_config.sh index fba3792..fdfed67 100644 --- a/lib/worker_config.sh +++ b/lib/worker_config.sh @@ -37,7 +37,7 @@ merge_worker_configs() { # If a user-provided configuration exists (and path is not empty), merge it if [[ -f "$USER_CONFIG" && -n "$USER_CONFIG" ]]; then - log_info "User configuration detected at $USER_CONFIG" + log_success "Worker configuration" "User configuration detected at $USER_CONFIG" if ! yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' "$BUILT_IN_CONFIG" "$USER_CONFIG" > "$MERGED_CONFIG"; then log_error "Worker configuration" "Failed to merge configurations. yq returned an error." @@ -79,7 +79,7 @@ export_variables_from_config() { log_info "No variables found in the configuration." return 0 else - log_info "Found variables in the configuration. Exporting..." + log_success "Worker configuration" "Found variables in the configuration. Exporting..." fi # Iterate over variables and export them into the main shell diff --git a/src/tests/tasks/10_dependencies.sh b/src/tests/tasks/10_dependencies.sh index ca2b357..3bcc99d 100755 --- a/src/tests/tasks/10_dependencies.sh +++ b/src/tests/tasks/10_dependencies.sh @@ -10,7 +10,7 @@ check_command() { local cmd_path cmd_path=$(command -v "$1") if [ -x "$cmd_path" ]; then - log_info "$1 is installed at $cmd_path" + log_success "$1" "$1 is installed at $cmd_path" local version version=$($1 --version 2>&1 | head -n 1) log_success "$1" "Version: $version" diff --git a/src/tests/tasks/40_services.sh b/src/tests/tasks/40_services.sh index fe2a506..c0f6c70 100644 --- a/src/tests/tasks/40_services.sh +++ b/src/tests/tasks/40_services.sh @@ -11,7 +11,7 @@ test_services() { # Test 1: Check service configuration log_info "Testing service configuration..." - if ! worker service config | grep -q "test_service"; then + if ! worker service config 2>/dev/null | grep -q "test_service"; then log_error "$test_name" "test_service not found in configuration" return 1 fi @@ -21,27 +21,27 @@ test_services() { log_info "Testing service lifecycle..." # Start service - worker service start test_service + worker service start test_service > /dev/null sleep 2 - if ! worker service status test_service | grep -q "RUNNING"; then + if ! worker service status test_service 2>/dev/null | grep -q "RUNNING"; then log_error "$test_name" "Failed to start service" return 1 fi log_success "$test_name" "Service started successfully" # Stop service - worker service stop test_service + worker service stop test_service > /dev/null sleep 5 # Allow time for graceful shutdown - if worker service status test_service | grep -q "RUNNING"; then + if worker service status test_service 2>/dev/null | grep -q "RUNNING"; then log_error "$test_name" "Failed to stop service" return 1 fi log_success "$test_name" "Service stopped successfully" # Restart service - worker service restart test_service + worker service restart test_service > /dev/null sleep 2 - if ! worker service status test_service | grep -q "RUNNING"; then + if ! worker service status test_service 2>/dev/null | grep -q "RUNNING"; then log_error "$test_name" "Failed to restart service" return 1 fi @@ -57,16 +57,16 @@ test_services() { # Test 4: Graceful shutdown log_info "Testing graceful shutdown..." - worker service stop test_service + worker service stop test_service > /dev/null sleep 1 # Check logs for cleanup - if ! worker service logs test_service | grep -q "Starting cleanup..."; then + if ! worker service logs test_service 2>/dev/null | grep -q "Starting cleanup..."; then log_error "$test_name" "Service did not initiate cleanup" return 1 fi sleep 5 - if ! worker service logs test_service | grep -q "Cleanup completed"; then + if ! worker service logs test_service 2>/dev/null | grep -q "Cleanup completed"; then log_error "$test_name" "Service did not complete cleanup" return 1 fi diff --git a/src/tests/tasks/50_graceful_shutdown.sh b/src/tests/tasks/50_graceful_shutdown.sh index 854242b..efa69b4 100644 --- a/src/tests/tasks/50_graceful_shutdown.sh +++ b/src/tests/tasks/50_graceful_shutdown.sh @@ -28,37 +28,37 @@ test_graceful_shutdown() { log_info "$test_name: Test graceful shutdown of services" # Start the long-running service defined in services.yaml - worker service start test_service + worker service start test_service > /dev/null sleep 5 # Check if service is running - if ! worker service status test_service | grep -q "RUNNING"; then + if ! worker service status test_service 2>/dev/null | grep -q "RUNNING"; then log_error "$test_name" "Service failed to start" return 1 fi log_success "$test_name" "Service started successfully" # Stop the service - worker service stop test_service + worker service stop test_service > /dev/null # 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 + if worker service status test_service 2>/dev/null | 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 + if ! worker service logs test_service 2>/dev/null | 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 + if ! worker service logs test_service 2>/dev/null | grep -q "Cleanup completed"; then log_error "$test_name" "Service did not complete cleanup" return 1 fi