|
| 1 | +# Executes the given block starting a dummy Sentry server that collects and logs requests. |
| 2 | +# The block is given the server URL as a first argument. |
| 3 | +# Returns the dummy server logs. |
| 4 | + |
| 5 | +$ServerUri = "http://127.0.0.1:8000" |
| 6 | + |
| 7 | +class InvokeSentryResult |
| 8 | +{ |
| 9 | + [string[]]$ServerStdOut |
| 10 | + [string[]]$ServerStdErr |
| 11 | + [string[]]$ScriptOutput |
| 12 | + |
| 13 | + # It is common to test debug files uploaded to the server so this function gives you a list. |
| 14 | + [string[]]UploadedDebugFiles() |
| 15 | + { |
| 16 | + $prefix = "upload-dif:" |
| 17 | + return @($this.ServerStdOut | Where-Object { $_.StartsWith($prefix) } | ForEach-Object { $_.Substring($prefix.Length).Trim() }) |
| 18 | + } |
| 19 | + |
| 20 | + [bool]HasErrors() |
| 21 | + { |
| 22 | + return $this.ServerStdErr.Length -gt 0 |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function IsNullOrEmpty([string] $value) |
| 27 | +{ |
| 28 | + "$value".Trim().Length -eq 0 |
| 29 | +} |
| 30 | + |
| 31 | +function OutputToArray($output, [string] $uri = $null) |
| 32 | +{ |
| 33 | + if ($output -isnot [system.array]) |
| 34 | + { |
| 35 | + $output = ("$output".Trim() -replace "`r`n", "`n") -split "`n" |
| 36 | + } |
| 37 | + |
| 38 | + if (!(IsNullOrEmpty $uri)) |
| 39 | + { |
| 40 | + $output = $output -replace $uri, "<ServerUri>" |
| 41 | + } |
| 42 | + $output | ForEach-Object { "$_".Trim() } |
| 43 | +} |
| 44 | + |
| 45 | +function RunApiServer([string] $ServerScript, [string] $Uri = $ServerUri) |
| 46 | +{ |
| 47 | + $result = "" | Select-Object -Property process, outFile, errFile, stop, output, dispose |
| 48 | + Write-Host "Starting the $ServerScript on $Uri" -ForegroundColor DarkYellow |
| 49 | + $stopwatch = [system.diagnostics.stopwatch]::StartNew() |
| 50 | + |
| 51 | + $result.outFile = New-TemporaryFile |
| 52 | + $result.errFile = New-TemporaryFile |
| 53 | + |
| 54 | + $result.process = Start-Process "python3" -ArgumentList @("$PSScriptRoot/$ServerScript.py", $Uri) ` |
| 55 | + -NoNewWindow -PassThru -RedirectStandardOutput $result.outFile -RedirectStandardError $result.errFile |
| 56 | + |
| 57 | + $out = New-Object InvokeSentryResult |
| 58 | + $out.ServerStdOut = @() |
| 59 | + $out.ServerStdErr = @() |
| 60 | + |
| 61 | + # We must reassign functions as variables to make them available in a block scope together with GetNewClosure(). |
| 62 | + $OutputToArray = { OutputToArray $args[0] $args[1] } |
| 63 | + $IsNullOrEmpty = { IsNullOrEmpty $args[0] } |
| 64 | + |
| 65 | + $result.dispose = { |
| 66 | + $result.stop.Invoke() |
| 67 | + |
| 68 | + $stdout = Get-Content $result.outFile -Raw |
| 69 | + Write-Host "Server stdout:" -ForegroundColor Yellow |
| 70 | + Write-Host $stdout |
| 71 | + |
| 72 | + $out.ServerStdOut += & $OutputToArray $stdout $Uri |
| 73 | + |
| 74 | + $stderr = Get-Content $result.errFile -Raw |
| 75 | + if (!(& $IsNullOrEmpty $stderr)) |
| 76 | + { |
| 77 | + Write-Host "Server stderr:" -ForegroundColor Yellow |
| 78 | + Write-Host $stderr |
| 79 | + $out.ServerStdErr += & $OutputToArray $stderr $Uri |
| 80 | + } |
| 81 | + |
| 82 | + Remove-Item $result.outFile -ErrorAction Continue |
| 83 | + Remove-Item $result.errFile -ErrorAction Continue |
| 84 | + return $out |
| 85 | + }.GetNewClosure() |
| 86 | + |
| 87 | + $result.stop = { |
| 88 | + # Stop the HTTP server |
| 89 | + Write-Host "Stopping the $ServerScript ... " -NoNewline |
| 90 | + try |
| 91 | + { |
| 92 | + Write-Host (Invoke-WebRequest -Uri "$Uri/STOP").StatusDescription |
| 93 | + } |
| 94 | + catch |
| 95 | + { |
| 96 | + Write-Host "/STOP request failed: $_ - killing the server process instead" |
| 97 | + $result.process | Stop-Process -Force -ErrorAction SilentlyContinue |
| 98 | + } |
| 99 | + $result.process | Wait-Process -Timeout 10 -ErrorAction Continue |
| 100 | + $result.stop = {} |
| 101 | + }.GetNewClosure() |
| 102 | + |
| 103 | + $startupFailed = $false |
| 104 | + while ($true) |
| 105 | + { |
| 106 | + Start-Sleep -Milliseconds 100 |
| 107 | + try |
| 108 | + { |
| 109 | + if ((Invoke-WebRequest -Uri "$Uri/_check" -SkipHttpErrorCheck -Method Head).StatusCode -eq 999) |
| 110 | + { |
| 111 | + $msg = "Server started successfully in $($stopwatch.ElapsedMilliseconds) ms." |
| 112 | + Write-Host $additionalOutput -ForegroundColor Green |
| 113 | + $out.ServerStdOut += $msg |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + catch |
| 118 | + {} |
| 119 | + if ($stopwatch.ElapsedMilliseconds -gt 60000) |
| 120 | + { |
| 121 | + $msg = "Server startup timed out." |
| 122 | + Write-Warning $msg |
| 123 | + $out.ServerStdErr += $msg |
| 124 | + $startupFailed = $true; |
| 125 | + break; |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + Write-Host "Waiting for server to become available..." |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if ($result.process.HasExited -or $startupFailed) |
| 134 | + { |
| 135 | + $result.stop.Invoke() |
| 136 | + $result.dispose.Invoke() |
| 137 | + throw Write-Host "Couldn't start the $ServerScript" |
| 138 | + } |
| 139 | + |
| 140 | + return $result |
| 141 | +} |
| 142 | + |
| 143 | +function Invoke-SentryServer([ScriptBlock] $Callback) |
| 144 | +{ |
| 145 | + # start the server |
| 146 | + $httpServer = RunApiServer "sentry-server" |
| 147 | + |
| 148 | + $result = $null |
| 149 | + $output = $null |
| 150 | + try |
| 151 | + { |
| 152 | + # run the test |
| 153 | + $output = & $Callback $ServerUri |
| 154 | + } |
| 155 | + finally |
| 156 | + { |
| 157 | + $result = $httpServer.dispose.Invoke()[0] |
| 158 | + } |
| 159 | + |
| 160 | + if ($null -ne $result) |
| 161 | + { |
| 162 | + $result.ScriptOutput = OutputToArray $output |
| 163 | + } |
| 164 | + return $result |
| 165 | +} |
| 166 | + |
| 167 | +Export-ModuleMember -Function Invoke-SentryServer |
0 commit comments