94 lines
3.2 KiB
PowerShell
94 lines
3.2 KiB
PowerShell
param(
|
|
[string] $ApiExecutablePath,
|
|
[string] $ApiContentRoot
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$workspaceRoot = Split-Path -Parent $PSScriptRoot
|
|
$apiExecutable = if ([string]::IsNullOrWhiteSpace($ApiExecutablePath)) {
|
|
Join-Path $workspaceRoot 'src/Jiaowu.Api/bin/Debug/net10.0/Jiaowu.Api.exe'
|
|
}
|
|
else {
|
|
[System.IO.Path]::GetFullPath($ApiExecutablePath, $workspaceRoot)
|
|
}
|
|
$apiWorkingDirectory = if ([string]::IsNullOrWhiteSpace($ApiContentRoot)) {
|
|
Join-Path $workspaceRoot 'src/Jiaowu.Api'
|
|
}
|
|
else {
|
|
[System.IO.Path]::GetFullPath($ApiContentRoot, $workspaceRoot)
|
|
}
|
|
$stdoutPath = Join-Path $env:TEMP 'jiaowu-api-smoke.out.log'
|
|
$stderrPath = Join-Path $env:TEMP 'jiaowu-api-smoke.err.log'
|
|
$env:ASPNETCORE_ENVIRONMENT = 'Development'
|
|
$env:ASPNETCORE_URLS = 'http://localhost:5255'
|
|
$startParameters = @{
|
|
FilePath = $apiExecutable
|
|
WorkingDirectory = $apiWorkingDirectory
|
|
WindowStyle = 'Hidden'
|
|
RedirectStandardOutput = $stdoutPath
|
|
RedirectStandardError = $stderrPath
|
|
PassThru = $true
|
|
}
|
|
$process = Start-Process @startParameters
|
|
|
|
try {
|
|
$healthy = $false
|
|
for ($attempt = 0; $attempt -lt 30; $attempt++) {
|
|
Start-Sleep -Milliseconds 500
|
|
try {
|
|
$health = Invoke-RestMethod -Uri 'http://localhost:5255/health' -TimeoutSec 2
|
|
$healthy = $true
|
|
break
|
|
}
|
|
catch {
|
|
# The API may still be starting.
|
|
}
|
|
}
|
|
|
|
if (-not $healthy) {
|
|
Get-Content -LiteralPath $stdoutPath -ErrorAction SilentlyContinue
|
|
Get-Content -LiteralPath $stderrPath -ErrorAction SilentlyContinue
|
|
throw 'API did not become healthy.'
|
|
}
|
|
|
|
$loginBody = @{
|
|
userName = 'admin'
|
|
password = 'Admin@123456'
|
|
} | ConvertTo-Json
|
|
$loginParameters = @{
|
|
Method = 'Post'
|
|
Uri = 'http://localhost:5255/api/auth/login'
|
|
ContentType = 'application/json'
|
|
Body = $loginBody
|
|
}
|
|
$login = Invoke-RestMethod @loginParameters
|
|
$headers = @{ Authorization = "Bearer $($login.token)" }
|
|
$dashboard = Invoke-RestMethod -Uri 'http://localhost:5255/api/dashboard' -Headers $headers
|
|
$campuses = Invoke-RestMethod -Uri 'http://localhost:5255/api/base-data/campuses' -Headers $headers
|
|
$frontend = Invoke-WebRequest -Uri 'http://localhost:5255/' -TimeoutSec 5
|
|
$spaFallback = Invoke-WebRequest -Uri 'http://localhost:5255/base-data' -TimeoutSec 5
|
|
$unknownApiParameters = @{
|
|
Uri = 'http://localhost:5255/api/does-not-exist'
|
|
SkipHttpErrorCheck = $true
|
|
TimeoutSec = 5
|
|
}
|
|
$unknownApi = Invoke-WebRequest @unknownApiParameters
|
|
|
|
[pscustomobject]@{
|
|
Health = $health.status
|
|
Database = $health.database
|
|
User = $login.user.displayName
|
|
Term = $dashboard.currentTerm.name
|
|
Campuses = @($campuses).Count
|
|
StaticIndex = $frontend.Content.Contains('明序教务管理系统')
|
|
SpaFallback = $spaFallback.StatusCode
|
|
ApiNotFound = $unknownApi.StatusCode
|
|
} | Format-List
|
|
}
|
|
finally {
|
|
if (-not $process.HasExited) {
|
|
Stop-Process -Id $process.Id -Force
|
|
}
|
|
}
|