Files
sms_forwarder/scripts/test-docker.sh

115 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Docker 环境测试脚本
set -e
echo "🧪 SMS Forwarder Docker 测试"
echo "============================"
# 检查服务是否运行
check_service() {
if ! docker ps --filter "name=sms-forwarder" --filter "status=running" | grep -q sms-forwarder; then
echo "❌ SMS Forwarder 服务未运行"
echo "请先运行: docker-compose up -d"
exit 1
fi
echo "✅ 服务正在运行"
}
# 获取 API 密钥
get_api_key() {
if [ -f "config.yaml" ]; then
API_KEY=$(grep "api_key:" config.yaml | awk '{print $2}' | tr -d '"')
if [ -z "$API_KEY" ]; then
echo "❌ 无法从配置文件获取 API 密钥"
exit 1
fi
echo "✅ 获取到 API 密钥"
else
echo "❌ 配置文件不存在"
exit 1
fi
}
# 测试健康检查
test_health() {
echo "🔍 测试健康检查..."
if curl -f http://localhost:12152/health &> /dev/null; then
echo "✅ 健康检查通过"
else
echo "❌ 健康检查失败"
return 1
fi
}
# 测试状态接口
test_status() {
echo "📊 测试状态接口..."
STATUS=$(curl -s http://localhost:12152/status)
if [ $? -eq 0 ]; then
echo "✅ 状态接口正常"
echo "服务状态: $STATUS"
else
echo "❌ 状态接口异常"
return 1
fi
}
# 测试通知发送
test_notification() {
echo "📱 测试通知发送..."
RESPONSE=$(curl -s -X POST "http://localhost:12152/notify/simple" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"$API_KEY\",
\"content\": \"Docker 测试通知 - $(date)\",
\"sender\": \"Docker 测试\"
}")
if echo "$RESPONSE" | grep -q '"success":true'; then
echo "✅ 通知发送成功"
echo "响应: $RESPONSE"
else
echo "❌ 通知发送失败"
echo "响应: $RESPONSE"
return 1
fi
}
# 显示容器信息
show_container_info() {
echo ""
echo "📋 容器信息:"
docker-compose ps
echo ""
echo "💾 资源使用:"
docker stats sms-forwarder --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"
}
# 主函数
main() {
check_service
get_api_key
echo ""
echo "开始测试..."
test_health
test_status
test_notification
echo ""
echo "🎉 所有测试通过!"
show_container_info
echo ""
echo "如果你的 Android 设备收到了通知,说明 Docker 部署成功!"
}
# 运行主函数
main "$@"