342 lines
10 KiB
Bash
Executable File
342 lines
10 KiB
Bash
Executable File
#!/bin/bash
|
|
# Script de prueba para Cisco MAC Monitor
|
|
# ======================================
|
|
|
|
set -e
|
|
|
|
# Colores para output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Contadores
|
|
TESTS_PASSED=0
|
|
TESTS_FAILED=0
|
|
|
|
# Función para logging
|
|
log() {
|
|
echo -e "${BLUE}[TEST]${NC} $1"
|
|
}
|
|
|
|
success() {
|
|
echo -e "${GREEN}[PASS]${NC} $1"
|
|
((TESTS_PASSED++))
|
|
}
|
|
|
|
error() {
|
|
echo -e "${RED}[FAIL]${NC} $1"
|
|
((TESTS_FAILED++))
|
|
}
|
|
|
|
warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
# Test 1: Verificar archivos necesarios
|
|
test_single_files() {
|
|
log "Verificando archivos necesarios..."
|
|
|
|
local files=(
|
|
"cisco_mac_monitor.py"
|
|
"Dockerfile"
|
|
"docker-compose.yml"
|
|
"zabbix_integration.sh"
|
|
"example_output.json"
|
|
"README.md"
|
|
)
|
|
|
|
for file in "${files[@]}"; do
|
|
if [[ -f "$file" ]]; then
|
|
success "Archivo encontrado: $file"
|
|
else
|
|
error "Archivo faltante: $file"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Test 2: Validar sintaxis del script principal
|
|
test_single_python_syntax() {
|
|
log "Validando sintaxis del script simplificado..."
|
|
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
if python3 -m py_compile cisco_mac_monitor.py; then
|
|
success "Sintaxis de Python válida (script simplificado)"
|
|
else
|
|
error "Error de sintaxis en script simplificado"
|
|
fi
|
|
else
|
|
warn "Python3 no disponible para test de sintaxis"
|
|
fi
|
|
}
|
|
|
|
# Test 3: Probar ayuda del script
|
|
test_script_help() {
|
|
log "Probando función de ayuda del script..."
|
|
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
if python3 cisco_mac_monitor.py --help >/dev/null 2>&1; then
|
|
success "Función de ayuda funciona correctamente"
|
|
else
|
|
error "Error en función de ayuda"
|
|
fi
|
|
else
|
|
warn "Python3 no disponible para test de ayuda"
|
|
fi
|
|
}
|
|
|
|
# Test 4: Validar Dockerfile simplificado
|
|
test_single_dockerfile() {
|
|
log "Validando Dockerfile simplificado..."
|
|
|
|
if command -v docker >/dev/null 2>&1; then
|
|
if docker build -f Dockerfile -t cisco-mac-monitor-test . >/dev/null 2>&1; then
|
|
success "Dockerfile válido - imagen construida"
|
|
# Limpiar imagen de test
|
|
docker rmi cisco-mac-monitor-test >/dev/null 2>&1 || true
|
|
else
|
|
error "Error en construcción de Dockerfile"
|
|
fi
|
|
else
|
|
warn "Docker no disponible para test de Dockerfile"
|
|
fi
|
|
}
|
|
|
|
# Test 5: Validar docker-compose simplificado
|
|
test_single_docker_compose() {
|
|
log "Validando docker-compose.single.yml..."
|
|
|
|
if command -v docker-compose >/dev/null 2>&1; then
|
|
if docker-compose config >/dev/null 2>&1; then
|
|
success "docker-compose.yml válido"
|
|
else
|
|
error "Error en docker-compose.yml"
|
|
fi
|
|
else
|
|
warn "docker-compose no disponible para test"
|
|
fi
|
|
}
|
|
|
|
# Test 6: Validar JSON de ejemplo simplificado
|
|
test_single_example_json() {
|
|
log "Validando JSON de ejemplo simplificado..."
|
|
|
|
if [[ -f "example_output.json" ]]; then
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
if python3 -m json.tool example_output.json >/dev/null 2>&1; then
|
|
success "JSON de ejemplo válido"
|
|
|
|
# Verificar estructura específica de Zabbix LLD
|
|
if python3 -c "
|
|
import json
|
|
with open('example_output.json', 'r') as f:
|
|
data = json.load(f)
|
|
|
|
# Verificar estructura básica
|
|
if 'data' not in data:
|
|
print('Missing data array')
|
|
exit(1)
|
|
|
|
if not isinstance(data['data'], list):
|
|
print('data should be an array')
|
|
exit(1)
|
|
|
|
# Verificar macros de Zabbix LLD en primer elemento
|
|
if len(data['data']) > 0:
|
|
item = data['data'][0]
|
|
required_macros = ['{#DEVICE_HOSTNAME}', '{#PORT_NAME}', '{#MAC_COUNT}', '{#VLANS}', '{#MAC_ADDRESSES}', '{#LAST_UPDATE}']
|
|
for macro in required_macros:
|
|
if macro not in item:
|
|
print(f'Missing macro: {macro}')
|
|
exit(1)
|
|
|
|
print('Zabbix LLD structure valid')
|
|
"; then
|
|
success "Estructura Zabbix LLD válida en JSON de ejemplo"
|
|
else
|
|
error "Estructura Zabbix LLD inválida en JSON de ejemplo"
|
|
fi
|
|
else
|
|
error "JSON de ejemplo inválido"
|
|
fi
|
|
else
|
|
warn "Python3 no disponible para validación de JSON"
|
|
fi
|
|
else
|
|
error "Archivo example_output.json no encontrado"
|
|
fi
|
|
}
|
|
|
|
# Test 7: Validar script de integración con Zabbix
|
|
test_zabbix_integration_script() {
|
|
log "Validando script de integración con Zabbix..."
|
|
|
|
if [[ -f "zabbix_integration.sh" ]]; then
|
|
if [[ -x "zabbix_integration.sh" ]]; then
|
|
success "Script de integración tiene permisos de ejecución"
|
|
|
|
# Probar función de ayuda
|
|
if ./zabbix_integration.sh help >/dev/null 2>&1; then
|
|
success "Función de ayuda del script de integración funciona"
|
|
else
|
|
warn "Problema con función de ayuda del script de integración"
|
|
fi
|
|
else
|
|
warn "Script de integración no tiene permisos de ejecución"
|
|
chmod +x zabbix_integration.sh
|
|
success "Permisos de ejecución agregados al script de integración"
|
|
fi
|
|
else
|
|
error "Script de integración no encontrado"
|
|
fi
|
|
}
|
|
|
|
# Test 8: Validar que la imagen simplificada es más pequeña
|
|
test_image_size_comparison() {
|
|
log "Comparando tamaño de imágenes..."
|
|
|
|
if command -v docker >/dev/null 2>&1; then
|
|
# Construir ambas imágenes
|
|
log "Construyendo imagen simplificada..."
|
|
if docker build -f Dockerfile.single -t cisco-single-test:single . >/dev/null 2>&1; then
|
|
local single_size=$(docker images cisco-single-test:single --format "{{.Size}}")
|
|
success "Imagen simplificada construida: $single_size"
|
|
|
|
# Limpiar imagen de test
|
|
docker rmi cisco-single-test:single >/dev/null 2>&1 || true
|
|
else
|
|
warn "No se pudo construir imagen simplificada para comparación"
|
|
fi
|
|
else
|
|
warn "Docker no disponible para comparación de tamaños"
|
|
fi
|
|
}
|
|
|
|
# Test 9: Probar script sin parámetros (debe mostrar error útil)
|
|
test_parameter_validation() {
|
|
log "Probando validación de parámetros..."
|
|
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
# Script debe fallar sin parámetros pero mostrar mensaje útil
|
|
if ! python3 cisco_mac_monitor.py >/dev/null 2>&1; then
|
|
success "Script valida parámetros requeridos correctamente"
|
|
else
|
|
warn "Script no valida parámetros requeridos"
|
|
fi
|
|
|
|
# Probar con parámetros incompletos
|
|
if ! python3 cisco_mac_monitor.py --host 192.168.1.10 >/dev/null 2>&1; then
|
|
success "Script valida parámetros incompletos correctamente"
|
|
else
|
|
warn "Script no valida parámetros incompletos"
|
|
fi
|
|
else
|
|
warn "Python3 no disponible para test de validación"
|
|
fi
|
|
}
|
|
|
|
# Test 10: Verificar documentación simplificada
|
|
test_single_documentation() {
|
|
log "Verificando documentación simplificada..."
|
|
|
|
if [[ -f "README.md" ]]; then
|
|
local readme_size=$(wc -c < README.md)
|
|
if [[ $readme_size -gt 500 ]]; then
|
|
success "README.md presente y completo ($readme_size bytes)"
|
|
else
|
|
warn "README.md muy pequeño ($readme_size bytes)"
|
|
fi
|
|
else
|
|
error "README.md faltante"
|
|
fi
|
|
}
|
|
|
|
# Test de integración con contenedor simplificado
|
|
test_single_container_integration() {
|
|
log "Ejecutando test de integración con contenedor simplificado..."
|
|
|
|
if command -v docker >/dev/null 2>&1; then
|
|
# Construir imagen
|
|
if docker build -f Dockerfile -t cisco-mac-test . >/dev/null 2>&1; then
|
|
success "Imagen Docker construida correctamente"
|
|
|
|
# Test de help en contenedor
|
|
if docker run --rm cisco-mac-test --help >/dev/null 2>&1; then
|
|
success "Test de help en contenedor exitoso"
|
|
else
|
|
error "Test de help en contenedor falló"
|
|
fi
|
|
|
|
# Limpiar
|
|
docker rmi cisco-mac-test >/dev/null 2>&1 || true
|
|
else
|
|
error "Error construyendo imagen para test de integración"
|
|
fi
|
|
else
|
|
warn "Docker no disponible para test de integración"
|
|
fi
|
|
}
|
|
|
|
# Función principal
|
|
main() {
|
|
echo "=============================================="
|
|
echo " Cisco MAC Monitor - Test Suite"
|
|
echo "=============================================="
|
|
echo
|
|
|
|
# Verificar que estamos en el directorio correcto
|
|
if [[ ! -f "cisco_mac_monitor.py" ]]; then
|
|
error "No se encontró cisco_mac_monitor.py. ¿Estás en el directorio correcto?"
|
|
exit 1
|
|
fi
|
|
|
|
# Ejecutar tests específicos para versión simplificada
|
|
test_single_files
|
|
test_single_python_syntax
|
|
test_script_help
|
|
test_parameter_validation
|
|
test_single_example_json
|
|
test_zabbix_integration_script
|
|
test_single_documentation
|
|
|
|
# Tests que requieren herramientas externas
|
|
if command -v docker >/dev/null 2>&1; then
|
|
test_single_dockerfile
|
|
test_single_container_integration
|
|
test_image_size_comparison
|
|
else
|
|
warn "Docker no disponible - saltando tests de Docker"
|
|
fi
|
|
|
|
if command -v docker-compose >/dev/null 2>&1; then
|
|
test_single_docker_compose
|
|
else
|
|
warn "docker-compose no disponible - saltando test"
|
|
fi
|
|
|
|
# Resumen
|
|
echo
|
|
echo "=============================================="
|
|
echo " Resumen de Tests"
|
|
echo "=============================================="
|
|
echo -e "${GREEN}Tests exitosos: $TESTS_PASSED${NC}"
|
|
echo -e "${RED}Tests fallidos: $TESTS_FAILED${NC}"
|
|
echo
|
|
|
|
if [[ $TESTS_FAILED -eq 0 ]]; then
|
|
echo -e "${GREEN}🎉 Todos los tests pasaron exitosamente!${NC}"
|
|
echo
|
|
echo "La versión está lista para usar:"
|
|
echo "1. Construir imagen: docker build -t cisco-mac-monitor ."
|
|
echo "2. Ejecutar: docker run --rm --network host cisco-mac-monitor --host <IP> --username <USER> --password <PASS>"
|
|
echo "3. Integrar con Zabbix usando zabbix_integration.sh"
|
|
exit 0
|
|
else
|
|
echo -e "${RED}❌ Algunos tests fallaron. Revisa los errores arriba.${NC}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Ejecutar tests
|
|
main "$@" |