This commit is contained in:
Madzone
2026-10-09 14:50:28 +02:00
parent afbad0cb50
commit 2500b1f354
16 changed files with 1859 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
# Isolierte Tests: keine Serverzugriffe oder echten Loeschungen.
$ErrorActionPreference = 'Stop'
$path = Join-Path (Split-Path $PSScriptRoot -Parent) 'NDTrack-Geprueft.ps1'
$t=$null; $e=$null
[void][System.Management.Automation.Language.Parser]::ParseFile($path,[ref]$t,[ref]$e)
if ($e.Count) { throw ($e | Out-String) }
. $path -LibraryOnly
$script:checks=0
function Assert { param($Condition,$Message) if(-not $Condition){throw $Message}; $script:checks++ }
function Invoke-Command { param($ComputerName,$ScriptBlock,[object[]]$ArgumentList,$ErrorAction) & $ScriptBlock @ArgumentList }
function Test-Path { $true }
$priorSystemDrive=$env:SystemDrive
$env:SystemDrive='E:'
$script:DestinationPath=''
Assert ((Resolve-WindowsFolder 'server01') -eq 'E:\ndtrack') 'Systemlaufwerk E nicht verwendet'
$env:SystemDrive='D:'
Assert ((Resolve-WindowsFolder 'server02') -eq 'D:\ndtrack') 'Systemlaufwerk nicht pro Server aufgeloest'
$env:SystemDrive=$priorSystemDrive
$script:DestinationPath='D:\Tools\ndtrack'
Assert ((Resolve-WindowsFolder 'server01') -eq 'D:\Tools\ndtrack') 'Dynamischer Zielpfad falsch'
$rejected=$false
try { Resolve-WindowsFolder 'server;invalid' } catch { $rejected=$true }
Assert $rejected 'Ungueltiger Host akzeptiert'
$script:DestinationPath='C:\Tools\..\ndtrack'
$rejected=$false
try { Resolve-WindowsFolder 'server01' } catch { $rejected=$true }
Assert $rejected 'Pfad mit .. akzeptiert'
# Echte lokale Exitcodes; nur ein leerer PowerShell-Kindprozess.
$program=(Get-Process -Id $PID).Path
$rejected=$false
try { Invoke-NativeChecked $program @('-NoProfile','-Command','exit 7') } catch { $rejected=$true }
Assert $rejected 'Native Fehler nicht erkannt'
Invoke-NativeChecked $program @('-NoProfile','-Command','exit 0')
$script:checks++
# Alle acht Menueablaeufe mit eindeutigem Erfolg/Fehlschlag der vorgeschalteten Schritte.
function GetADServer { @('ok','failed') }
function GetRDSServer { @('rds01') }
function GetLinuxServer { @('linuxok','linuxfailed') }
function Get-LinuxUser { 'scanuser' }
function Get-Credential { [pscredential]::new('DOMAIN\scan',(ConvertTo-SecureString 'test-only' -AsPlainText -Force)) }
$script:trace=@()
function CopyFile { param($Servers) $script:trace+='copy'; 'ok' }
function RunNDTRACKRemotely { param($Servers) Assert (($Servers -join ',') -eq 'ok') 'Option 1 startet fehlgeschlagene Server'; $script:trace+='run' }
function CreateScheduledTask { param($Servers,$Credential) Assert (($Servers -join ',') -eq 'ok,failed') 'Option 2 erhaelt falsche Ziele'; Assert ($Credential.UserName -eq 'DOMAIN\scan') 'Konto fehlt'; $script:trace+='task' }
function CopyFileLinux { param($Servers,$User) Assert ($User -eq 'scanuser') 'Linux-Konto fehlt'; $script:trace+='linuxcopy'; 'linuxok' }
function RunNDTRACKRemotelyLinux { param($Servers,$User) Assert (($Servers -join ',') -eq 'linuxok,linuxfailed') 'Option 5 erhaelt falsche Ziele'; $script:trace+='linuxrun' }
function GetInventoryFiles { param($Servers) $script:trace+='collect' }
function GetInventoryFilesLinux { param($Servers,$User) Assert ($User -eq 'scanuser') 'Konto Option 5 fehlt'; $script:trace+='linuxcollect' }
function DeleteScheduledTask { param($Servers) $script:trace+='stoptask'; 'ok' }
function DeleteFile { param($Servers) Assert (($Servers -join ',') -eq 'ok') 'Option 6 loescht trotz Task-Fehler'; $script:trace+='delete' }
function DeleteFileLinux { param($Servers,$User) $script:trace+='linuxdelete' }
function Scan_RDS_SINA { param($Servers) Assert (($Servers -join ',') -eq 'rds01') 'RDS-Ziele falsch'; $script:trace+='rds' }
$expected=@('copy','task','collect','linuxcopy','linuxrun','linuxcollect','stoptask,delete','linuxdelete')
foreach($option in 1..8){
$script:trace=@()
Invoke-MenuOption ([string]$option)
Assert (($script:trace -join ',') -eq $expected[$option-1]) "Ablauf Option $option falsch"
Write-Host "Option ${option}: Ablauf bestanden"
}
function CopyFile { param($Servers) }
$script:trace=@(); Invoke-MenuOption '1'; Assert ($script:trace.Count -eq 0) 'Scan trotz leerer Kopiererfolge'
$script:trace=@(); Invoke-MenuOption '0'; Assert ($script:trace.Count -eq 0) 'Option 0 fuehrt Aktionen aus'
function Get-Credential { $null }
$rejected=$false
try { Invoke-MenuOption '2' } catch { $rejected=$true }
Assert $rejected 'Abgebrochene Kontoabfrage nicht erkannt'
Write-Host "$script:checks Pruefungen bestanden. Keine echten Zielsysteme getestet."
# Originalfunktionen erneut laden und deren Fehlerpfade pruefen.
. $path -LibraryOnly
$script:DestinationPath='C:\ndtrack'
$script:TaskName='NDTrackTask'
$script:WindowsLauncher='RunNdTrackWindows_Local.cmd'
$script:LinuxSourcePath='test-source'
$script:LinuxLauncher='RunNdTrackLinuxUnix_Local.sh'
$script:Ergebnisordner='test-results'
$script:StartDelayMinutes=5
function Test-Path { $true }
function Test-SourceFiles { }
function Join-Path { param($Path,$ChildPath) "$Path/$ChildPath" }
function Invoke-Command { throw 'Simulierter Remote-Fehler' }
function Invoke-LinuxCommand { throw 'Simulierter SSH-Fehler' }
$credential=[pscredential]::new('DOMAIN\scan',(ConvertTo-SecureString 'test-only' -AsPlainText -Force))
$cases=@(
{ CreateScheduledTask @('server01') $credential },
{ RunNDTRACKRemotely @('server01') },
{ DeleteScheduledTask @('server01') },
{ DeleteFile @('server01') },
{ GetInventoryFiles @('server01') },
{ CopyFileLinux @('server01') 'scanuser' },
{ RunNDTRACKRemotelyLinux @('server01') 'scanuser' },
{ GetInventoryFilesLinux @('server01') 'scanuser' },
{ DeleteFileLinux @('server01') 'scanuser' }
)
foreach($case in $cases){
$output=@(& $case *>&1)
Assert (@($output | Where-Object { $_ -is [System.Management.Automation.WarningRecord] }).Count -gt 0) 'Fehler wurde nicht gemeldet'
Assert (-not (($output | Out-String) -match 'START ANGEFORDERT:|GEPLANT:|BEENDET:|ENTFERNT:|EINGESAMMELT:|KOPIERT:')) 'Falsche Erfolgsmeldung nach Fehler'
}
Write-Host "Gesamt: $script:checks Pruefungen bestanden."
+33
View File
@@ -0,0 +1,33 @@
$ErrorActionPreference='Stop'
$preScript=Join-Path (Split-Path $PSScriptRoot -Parent) 'PreScript.ps1'
$root=Join-Path ([IO.Path]::GetTempPath()) ('ndtrack-pre-'+[guid]::NewGuid().ToString('N'))
$program=(Get-Process -Id $PID).Path
try {
& $program -NoProfile -File $preScript -BasePath $root
if($LASTEXITCODE -ne 1){throw 'Fehlende Ordner akzeptiert'}
if(Test-Path -LiteralPath $root){throw 'PreScript hat Ordner angelegt'}
foreach($relative in @('SCRIPT','NDTRACK','RESULTS','SCRIPT/Logs')){
New-Item -Path (Join-Path $root $relative) -ItemType Directory -Force | Out-Null
}
& $program -NoProfile -File $preScript -BasePath $root
if($LASTEXITCODE -ne 1){throw 'Leeres Paket akzeptiert'}
# Harmloses Test-Hauptskript dokumentiert nur seinen Start.
$fixture='Set-Content -LiteralPath (Join-Path $PSScriptRoot "started.txt") -Value "started"'
$main=Join-Path $root 'SCRIPT/NDTrack-Geprueft.ps1'
$marker=Join-Path $root 'SCRIPT/started.txt'
Set-Content -LiteralPath $main -Value $fixture
Set-Content -LiteralPath (Join-Path $root 'NDTRACK/RunNdTrackWindows_Local.cmd') -Value '@echo test'
& $program -NoProfile -File $preScript -BasePath $root
if($LASTEXITCODE -ne 0 -or -not(Test-Path -LiteralPath $marker)){throw 'Hauptskript nicht gestartet'}
Remove-Item -LiteralPath $marker
& $program -NoProfile -File $preScript -BasePath $root -Platform Both
if($LASTEXITCODE -ne 1 -or (Test-Path -LiteralPath $marker)){throw 'Start trotz fehlender Linux-Datei'}
Set-Content -LiteralPath (Join-Path $root 'NDTRACK/RunNdTrackLinuxUnix_Local.sh') -Value '#!/bin/sh'
& $program -NoProfile -File $preScript -BasePath $root -Platform Both
if($LASTEXITCODE -ne 0 -or -not(Test-Path -LiteralPath $marker)){throw 'Start fuer beide Plattformen fehlgeschlagen'}
Remove-Item -LiteralPath $marker
Set-Content -LiteralPath $main -Value 'function Broken {'
& $program -NoProfile -File $preScript -BasePath $root
if($LASTEXITCODE -ne 1 -or (Test-Path -LiteralPath $marker)){throw 'Syntaxfehler akzeptiert'}
Write-Host 'PreScript: 6 Szenarien bestanden; fehlende Ordner nicht angelegt; Hauptskript nur bei Erfolg gestartet.'
} finally { if(Test-Path -LiteralPath $root){Remove-Item -LiteralPath $root -Recurse -Force} }
+39
View File
@@ -0,0 +1,39 @@
$ErrorActionPreference = 'Stop'
. (Join-Path (Split-Path $PSScriptRoot -Parent) 'NDTrack-Geprueft.ps1') -LibraryOnly
$root = Join-Path ([IO.Path]::GetTempPath()) ('ndtrack-test-' + [guid]::NewGuid().ToString('N'))
$script:checks=0
function Assert { param($Condition,$Message) if(-not $Condition){throw $Message}; $script:checks++ }
function Assert-Fails { param([scriptblock]$Action,[string]$Pattern)
$message=''
try { & $Action } catch { $message=$_.Exception.Message }
Assert ($message -like $Pattern) "Erwarteter Fehler fehlt: $Pattern / $message"
}
try {
$script:ScriptFolder=Join-Path $root 'SCRIPT'
$script:SourcePath=Join-Path $root 'NDTRACK'
$script:LinuxSourcePath=$script:SourcePath
$script:Ergebnisordner=Join-Path $root 'RESULTS'
$script:LogFolder=Join-Path $script:ScriptFolder 'Logs'
CheckDirectory
foreach($path in @($script:ScriptFolder,$script:SourcePath,$script:Ergebnisordner,$script:LogFolder)) {
Assert (Test-Path -LiteralPath $path -PathType Container) "Ordner fehlt: $path"
}
CheckDirectory # Wiederholbarer Aufruf
Assert-Fails { Test-SourceFiles $script:SourcePath } '*Keine Dateien*'
New-Item -Path (Join-Path $script:SourcePath 'empty') -ItemType Directory | Out-Null
Assert-Fails { Test-SourceFiles $script:SourcePath } '*Keine Dateien*'
Set-Content -LiteralPath (Join-Path $script:SourcePath 'payload.dat') -Value 'test'
Test-SourceFiles $script:SourcePath
$script:checks++
Assert-Fails { Test-SourceFiles $script:SourcePath 'Run.cmd' } '*Startdatei fehlt*'
[IO.File]::WriteAllText((Join-Path $script:SourcePath 'Run.cmd'),'')
Assert-Fails { Test-SourceFiles $script:SourcePath 'Run.cmd' } '*Startdatei ist leer*'
Set-Content -LiteralPath (Join-Path $script:SourcePath 'Run.cmd') -Value '@echo test'
Test-SourceFiles $script:SourcePath 'Run.cmd'
$script:checks++
$script:ScriptFolder=Join-Path $root 'file-instead-of-folder'
Set-Content -LiteralPath $script:ScriptFolder -Value 'keep'
Assert-Fails { CheckDirectory } '*Datei statt eines Ordners*'
Assert ((Get-Content -LiteralPath $script:ScriptFolder) -eq 'keep') 'Bestehende Datei veraendert'
Write-Host "$script:checks Ordner-/Dateipruefungen bestanden."
} finally { if(Test-Path -LiteralPath $root){Remove-Item -LiteralPath $root -Recurse -Force} }
+22
View File
@@ -0,0 +1,22 @@
$ErrorActionPreference='Stop'
. (Join-Path (Split-Path $PSScriptRoot -Parent) 'NDTrack-Geprueft.ps1') -LibraryOnly
$script:TaskName='NDTRACK'; $script:WindowsLauncher='Run.cmd'
$script:events=@(); $script:hasFile=$true; $script:running=$false
function Resolve-WindowsFolder { '/simulated/ndtrack' }
function Invoke-Command { param($ComputerName,$ScriptBlock,[object[]]$ArgumentList,$ErrorAction) & $ScriptBlock @ArgumentList }
function Test-Path { param($LiteralPath,$PathType) if($LiteralPath -like '*.ndtrack-running'){return $false}; $script:hasFile }
function Get-ScheduledTask { if($script:running){[pscustomobject]@{TaskName='NDTRACK';TaskPath='\';State='Running'}} }
function New-ScheduledTaskAction { 'action' }
function New-ScheduledTaskSettingsSet { 'settings' }
function Register-ScheduledTask { param($TaskName) if($TaskName -ne 'NDTRACK'){throw 'Falscher Taskname'}; $script:events+='register' }
function Start-ScheduledTask { param($TaskName) if($TaskName -ne 'NDTRACK'){throw 'Falscher Taskname'}; $script:events+='start' }
$credential=[pscredential]::new('test',(ConvertTo-SecureString 'test' -AsPlainText -Force))
CreateScheduledTask @('server01') $credential
if(($script:events -join ',') -ne 'register,start'){throw 'Task nicht zuerst registriert und danach gestartet'}
$script:events=@(); $script:hasFile=$false
CreateScheduledTask @('server01') $credential
if($script:events.Count){throw 'Task trotz fehlender CMD erstellt'}
$script:hasFile=$true; $script:running=$true
CreateScheduledTask @('server01') $credential
if($script:events.Count){throw 'Aktiven Task ueberschrieben'}
Write-Host '3 Task-Start-Szenarien bestanden.'