Files
git.stella-ops.org/devops/compose/scripts/router-routeprefix-smoke.ps1

103 lines
3.2 KiB
PowerShell

param(
[string]$RouterConfigPath = "devops/compose/router-gateway-local.json",
[string]$OpenApiPath = "devops/compose/openapi_current.json",
[string]$GatewayBaseUrl = "https://127.1.0.1",
[ValidateSet("Microservice", "ReverseProxy", "StaticFiles")]
[string]$RouteType = "Microservice",
[string]$OutputCsv = "devops/compose/openapi_routeprefix_smoke.csv"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
function Get-JsonFromFile {
param([Parameter(Mandatory = $true)][string]$Path)
if (-not (Test-Path -LiteralPath $Path)) {
throw "File not found: $Path"
}
return Get-Content -LiteralPath $Path -Raw | ConvertFrom-Json
}
function Get-OpenApiPathMap {
param([Parameter(Mandatory = $true)]$OpenApi)
$map = @{}
foreach ($prop in $OpenApi.paths.PSObject.Properties) {
$map[$prop.Name] = $prop.Value
}
return $map
}
function Get-HttpStatusCode {
param(
[Parameter(Mandatory = $true)][string]$Url
)
$statusText = (& curl.exe -k -s -o NUL -w "%{http_code}" $Url).Trim()
if ($statusText -match "^\d{3}$") {
return [int]$statusText
}
return -1
}
$routerConfig = Get-JsonFromFile -Path $RouterConfigPath
$openApi = Get-JsonFromFile -Path $OpenApiPath
$openApiPathMap = Get-OpenApiPathMap -OpenApi $openApi
$openApiPaths = @($openApiPathMap.Keys)
$routes = @($routerConfig.Gateway.Routes | Where-Object { $_.Type -eq $RouteType })
$rows = New-Object System.Collections.Generic.List[object]
foreach ($route in $routes) {
$prefix = [string]$route.Path
$matches = @()
foreach ($candidate in $openApiPaths) {
if ($candidate.StartsWith($prefix, [System.StringComparison]::OrdinalIgnoreCase)) {
$operation = $openApiPathMap[$candidate]
if ($null -ne $operation -and $operation.PSObject.Properties.Match("get").Count -gt 0) {
$matches += $candidate
}
}
}
$selectedPath = $null
if ($matches.Count -gt 0) {
$selectedPath = ($matches | Sort-Object `
@{ Expression = { $_ -match '\{[^}]+\}' } }, `
@{ Expression = { $_ -match '(^|/)(startupz|readyz|livez)$' } }, `
@{ Expression = { $_.Length } }, `
@{ Expression = { $_ } })[0]
}
$status = $null
if ($null -ne $selectedPath) {
$url = "$GatewayBaseUrl$selectedPath"
$status = Get-HttpStatusCode -Url $url
}
$rows.Add([pscustomobject]@{
RouteType = $RouteType
RoutePath = $prefix
RouteTarget = [string]$route.TranslatesTo
SelectedOpenApiPath = $selectedPath
StatusCode = $status
})
}
$rows | Export-Csv -LiteralPath $OutputCsv -NoTypeInformation -Encoding UTF8
$statusSummary = $rows |
Where-Object { $null -ne $_.StatusCode } |
Group-Object -Property StatusCode |
Sort-Object { [int]$_.Name } |
ForEach-Object { "$($_.Name)=$($_.Count)" }
Write-Host "routes_total=$($routes.Count)"
Write-Host "routes_with_selected_get=$(@($rows | Where-Object { $_.SelectedOpenApiPath }).Count)"
Write-Host "status_summary=$($statusSummary -join ',')"
Write-Host "output_csv=$OutputCsv"