Skip to content

Commit

Permalink
Setup swapfile in tests/cgroup if needed (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomponline authored Jan 15, 2025
2 parents 36955ff + d4d0c4d commit 0b4d79b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
30 changes: 30 additions & 0 deletions bin/helpers
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,33 @@ certificateFingerprintShort() (
certificateFingerprint "${1}" | head -c12
)

# setup_swap: create a temporary swapfile for lxd-ci usage but only if there is no existing swap.
setup_swap() {
if [ "$(swapon --noheading --raw)" != "" ]; then
# Swap detected, nothing to do
return
fi

SIZE="${1:-1G}"
LXD_CI_SWAPFILE="$(mktemp --tmpdir lxd-ci.swapfile.XXXXXXX)"
fallocate -l "${SIZE}" "${LXD_CI_SWAPFILE}"
chmod 0000 "${LXD_CI_SWAPFILE}"
mkswap "${LXD_CI_SWAPFILE}"
swapon "${LXD_CI_SWAPFILE}"
export LXD_CI_SWAPFILE
}

# teardown_swap: deactivate and remove any swapfile created by lxd-ci
teardown_swap() {
if [ -z "${LXD_CI_SWAPFILE:-}" ]; then
return
fi

swapoff "${LXD_CI_SWAPFILE}"
rm "${LXD_CI_SWAPFILE}"
}


# cleanup: report if the test passed or not and return the appropriate return code.
cleanup() {
set +e
Expand Down Expand Up @@ -393,6 +420,9 @@ cleanup() {
exit 1
fi

# Teardown any swapfile created by lxd-ci
teardown_swap

echo "Test passed"
exit 0
}
Expand Down
3 changes: 3 additions & 0 deletions tests/cgroup
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash
set -eu

# Setup swap if none exist
setup_swap

# Install dependencies
install_deps jq iperf3

Expand Down

0 comments on commit 0b4d79b

Please sign in to comment.