Untitled

 avatar
unknown
plain_text
a month ago
3.3 kB
6
Indexable
#!/bin/bash
# test_basic.sh — Testa funcionalidades básicas do sistema
# Verifica: submissão, execução, consulta, redirecionamentos, shutdown

PASS=0
FAIL=0

pass() { echo "[OK]  $1"; ((PASS++)); }
fail() { echo "[ERR] $1"; ((FAIL++)); }

cleanup() {
    [ -n "$CTRL_PID" ] && kill "$CTRL_PID" 2>/dev/null
    sleep 0.3
    rm -f controller.log fifo_* lista.txt out.txt
}
trap cleanup EXIT

echo "=== TESTE DE FUNCIONALIDADE BÁSICA ==="

# --- Setup ---
make -s clean && make -s
if [ $? -ne 0 ]; then
    echo "[ERRO FATAL] Compilação falhou"
    exit 1
fi

rm -f controller.log fifo_*

./controller 2 fifo &
CTRL_PID=$!

# Aguardar FIFO
for i in {1..20}; do [ -p fifo_controller ] && break; sleep 0.1; done
if [ ! -p fifo_controller ]; then
    fail "fifo_controller não foi criado"
    kill $CTRL_PID 2>/dev/null
    exit 1
fi

# --- Teste 1: execução simples ---
OUTPUT=$(./runner -e user1 "echo hello" 2>&1)
if echo "$OUTPUT" | grep -q "hello"; then
    pass "Execução simples (echo hello)"
else
    fail "Execução simples (echo hello) — output: $OUTPUT"
fi

# --- Teste 2: mensagens do runner ---
if echo "$OUTPUT" | grep -q "submitted" && echo "$OUTPUT" | grep -q "executing" && echo "$OUTPUT" | grep -q "finished"; then
    pass "Mensagens submitted/executing/finished presentes"
else
    fail "Mensagens submitted/executing/finished em falta"
fi

# --- Teste 3: redirecionamento de output ---
./runner -e user1 "ls > lista.txt" > /dev/null 2>&1
sleep 0.5
if [ -f lista.txt ] && [ -s lista.txt ]; then
    pass "Redirecionamento > (lista.txt criado e não vazio)"
else
    fail "Redirecionamento > falhou"
fi

# --- Teste 4: pipe ---
./runner -e user1 "ls | wc -l > out.txt" > /dev/null 2>&1
sleep 0.5
if [ -f out.txt ] && [ -s out.txt ]; then
    pass "Pipe com redirecionamento (ls | wc -l > out.txt)"
else
    fail "Pipe com redirecionamento falhou"
fi

# --- Teste 5: consulta -c ---
./runner -e user1 "sleep 5" > /dev/null 2>&1 &
./runner -e user2 "sleep 5" > /dev/null 2>&1 &
./runner -e user3 "sleep 5" > /dev/null 2>&1 &
sleep 1

CONSULT=$(./runner -c 2>&1)
if echo "$CONSULT" | grep -q "Executing" && echo "$CONSULT" | grep -q "Scheduled"; then
    pass "Consulta -c devolve secções Executing/Scheduled"
else
    fail "Consulta -c incompleta — output: $CONSULT"
fi

if echo "$CONSULT" | grep -q "user1\|user2\|user3"; then
    pass "Consulta -c lista os utilizadores corretamente"
else
    fail "Consulta -c não lista utilizadores"
fi

wait

# --- Teste 6: log persistente ---
sleep 0.5
if [ -f controller.log ] && [ -s controller.log ]; then
    pass "controller.log criado e não vazio"
else
    fail "controller.log não existe ou está vazio"
fi

if grep -qE "total_time [0-9]+\.[0-9]+" controller.log; then
    pass "controller.log contém tempos válidos"
else
    fail "controller.log não tem tempos válidos"
fi

# --- Teste 7: shutdown ---
./runner -s
sleep 1
if ps -p $CTRL_PID > /dev/null 2>&1; then
    fail "Controller ainda está a correr após -s"
    kill $CTRL_PID 2>/dev/null
else
    pass "Shutdown: controller terminou corretamente"
fi

# --- Resumo ---
echo ""
echo "========================================"
echo " Resultado: $PASS passaram, $FAIL falharam"
echo "========================================"

rm -f lista.txt out.txt fifo_*
exit $FAIL
Editor is loading...
Leave a Comment