67 lines
4.1 KiB
PowerShell
67 lines
4.1 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
$project = 'C:\zyt\identity-admin'
|
|
$fixture = Join-Path $project ('.test-runtime/mysql-' + [Guid]::NewGuid().ToString('N'))
|
|
$data = Join-Path $fixture 'data'
|
|
$dbBin = 'C:\Users\isard\.cache\zyt-tools\mariadb\bin'
|
|
$php = 'C:\Users\isard\.cache\zyt-tools\php\php.exe'
|
|
$dbProcess = $null
|
|
$apiProcess = $null
|
|
$envKeys = @('MYSQL_PWD','IAM_DB_DRIVER','IAM_DB_HOST','IAM_DB_PORT','IAM_DB_DATABASE','IAM_DB_USERNAME','IAM_DB_PASSWORD','IAM_RUNTIME_PATH','IAM_ADMIN_PASSWORD','IAM_TEST_BASE_URL','IAM_TEST_ACCOUNT','IAM_TEST_PASSWORD','IAM_TEST_ALLOW_WRITE')
|
|
$saved = @{}
|
|
foreach ($key in $envKeys) { $saved[$key] = [Environment]::GetEnvironmentVariable($key, 'Process') }
|
|
New-Item -ItemType Directory -Path $fixture -Force | Out-Null
|
|
try {
|
|
$env:MYSQL_PWD = [Guid]::NewGuid().ToString('N')
|
|
& (Join-Path $dbBin 'mariadb-install-db.exe') "--datadir=$data" "--password=$env:MYSQL_PWD" --port=33319 --silent
|
|
if ($LASTEXITCODE -ne 0) { throw '独立 MariaDB 初始化失败' }
|
|
$dbProcess = Start-Process -FilePath (Join-Path $dbBin 'mysqld.exe') -ArgumentList @("--defaults-file=$data/my.ini", '--bind-address=127.0.0.1', '--port=33319', '--console') -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $fixture 'mysql.log') -RedirectStandardError (Join-Path $fixture 'mysql-error.log')
|
|
$ready = $false
|
|
for ($i=0; $i -lt 60; $i++) {
|
|
Start-Sleep -Milliseconds 200
|
|
if ($dbProcess.HasExited) { throw "MariaDB 启动失败:$fixture" }
|
|
& (Join-Path $dbBin 'mysqladmin.exe') --host=127.0.0.1 --port=33319 --user=root --silent ping 2>$null
|
|
if ($LASTEXITCODE -eq 0) { $ready = $true; break }
|
|
}
|
|
if (-not $ready) { throw 'MariaDB 未就绪' }
|
|
'CREATE DATABASE identity_fixture CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;' | & (Join-Path $dbBin 'mysql.exe') --host=127.0.0.1 --port=33319 --user=root
|
|
if ($LASTEXITCODE -ne 0) { throw '独立 MySQL 测试库创建失败' }
|
|
$env:IAM_DB_DRIVER = 'mysql'
|
|
$env:IAM_DB_HOST = '127.0.0.1'
|
|
$env:IAM_DB_PORT = '33319'
|
|
$env:IAM_DB_DATABASE = 'identity_fixture'
|
|
$env:IAM_DB_USERNAME = 'root'
|
|
$env:IAM_DB_PASSWORD = $env:MYSQL_PWD
|
|
$env:IAM_RUNTIME_PATH = $fixture
|
|
$env:IAM_ADMIN_PASSWORD = 'Aa1!' + [Guid]::NewGuid().ToString('N')
|
|
$env:IAM_TEST_ACCOUNT = 'verify_admin'
|
|
$env:IAM_TEST_PASSWORD = $env:IAM_ADMIN_PASSWORD
|
|
$env:IAM_TEST_BASE_URL = 'http://127.0.0.1:8090/adminapi'
|
|
$env:IAM_TEST_ALLOW_WRITE = '1'
|
|
Push-Location -LiteralPath (Join-Path $project 'server')
|
|
try {
|
|
& $php scripts/init.php --account=verify_admin --name=验证管理员
|
|
if ($LASTEXITCODE -ne 0) { throw 'MySQL 模式 API 初始化失败' }
|
|
} finally { Pop-Location }
|
|
$apiProcess = Start-Process -FilePath $php -ArgumentList @('-S', '127.0.0.1:8090', '-t', 'public', 'public/router.php') -WorkingDirectory (Join-Path $project 'server') -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $fixture 'api.log') -RedirectStandardError (Join-Path $fixture 'api-error.log')
|
|
$ready = $false
|
|
for ($i=0; $i -lt 30; $i++) {
|
|
Start-Sleep -Milliseconds 200
|
|
if ($apiProcess.HasExited) { throw 'MySQL 模式 API 启动失败' }
|
|
try {
|
|
$health = Invoke-RestMethod -Uri "$env:IAM_TEST_BASE_URL/health" -TimeoutSec 2
|
|
if ($health.code -eq 1) { $ready = $true; break }
|
|
} catch { }
|
|
}
|
|
if (-not $ready) { throw 'MySQL 模式 API 未就绪' }
|
|
node --test (Join-Path $project 'tests/http.test.mjs')
|
|
if ($LASTEXITCODE -ne 0) { throw "MySQL 模式 HTTP 测试失败:$fixture" }
|
|
Write-Output 'MySQL-compatible MariaDB HTTP verification passed.'
|
|
} finally {
|
|
if ($null -ne $apiProcess -and -not $apiProcess.HasExited) { Stop-Process -Id $apiProcess.Id }
|
|
if ($null -ne $dbProcess -and -not $dbProcess.HasExited) {
|
|
& (Join-Path $dbBin 'mysqladmin.exe') --host=127.0.0.1 --port=33319 --user=root shutdown 2>$null
|
|
if (-not $dbProcess.WaitForExit(10000)) { Stop-Process -Id $dbProcess.Id }
|
|
}
|
|
foreach ($key in $envKeys) { [Environment]::SetEnvironmentVariable($key, $saved[$key], 'Process') }
|
|
}
|