two-images/test.sh
GnomeZworc 344363d49c
All checks were successful
Tests / check (push) Successful in 38s
two-images: rocky10, profils NM de cloud-init nommés par interface #1
cloud-init 24.4 nomme le profil NetworkManager d'après la clé du
network-config (eth0) et le cherche d'après l'interface (ens3) : la
2e interface n'est jamais activée, et au redémarrage le profil de
repli porte le même UUID, nmcli up échoue (degraded done).

Correctif dans l'image : fichier nommé par interface, contrôlé par
check.sh. test.sh sert un network-config au format de two (2 NIC
décrites, 1 sans entrée) et vérifie cloud-init après redémarrage.

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-11-06 00:32:01 +01:00

187 lines
6.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
DIST="${TWO_IMAGES_DIST:-${ROOT}/dist}"
PORT="${TWO_IMAGES_SSH_PORT:-2229}"
RUN=""
QEMU_PID=""
FAILED=0
KEEP=0
MAC0=52:54:00:00:00:01
MAC1=52:54:00:00:00:02
MAC2=52:54:00:00:00:03
die () { KEEP=1; echo "!! ${1}" >&2; exit 1; }
stop_qemu () {
local rc=0
kill "${QEMU_PID}"
wait "${QEMU_PID}" || rc=$?
[ "${rc}" -eq 0 ] || [ "${rc}" -eq 143 ] || echo "!! QEMU sorti avec le code ${rc}" >&2
QEMU_PID=""
}
cleanup () {
if [ -n "${QEMU_PID}" ] && kill -0 "${QEMU_PID}"; then
stop_qemu
fi
[ -n "${RUN}" ] || return 0
rm -f "${RUN:?}/key" "${RUN:?}/key.pub" "${RUN:?}/root.qcow2" "${RUN:?}/data.qcow2" \
"${RUN:?}/seed/meta-data" "${RUN:?}/seed/user-data" "${RUN:?}/seed/vendor-data" \
"${RUN:?}/seed/network-config"
rmdir "${RUN}/seed"
if [ "${KEEP}" -eq 1 ]; then
echo "!! console conservée : ${RUN}/console.log" >&2
return 0
fi
rm -f "${RUN:?}/console.log"
rmdir "${RUN}"
}
vm_closing () {
local rc=0
vm "$@" || rc=$?
[ "${rc}" -eq 0 ] || [ "${rc}" -eq 255 ] || die "commande « $* » en échec (code ${rc})"
}
boot_id () {
vm 'cat /proc/sys/kernel/random/boot_id'
}
wait_reboot () {
local before="${1}" id=""
for _ in $(seq 1 120); do
if id=$(boot_id 2>/dev/null) && [ -n "${id}" ] && [ "${id}" != "${before}" ]; then
return 0
fi
sleep 10
done
die "pas de redémarrage constaté"
}
wait_exit () {
for _ in $(seq 1 60); do
kill -0 "${QEMU_PID}" 2>/dev/null || { wait "${QEMU_PID}"; QEMU_PID=""; return 0; }
sleep 5
done
die "la VM ne s'arrête pas"
}
check () {
local desc="${1}"
shift
if "$@"; then
echo "RÉUSSI: ${desc}"
else
echo "ÉCHOUÉ: ${desc}"
FAILED=1
fi
}
accel () {
case "$(uname -s)" in
Linux) echo "-accel kvm -cpu host" ;;
*) echo "-accel tcg,thread=multi -cpu max" ;;
esac
}
vm () {
ssh -i "${RUN}/key" -p "${PORT}" -o IdentitiesOnly=yes -o IdentityAgent=none -o AddKeysToAgent=no \
-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o LogLevel=ERROR -o ConnectTimeout=5 -o BatchMode=yes -n syonad@127.0.0.1 "$@"
}
wait_ssh () {
for _ in $(seq 1 120); do
vm true 2>/dev/null && return 0
sleep 10
done
die "SSH injoignable (console : ${RUN}/console.log)"
}
boot () {
local -a accel_args
read -r -a accel_args <<< "$(accel)"
qemu-system-x86_64 "${accel_args[@]}" -machine pc -m 1536 -smp 2 -nographic \
-serial "file:${RUN}/console.log" -monitor none \
-drive "file=${RUN}/root.qcow2,format=qcow2,if=none,id=root" \
-device virtio-blk-pci,drive=root,bootindex=0 \
-device virtio-scsi-pci,id=scsi0 \
-drive "file=${RUN}/data.qcow2,format=qcow2,if=none,id=data" \
-device scsi-hd,drive=data,bus=scsi0.0,serial=xvdb \
-netdev "user,id=n0,net=169.254.169.0/24,hostfwd=tcp:127.0.0.1:${PORT}-:22,guestfwd=tcp:169.254.169.254:80-cmd:python3 -I ${ROOT}/metadata.py ${RUN}/seed" \
-device "virtio-net-pci,netdev=n0,mac=${MAC0}" \
-netdev user,id=n1,net=10.0.3.0/24 -device "virtio-net-pci,netdev=n1,mac=${MAC1}" \
-netdev user,id=n2,net=10.0.4.0/24 -device "virtio-net-pci,netdev=n2,mac=${MAC2}" &
QEMU_PID=$!
}
three_leases () {
[ "$(vm "ip -4 -o addr show | awk '\$2 ~ /^en/ { print \$2 }' | sort -u | wc -l")" -ge 3 ]
}
nic_without_config_by_two () {
local nic
nic=$(vm "ip -br link show | awk '\$3 == \"${MAC2}\" { print \$1 }'")
[ -n "${nic}" ] || return 1
if vm 'command -v nmcli' >/dev/null; then
[ "$(vm "nmcli -g GENERAL.CONNECTION device show ${nic}")" = two-all-en ]
else
vm "networkctl status ${nic}" | grep -q 'Network File: /etc/systemd/network/90-all-en.network'
fi
}
cloud_init_done () {
vm 'cloud-init status --wait' | grep -q 'status: done'
}
no_ntp_source () {
local sources
sources=$(vm 'chronyc -n -c sources') || return 1
[ -z "${sources}" ]
}
image="${1:-}"
if [ -z "${image}" ] || [ ! -f "${DIST}/${image}.qcow2" ]; then
die "usage : ${0} <image> (fichier ${DIST}/<image>.qcow2)"
fi
RUN=$(mktemp -d "${TMPDIR:-/tmp}/two-images-test.XXXXXX")
trap cleanup EXIT
mkdir "${RUN}/seed"
ssh-keygen -q -t ed25519 -N '' -C two-images-test -f "${RUN}/key"
printf 'instance-id: two-images-test\nlocal-hostname: two-images-test\n' > "${RUN}/seed/meta-data"
printf '#cloud-config\nusers:\n - name: syonad\n shell: /bin/bash\n sudo: "ALL=(ALL) NOPASSWD:ALL"\n ssh_authorized_keys:\n - %s\n' \
"$(cat "${RUN}/key.pub")" > "${RUN}/seed/user-data"
: > "${RUN}/seed/vendor-data"
printf 'version: 2\nethernets:\n eth0:\n match:\n macaddress: "%s"\n dhcp4: true\n eth1:\n match:\n macaddress: "%s"\n dhcp4: true\n' \
"${MAC0}" "${MAC1}" > "${RUN}/seed/network-config"
qemu-img create -q -f qcow2 -b "${DIST}/${image}.qcow2" -F qcow2 "${RUN}/root.qcow2"
qemu-img create -q -f qcow2 "${RUN}/data.qcow2" 1G
boot
wait_ssh
check "cloud-init terminé sans erreur" cloud_init_done
check "nom d'hôte servi par 169.254.169.254" test "$(vm hostname)" = two-images-test
check "bail DHCP sur les trois interfaces" three_leases
check "interface absente du network-config configurée par le fichier de two" nic_without_config_by_two
check "disque additionnel en /dev/xvdb" vm 'test -b /dev/xvdb'
check "aucune clé de construction" vm '! sudo grep -rq two-images-build /home /root /etc/ssh'
if vm 'command -v getenforce' >/dev/null; then
check "SELinux désactivé" test "$(vm getenforce)" = Disabled
fi
if vm 'command -v chronyc' >/dev/null; then
check "aucune source NTP hors DHCP (QEMU n'en annonce pas)" no_ntp_source
fi
BOOT=$(boot_id)
vm_closing 'sudo systemctl reboot'
wait_reboot "${BOOT}"
check "cloud-init terminé sans erreur après redémarrage" cloud_init_done
check "bail DHCP sur les trois interfaces après redémarrage" three_leases
check "disque additionnel en /dev/xvdb après redémarrage" vm 'test -b /dev/xvdb'
vm_closing 'sudo systemctl poweroff'
wait_exit
[ "${FAILED}" -eq 0 ] || die "${image} : test en échec"
echo "== ${image} : test OK"