Files
proxmox-ha-setup/scripts/test-pyc-image.sh
2025-11-29 19:51:15 +01:00

191 lines
6.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# test-pyc-image.sh
# Script per testare che l'immagine Docker funzioni correttamente con solo file .pyc
# Uso: ./test-pyc-image.sh [image_name]
# Esempio: ./test-pyc-image.sh orchestrator-app:latest
set -e
# ==================== CONFIGURAZIONE ====================
IMAGE_NAME=${1:-"orchestrator-app:latest"}
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# ==================== FUNZIONI ====================
print_header() {
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}================================================${NC}"
}
print_success() { echo -e "${GREEN}$1${NC}"; }
print_warning() { echo -e "${YELLOW}$1${NC}"; }
print_error() { echo -e "${RED}$1${NC}"; }
print_info() { echo -e "${CYAN} $1${NC}"; }
# ==================== SCRIPT PRINCIPALE ====================
print_header "TEST IMMAGINE DOCKER CON BYTECODE"
print_info "Immagine da testare: ${CYAN}$IMAGE_NAME${NC}"
echo ""
# Verifica che l'immagine esista
if ! docker image inspect "$IMAGE_NAME" >/dev/null 2>&1; then
print_error "Immagine non trovata: $IMAGE_NAME"
print_info "Esegui prima: ./scripts/build-and-push-image.sh"
exit 1
fi
print_success "Immagine trovata"
# ==================== TEST 1: Verifica assenza file .py ====================
print_header "TEST 1: Verifica rimozione file sorgente .py"
print_info "Controllo file .py in /app/src..."
PY_FILES=$(docker run --rm "$IMAGE_NAME" find /app/src -type f -name "*.py" 2>/dev/null | wc -l)
if [ "$PY_FILES" -eq 0 ]; then
print_success "Nessun file .py trovato in /app/src - OK!"
else
print_warning "Trovati $PY_FILES file .py in /app/src"
docker run --rm "$IMAGE_NAME" find /app/src -type f -name "*.py"
fi
# ==================== TEST 2: Verifica presenza file .pyc ====================
print_header "TEST 2: Verifica presenza file bytecode .pyc"
print_info "Controllo file .pyc in /app/src..."
PYC_FILES=$(docker run --rm "$IMAGE_NAME" find /app/src -type f -name "*.pyc" 2>/dev/null | wc -l)
if [ "$PYC_FILES" -gt 0 ]; then
print_success "Trovati $PYC_FILES file .pyc - OK!"
else
print_error "Nessun file .pyc trovato - qualcosa è andato storto"
exit 1
fi
# ==================== TEST 3: Verifica importazione moduli ====================
print_header "TEST 3: Test importazione moduli Python"
print_info "Test import di src..."
if docker run --rm "$IMAGE_NAME" python -c "import src; print('Import src: OK')" 2>/dev/null; then
print_success "Import src funziona correttamente"
else
print_error "Import src fallito"
exit 1
fi
# ==================== TEST 4: Lista moduli disponibili ====================
print_header "TEST 4: Verifica moduli disponibili"
print_info "Moduli in src:"
docker run --rm "$IMAGE_NAME" python -c "
import pkgutil
import src
for importer, modname, ispkg in pkgutil.walk_packages(path=src.__path__, prefix='src.'):
print(f' - {modname} (package)' if ispkg else f' - {modname}')
" 2>/dev/null || print_warning "Impossibile elencare i moduli"
# ==================== TEST 5: Verifica dimensione immagine ====================
print_header "TEST 5: Analisi dimensione immagine"
IMAGE_SIZE=$(docker images "$IMAGE_NAME" --format "{{.Size}}")
print_info "Dimensione immagine: ${CYAN}$IMAGE_SIZE${NC}"
# Conta i file per tipo
print_info "Statistiche file in /app/src:"
docker run --rm "$IMAGE_NAME" sh -c '
echo " File .py: $(find /app/src -type f -name "*.py" 2>/dev/null | wc -l)"
echo " File .pyc: $(find /app/src -type f -name "*.pyc" 2>/dev/null | wc -l)"
echo " Totale: $(find /app/src -type f 2>/dev/null | wc -l)"
'
# ==================== TEST 6: Test esecuzione moduli ====================
print_header "TEST 6: Test esecuzione moduli orchestrator (dry-run)"
print_info "Test esecuzione load_orchestrator..."
if docker run --rm "$IMAGE_NAME" timeout 2 python -c "
try:
import src.load_orchestrator
print('Module src.load_orchestrator: OK')
except ImportError as e:
print(f'Import error: {e}')
exit(1)
" 2>/dev/null; then
print_success "Modulo load_orchestrator importabile"
else
print_warning "Test load_orchestrator timeout o errore (normale se richiede DB)"
fi
print_info "Test esecuzione elab_orchestrator..."
if docker run --rm "$IMAGE_NAME" timeout 2 python -c "
try:
import src.elab_orchestrator
print('Module src.elab_orchestrator: OK')
except ImportError as e:
print(f'Import error: {e}')
exit(1)
" 2>/dev/null; then
print_success "Modulo elab_orchestrator importabile"
else
print_warning "Test elab_orchestrator timeout o errore (normale se richiede DB)"
fi
# ==================== TEST 7: Verifica variabili ambiente ====================
print_header "TEST 7: Verifica configurazione ambiente"
docker run --rm "$IMAGE_NAME" python -c "
import os
import sys
print('Python version:', sys.version)
print('PYTHONUNBUFFERED:', os.getenv('PYTHONUNBUFFERED', 'not set'))
print('PYTHONDONTWRITEBYTECODE:', os.getenv('PYTHONDONTWRITEBYTECODE', 'not set'))
print('PYTHONPATH:', os.getenv('PYTHONPATH', 'not set'))
print('Python path:', sys.path)
"
# ==================== RIEPILOGO ====================
print_header "TEST COMPLETATI! ✅"
echo ""
print_success "L'immagine contiene solo bytecode Python (.pyc)"
print_success "I file sorgente .py sono stati rimossi correttamente"
print_success "I moduli sono importabili e funzionanti"
echo ""
print_info "Vantaggi:"
print_info " ✓ Protezione del codice sorgente"
print_info " ✓ Riduzione dimensione immagine"
print_info " ✓ Leggero miglioramento delle performance"
print_info " ✓ Rimozione docstring e assert (con -OO)"
echo ""
print_warning "Note:"
print_warning " - I traceback mostreranno solo numeri di riga, non il codice"
print_warning " - Il debugging sarà più difficile senza codice sorgente"
print_warning " - Mantieni i sorgenti in un repository separato per sviluppo"
echo ""
print_info "Prossimi passi:"
print_info " 1. Push dell'immagine al registry: ./scripts/build-and-push-image.sh"
print_info " 2. Deploy sulle VM: docker compose pull && docker compose up -d"
echo ""