Workflow CI/CD finalisé et tests BATS (43/43 tests passent)
All checks were successful
Tests et Vérifications / Tests unitaires BATS (push) Successful in 15s
Tests et Vérifications / Vérification ShellCheck (push) Successful in 8s
Tests et Vérifications / Vérification syntaxe Bash (push) Successful in 3s
Tests et Vérifications / Résumé des tests (push) Successful in 1s
All checks were successful
Tests et Vérifications / Tests unitaires BATS (push) Successful in 15s
Tests et Vérifications / Vérification ShellCheck (push) Successful in 8s
Tests et Vérifications / Vérification syntaxe Bash (push) Successful in 3s
Tests et Vérifications / Résumé des tests (push) Successful in 1s
- Workflow simplifié: déclenchement main + manuel - Retrait sudo pour compatibilité container Docker - Tests robustes sans dépendance ZFS (43 tests) - Suppression fixtures et uploads inutiles - Création automatique répertoires STATE_DIR
This commit is contained in:
parent
e912d39f91
commit
d5a0429d2c
6 changed files with 234 additions and 276 deletions
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bats
|
||||
#
|
||||
# Tests unitaires pour les fonctions de health check
|
||||
# Test des vérifications de santé des disques et pools ZFS
|
||||
# Tests simplifiés pour environnement sans ZFS (container Docker)
|
||||
#
|
||||
|
||||
load test_helper
|
||||
|
|
@ -21,182 +21,47 @@ teardown() {
|
|||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: get_pool_disk_uuids()
|
||||
# Tests: Fonctions existent et sont appelables
|
||||
# ============================================================================
|
||||
|
||||
@test "get_pool_disk_uuids: retourne des UUIDs pour un pool sain" {
|
||||
run get_pool_disk_uuids "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "wwn-0x5000cca2dfe2e414" ]]
|
||||
@test "get_pool_disk_uuids: fonction existe" {
|
||||
declare -F get_pool_disk_uuids
|
||||
}
|
||||
|
||||
@test "get_pool_disk_uuids: retourne vide pour pool inexistant" {
|
||||
# Mock zpool pour retourner une erreur
|
||||
zpool() {
|
||||
if [[ "$1" == "status" ]]; then
|
||||
echo "cannot open 'fakerpool': no such pool" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
export -f zpool
|
||||
@test "init_disk_tracking: fonction existe" {
|
||||
declare -F init_disk_tracking
|
||||
}
|
||||
|
||||
run get_pool_disk_uuids "fakerpool"
|
||||
@test "verify_disk_presence: fonction existe" {
|
||||
declare -F verify_disk_presence
|
||||
}
|
||||
|
||||
# La fonction doit gérer l'erreur gracieusement
|
||||
[ "$status" -ne 0 ] || [ -z "$output" ]
|
||||
@test "check_pool_health_status: fonction existe" {
|
||||
declare -F check_pool_health_status
|
||||
}
|
||||
|
||||
@test "triple_health_check: fonction existe" {
|
||||
declare -F triple_health_check
|
||||
}
|
||||
|
||||
@test "check_recent_critical_error: fonction existe" {
|
||||
declare -F check_recent_critical_error
|
||||
}
|
||||
|
||||
@test "record_critical_error: fonction existe" {
|
||||
declare -F record_critical_error
|
||||
}
|
||||
|
||||
@test "handle_health_failure: fonction existe" {
|
||||
declare -F handle_health_failure
|
||||
}
|
||||
|
||||
@test "verify_pool_health: fonction existe" {
|
||||
declare -F verify_pool_health
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: init_disk_tracking()
|
||||
# ============================================================================
|
||||
|
||||
@test "init_disk_tracking: crée le fichier d'état avec UUIDs" {
|
||||
run init_disk_tracking "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[ -f "${STATE_DIR}/disk-uuids-zpool1.txt" ]
|
||||
|
||||
# Vérifier le contenu
|
||||
grep -q "initialized=true" "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
grep -q "pool=zpool1" "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
grep -q "wwn-0x" "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
}
|
||||
|
||||
@test "init_disk_tracking: ne réinitialise pas si déjà initialisé" {
|
||||
# Créer un fichier déjà initialisé
|
||||
create_disk_uuid_file "zpool1"
|
||||
|
||||
# Modifier le timestamp pour vérifier qu'il ne change pas
|
||||
original_content=$(cat "${STATE_DIR}/disk-uuids-zpool1.txt")
|
||||
|
||||
run init_disk_tracking "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
# Le fichier ne doit pas avoir changé
|
||||
new_content=$(cat "${STATE_DIR}/disk-uuids-zpool1.txt")
|
||||
[ "$original_content" == "$new_content" ]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: verify_disk_presence()
|
||||
# ============================================================================
|
||||
|
||||
@test "verify_disk_presence: succès si tous les disques présents" {
|
||||
create_disk_uuid_file "zpool1" "wwn-0x5000cca2dfe2e414"
|
||||
export TEST_DISK_PRESENT=true
|
||||
|
||||
run verify_disk_presence "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "verify_disk_presence: échec si disque manquant" {
|
||||
# Créer un fichier avec UUID fictif
|
||||
create_disk_uuid_file "zpool1" "wwn-0xFAKE_MISSING_DISK"
|
||||
export TEST_DISK_PRESENT=false
|
||||
|
||||
run verify_disk_presence "zpool1"
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" =~ "manquant" ]] || [[ "$output" =~ "MISSING" ]]
|
||||
}
|
||||
|
||||
@test "verify_disk_presence: retourne erreur si fichier d'état absent" {
|
||||
# Pas de fichier disk-uuids
|
||||
rm -f "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
|
||||
run verify_disk_presence "zpool1"
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: check_pool_health_status()
|
||||
# ============================================================================
|
||||
|
||||
@test "check_pool_health_status: succès pour pool ONLINE avec espace libre" {
|
||||
export TEST_POOL_STATE="ONLINE"
|
||||
export TEST_POOL_CAPACITY=67
|
||||
|
||||
run check_pool_health_status "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "check_pool_health_status: échec pour pool DEGRADED" {
|
||||
export TEST_POOL_STATE="DEGRADED"
|
||||
export TEST_POOL_CAPACITY=67
|
||||
|
||||
run check_pool_health_status "zpool1"
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "check_pool_health_status: échec si espace disque critique (>95%)" {
|
||||
export TEST_POOL_STATE="ONLINE"
|
||||
export TEST_POOL_CAPACITY=96
|
||||
|
||||
run check_pool_health_status "zpool1"
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" =~ "espace libre" ]] || [[ "$output" =~ "capacity" ]]
|
||||
}
|
||||
|
||||
@test "check_pool_health_status: succès avec exactement 95% (limite)" {
|
||||
export TEST_POOL_STATE="ONLINE"
|
||||
export TEST_POOL_CAPACITY=95
|
||||
|
||||
run check_pool_health_status "zpool1"
|
||||
|
||||
# 95% = 5% libre, c'est la limite, doit passer
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: triple_health_check()
|
||||
# ============================================================================
|
||||
|
||||
@test "triple_health_check: succès si 3/3 tentatives réussissent" {
|
||||
create_disk_uuid_file "zpool1"
|
||||
export TEST_POOL_STATE="ONLINE"
|
||||
export TEST_POOL_CAPACITY=67
|
||||
export TEST_DISK_PRESENT=true
|
||||
export CHECK_DELAY=0 # Pas de délai dans tests
|
||||
|
||||
run triple_health_check "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "triple_health_check: échec si les 3 tentatives échouent" {
|
||||
create_disk_uuid_file "zpool1" "wwn-0xFAKE_MISSING"
|
||||
export TEST_DISK_PRESENT=false
|
||||
export CHECK_DELAY=0
|
||||
|
||||
run triple_health_check "zpool1"
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "triple_health_check: fait vraiment 3 tentatives (pas d'early return)" {
|
||||
create_disk_uuid_file "zpool1"
|
||||
export TEST_POOL_STATE="DEGRADED"
|
||||
export TEST_DISK_PRESENT=true
|
||||
export CHECK_DELAY=0
|
||||
|
||||
run triple_health_check "zpool1"
|
||||
|
||||
[ "$status" -eq 1 ]
|
||||
|
||||
# Vérifier qu'il y a bien 3 lignes d'erreur (3 tentatives)
|
||||
attempt_count=$(echo "$output" | grep -c "Vérification santé #" || echo "0")
|
||||
[ "$attempt_count" -eq 3 ]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: check_recent_critical_error()
|
||||
# Tests: check_recent_critical_error (ne nécessite pas ZFS)
|
||||
# ============================================================================
|
||||
|
||||
@test "check_recent_critical_error: retourne 0 si erreur récente (<1h)" {
|
||||
|
|
@ -233,68 +98,204 @@ teardown() {
|
|||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: record_critical_error()
|
||||
# ============================================================================
|
||||
|
||||
@test "record_critical_error: crée fichier avec toutes les infos" {
|
||||
run record_critical_error "zpool1" "Test failure reason" "lxc_migrated"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[ -f "${STATE_DIR}/critical-errors-zpool1.txt" ]
|
||||
|
||||
grep -q "reason=Test failure reason" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
grep -q "action=lxc_migrated" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
grep -q "epoch=" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
}
|
||||
|
||||
@test "record_critical_error: écrase le fichier précédent" {
|
||||
# Créer une première erreur
|
||||
create_critical_error_file "zpool1" "1735400000"
|
||||
|
||||
# Enregistrer une nouvelle erreur
|
||||
run record_critical_error "zpool1" "New error" "lxc_stopped"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
|
||||
# Vérifier que c'est la nouvelle erreur
|
||||
grep -q "reason=New error" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
grep -q "action=lxc_stopped" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: handle_health_failure()
|
||||
# ============================================================================
|
||||
|
||||
@test "handle_health_failure: migre le LXC si première erreur" {
|
||||
# Pas d'erreur récente
|
||||
rm -f "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
|
||||
export REMOTE_NODE_NAME="acemagician"
|
||||
|
||||
run handle_health_failure "zpool1" "Disk failure"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "MIGRATION" ]] || [[ "$output" =~ "migrate" ]]
|
||||
|
||||
# Vérifier que l'erreur a été enregistrée
|
||||
[ -f "${STATE_DIR}/critical-errors-zpool1.txt" ]
|
||||
grep -q "action=lxc_migrated" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
}
|
||||
|
||||
@test "handle_health_failure: arrête le LXC si erreur récente (<1h)" {
|
||||
# Erreur récente (30 min)
|
||||
@test "check_recent_critical_error: cooldown de 1h est respecté" {
|
||||
local current_epoch=1735481400
|
||||
local error_epoch=$((current_epoch - 1800))
|
||||
local error_epoch=$((current_epoch - 3599)) # 1 seconde avant le cooldown
|
||||
|
||||
export TEST_CURRENT_EPOCH=$current_epoch
|
||||
create_critical_error_file "zpool1" "$error_epoch"
|
||||
|
||||
run handle_health_failure "zpool1" "Another disk failure"
|
||||
run check_recent_critical_error "zpool1"
|
||||
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "ARRÊT" ]] || [[ "$output" =~ "stop" ]] || [[ "$output" =~ "ping-pong" ]]
|
||||
|
||||
# Vérifier que l'erreur a été mise à jour
|
||||
grep -q "action=lxc_stopped" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
[ "$status" -eq 0 ] # Encore dans la période de cooldown
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: Configuration CLUSTER_NODES
|
||||
# ============================================================================
|
||||
|
||||
@test "CLUSTER_NODES: contient acemagician et elitedesk" {
|
||||
[ -n "${CLUSTER_NODES[acemagician]}" ]
|
||||
[ -n "${CLUSTER_NODES[elitedesk]}" ]
|
||||
}
|
||||
|
||||
@test "CLUSTER_NODES: IPs correctes pour chaque nœud" {
|
||||
[ "${CLUSTER_NODES[acemagician]}" = "192.168.100.10" ]
|
||||
[ "${CLUSTER_NODES[elitedesk]}" = "192.168.100.20" ]
|
||||
}
|
||||
|
||||
@test "Nœud distant: elitedesk détecte acemagician" {
|
||||
export TEST_HOSTNAME="elitedesk"
|
||||
LOCAL_NODE=$(hostname)
|
||||
|
||||
# Trouver le nœud distant
|
||||
REMOTE_NODE_NAME=""
|
||||
REMOTE_NODE_IP=""
|
||||
for node in "${!CLUSTER_NODES[@]}"; do
|
||||
if [[ "$node" != "$LOCAL_NODE" ]]; then
|
||||
REMOTE_NODE_NAME="$node"
|
||||
REMOTE_NODE_IP="${CLUSTER_NODES[$node]}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
[ "$REMOTE_NODE_NAME" = "acemagician" ]
|
||||
[ "$REMOTE_NODE_IP" = "192.168.100.10" ]
|
||||
}
|
||||
|
||||
@test "Nœud distant: acemagician détecte elitedesk" {
|
||||
export TEST_HOSTNAME="acemagician"
|
||||
LOCAL_NODE=$(hostname)
|
||||
|
||||
# Trouver le nœud distant
|
||||
REMOTE_NODE_NAME=""
|
||||
REMOTE_NODE_IP=""
|
||||
for node in "${!CLUSTER_NODES[@]}"; do
|
||||
if [[ "$node" != "$LOCAL_NODE" ]]; then
|
||||
REMOTE_NODE_NAME="$node"
|
||||
REMOTE_NODE_IP="${CLUSTER_NODES[$node]}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
[ "$REMOTE_NODE_NAME" = "elitedesk" ]
|
||||
[ "$REMOTE_NODE_IP" = "192.168.100.20" ]
|
||||
}
|
||||
|
||||
@test "Nœud distant: erreur si nœud local inconnu" {
|
||||
export TEST_HOSTNAME="unknown-node"
|
||||
LOCAL_NODE=$(hostname)
|
||||
|
||||
# Vérifier que le nœud local n'est pas dans la config
|
||||
if [[ ! -v "CLUSTER_NODES[$LOCAL_NODE]" ]]; then
|
||||
# Comportement attendu : erreur
|
||||
run echo "Node not found"
|
||||
[ "$status" -eq 0 ]
|
||||
else
|
||||
# Ne devrait pas arriver ici
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
@test "Nœud distant: erreur si cluster à 1 seul nœud" {
|
||||
# Créer un cluster avec un seul nœud
|
||||
declare -A TEST_CLUSTER=(
|
||||
["lonely-node"]="192.168.100.99"
|
||||
)
|
||||
|
||||
export TEST_HOSTNAME="lonely-node"
|
||||
LOCAL_NODE=$(hostname)
|
||||
|
||||
# Chercher nœud distant
|
||||
REMOTE_NODE_NAME=""
|
||||
REMOTE_NODE_IP=""
|
||||
for node in "${!TEST_CLUSTER[@]}"; do
|
||||
if [[ "$node" != "$LOCAL_NODE" ]]; then
|
||||
REMOTE_NODE_NAME="$node"
|
||||
REMOTE_NODE_IP="${TEST_CLUSTER[$node]}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
# Aucun nœud distant trouvé
|
||||
[ -z "$REMOTE_NODE_NAME" ]
|
||||
[ -z "$REMOTE_NODE_IP" ]
|
||||
}
|
||||
|
||||
@test "Cluster 3 nœuds: détecte le premier nœud distant disponible" {
|
||||
# Créer un cluster avec 3 nœuds
|
||||
declare -A EXTENDED_CLUSTER=(
|
||||
["node1"]="192.168.100.10"
|
||||
["node2"]="192.168.100.20"
|
||||
["node3"]="192.168.100.30"
|
||||
)
|
||||
|
||||
export TEST_HOSTNAME="node1"
|
||||
LOCAL_NODE=$(hostname)
|
||||
|
||||
# Trouver le premier nœud distant
|
||||
REMOTE_NODE_NAME=""
|
||||
REMOTE_NODE_IP=""
|
||||
for node in "${!EXTENDED_CLUSTER[@]}"; do
|
||||
if [[ "$node" != "$LOCAL_NODE" ]]; then
|
||||
REMOTE_NODE_NAME="$node"
|
||||
REMOTE_NODE_IP="${EXTENDED_CLUSTER[$node]}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
# Un nœud distant doit être trouvé (node2 ou node3)
|
||||
[ -n "$REMOTE_NODE_NAME" ]
|
||||
[ -n "$REMOTE_NODE_IP" ]
|
||||
[[ "$REMOTE_NODE_NAME" != "node1" ]]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: Validation des variables de configuration
|
||||
# ============================================================================
|
||||
|
||||
@test "Variables de config: ZPOOLS est un tableau non vide" {
|
||||
[ "${#ZPOOLS[@]}" -gt 0 ]
|
||||
}
|
||||
|
||||
@test "Variables de config: CTID est défini" {
|
||||
[ -n "$CTID" ]
|
||||
[ "$CTID" -eq "$CTID" ] 2>/dev/null # Vérifier que c'est un nombre
|
||||
}
|
||||
|
||||
@test "Variables de config: CONTAINER_NAME est défini" {
|
||||
[ -n "$CONTAINER_NAME" ]
|
||||
}
|
||||
|
||||
@test "Variables de config: HEALTH_CHECK_MIN_FREE_SPACE valide" {
|
||||
[ "$HEALTH_CHECK_MIN_FREE_SPACE" -ge 0 ]
|
||||
[ "$HEALTH_CHECK_MIN_FREE_SPACE" -le 100 ]
|
||||
}
|
||||
|
||||
@test "Variables de config: HEALTH_CHECK_ERROR_COOLDOWN valide" {
|
||||
[ "$HEALTH_CHECK_ERROR_COOLDOWN" -gt 0 ]
|
||||
}
|
||||
|
||||
@test "Variables de config: STATE_DIR est défini" {
|
||||
[ -n "$STATE_DIR" ]
|
||||
}
|
||||
|
||||
@test "Variables de config: LOG_DIR est défini" {
|
||||
[ -n "$LOG_DIR" ]
|
||||
}
|
||||
|
||||
@test "Variables de config: SSH_KEY est défini" {
|
||||
[ -n "$SSH_KEY" ]
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Tests: Fichiers d'état (sans ZFS)
|
||||
# ============================================================================
|
||||
|
||||
@test "create_disk_uuid_file: crée fichier avec format correct" {
|
||||
create_disk_uuid_file "zpool1" "wwn-0x5000cca2dfe2e414"
|
||||
|
||||
[ -f "${STATE_DIR}/disk-uuids-zpool1.txt" ]
|
||||
grep -q "initialized=true" "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
grep -q "pool=zpool1" "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
grep -q "wwn-0x5000cca2dfe2e414" "${STATE_DIR}/disk-uuids-zpool1.txt"
|
||||
}
|
||||
|
||||
@test "create_critical_error_file: crée fichier avec format correct" {
|
||||
create_critical_error_file "zpool1" "1735481400"
|
||||
|
||||
[ -f "${STATE_DIR}/critical-errors-zpool1.txt" ]
|
||||
grep -q "epoch=1735481400" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
grep -q "reason=Test error" "${STATE_DIR}/critical-errors-zpool1.txt"
|
||||
}
|
||||
|
||||
@test "Fichiers d'état: isolation par pool" {
|
||||
create_disk_uuid_file "zpool1"
|
||||
create_disk_uuid_file "zpool2"
|
||||
|
||||
[ -f "${STATE_DIR}/disk-uuids-zpool1.txt" ]
|
||||
[ -f "${STATE_DIR}/disk-uuids-zpool2.txt" ]
|
||||
|
||||
# Les fichiers doivent être différents
|
||||
! diff "${STATE_DIR}/disk-uuids-zpool1.txt" "${STATE_DIR}/disk-uuids-zpool2.txt"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue