|
| 1 | +param( |
| 2 | + [string]$ServiceName = "LanternSvc", |
| 3 | + [string]$ServiceExe = "build/windows/x64/runner/Release/lanternsvc.exe", |
| 4 | + [string]$InstallerPath = "", |
| 5 | + [string]$TokenPath = "C:\ProgramData\Lantern\ipc-token", |
| 6 | + [string]$TestPath = "integration_test/vpn/windows_connect_smoke_test.dart", |
| 7 | + [int]$WaitSeconds = 30, |
| 8 | + [int]$InstallerTimeoutSeconds = 180, |
| 9 | + [int]$UninstallTimeoutSeconds = 180, |
| 10 | + [int]$HeartbeatSeconds = 15, |
| 11 | + [switch]$EnableIpCheck, |
| 12 | + [switch]$UseInstaller |
| 13 | +) |
| 14 | + |
| 15 | +$ErrorActionPreference = "Stop" |
| 16 | + |
| 17 | +function Write-Step { |
| 18 | + param([string]$Message) |
| 19 | + Write-Host ("[{0}] {1}" -f (Get-Date -Format "HH:mm:ss"), $Message) |
| 20 | +} |
| 21 | + |
| 22 | +function Wait-ProcessWithTimeout { |
| 23 | + param( |
| 24 | + [Parameter(Mandatory = $true)] |
| 25 | + [System.Diagnostics.Process]$Process, |
| 26 | + [int]$TimeoutSeconds, |
| 27 | + [int]$PulseSeconds, |
| 28 | + [string]$Description |
| 29 | + ) |
| 30 | + |
| 31 | + $elapsedSeconds = 0 |
| 32 | + while (-not $Process.HasExited) { |
| 33 | + if ($elapsedSeconds -ge $TimeoutSeconds) { |
| 34 | + try { |
| 35 | + Stop-Process -Id $Process.Id -Force -ErrorAction SilentlyContinue |
| 36 | + } catch { |
| 37 | + } |
| 38 | + throw "$Description timed out after $TimeoutSeconds seconds" |
| 39 | + } |
| 40 | + |
| 41 | + if ($elapsedSeconds -gt 0 -and ($elapsedSeconds % $PulseSeconds) -eq 0) { |
| 42 | + Write-Step "$Description still running ($elapsedSeconds/$TimeoutSeconds s)" |
| 43 | + } |
| 44 | + |
| 45 | + Start-Sleep -Seconds 1 |
| 46 | + $elapsedSeconds++ |
| 47 | + $Process.Refresh() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function Invoke-ProcessWithTimeout { |
| 52 | + param( |
| 53 | + [string]$FilePath, |
| 54 | + [string[]]$ArgumentList, |
| 55 | + [int]$TimeoutSeconds, |
| 56 | + [int]$PulseSeconds, |
| 57 | + [string]$Description |
| 58 | + ) |
| 59 | + |
| 60 | + $arguments = $ArgumentList -join " " |
| 61 | + Write-Step ("{0}: {1} {2}" -f $Description, $FilePath, $arguments) |
| 62 | + $proc = Start-Process -FilePath $FilePath -ArgumentList $ArgumentList -PassThru |
| 63 | + Wait-ProcessWithTimeout -Process $proc -TimeoutSeconds $TimeoutSeconds -PulseSeconds $PulseSeconds -Description $Description |
| 64 | + $proc.Refresh() |
| 65 | + if ($proc.ExitCode -ne 0) { |
| 66 | + throw "$Description failed with exit code $($proc.ExitCode)" |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function Get-ServicePathName { |
| 71 | + param([string]$Name) |
| 72 | + |
| 73 | + $svc = Get-CimInstance Win32_Service -Filter "Name='$Name'" -ErrorAction SilentlyContinue |
| 74 | + if (-not $svc) { |
| 75 | + return $null |
| 76 | + } |
| 77 | + return $svc.PathName |
| 78 | +} |
| 79 | + |
| 80 | +function Get-ServiceExecutablePath { |
| 81 | + param([string]$PathName) |
| 82 | + |
| 83 | + if ([string]::IsNullOrWhiteSpace($PathName)) { |
| 84 | + return $null |
| 85 | + } |
| 86 | + $trimmed = $PathName.Trim() |
| 87 | + if ($trimmed.StartsWith('"')) { |
| 88 | + $end = $trimmed.IndexOf('"', 1) |
| 89 | + if ($end -gt 1) { |
| 90 | + return $trimmed.Substring(1, $end - 1) |
| 91 | + } |
| 92 | + } |
| 93 | + return ($trimmed -split '\s+')[0] |
| 94 | +} |
| 95 | + |
| 96 | +function Remove-ServiceIfPresent { |
| 97 | + param([string]$Name) |
| 98 | + |
| 99 | + if (Get-Service -Name $Name -ErrorAction SilentlyContinue) { |
| 100 | + Write-Step "Stopping existing Windows service $Name" |
| 101 | + sc.exe stop $Name | Out-Null |
| 102 | + Start-Sleep -Seconds 2 |
| 103 | + Write-Step "Deleting existing Windows service $Name" |
| 104 | + sc.exe delete $Name | Out-Null |
| 105 | + Start-Sleep -Seconds 2 |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function Wait-ServiceRunning { |
| 110 | + param( |
| 111 | + [string]$Name, |
| 112 | + [int]$TimeoutSeconds |
| 113 | + ) |
| 114 | + |
| 115 | + for ($i = 0; $i -lt $TimeoutSeconds; $i++) { |
| 116 | + $service = Get-Service -Name $Name -ErrorAction SilentlyContinue |
| 117 | + if ($service -and $service.Status -eq "Running") { |
| 118 | + Write-Step "Windows service $Name is Running" |
| 119 | + return |
| 120 | + } |
| 121 | + if ($i -gt 0 -and ($i % 5) -eq 0) { |
| 122 | + Write-Step "Waiting for service $Name to be Running ($i/$TimeoutSeconds s)" |
| 123 | + } |
| 124 | + Start-Sleep -Seconds 1 |
| 125 | + } |
| 126 | + |
| 127 | + sc.exe query $Name |
| 128 | + throw "Windows service did not reach Running state" |
| 129 | +} |
| 130 | + |
| 131 | +function Wait-TokenFile { |
| 132 | + param( |
| 133 | + [string]$Path, |
| 134 | + [int]$TimeoutSeconds |
| 135 | + ) |
| 136 | + |
| 137 | + for ($i = 0; $i -lt $TimeoutSeconds; $i++) { |
| 138 | + if (Test-Path $Path) { |
| 139 | + Write-Step "IPC token detected at $Path" |
| 140 | + return |
| 141 | + } |
| 142 | + if ($i -gt 0 -and ($i % 5) -eq 0) { |
| 143 | + Write-Step "Waiting for IPC token at $Path ($i/$TimeoutSeconds s)" |
| 144 | + } |
| 145 | + Start-Sleep -Seconds 1 |
| 146 | + } |
| 147 | + throw "IPC token file not found at $Path" |
| 148 | +} |
| 149 | + |
| 150 | +function Install-FromInstaller { |
| 151 | + param( |
| 152 | + [string]$Path, |
| 153 | + [int]$TimeoutSeconds, |
| 154 | + [string]$Name |
| 155 | + ) |
| 156 | + |
| 157 | + if ([string]::IsNullOrWhiteSpace($Path)) { |
| 158 | + throw "InstallerPath must be set when -UseInstaller is enabled" |
| 159 | + } |
| 160 | + $resolvedInstaller = (Resolve-Path $Path).Path |
| 161 | + Invoke-ProcessWithTimeout ` |
| 162 | + -FilePath $resolvedInstaller ` |
| 163 | + -ArgumentList @("/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART", "/SP-") ` |
| 164 | + -TimeoutSeconds $InstallerTimeoutSeconds ` |
| 165 | + -PulseSeconds $HeartbeatSeconds ` |
| 166 | + -Description "Running installer" |
| 167 | + |
| 168 | + Write-Step "Waiting for Windows service after installer" |
| 169 | + Wait-ServiceRunning -Name $Name -TimeoutSeconds $TimeoutSeconds |
| 170 | +} |
| 171 | + |
| 172 | +function Uninstall-FromInstalledService { |
| 173 | + param( |
| 174 | + [string]$Name |
| 175 | + ) |
| 176 | + |
| 177 | + $pathName = Get-ServicePathName -Name $Name |
| 178 | + $svcExe = Get-ServiceExecutablePath -PathName $pathName |
| 179 | + if (-not $svcExe) { |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + $installDir = Split-Path -Path $svcExe -Parent |
| 184 | + if (-not (Test-Path $installDir)) { |
| 185 | + return |
| 186 | + } |
| 187 | + |
| 188 | + $uninstaller = Get-ChildItem -Path $installDir -Filter "unins*.exe" -ErrorAction SilentlyContinue | |
| 189 | + Sort-Object -Property Name | |
| 190 | + Select-Object -First 1 |
| 191 | + |
| 192 | + if (-not $uninstaller) { |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + Invoke-ProcessWithTimeout ` |
| 197 | + -FilePath $uninstaller.FullName ` |
| 198 | + -ArgumentList @("/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART", "/SP-") ` |
| 199 | + -TimeoutSeconds $UninstallTimeoutSeconds ` |
| 200 | + -PulseSeconds $HeartbeatSeconds ` |
| 201 | + -Description "Running uninstaller" |
| 202 | +} |
| 203 | + |
| 204 | +try { |
| 205 | + if ($UseInstaller) { |
| 206 | + Write-Step "Smoke setup mode: installer" |
| 207 | + Install-FromInstaller -Path $InstallerPath -TimeoutSeconds $WaitSeconds -Name $ServiceName |
| 208 | + } else { |
| 209 | + Write-Step "Smoke setup mode: direct service binary" |
| 210 | + $resolvedServiceExe = (Resolve-Path $ServiceExe).Path |
| 211 | + Remove-ServiceIfPresent -Name $ServiceName |
| 212 | + Write-Step "Creating Windows service from $resolvedServiceExe" |
| 213 | + sc.exe create $ServiceName binPath= "`"$resolvedServiceExe`"" start= demand DisplayName= "Lantern Service (CI)" | Out-Null |
| 214 | + Write-Step "Starting Windows service $ServiceName" |
| 215 | + sc.exe start $ServiceName | Out-Null |
| 216 | + Wait-ServiceRunning -Name $ServiceName -TimeoutSeconds $WaitSeconds |
| 217 | + } |
| 218 | + |
| 219 | + Wait-TokenFile -Path $TokenPath -TimeoutSeconds $WaitSeconds |
| 220 | + |
| 221 | + $flutterArgs = @( |
| 222 | + "test", |
| 223 | + $TestPath, |
| 224 | + "-d", |
| 225 | + "windows", |
| 226 | + "--reporter=expanded", |
| 227 | + "--dart-define=DISABLE_SYSTEM_TRAY=true" |
| 228 | + ) |
| 229 | + if ($EnableIpCheck) { |
| 230 | + $flutterArgs += "--dart-define=ENABLE_IP_CHECK=true" |
| 231 | + } |
| 232 | + |
| 233 | + Write-Step ("Running Windows connect smoke test: flutter {0}" -f ($flutterArgs -join " ")) |
| 234 | + & flutter @flutterArgs |
| 235 | + if ($LASTEXITCODE -ne 0) { |
| 236 | + throw "Windows connect smoke test failed with exit code $LASTEXITCODE" |
| 237 | + } |
| 238 | +} |
| 239 | +finally { |
| 240 | + try { |
| 241 | + Write-Step "Starting cleanup" |
| 242 | + if ($UseInstaller) { |
| 243 | + Uninstall-FromInstalledService -Name $ServiceName |
| 244 | + } else { |
| 245 | + Remove-ServiceIfPresent -Name $ServiceName |
| 246 | + } |
| 247 | + Write-Step "Cleanup finished" |
| 248 | + } catch { |
| 249 | + Write-Warning ("Failed to clean up service {0}: {1}" -f $ServiceName, $_) |
| 250 | + } |
| 251 | +} |
0 commit comments