109 lines
3.8 KiB
PowerShell
109 lines
3.8 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
PS 14: Pester v5 Automatisiertes Testing & Mocking
|
|
.DESCRIPTION
|
|
Lerne professionelles Unit-Testing & TDD in PowerShell 7+ mit Pester v5:
|
|
- Describe, Context und It Blöcke
|
|
- Fluent Assertions (Should -Be, Should -Throw, Should -HaveCount)
|
|
- Isolierung mit Cmdlet-Mocks (Mock, Should -Invoke)
|
|
- Pester-Ergebnis-Analyse für CI/CD-Pipelines
|
|
#>
|
|
|
|
# 🎯 TEILZIEL 1 (TODO 1): Get-ServerHealthStatus
|
|
<#
|
|
.DESCRIPTION
|
|
Prüft den Gesundheitszustand eines Servers anhand eines Dienstes ('W3SVC' oder 'AppService')
|
|
und einer Netzwerkverbindung. Ist für Unit-Tests mit Mocks konzipiert.
|
|
.OUTPUTS
|
|
[PSCustomObject]@{ ServerName, IsHealthy, Status, Code }
|
|
#>
|
|
function Get-ServerHealthStatus {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$ServerName,
|
|
|
|
[Parameter()]
|
|
[string]$ServiceName = 'AppService'
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Rufe Get-Service -Name $ServiceName auf.
|
|
# 2. Rufe Test-Connection -TargetName $ServerName -Count 1 -Quiet auf (oder teste Erreichbarkeit).
|
|
# 3. Wenn Dienst 'Running' ist und Verbindung $true ist: IsHealthy = $true, Status = 'Operational', Code = 200.
|
|
# 4. Andernfalls: IsHealthy = $false, Status = 'Degraded', Code = 500.
|
|
# 5. Gib das PSCustomObject zurück.
|
|
return $null
|
|
}
|
|
|
|
# 🎯 TEILZIEL 2 (TODO 2): Restart-FailedService
|
|
<#
|
|
.DESCRIPTION
|
|
Prüft einen Dienst. Ist dieser nicht im Status 'Running', wird Restart-Service ausgeführt.
|
|
Schlägt der Dienststart nach $MaxRetries Versuchen fehl, wird ein Fehler geworfen (throw).
|
|
.OUTPUTS
|
|
[PSCustomObject]@{ ServiceName, Success, Attempts }
|
|
#>
|
|
function Restart-FailedService {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$ServiceName,
|
|
|
|
[Parameter()]
|
|
[int]$MaxRetries = 3
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Prüfe den Status mit Get-Service -Name $ServiceName.
|
|
# 2. Wenn Status 'Running' ist: Gib @{ ServiceName = $ServiceName; Success = $true; Attempts = 0 } zurück.
|
|
# 3. Versuche in einer Schleife bis zu $MaxRetries Mal:
|
|
# - Restart-Service -Name $ServiceName
|
|
# - Prüfe erneut Get-Service
|
|
# - Wenn 'Running': gib @{ ServiceName = $ServiceName; Success = $true; Attempts = $versuch } zurück.
|
|
# 4. Nach $MaxRetries erfolglosen Versuchen: throw "Dienst $ServiceName konnte nicht gestartet werden."
|
|
return $null
|
|
}
|
|
|
|
# 🎯 TEILZIEL 3 (TODO 3): Test-PesterResultAnalyzer
|
|
<#
|
|
.DESCRIPTION
|
|
Analysiert ein Pester-Ergebnis-Objekt und berechnet Qualitätsmetriken für CI/CD-Quality-Gates.
|
|
.OUTPUTS
|
|
[PSCustomObject]@{ Total, Passed, Failed, PassRate, Status, IsProductionReady }
|
|
#>
|
|
function Test-PesterResultAnalyzer {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[PSCustomObject]$PesterResult
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Berechne PassRate = [Math]::Round(($PesterResult.PassedCount / $PesterResult.TotalCount) * 100, 1) (0 falls TotalCount == 0).
|
|
# 2. Setze Status = 'Passed' wenn FailedCount == 0 und TotalCount > 0, sonst 'Failed'.
|
|
# 3. Setze IsProductionReady = ($PesterResult.FailedCount -eq 0 -and $PesterResult.TotalCount -gt 0).
|
|
# 4. Gib das aggregierte PSCustomObject zurück.
|
|
return $null
|
|
}
|
|
|
|
# 🎯 TEILZIEL 4 (TODO 4): Invoke-MockableApiAudit
|
|
<#
|
|
.DESCRIPTION
|
|
Führt einen API-Call über Invoke-RestMethod an $EndpointUrl aus und validiert den Rückgabestatus.
|
|
.OUTPUTS
|
|
[PSCustomObject]@{ Url, ResponseCode, PayloadValid, AuditTime }
|
|
#>
|
|
function Invoke-MockableApiAudit {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$EndpointUrl
|
|
)
|
|
|
|
# TODO:
|
|
# 1. Führe Invoke-RestMethod -Uri $EndpointUrl -Method GET aus.
|
|
# 2. Prüfe, ob das zurückgegebene Objekt gültig ist (nicht null und Property 'status' vorhanden).
|
|
# 3. Gib das strukturierte Audit-Objekt zurück.
|
|
return $null
|
|
}
|