Files
OpenSourceTools/tools/smoke-test/run-smoke-test.ps1
T

105 lines
4.6 KiB
PowerShell

# OpenSourceTools 冒烟测试脚本 (Paper 1.21.11)
# 流程: 校验构建产物 -> 准备服务器目录 -> 启动 -> 等待 Done -> 校验插件日志 -> 优雅停止 -> 校验卸载日志
param(
[int]$BootTimeoutSec = 300,
[int]$ShutdownTimeoutSec = 120
)
$ErrorActionPreference = 'Stop'
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path # tools\smoke-test
$ProjectRoot = Split-Path -Parent (Split-Path -Parent $Root) # 项目根
$CacheDir = Join-Path $env:USERPROFILE '.ost-smoke-cache'
$ServerDir = Join-Path $env:TEMP 'ost-smoke-test\server'
$BuiltJar = Join-Path $ProjectRoot 'target\OpenSourceTools-2.1.0.jar'
if (-not (Test-Path $BuiltJar)) { Write-Output "FATAL: 找不到构建产物 $BuiltJar"; exit 2 }
$PaperJar = Join-Path $CacheDir 'paper-1.21.11-132.jar'
$PapiJar = Join-Path $CacheDir 'PlaceholderAPI-2.12.3.jar'
if (-not (Test-Path $PaperJar)) { Write-Output "FATAL: 缓存缺少 $PaperJar"; exit 2 }
if (-not (Test-Path $PapiJar)) { Write-Output "FATAL: 缓存缺少 $PapiJar"; exit 2 }
# 准备服务器目录(每次全新)
if (Test-Path $ServerDir) { Remove-Item -Recurse -Force $ServerDir -ErrorAction SilentlyContinue }
New-Item -ItemType Directory -Force -Path (Join-Path $ServerDir 'plugins') | Out-Null
Copy-Item -Force (Join-Path $Root 'server.properties') (Join-Path $ServerDir 'server.properties')
Copy-Item -Force (Join-Path $Root 'eula.txt') (Join-Path $ServerDir 'eula.txt')
Copy-Item -Force $PaperJar (Join-Path $ServerDir 'paper.jar')
Copy-Item -Force $PapiJar (Join-Path $ServerDir 'plugins\PlaceholderAPI.jar')
Copy-Item -Force $BuiltJar (Join-Path $ServerDir 'plugins\OpenSourceTools.jar')
Write-Output "启动服务器: paper.jar + PlaceholderAPI.jar + OpenSourceTools.jar"
$psi = [System.Diagnostics.ProcessStartInfo]::new()
$psi.FileName = 'java'
$psi.Arguments = "-Xmx1024M -XX:+UseG1GC -jar `"$((Join-Path $ServerDir 'paper.jar'))`" nogui"
$psi.WorkingDirectory = $ServerDir
$psi.UseShellExecute = $false
$psi.RedirectStandardInput = $true
$psi.RedirectStandardOutput = $false
$psi.RedirectStandardError = $false
$proc = [System.Diagnostics.Process]::Start($psi)
Write-Output "服务器进程已启动 PID=$($proc.Id)"
$logFile = Join-Path $ServerDir 'logs\latest.log'
$booted = $false
$deadline = (Get-Date).AddSeconds($BootTimeoutSec)
while (-not $proc.HasExited -and (Get-Date) -lt $deadline) {
Start-Sleep -Seconds 2
if (Test-Path $logFile) {
$tail = Get-Content $logFile -Raw -Encoding UTF8 -ErrorAction SilentlyContinue
if ($tail -match 'Done \(') { $booted = $true; break }
if ($tail -match 'FAILED TO BIND TO PORT|Exception in thread "main"') { break }
}
}
if (-not $booted) {
Write-Output 'FAIL: 服务器未能在超时时间内完成启动'
if (Test-Path $logFile) { Get-Content $logFile -Tail 40 -Encoding UTF8 }
if (-not $proc.HasExited) { $proc.Kill(); $proc.WaitForExit(15000) }
exit 1
}
Write-Output '服务器启动完成 (Done),正在发送 stop...'
Start-Sleep -Seconds 2
$proc.StandardInput.WriteLine('stop')
$proc.StandardInput.Flush()
if (-not $proc.WaitForExit($ShutdownTimeoutSec * 1000)) {
Write-Output 'FAIL: 服务器未能优雅停止'
$proc.Kill()
exit 1
}
Write-Output "服务器已退出, ExitCode=$($proc.ExitCode)"
# ---------- 日志校验 ----------
$log = Get-Content $logFile -Raw -Encoding UTF8
$errors = @()
function Check-Pass($name, $pattern) {
if ($log -match $pattern) { Write-Output "PASS: $name" }
else { Write-Output "FAIL: $name"; $script:errors += $name }
}
function Check-Absent($name, $pattern) {
if ($log -match $pattern) { Write-Output "FAIL: $name"; $script:errors += $name }
else { Write-Output "PASS: $name" }
}
Check-Pass '检测到PlaceholderAPI前置' '已检测到PlaceholderAPI前置'
Check-Pass '插件启动成功' '插件启动成功'
Check-Pass '插件已卸载' '插件已卸载'
Check-Absent '无插件加载错误' 'Error occurred while enabling OpenSourceTools|Could not load ''plugins/OpenSourceTools|Invalid plugin.yml|Invalid description'
if ($proc.ExitCode -ne 0) { Write-Output "FAIL: 服务器退出码非0 ($($proc.ExitCode))"; $errors += "退出码$($proc.ExitCode)" }
if ($errors.Count -eq 0) {
Write-Output '==================== 冒烟测试全部通过 ===================='
exit 0
} else {
Write-Output "==================== 冒烟测试失败 ($($errors -join ', ')) ===================="
Write-Output '----- 关键日志片段 -----'
Get-Content $logFile -Encoding UTF8 | Select-String -Pattern 'OpenSourceTools|PlaceholderAPI|ERROR|Caused by' | Select-Object -First 40
exit 1
}