235 lines
13 KiB
Bash
Executable file
235 lines
13 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PASS=0
|
|
FAIL=0
|
|
|
|
fail () { echo "FAIL: ${CURRENT}: ${1}"; FAIL=$(( FAIL + 1 )); CASE_OK=0; }
|
|
|
|
setup () {
|
|
CURRENT="${1}"
|
|
CASE_OK=1
|
|
WORK=$(mktemp -d "${TMPDIR:-/tmp}/scenario.XXXXXX")
|
|
mkdir -p "${WORK}/scenarios"
|
|
cat > "${WORK}/lab-host" <<'FAKE'
|
|
#!/usr/bin/env bash
|
|
N=$(ls "${FAKE_DIR}" | grep -c '^payload\.')
|
|
cat > "${FAKE_DIR}/payload.$(( N + 1 ))"
|
|
printf '%s\n' "$*" >> "${FAKE_DIR}/calls.log"
|
|
if ! bash -n "${FAKE_DIR}/payload.$(( N + 1 ))" 2>"${FAKE_DIR}/syntax.$(( N + 1 ))"; then
|
|
echo "ÉCHOUÉ: syntaxe du bloc $(( N + 1 ))"
|
|
exit 0
|
|
fi
|
|
[ -n "${FAKE_OUT:-}" ] && printf '%b\n' "${FAKE_OUT}"
|
|
exit 0
|
|
FAKE
|
|
chmod +x "${WORK}/lab-host"
|
|
: > "${WORK}/calls.log"
|
|
}
|
|
|
|
run () {
|
|
env LAB_HOST="${WORK}/lab-host" FAKE_DIR="${WORK}" SCENARIO_DIR="${SCENARIO_DIR:-${WORK}/scenarios}" \
|
|
FAKE_OUT="${FAKE_OUT:-}" bash "${DIR}/run.sh" "$@" > "${WORK}/out.log" 2>&1
|
|
}
|
|
|
|
teardown () {
|
|
[[ "${CASE_OK}" -eq 1 ]] && PASS=$(( PASS + 1 )) && echo "ok: ${CURRENT}"
|
|
[[ "${CASE_OK}" -eq 1 ]] || { echo " sortie :"; sed 's/^/ | /' "${WORK}/out.log"; }
|
|
rm -rf "${WORK}"
|
|
}
|
|
|
|
node () {
|
|
bash -c ". '${DIR}/lib/node.sh'; ${1}" 2>&1
|
|
}
|
|
|
|
test_on_sends_assignments_library_and_block_to_the_node () {
|
|
setup "on : variables, bibliothèque et bloc envoyés au bon nœud"
|
|
printf '%s\n' "on hv2 KEY='ssh-ed25519 AAAA x' NAME=vm <<'NODE'" 'echo "${NAME}"' 'NODE' > "${WORK}/scenarios/t1.sh"
|
|
FAKE_OUT='RÉUSSI: un' run t1 || fail "code de sortie $?"
|
|
[[ $(cat "${WORK}/calls.log") == "ssh ./labctl ssh hv2 'sudo bash -s'" ]] || fail "appel : $(cat "${WORK}/calls.log")"
|
|
grep -q '^check () {' "${WORK}/payload.1" || fail "node.sh absent du bloc"
|
|
[[ $(tail -n 1 "${WORK}/payload.1") == 'echo "${NAME}"' ]] || fail "bloc du scénario absent en fin d'envoi"
|
|
[[ $(bash -c "$(grep '^export ' "${WORK}/payload.1"); printf '%s|%s' \"\${KEY}\" \"\${NAME}\"") == "ssh-ed25519 AAAA x|vm" ]] \
|
|
|| fail "variables mal transmises : $(grep '^export ' "${WORK}/payload.1")"
|
|
teardown
|
|
}
|
|
|
|
test_scenario_result_counts_and_exit_codes () {
|
|
local CASE
|
|
for CASE in ok fail none; do
|
|
setup "résultat d'un scénario (${CASE})"
|
|
printf '%s\n' "on hv1 <<'NODE'" 'true' 'NODE' > "${WORK}/scenarios/t2.sh"
|
|
case "${CASE}" in
|
|
ok) FAKE_OUT='RÉUSSI: a\nRÉUSSI: b' run t2; [[ $? -eq 0 ]] || fail "code non nul"
|
|
grep -q '=== t2 : 2 réussi(s), 0 échoué(s)' "${WORK}/out.log" || fail "bilan absent" ;;
|
|
fail) FAKE_OUT='RÉUSSI: a\nÉCHOUÉ: b — détail' run t2; [[ $? -eq 1 ]] || fail "code différent de 1"
|
|
grep -q '=== t2 : 1 réussi(s), 1 échoué(s)' "${WORK}/out.log" || fail "bilan absent" ;;
|
|
none) FAKE_OUT='INFO: rien' run t2; [[ $? -eq 1 ]] || fail "un scénario sans vérification a réussi" ;;
|
|
esac
|
|
teardown
|
|
done
|
|
}
|
|
|
|
test_unknown_scenario_and_no_argument () {
|
|
setup "scénario inconnu ou absent : usage, code 2"
|
|
run nope; [[ $? -eq 2 ]] || fail "scénario inconnu : code $?"
|
|
run; [[ $? -eq 2 ]] || fail "sans argument : code $?"
|
|
[[ -s "${WORK}/calls.log" ]] && fail "un nœud a été appelé"
|
|
teardown
|
|
}
|
|
|
|
test_all_runs_every_scenario_and_fails_if_one_fails () {
|
|
setup "all : chaque scénario, échec si l'un échoue"
|
|
printf '%s\n' "on hv1 <<'NODE'" 'true' 'NODE' > "${WORK}/scenarios/a.sh"
|
|
printf '%s\n' "on hv1 <<'NODE'" 'true' 'NODE' > "${WORK}/scenarios/b.sh"
|
|
FAKE_OUT='ÉCHOUÉ: x' run all; [[ $? -eq 1 ]] || fail "code différent de 1"
|
|
[[ $(grep -c '^=== [ab] :' "${WORK}/out.log") -eq 2 ]] || fail "les deux scénarios n'ont pas tourné"
|
|
teardown
|
|
}
|
|
|
|
test_every_shipped_block_is_valid_bash () {
|
|
local f
|
|
setup "chaque bloc envoyé par les scénarios livrés est du bash valide"
|
|
SCENARIO_DIR="${DIR}/scenarios" FAKE_OUT='RÉUSSI: x' run all
|
|
for f in "${WORK}"/syntax.*; do
|
|
[[ -s "${f}" ]] && fail "${f##*/} : $(cat "${f}")"
|
|
done
|
|
[[ $(ls "${WORK}" | grep -c '^payload\.') -ge 10 ]] || fail "trop peu de blocs envoyés : $(ls "${WORK}" | grep -c '^payload\.')"
|
|
grep -q 'ÉCHOUÉ: syntaxe' "${WORK}/out.log" && fail "bloc invalide"
|
|
teardown
|
|
}
|
|
|
|
test_check_reports_success_and_failure_with_reason () {
|
|
setup "check : RÉUSSI, ou ÉCHOUÉ avec la dernière ligne de sortie"
|
|
[[ $(node 'check "a" true') == "RÉUSSI: a" ]] || fail "succès : $(node 'check "a" true')"
|
|
[[ $(node 'check "b" sh -c "echo un; echo deux >&2; exit 3"') == "ÉCHOUÉ: b — deux" ]] || fail "échec : $(node 'check "b" sh -c "echo un; echo deux >&2; exit 3"')"
|
|
[[ $(node 'check "c" false') == "ÉCHOUÉ: c" ]] || fail "échec muet : $(node 'check "c" false')"
|
|
teardown
|
|
}
|
|
|
|
test_vm_fails_only_when_ssh_worked_and_command_failed () {
|
|
setup "vm_fails : réussit seulement si SSH marche et la commande échoue"
|
|
[[ $(node 'vm_ssh () { echo rc=1; }; vm_fails v 1.2.3.4 x && echo OUI') == "OUI" ]] || fail "commande échouée non reconnue"
|
|
[[ $(node 'vm_ssh () { echo rc=0; }; vm_fails v 1.2.3.4 x || echo NON') == *NON ]] || fail "commande réussie prise pour un échec"
|
|
[[ $(node 'vm_ssh () { return 255; }; vm_fails v 1.2.3.4 x || echo NON') == *NON ]] || fail "SSH en panne pris pour une isolation"
|
|
[[ $(node 'vm_ssh () { echo bizarre; }; vm_fails v 1.2.3.4 x || echo NON') == *NON ]] || fail "sortie inattendue acceptée"
|
|
teardown
|
|
}
|
|
|
|
test_route_via_matches_the_next_hop_exactly () {
|
|
setup "route_via : le next-hop exact, pas un préfixe"
|
|
[[ $(node 'vm_ssh () { echo "default via 10.220.1.1 dev ens3 proto dhcp"; }; route_via v ip default 10.220.1.1 && echo OUI') == OUI ]] || fail "next-hop exact refusé"
|
|
[[ $(node 'vm_ssh () { echo "default via 10.220.1.10 dev ens3"; }; route_via v ip default 10.220.1.1 || echo NON') == NON ]] || fail "10.220.1.10 pris pour 10.220.1.1"
|
|
teardown
|
|
}
|
|
|
|
test_wait_state () {
|
|
setup "wait_state : état atteint, erreur, suppression"
|
|
[[ $(node 'api_state () { echo running; }; wait_state vms/x running && echo OUI') == OUI ]] || fail "running"
|
|
local start=${SECONDS}
|
|
[[ $(node 'api_state () { echo error; }; wait_state vms/x running 2>&1 || echo NON') == *NON ]] || fail "error accepté"
|
|
(( SECONDS - start < 5 )) || fail "une ressource en error n'arrête pas l'attente immédiatement ($(( SECONDS - start )) s)"
|
|
[[ $(node 'api_state () { echo; }; wait_state vms/x deleted && echo OUI') == OUI ]] || fail "absence = supprimé"
|
|
teardown
|
|
}
|
|
|
|
test_vrf_route_via_matches_the_next_hop_exactly () {
|
|
setup "vrf_route_via : next-hop exact dans la bonne VRF"
|
|
local stub='ip () { [ "$*" = "-4 route show vrf vrf-s7 10.241.1.10" ] && echo "10.241.1.10 nhid 12 via 192.168.14.11 dev brl3-s7 proto bgp metric 20 onlink"; }; '
|
|
[[ $(node "${stub}vrf_route_via vp-s7 10.241.1.10 192.168.14.11 && echo OUI") == OUI ]] || fail "route présente refusée"
|
|
[[ $(node "${stub}vrf_route_via vp-s7 10.241.1.10 192.168.14.1 || echo NON") == NON ]] || fail "192.168.14.1 pris pour 192.168.14.11"
|
|
[[ $(node "${stub}vrf_route_via vp-s7b 10.241.1.10 192.168.14.11 || echo NON") == NON ]] || fail "mauvaise VRF acceptée"
|
|
teardown
|
|
}
|
|
|
|
test_vrf_members_lists_interface_names () {
|
|
setup "vrf_members : noms triés, sans le suffixe @ du veth"
|
|
[[ $(node 'ip () { printf "%s\n" "15: l3-s7-o@if14: <UP> mtu 1500 master vrf-s7" "12: brl3-s7: <UP> mtu 1500 master vrf-s7"; }; vrf_members vp-s7') == "brl3-s7 l3-s7-o" ]] \
|
|
|| fail "lu : $(node 'ip () { printf "%s\n" "15: l3-s7-o@if14: <UP>" "12: brl3-s7: <UP>"; }; vrf_members vp-s7')"
|
|
teardown
|
|
}
|
|
|
|
test_vm_create_is_idempotent () {
|
|
setup "vm_create : VM déjà en running, rien n'est copié ni envoyé"
|
|
[[ $(node 'api_state () { echo running; }; cp () { echo COPIE; }; api_post () { echo POST; }; vm_create v sn 10.0.0.10 && echo OK') == OK ]] \
|
|
|| fail "VM existante recréée"
|
|
teardown
|
|
}
|
|
|
|
test_l3vni_ready_requires_type_state_and_rmac () {
|
|
setup "l3vni_ready : type L3, état Up et RMAC exigés"
|
|
local out='VNI: 2700\n Type: L3\n State: Up\n Router MAC: 02:00:00:00:27:02'
|
|
[[ $(node "vtysh () { printf '${out}\n'; }; l3vni_ready 2700 02:00:00:00:27:02 && echo OUI") == OUI ]] || fail "VNI prête refusée"
|
|
[[ $(node "vtysh () { printf '${out}\n'; }; l3vni_ready 2700 02:00:00:00:27:01 || echo NON") == NON ]] || fail "mauvaise RMAC acceptée"
|
|
[[ $(node "vtysh () { printf '${out/Up/Down}\n'; }; l3vni_ready 2700 02:00:00:00:27:02 || echo NON") == NON ]] || fail "état Down accepté"
|
|
[[ $(node "vtysh () { printf '${out/L3/L2}\n'; }; l3vni_ready 2700 02:00:00:00:27:02 || echo NON") == NON ]] || fail "VNI L2 acceptée"
|
|
teardown
|
|
}
|
|
|
|
test_vrf_nexthops_lists_every_ecmp_member () {
|
|
setup "vrf_nexthops : un chemin, plusieurs chemins, aucun"
|
|
[[ $(node 'ip () { echo "10.242.1.50 nhid 9 via 192.168.14.12 dev brl3-s8 proto bgp metric 20 onlink"; }; vrf_nexthops vp-s8 10.242.1.50') == 192.168.14.12 ]] || fail "un chemin"
|
|
[[ $(node 'ip () { printf "%s\n" "10.242.1.0/24 nhid 90 proto bgp metric 20" " nexthop via 192.168.14.12 dev brl3-s8 weight 1 onlink" " nexthop via 192.168.14.11 dev brl3-s8 weight 1 onlink"; }; vrf_nexthops vp-s8 10.242.1.0/24') == "192.168.14.11 192.168.14.12" ]] || fail "ECMP"
|
|
[[ -z $(node 'ip () { :; }; vrf_nexthops vp-s8 10.242.1.50') ]] || fail "aucun chemin"
|
|
teardown
|
|
}
|
|
|
|
test_bgp_best_reports_next_hop_and_reason () {
|
|
setup "bgp_paths / bgp_best : nombre de chemins, meilleur next-hop et raison"
|
|
local json='{"prefix":"10.242.1.50/32","paths":[{"nexthops":[{"ip":"192.168.14.12"}]},{"nexthops":[{"ip":"192.168.14.11"}],"bestpath":{"overall":true,"selectionReason":"Router ID"}}]}'
|
|
[[ $(node "vtysh () { echo '${json}'; }; bgp_paths vp-s8 10.242.1.50/32") == 2 ]] || fail "nombre de chemins"
|
|
[[ $(node "vtysh () { echo '${json}'; }; bgp_best vp-s8 10.242.1.50/32") == "192.168.14.11 (Router ID)" ]] || fail "meilleur : $(node "vtysh () { echo '${json}'; }; bgp_best vp-s8 10.242.1.50/32")"
|
|
[[ $(node "vtysh () { echo '{}'; }; bgp_paths vp-s8 10.242.1.50/32") == 0 ]] || fail "préfixe absent"
|
|
teardown
|
|
}
|
|
|
|
test_vpc_create_sends_the_l3vni_only_when_given () {
|
|
setup "vpc_create : l3vni transmise si donnée, absente sinon"
|
|
local stub='api_state () { echo; }; wait_state () { :; }; api_post () { echo "$1 $2"; }; '
|
|
[[ $(node "${stub}vpc_create vp-s7 10.241.0.0/16 2700") == 'vpcs {"name":"vp-s7","cidr":"10.241.0.0/16","l3vni":2700}' ]] \
|
|
|| fail "avec l3vni : $(node "${stub}vpc_create vp-s7 10.241.0.0/16 2700")"
|
|
[[ $(node "${stub}vpc_create vp-s1 10.210.0.0/16") == 'vpcs {"name":"vp-s1","cidr":"10.210.0.0/16"}' ]] \
|
|
|| fail "sans l3vni : $(node "${stub}vpc_create vp-s1 10.210.0.0/16")"
|
|
teardown
|
|
}
|
|
|
|
test_subnet_create_lets_the_agent_compute_the_gateway () {
|
|
setup "subnet_create : vxlan sans interface_ip, champs supplémentaires ajoutés"
|
|
local stub='api_state () { echo; }; wait_state () { :; }; api_post () { echo "$1 $2"; }; '
|
|
[[ $(node "${stub}subnet_create sn-s1a vp-s1 2101 10.210.1.0/24") == 'subnets {"name":"sn-s1a","vpc":"vp-s1","mode":"vxlan","vxlan_id":2101,"iface_type":"vms","cidr":"10.210.1.0/24"}' ]] \
|
|
|| fail "sans extra : $(node "${stub}subnet_create sn-s1a vp-s1 2101 10.210.1.0/24")"
|
|
[[ $(node "${stub}subnet_create sn-s2b vp-s2 2202 10.220.2.0/24 ',\"default_route\":true'") == 'subnets {"name":"sn-s2b","vpc":"vp-s2","mode":"vxlan","vxlan_id":2202,"iface_type":"vms","cidr":"10.220.2.0/24","default_route":true}' ]] \
|
|
|| fail "avec extra : $(node "${stub}subnet_create sn-s2b vp-s2 2202 10.220.2.0/24 ',\"default_route\":true'")"
|
|
teardown
|
|
}
|
|
|
|
test_anycast_mac_encodes_the_vni_on_24_bits () {
|
|
setup "anycast_mac : 02:01:00 suivi de la VNI sur 24 bits"
|
|
[[ $(node 'anycast_mac 2911') == 02:01:00:00:0b:5f ]] || fail "2911 : $(node 'anycast_mac 2911')"
|
|
[[ $(node 'anycast_mac 16777215') == 02:01:00:ff:ff:ff ]] || fail "VNI max : $(node 'anycast_mac 16777215')"
|
|
teardown
|
|
}
|
|
|
|
test_api_code_returns_the_http_status () {
|
|
setup "api_code : méthode, chemin et code HTTP"
|
|
[[ $(node 'curl () { echo "$*" >&2; echo 409; }; api_code DELETE subnets/sn-a 2>/dev/null') == 409 ]] || fail "code"
|
|
[[ $(node 'curl () { echo "$*"; }; api_code DELETE subnets/sn-a') == "-s -o /dev/null -w %{http_code} -X DELETE http://127.0.0.1:8080/subnets/sn-a" ]] \
|
|
|| fail "appel : $(node 'curl () { echo "$*"; }; api_code DELETE subnets/sn-a')"
|
|
teardown
|
|
}
|
|
|
|
test_watchdog_reported_matches_only_watchdog_alerts () {
|
|
setup "watchdog_reported : alerte du watchdog contenant le texte, rien d'autre"
|
|
local alert='{"level":"ERROR","msg":"watchdog: inconsistency detected","kind":"vpc","name":"vp-s8","problem":"route 10.242.1.99/32 exported in vrf-s8 without a running owner: its traffic is black-holed"}'
|
|
local other='{"level":"ERROR","msg":"command failed","error":"route 10.242.1.99/32 exported in vrf-s8 without a running owner"}'
|
|
[[ $(node "journalctl () { echo '${alert}'; }; watchdog_reported 'route 10.242.1.99/32 exported in vrf-s8 without a running owner' && echo OUI") == OUI ]] || fail "alerte non trouvée"
|
|
[[ $(node "journalctl () { echo '${other}'; }; watchdog_reported 'route 10.242.1.99/32 exported in vrf-s8' || echo NON") == NON ]] || fail "message hors watchdog accepté"
|
|
[[ $(node "journalctl () { echo '${alert}'; }; watchdog_reported 'route 10.242.1.98/32' || echo NON") == NON ]] || fail "autre route acceptée"
|
|
teardown
|
|
}
|
|
|
|
for T in $(declare -F | awk '{print $3}' | grep '^test_'); do
|
|
"${T}"
|
|
done
|
|
echo "${PASS} réussi(s), ${FAIL} échec(s)"
|
|
[[ "${FAIL}" -eq 0 ]]
|