1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
# Incus-related test helpers.
spawn_incus() {
set +x
# INCUS_DIR is local here because since $(inc) is actually a function, it
# overwrites the environment and we would lose INCUS_DIR's value otherwise.
# shellcheck disable=2039,3043
local INCUS_DIR incusdir incus_backend
incusdir=${1}
shift
storage=${1}
shift
# shellcheck disable=SC2153
if [ "$INCUS_BACKEND" = "random" ]; then
incus_backend="$(random_storage_backend)"
else
incus_backend="$INCUS_BACKEND"
fi
if [ "${INCUS_BACKEND}" = "ceph" ] && [ -z "${INCUS_CEPH_CLUSTER:-}" ]; then
echo "A cluster name must be specified when using the CEPH driver." >&2
exit 1
fi
# setup storage
"$incus_backend"_setup "${incusdir}"
echo "$incus_backend" > "${incusdir}/incus.backend"
echo "==> Spawning incusd in ${incusdir}"
# shellcheck disable=SC2086
if [ "${INCUS_NETNS}" = "" ]; then
INCUS_DIR="${incusdir}" incusd --logfile "${incusdir}/incus.log" "${DEBUG-}" "$@" 2>&1 &
else
# shellcheck disable=SC2153
pid="$(cat "${TEST_DIR}/ns/${INCUS_NETNS}/PID")"
INCUS_DIR="${incusdir}" nsenter -n -m -t "${pid}" incusd --logfile "${incusdir}/incus.log" "${DEBUG-}" "$@" 2>&1 &
fi
INCUS_PID=$!
echo "${INCUS_PID}" > "${incusdir}/incus.pid"
# shellcheck disable=SC2153
echo "${incusdir}" >> "${TEST_DIR}/daemons"
echo "==> Spawned Incus (PID is ${INCUS_PID})"
echo "==> Confirming incusd is responsive (PID is ${INCUS_PID})"
INCUS_DIR="${incusdir}" incus admin waitready --timeout=300 || (
echo "Killing PID ${INCUS_PID}"
kill -9 "${INCUS_PID}"
false
)
if [ "${INCUS_NETNS}" = "" ]; then
echo "==> Binding to network"
for _ in $(seq 10); do
addr="127.0.0.1:$(local_tcp_port)"
INCUS_DIR="${incusdir}" incus config set core.https_address "${addr}" || continue
echo "${addr}" > "${incusdir}/incus.addr"
echo "==> Bound to ${addr}"
break
done
fi
if [ -n "${DEBUG:-}" ]; then
set -x
fi
if [ "${INCUS_NETNS}" = "" ]; then
echo "==> Setting up networking"
INCUS_DIR="${incusdir}" incus profile device add default eth0 nic nictype=p2p name=eth0
fi
if [ "${storage}" = true ]; then
echo "==> Configuring storage backend"
"$incus_backend"_configure "${incusdir}"
fi
}
respawn_incus() {
set +x
# INCUS_DIR is local here because since $(inc) is actually a function, it
# overwrites the environment and we would lose INCUS_DIR's value otherwise.
# shellcheck disable=2039,3043
local INCUS_DIR
incusdir=${1}
shift
wait=${1}
shift
echo "==> Spawning incusd in ${incusdir}"
# shellcheck disable=SC2086
if [ "${INCUS_NETNS}" = "" ]; then
INCUS_DIR="${incusdir}" incusd --logfile "${incusdir}/incus.log" "${DEBUG-}" "$@" 2>&1 &
else
pid="$(cat "${TEST_DIR}/ns/${INCUS_NETNS}/PID")"
INCUS_DIR="${incusdir}" nsenter -n -m -t "${pid}" incusd --logfile "${incusdir}/incus.log" "${DEBUG-}" "$@" 2>&1 &
fi
INCUS_PID=$!
echo "${INCUS_PID}" > "${incusdir}/incus.pid"
echo "==> Spawned Incus (PID is ${INCUS_PID})"
if [ "${wait}" = true ]; then
echo "==> Confirming incusd is responsive (PID is ${INCUS_PID})"
INCUS_DIR="${incusdir}" incus admin waitready --timeout=300 || (
echo "Killing PID ${INCUS_PID}"
kill -9 "${INCUS_PID}"
false
)
fi
if [ -n "${DEBUG:-}" ]; then
set -x
fi
}
kill_incus() {
# INCUS_DIR is local here because since $(inc) is actually a function, it
# overwrites the environment and we would lose INCUS_DIR's value otherwise.
# shellcheck disable=2039,3043
local INCUS_DIR daemon_dir daemon_pid check_leftovers incus_backend
daemon_dir=${1}
INCUS_DIR=${daemon_dir}
# Check if already killed
if [ ! -f "${daemon_dir}/incus.pid" ]; then
return
fi
daemon_pid=$(cat "${daemon_dir}/incus.pid")
check_leftovers="false"
incus_backend=$(storage_backend "$daemon_dir")
echo "==> Killing Incus at ${daemon_dir} (${daemon_pid})"
if [ -e "${daemon_dir}/unix.socket" ]; then
# Delete all containers
echo "==> Deleting all containers"
for container in $(timeout -k 2 2 incus list --force-local --format csv --columns n); do
timeout -k 20 20 incus delete "${container}" --force-local -f || true
done
# Delete all images
echo "==> Deleting all images"
for image in $(timeout -k 2 2 incus image list --force-local --format csv --columns f); do
timeout -k 20 20 incus image delete "${image}" --force-local || true
done
# Delete all profiles
echo "==> Deleting all profiles"
for profile in $(timeout -k 2 2 incus profile list --force-local --format csv | cut -d, -f1); do
timeout -k 30 30 incus profile delete "${profile}" --force-local || true
done
# Delete all networks
echo "==> Deleting all managed networks"
for network in $(timeout -k 2 2 incus network list --force-local --format csv | awk -F, '{if ($3 == "YES") {print $1}}'); do
timeout -k 10 10 incus network delete "${network}" --force-local || true
done
# Clear config of the default profile since the profile itself cannot
# be deleted.
echo "==> Clearing config of default profile"
printf 'config: {}\ndevices: {}' | timeout -k 5 5 incus profile edit default
echo "==> Deleting all storage pools"
for storage_pool in $(incus query "/1.0/storage-pools?recursion=1" | jq .[].name -r); do
# Delete the storage volumes.
for volume in $(incus query "/1.0/storage-pools/${storage_pool}/volumes/custom?recursion=1" | jq .[].name -r); do
echo "==> Deleting storage volume ${volume} on ${storage_pool}"
timeout -k 20 20 incus storage volume delete "${storage_pool}" "${volume}" --force-local || true
done
# Delete the storage buckets.
for bucket in $(incus query "/1.0/storage-pools/${storage_pool}/buckets?recursion=1" | jq .[].name -r); do
echo "==> Deleting storage bucket ${bucket} on ${storage_pool}"
timeout -k 20 20 incus storage bucket delete "${storage_pool}" "${bucket}" --force-local || true
done
## Delete the storage pool.
timeout -k 20 20 incus storage delete "${storage_pool}" --force-local || true
done
echo "==> Checking for locked DB tables"
for table in $(echo .tables | sqlite3 "${daemon_dir}/local.db"); do
echo "SELECT * FROM ${table};" | sqlite3 "${daemon_dir}/local.db" > /dev/null
done
# Kill the daemon
timeout -k 30 30 incus admin shutdown || kill -9 "${daemon_pid}" 2> /dev/null || true
sleep 2
# Cleanup shmounts (needed due to the forceful kill)
find "${daemon_dir}" -name shmounts -exec "umount" "-l" "{}" \; > /dev/null 2>&1 || true
find "${daemon_dir}" -name dev_incus -exec "umount" "-l" "{}" \; > /dev/null 2>&1 || true
check_leftovers="true"
fi
if [ -n "${INCUS_LOGS:-}" ]; then
echo "==> Copying the logs"
mkdir -p "${INCUS_LOGS}/${daemon_pid}"
cp -R "${daemon_dir}/logs/" "${INCUS_LOGS}/${daemon_pid}/"
cp "${daemon_dir}/incus.log" "${INCUS_LOGS}/${daemon_pid}/"
fi
if [ "${check_leftovers}" = "true" ]; then
echo "==> Checking for leftover files"
rm -f "${daemon_dir}/containers/lxc-monitord.log"
# Support AppArmor policy cache directory
if apparmor_parser --help | grep -q -- '--print-cache.dir'; then
apparmor_cache_dir="$(apparmor_parser -L "${daemon_dir}"/security/apparmor/cache --print-cache-dir)"
else
apparmor_cache_dir="${daemon_dir}/security/apparmor/cache"
fi
rm -f "${apparmor_cache_dir}/.features"
check_empty "${daemon_dir}/containers/"
check_empty "${daemon_dir}/devices/"
check_empty "${daemon_dir}/images/"
# FIXME: Once container logging rework is done, uncomment
# check_empty "${daemon_dir}/logs/"
check_empty "${apparmor_cache_dir}"
check_empty "${daemon_dir}/security/apparmor/profiles/"
check_empty "${daemon_dir}/security/seccomp/"
check_empty "${daemon_dir}/shmounts/"
check_empty "${daemon_dir}/containers-snapshots/"
echo "==> Checking for leftover DB entries"
check_empty_table "${daemon_dir}/database/global/db.bin" "instances"
check_empty_table "${daemon_dir}/database/global/db.bin" "instances_config"
check_empty_table "${daemon_dir}/database/global/db.bin" "instances_devices"
check_empty_table "${daemon_dir}/database/global/db.bin" "instances_devices_config"
check_empty_table "${daemon_dir}/database/global/db.bin" "instances_profiles"
check_empty_table "${daemon_dir}/database/global/db.bin" "images"
check_empty_table "${daemon_dir}/database/global/db.bin" "images_aliases"
check_empty_table "${daemon_dir}/database/global/db.bin" "images_properties"
check_empty_table "${daemon_dir}/database/global/db.bin" "images_source"
check_empty_table "${daemon_dir}/database/global/db.bin" "images_nodes"
check_empty_table "${daemon_dir}/database/global/db.bin" "networks"
check_empty_table "${daemon_dir}/database/global/db.bin" "networks_config"
check_empty_table "${daemon_dir}/database/global/db.bin" "profiles"
check_empty_table "${daemon_dir}/database/global/db.bin" "profiles_config"
check_empty_table "${daemon_dir}/database/global/db.bin" "profiles_devices"
check_empty_table "${daemon_dir}/database/global/db.bin" "profiles_devices_config"
check_empty_table "${daemon_dir}/database/global/db.bin" "storage_pools"
check_empty_table "${daemon_dir}/database/global/db.bin" "storage_pools_nodes"
check_empty_table "${daemon_dir}/database/global/db.bin" "storage_pools_config"
check_empty_table "${daemon_dir}/database/global/db.bin" "storage_volumes"
check_empty_table "${daemon_dir}/database/global/db.bin" "storage_volumes_config"
fi
# teardown storage
"$incus_backend"_teardown "${daemon_dir}"
# Wipe the daemon directory
wipe "${daemon_dir}"
# Remove the daemon from the list
sed "\\|^${daemon_dir}|d" -i "${TEST_DIR}/daemons"
}
shutdown_incus() {
# INCUS_DIR is local here because since $(inc) is actually a function, it
# overwrites the environment and we would lose INCUS_DIR's value otherwise.
# shellcheck disable=2039,3043
local INCUS_DIR
daemon_dir=${1}
# shellcheck disable=2034
INCUS_DIR=${daemon_dir}
daemon_pid=$(cat "${daemon_dir}/incus.pid")
echo "==> Shutting down Incus at ${daemon_dir} (${daemon_pid})"
# Shutting down the daemon
incus admin shutdown || kill -9 "${daemon_pid}" 2> /dev/null || true
# Wait for any cleanup activity that might be happening right
# after the websocket is closed.
sleep 0.5
}
wait_for() {
# shellcheck disable=SC2039,3043
local addr op
addr=${1}
shift
op=$("$@" | jq -r .operation)
my_curl "https://${addr}${op}/wait"
}
wipe() {
if command -v btrfs > /dev/null 2>&1; then
rm -Rf "${1}" 2> /dev/null || true
if [ -d "${1}" ]; then
find "${1}" | tac | xargs btrfs subvolume delete > /dev/null 2>&1 || true
fi
fi
# shellcheck disable=SC2039,3043
local pid
# shellcheck disable=SC2009
ps aux | grep lxc-monitord | grep "${1}" | awk '{print $2}' | while read -r pid; do
kill -9 "${pid}" || true
done
if mountpoint -q "${1}"; then
umount -l "${1}"
fi
rm -Rf "${1}"
}
# Kill and cleanup Incus instances and related resources
cleanup_incus() {
# shellcheck disable=SC2039,3043
local test_dir daemon_dir
test_dir="$1"
# Kill all Incus instances
while read -r daemon_dir; do
kill_incus "${daemon_dir}"
done < "${test_dir}/daemons"
# Cleanup leftover networks
# shellcheck disable=SC2009
ps aux | grep "interface=inct$$ " | grep -v grep | awk '{print $2}' | while read -r line; do
kill -9 "${line}"
done
if [ -e "/sys/class/net/inct$$" ]; then
ip link del inct$$
fi
# Cleanup clustering networking, if any
teardown_clustering_netns
teardown_clustering_bridge
# Wipe the test environment
wipe "$test_dir"
umount_loops "$test_dir"
}
|