From 8cc26b741da21447c6f4622b541b9d3e1e1d9743 Mon Sep 17 00:00:00 2001 From: Gabriel Mougard Date: Wed, 26 Feb 2025 17:04:45 +0100 Subject: [PATCH] SQUASH with tests/tests utilities Signed-off-by: Gabriel Mougard --- lxd/daemon.go | 9 ++-- test/includes/lxd.sh | 100 +++++++--------------------------------- test/suites/shutdown.sh | 32 ++++++------- 3 files changed, 33 insertions(+), 108 deletions(-) diff --git a/lxd/daemon.go b/lxd/daemon.go index 83796ad5729d..7cbbeb90d6dc 100644 --- a/lxd/daemon.go +++ b/lxd/daemon.go @@ -2070,12 +2070,9 @@ func (d *Daemon) Stop(ctx context.Context, sig os.Signal) error { // Handle shutdown (unix.SIGPWR) and reload (unix.SIGTERM) signals. if sig == unix.SIGPWR || sig == unix.SIGTERM { - var opsTracker *OperationTracker - if d.db.Cluster != nil { - opsTracker, err = entityToPendingOperations() - if err != nil { - logger.Error("Failed to get entity to pending operations map", logger.Ctx{"err": err}) - } + opsTracker, err := entityToPendingOperations() + if err != nil { + logger.Error("Failed to get entity to pending operations map", logger.Ctx{"err": err}) } // Unmount daemon image and backup volumes if set. diff --git a/test/includes/lxd.sh b/test/includes/lxd.sh index 1e3298bc257e..80219fd08b55 100644 --- a/test/includes/lxd.sh +++ b/test/includes/lxd.sh @@ -357,8 +357,7 @@ cleanup_lxds() { # capture_lxd_logs starts a background process that captures LXD logs. capture_lxd_logs() { local scenario="$1" # The scenario name - local logfile_prefix="lxd_shutdown_log" - local logfile="${logfile_prefix}_${scenario}.log" + local logfile="${scenario}.log" { lxc monitor --pretty @@ -366,97 +365,46 @@ capture_lxd_logs() { local monitor_pid=$! - echo "LXD monitor started in background (PID: $monitor_pid), logging to $logfile." echo "$monitor_pid" - echo "$logfile" } -# lxd_shutdown_restart shutdowns and then restart the LXD daemon. -lxd_shutdown_restart() { - local scenario="$1" # The scenario name. +# lxd_shutdown_restart_with_logs shutdowns and then restart the LXD daemon. +lxd_shutdown_restart_with_logs() { + local scenario LXD_DIR + scenario=${1} + LXD_DIR=${2} - # Start capturing LXD logs in the background - local monitor_pid logfile - IFS=$'\n' read -r monitor_pid logfile < <(capture_lxd_logs "$scenario") - unset IFS + daemon_pid=$(cat "${LXD_DIR}/lxd.pid") + echo "==> Shutting down LXD at ${LXD_DIR} (${daemon_pid})" - if [ -z "$monitor_pid" ] || [ -z "$logfile" ]; then - echo "Error: Failed to start LXD monitor. Check capture_lxd_logs function." + # Start capturing LXD logs in the background + read monitor_pid < <(capture_lxd_logs "$scenario") + if [ -z "$monitor_pid" ]; then + echo "Error: Failed to start LXD monitor" exit 1 fi - echo "Starting LXD shutdown sequence..." + echo "LXD monitor started in background (PID: $monitor_pid)." + echo "Starting LXD shutdown sequence for scenario $scenario ..." sleep 2 - systemctl stop snap.lxd.daemon - local stop_lxd_status=$? - if [ "$stop_lxd_status" -ne 0 ]; then - echo "Failed to stop LXD daemon." - echo "Check logs: $logfile" - sleep 10 - kill "$monitor_pid" - wait "$monitor_pid" - exit 1 - fi + LXD_DIR="${LXD_DIR}" lxd shutdown echo "LXD shutdown sequence completed." - systemctl start snap.lxd.daemon - local start_lxd_status=$? - if [ "$start_lxd_status" -ne 0 ]; then - echo "Failed to start LXD daemon." - echo "Check logs: $logfile" - sleep 10 - kill "$monitor_pid" - wait "$monitor_pid" - exit 1 - fi + respawn_lxd "${LXD_DIR}" true echo "LXD restarted successfully." - sleep 2 - - echo "Stopping LXD monitor (PID: $monitor_pid)..." - kill "$monitor_pid" - wait "$monitor_pid" # Wait for it to terminate. - - echo "LXD monitor stopped. Logs are in $logfile" - sleep 1 } # create_instances creates a specified number of instances in the background. # The instance are called i1, i2, i3, etc. create_instances() { local n="$1" # Number of instances to create. - local NUM_JOBS=8 - local pids=() - local errors=0 for i in $(seq 1 "$n"); do echo "Creating instance i$i..." - lxc launch testimage "i$i" & - pids+=($!) - # Throttle the number of concurrent jobs. - if (( ${#pids[@]} >= NUM_JOBS )); then - wait -n 1 # Wait for *any* one of the background jobs to complete. - job_pid=$(jobs -p | head -n 1) - if [[ -n "$job_pid" ]]; then - unset -v "pids[$(jobs -p | awk -v pid="$job_pid" '$1 == pid {print NR-1}')]" - fi - fi + lxc launch testimage "i$i" done - wait - - for i in $(seq 1 "$n"); do - if ! wait "${pids[$((i - 1))]}"; then # corrected indexing of pid, wait to ensure instance finish and give a return code. - echo "Something went wrong during the launch of instance i$i" - errors=$((errors + 1)) - fi - done - - if [ "$errors" -gt 0 ]; then - echo "One or more instances failed to create." - return 1 - fi - echo "All instances created successfully." return 0 } @@ -465,25 +413,11 @@ create_instances() { # The instances should be called i1, i2, i3, etc. delete_instances() { local n="$1" # Number of instances to delete. - local NUM_JOBS=8 - local pids=() - local errors=0 for i in $(seq 1 "$n"); do echo "Deleting i$i..." lxc delete "i$i" --force & - pids+=($!) - if (( ${#pids[@]} >= NUM_JOBS )); then - wait -n 1 - - job_pid=$(jobs -p | head -n 1) - if [[ -n "$job_pid" ]]; then - unset -v "pids[$(jobs -p | awk -v pid="$job_pid" '$1 == pid {print NR-1}')]" - fi - fi done - wait - return 0 } \ No newline at end of file diff --git a/test/suites/shutdown.sh b/test/suites/shutdown.sh index 97487e35a99c..efc3e576529c 100644 --- a/test/suites/shutdown.sh +++ b/test/suites/shutdown.sh @@ -2,22 +2,15 @@ test_shutdown() { ensure_import_testimage lxd_backend=$(storage_backend "$LXD_DIR") - echo "Scenario 1" + scenario_name="scenario1" + echo "$scenario_name" echo "- LXD shutdown sequence with no instances running." echo "Expected behavior: LXD should shutdown without any issues." echo "----------------------------------------------------------" - echo "Starting LXD shutdown sequence..." - if ! systemctl stop snap.lxd.daemon; then - echo "Failed to stop LXD daemon." - exit 1 - fi - - echo "LXD shutdown sequence completed." - if ! systemctl start snap.lxd.daemon; then - echo "Failed to start LXD daemon." - exit 1 - fi + sleep 5 + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" + sleep 5 echo "LXD restarted started successfully." @@ -34,7 +27,7 @@ test_shutdown() { fi sleep 5 - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 # Check the logs for expected messages that should be shown in the LXD shutdown sequence. @@ -48,6 +41,7 @@ test_shutdown() { ) if ! check_log_presence "$scenario_name.log" "${expected_msgs[@]}"; then echo "Failed to find expected messages in the log file." + cat "$scenario_name.log" # TO REMOVE exit 1 fi @@ -89,7 +83,7 @@ test_shutdown() { sleep 5 # Initiate the LXD shutdown sequence. # This call should block until before the global timeout is reached. - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 # Check the logs for expected messages that should be shown in the LXD shutdown sequence. @@ -176,7 +170,7 @@ test_shutdown() { done sleep 5 - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 expected_msgs=( @@ -281,7 +275,7 @@ test_shutdown() { lxd_volume_operation backups backups_volume 10s & sleep 5 - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 expected_msgs=( @@ -394,7 +388,7 @@ test_shutdown() { lxd_volume_operation images images_volume 10s & sleep 5 - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 expected_msgs=( @@ -511,7 +505,7 @@ test_shutdown() { lxd_volume_operation mypool backups_volume 20s & sleep 5 - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 expected_msgs=( @@ -639,7 +633,7 @@ test_shutdown() { lxd_volume_operation mypool backups_volume 200s & sleep 5 - lxd_shutdown_restart "$scenario_name" + lxd_shutdown_restart_with_logs "$scenario_name" "$LXD_DIR" sleep 5 expected_msgs=(