forked from mtusi2026/mtusi2026lab7
75 lines
4.0 KiB
PowerShell
75 lines
4.0 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$EnvFile = '.env',
|
|
[switch]$SkipUnitTests
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
Set-Location (Join-Path $PSScriptRoot '..')
|
|
|
|
function Convert-DurationToMilliseconds([string]$Value) {
|
|
if($Value -notmatch '^([0-9]+)(ms|s|m)$'){ throw "Unsupported duration format: $Value" }
|
|
$number = [long]$Matches[1]
|
|
switch($Matches[2]) {
|
|
'ms' { return $number }
|
|
's' { return $number * 1000 }
|
|
'm' { return $number * 60000 }
|
|
}
|
|
}
|
|
|
|
$configText = & docker compose --env-file $EnvFile config --format json
|
|
if($LASTEXITCODE -ne 0){ throw 'docker compose config failed' }
|
|
$config = ($configText -join "`n") | ConvertFrom-Json
|
|
$worker = $config.services.worker
|
|
if($null -eq $worker){ throw 'worker service is missing' }
|
|
|
|
$cpu = [double]$worker.cpus
|
|
$memory = [long]$worker.mem_limit
|
|
$pids = [long]$worker.pids_limit
|
|
$environment = $worker.environment
|
|
$concurrency = [int]$environment.WORKER_CONCURRENCY
|
|
$prefetch = [int]$environment.WORKER_PREFETCH
|
|
$pixelLimit = [long]$environment.WORKER_MAX_IMAGE_PIXELS
|
|
$memoryBudget = [long]$environment.WORKER_MEMORY_BUDGET_BYTES
|
|
$jobTimeoutMs = Convert-DurationToMilliseconds ([string]$environment.JOB_TIMEOUT)
|
|
$leaseMs = Convert-DurationToMilliseconds ([string]$environment.JOB_LEASE_DURATION)
|
|
$memoryWaitMs = Convert-DurationToMilliseconds ([string]$environment.WORKER_MEMORY_WAIT)
|
|
|
|
if($cpu -le 0 -or $cpu -gt 1.0){ throw "worker CPU limit must be in (0,1], found $cpu" }
|
|
if($memory -ne 1GB){ throw "worker memory limit must be 1 GiB, found $memory bytes" }
|
|
if($pids -le 0 -or $pids -gt 256){ throw "worker PID limit is missing or excessive: $pids" }
|
|
if($concurrency -lt 1 -or $concurrency -gt 2){ throw "worker concurrency must be 1..2, found $concurrency" }
|
|
if($prefetch -lt 1 -or $prefetch -gt $concurrency){ throw "worker prefetch exceeds bounded concurrency" }
|
|
if($pixelLimit -ne 40000000){ throw "worker decoded-pixel limit must be 40,000,000" }
|
|
if($memoryBudget -le 0 -or $memoryBudget -gt 512MB -or $memoryBudget -ge $memory){
|
|
throw "worker in-process memory budget is not bounded below the container limit"
|
|
}
|
|
if($jobTimeoutMs -le 0 -or $jobTimeoutMs -gt 60000){ throw "worker job timeout exceeds 60 seconds" }
|
|
if($leaseMs -le $jobTimeoutMs){ throw "worker lease must outlive the processing timeout" }
|
|
if($memoryWaitMs -lt 0 -or $memoryWaitMs -gt 2000){ throw "worker memory permit wait exceeds 2 seconds" }
|
|
if($worker.read_only -ne $true -or $worker.cap_drop -notcontains 'ALL'){
|
|
throw 'worker must use read-only root and drop ALL capabilities'
|
|
}
|
|
|
|
$containerId = (& docker compose --env-file $EnvFile ps -q worker | Select-Object -First 1).Trim()
|
|
if(-not $containerId){ throw 'running worker container is required for runtime limit verification' }
|
|
$inspect = (& docker inspect $containerId | ConvertFrom-Json)[0]
|
|
if([long]$inspect.HostConfig.Memory -ne $memory){ throw 'runtime worker memory limit differs from rendered Compose' }
|
|
if([long]$inspect.HostConfig.NanoCpus -ne [long]($cpu * 1000000000)){ throw 'runtime worker CPU limit differs from rendered Compose' }
|
|
if(-not $inspect.HostConfig.ReadonlyRootfs -or $inspect.HostConfig.CapDrop -notcontains 'ALL'){
|
|
throw 'runtime worker filesystem/capability bounds differ from rendered Compose'
|
|
}
|
|
|
|
if(-not $SkipUnitTests) {
|
|
& .\gradlew.bat test `
|
|
--tests com.example.mtusi_messenger.worker.ImageVariantProcessorTest `
|
|
--tests com.example.mtusi_messenger.worker.AttachmentWorkerLimitsTest `
|
|
--no-daemon --console=plain
|
|
if($LASTEXITCODE -ne 0){ throw 'worker resource-bound unit tests failed' }
|
|
Write-Host 'PASS: image memory/pixel rejection and processing-timeout retry passed as executable unit tests.'
|
|
} else {
|
|
Write-Host 'INFO: worker unit tests skipped; only rendered/runtime bounds were verified.'
|
|
}
|
|
|
|
Write-Host "PASS: worker CPU=$cpu, RAM=$memory bytes, pids=$pids, concurrency=$concurrency, prefetch=$prefetch, pixels=$pixelLimit, image budget=$memoryBudget bytes, job timeout=$jobTimeoutMs ms and memory wait=$memoryWaitMs ms are bounded."
|