Skip to content

Commit

Permalink
SQUASH with tests/tests utilities
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Feb 27, 2025
1 parent c0f218f commit 42ae29a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 102 deletions.
100 changes: 17 additions & 83 deletions test/includes/lxd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -357,106 +357,54 @@ 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
} > "$logfile" 2>&1 &

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
}
Expand All @@ -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
}
32 changes: 13 additions & 19 deletions test/suites/shutdown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand All @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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=(
Expand Down Expand Up @@ -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=(
Expand Down Expand Up @@ -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=(
Expand Down Expand Up @@ -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=(
Expand Down Expand Up @@ -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=(
Expand Down

0 comments on commit 42ae29a

Please sign in to comment.