# Fakes for Test-UpgradeMxGatewayService.ps1. Dot-sourced AFTER Upgrade-MxGatewayService.ps1 so these replace # its environment seams. All state lives in $script:Box (built by New-FakeBox in the test script). # Nothing here touches the real service manager, registry, process table or network. function Invoke-Nssm { param([string[]]$NssmArgs, [switch]$AllowFail) $b = $script:Box $b.NssmCalls.Add(($NssmArgs -join ' ')) $verb = $NssmArgs[0] switch ($verb) { 'status' { if ($b.State -eq 'MISSING') { return [pscustomobject]@{ Code = 3; Text = "Can't open service!" } } if ($b.State -eq 'SERVICE_STOP_PENDING' -and -not @($b.Procs | Where-Object { $_.Role -eq 'Server' }).Count) { $b.State = 'SERVICE_STOPPED' } return [pscustomobject]@{ Code = 0; Text = $b.State } } 'get' { switch ($NssmArgs[2]) { 'Application' { return [pscustomobject]@{ Code = 0; Text = $b.Application } } 'AppDirectory' { return [pscustomobject]@{ Code = 0; Text = $b.AppDirectory } } 'AppEnvironmentExtra' { return [pscustomobject]@{ Code = 0; Text = ($b.Env -join "`r`n") } } } } 'stop' { if ($b.StopHangs) { $b.State = 'SERVICE_STOP_PENDING'; return [pscustomobject]@{ Code = 0; Text = '' } } $b.State = 'SERVICE_STOPPED' $b.Procs = @($b.Procs | Where-Object { $_.Role -eq 'Other' -or $b.Survivors -contains $_.Id }) return [pscustomobject]@{ Code = 0; Text = '' } } 'start' { $b.Starts++ $b.StartedWith.Add($(if ((Get-Content -LiteralPath $b.Application -Raw) -match 'sha=([0-9a-f]+)') { $Matches[1] } else { '?' })) $b.State = 'SERVICE_RUNNING' $sid = 7000 + $b.Starts * 10 $b.Procs = @($b.Procs) + @( [pscustomobject]@{ Id = $sid; ParentId = $b.HostPid; Name = [IO.Path]::GetFileNameWithoutExtension($b.Application); Path = $b.Application; Role = 'Server' }, [pscustomobject]@{ Id = $sid + 1; ParentId = $sid; Name = [IO.Path]::GetFileNameWithoutExtension($b.WorkerExe); Path = $b.WorkerExe; Role = 'Worker' }) if ($b.OnStart) { & $b.OnStart $b } return [pscustomobject]@{ Code = 0; Text = '' } } } throw "fake nssm: unexpected call $($NssmArgs -join ' ')" } function Read-EnvBlockLines { @($script:Box.Env) } # Return copies so the script's Add-Member (Role) cannot leak back into the fake state. function Get-ProcessTable { @($script:Box.Procs | ForEach-Object { [pscustomobject]@{ Id = $_.Id; ParentId = $_.ParentId; Name = $_.Name; Path = $_.Path } }) } function Get-ServiceHostPid { $script:Box.HostPid } function Get-OtherGatewayServices([string]$ExeName) { , @($script:Box.OtherServices) } function Stop-ProcessById([int]$Id) { $script:Box.Killed.Add($Id) $script:Box.Procs = @($script:Box.Procs | Where-Object { $_.Id -ne $Id }) } function Get-HttpStatus([string]$Url) { $script:Box.HttpCalls.Add($Url); $script:Box.Health } function Get-ProductSha([string]$Exe) { if (-not (Test-Path -LiteralPath $Exe)) { return $null } if ((Get-Content -LiteralPath $Exe -Raw) -match 'sha=([0-9a-f]+)') { return $Matches[1] } $null } function Get-ExeArchitecture([string]$Exe) { if ((Get-Content -LiteralPath $Exe -Raw) -match 'arch=(\w+)') { return $Matches[1] } $null } function Protect-Directory([string]$Path) { $script:Box.Protected.Add($Path) } function Export-ServiceKey([string]$Destination) { Set-Content -LiteralPath $Destination -Value 'fake reg export' } function Get-DirSddl([string]$Path) { $null } function Copy-ExplicitAcl([string]$From, [string]$To) { } # Fake clock: Start-Sleep advances it, so timeouts expire instantly and deterministically. function Start-Sleep { param([int]$Seconds, [int]$Milliseconds) $script:FakeNow = $script:FakeNow.AddSeconds([math]::Max($Seconds, 1)) } function Get-Date { param([string]$Format) if ($Format) { return (Microsoft.PowerShell.Utility\Get-Date -Format $Format) } $script:FakeNow }