test_all.sh 4.76 KB
#!/bin/bash

# Complete test script for SearchEngine
# This script performs full testing including data ingestion and service restart

cd "$(dirname "$0")"

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}SearchEngine完整测试脚本${NC}"
echo -e "${GREEN}========================================${NC}"

# Step 1: Setup environment
echo -e "\n${YELLOW}Step 1/4: 设置环境${NC}"
if [ -f "./setup.sh" ]; then
    ./setup.sh
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ 环境设置完成${NC}"
    else
        echo -e "${RED}✗ 环境设置失败${NC}"
        exit 1
    fi
else
    echo -e "${YELLOW}⚠ setup脚本不存在,跳过环境设置${NC}"
fi

# Step 2: Check and ingest data if needed
echo -e "\n${YELLOW}Step 2/4: 检查并准备数据${NC}"
source /home/tw/miniconda3/etc/profile.d/conda.sh
conda activate searchengine

# Check if index exists
INDEX_EXISTS=$(python -c "
from config.env_config import get_es_config
from utils.es_client import ESClient
from config import ConfigLoader

try:
    es_config = get_es_config()
    es_client = ESClient(hosts=[es_config['host']], username=es_config.get('username'), password=es_config.get('password'))

    config_loader = ConfigLoader('config/config.yaml')
    config = config_loader.load_config()

    if es_client.index_exists(config.es_index_name):
        doc_count = es_client.count(config.es_index_name)
        print(f'{doc_count}')
    else:
        print('0')
except Exception as e:
    print(f'0')
" 2>/dev/null || echo "0")

if [ "$INDEX_EXISTS" = "0" ]; then
    echo -e "${YELLOW}索引不存在,开始导入数据...${NC}"
    echo -e "${YELLOW}注意: 首次导入会下载模型文件,可能需要10-30分钟${NC}"
    echo -e "${YELLOW}导入1000条数据进行快速测试(跳过embedding以加快速度)${NC}"

    if [ -f "./scripts/ingest.sh" ]; then
        ./scripts/ingest.sh 1000 true
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}✓ 数据导入完成${NC}"
        else
            echo -e "${RED}✗ 数据导入失败${NC}"
            exit 1
        fi
    else
        echo -e "${RED}✗ 数据导入脚本不存在${NC}"
        exit 1
    fi
else
    echo -e "${GREEN}✓ 数据已存在,包含 $INDEX_EXISTS 条文档${NC}"
fi

# Step 3: Restart services (stop first, then start)
echo -e "\n${YELLOW}Step 3/4: 重启服务${NC}"
if [ -f "./restart.sh" ]; then
    ./restart.sh
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ 服务重启完成${NC}"
    else
        echo -e "${RED}✗ 服务重启失败${NC}"
        exit 1
    fi
else
    echo -e "${RED}✗ 重启脚本不存在${NC}"
    exit 1
fi

# Step 4: Test the services
echo -e "\n${YELLOW}Step 4/4: 测试服务${NC}"
sleep 3

# Test backend health
echo -e "${YELLOW}测试后端服务健康状态...${NC}"
if curl -s http://localhost:6002/admin/health > /dev/null 2>&1; then
    echo -e "${GREEN}✓ 后端服务健康检查通过${NC}"
else
    echo -e "${YELLOW}⚠ 后端服务健康检查失败,但服务可能仍在启动${NC}"
fi

# Test frontend
echo -e "${YELLOW}测试前端服务...${NC}"
if curl -s http://localhost:6003/ > /dev/null 2>&1; then
    echo -e "${GREEN}✓ 前端服务可访问${NC}"
else
    echo -e "${YELLOW}⚠ 前端服务可能还在启动中${NC}"
fi

# Test API endpoint
echo -e "${YELLOW}测试API端点...${NC}"
API_TEST_RESULT=$(curl -s -X GET "http://localhost:6002/search?q=test&size=5" 2>/dev/null | python -c "
import json
import sys
try:
    data = json.load(sys.stdin)
    if 'results' in data and len(data['results']) > 0:
        print('API_TEST_OK')
    else:
        print('API_TEST_EMPTY')
except:
    print('API_TEST_ERROR')
" 2>/dev/null || echo "API_TEST_ERROR")

case $API_TEST_RESULT in
    "API_TEST_OK")
        echo -e "${GREEN}✓ API测试通过,返回搜索结果${NC}"
        ;;
    "API_TEST_EMPTY")
        echo -e "${YELLOW}⚠ API测试通过,但返回空结果${NC}"
        ;;
    *)
        echo -e "${YELLOW}⚠ API测试失败,服务可能还在启动中${NC}"
        ;;
esac

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}完整测试流程结束!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "服务状态:"
echo -e "  ${GREEN}前端界面: http://localhost:6003${NC}"
echo -e "  ${GREEN}后端API: http://localhost:6002${NC}"
echo -e "  ${GREEN}API文档: http://localhost:6002/docs${NC}"
echo ""
echo -e "可用脚本:"
echo -e "  ${YELLOW}./run.sh${NC}     - 仅启动服务(不导入数据)"
echo -e "  ${YELLOW}./stop.sh${NC}    - 停止所有服务"
echo -e "  ${YELLOW}./restart.sh${NC} - 重启所有服务"
echo -e "  ${YELLOW}./test_all.sh${NC}- 完整测试(包含数据导入)"
echo ""