-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests, added ci, and updated readme
- Loading branch information
Craig Boileau
committed
Nov 18, 2024
1 parent
7b3d490
commit 5db69ba
Showing
5 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: PowerShell Tests | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test: | ||
runs-on: windows-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Install Pester | ||
shell: pwsh | ||
run: | | ||
Install-Module -Name Pester -Force -SkipPublisherCheck | ||
- name: Run Tests | ||
shell: pwsh | ||
run: | | ||
$config = New-PesterConfiguration | ||
$config.Run.Path = "Tests" | ||
$config.Output.Verbosity = "Detailed" | ||
Invoke-Pester -Configuration $config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter()] | ||
[ValidateSet('All', 'Installation', 'ActiveHours', 'Uninstallation')] | ||
[string]$TestType = 'All' | ||
) | ||
|
||
# Ensure Pester is installed | ||
if (-not (Get-Module -ListAvailable -Name Pester)) { | ||
Write-Host "Pester not found. Installing Pester..." -ForegroundColor Yellow | ||
Install-Module -Name Pester -Force -SkipPublisherCheck | ||
} | ||
|
||
Write-Host "Starting test run..." -ForegroundColor Cyan | ||
Write-Host "Test type: $TestType" -ForegroundColor Cyan | ||
Write-Host "----------------------------------------" -ForegroundColor Cyan | ||
|
||
# Configure Pester | ||
$config = New-PesterConfiguration | ||
$config.Run.Path = Join-Path $PSScriptRoot "Tests" | ||
$config.Output.Verbosity = "Detailed" | ||
$config.Run.PassThru = $true | ||
|
||
# Apply test filter if specified | ||
if ($TestType -ne 'All') { | ||
Write-Host "Running only $TestType tests..." -ForegroundColor Yellow | ||
$config.Filter.Tag = @($TestType) | ||
} else { | ||
Write-Host "Running all tests..." -ForegroundColor Yellow | ||
$config.Filter.Tag = @() | ||
} | ||
|
||
# Run tests | ||
$testResults = Invoke-Pester -Configuration $config | ||
|
||
# Display results summary | ||
Write-Host "`nTest Results Summary:" -ForegroundColor Cyan | ||
Write-Host "----------------------------------------" -ForegroundColor Cyan | ||
Write-Host "Total Tests: $($testResults.TotalCount)" -ForegroundColor White | ||
Write-Host "Passed: $($testResults.PassedCount)" -ForegroundColor Green | ||
Write-Host "Failed: $($testResults.FailedCount)" -ForegroundColor Red | ||
Write-Host "Skipped: $($testResults.SkippedCount)" -ForegroundColor Yellow | ||
Write-Host "----------------------------------------" -ForegroundColor Cyan | ||
|
||
if ($testResults.FailedCount -gt 0) { | ||
Write-Host "`nFailed Tests:" -ForegroundColor Red | ||
$testResults.Failed | ForEach-Object { | ||
Write-Host "- $($_.Name)" -ForegroundColor Red | ||
Write-Host " $($_.ErrorRecord)" -ForegroundColor Red | ||
} | ||
} | ||
|
||
Write-Host "`nPress Enter to exit..." -ForegroundColor Green | ||
$null = Read-Host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# Import Pester module for mocking | ||
Import-Module Pester | ||
|
||
BeforeAll { | ||
# Import the script content into a scriptblock | ||
$scriptPath = "$PSScriptRoot\..\FWindowsUpdateReboot.ps1" | ||
} | ||
|
||
Describe "FWindowsUpdateReboot Script Tests" { | ||
BeforeEach { | ||
# Mock functions that interact with the system | ||
Mock Set-ItemProperty { } | ||
Mock Get-ScheduledTask { throw "Task does not exist" } | ||
Mock Unregister-ScheduledTask { } | ||
Mock Copy-Item { } | ||
Mock Start-Process { | ||
return [PSCustomObject]@{ | ||
ExitCode = 0 | ||
} | ||
} | ||
Mock Write-Host { } | ||
Mock Write-Error { } | ||
Mock Read-Host { return "Y" } | ||
Mock Get-Date { return [DateTime]::Parse("2024-03-20 14:00:00") } | ||
|
||
# Mock admin check properly | ||
Mock New-Object { | ||
if ($TypeName -eq 'Security.Principal.WindowsPrincipal') { | ||
$mockPrincipal = New-Object PSObject | ||
$mockPrincipal | Add-Member -MemberType ScriptMethod -Name IsInRole -Value { param($role) $true } | ||
return $mockPrincipal | ||
} | ||
return $null | ||
} -ParameterFilter { $TypeName -eq 'Security.Principal.WindowsPrincipal' } | ||
} | ||
|
||
Context "Active Hours Rotation" -Tag "ActiveHours" { | ||
It "Should calculate correct active hours" { | ||
# Execute the script with Rotate parameter | ||
$global:PSCommandPath = $scriptPath | ||
. $scriptPath -Rotate | ||
|
||
# Check if Set-ItemProperty was called with correct values | ||
Should -Invoke Set-ItemProperty -Times 1 -Exactly -ParameterFilter { | ||
$Path -eq "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -and | ||
$Name -eq "ActiveHoursStart" -and | ||
$Value -eq 14 | ||
} | ||
|
||
Should -Invoke Set-ItemProperty -Times 1 -Exactly -ParameterFilter { | ||
$Path -eq "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" -and | ||
$Name -eq "ActiveHoursEnd" -and | ||
$Value -eq 8 # (14 + 18) % 24 = 8 | ||
} | ||
} | ||
} | ||
|
||
Context "Installation" -Tag "Installation" { | ||
It "Should create scheduled task correctly" { | ||
# Execute the script | ||
$global:PSCommandPath = $scriptPath | ||
. $scriptPath | ||
|
||
# Verify Start-Process was called with correct schtasks.exe parameters | ||
Should -Invoke Start-Process -Times 1 -Exactly -ParameterFilter { | ||
$FilePath -eq "schtasks.exe" -and | ||
$ArgumentList -contains "/Create" -and | ||
$ArgumentList -contains "/SC" -and | ||
$ArgumentList -contains "HOURLY" | ||
} | ||
} | ||
} | ||
|
||
Context "Uninstallation" -Tag "Uninstallation" { | ||
It "Should remove scheduled task and script" { | ||
# Mock Get-ScheduledTask to return a task this time | ||
Mock Get-ScheduledTask { return [PSCustomObject]@{} } | ||
|
||
# Execute the script with Uninstall parameter | ||
$global:PSCommandPath = $scriptPath | ||
. $scriptPath -Uninstall | ||
|
||
# Verify task removal was attempted | ||
Should -Invoke Unregister-ScheduledTask -Times 1 -Exactly | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter()] | ||
[ValidateSet('All', 'Installation', 'ActiveHours', 'Uninstallation')] | ||
[string]$TestType = 'All' | ||
) | ||
|
||
# Check if running as administrator | ||
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | ||
if (-not $isAdmin) { | ||
Write-Host "Tests must be run as Administrator. Restarting with elevation..." -ForegroundColor Yellow | ||
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -TestType $TestType" -Verb RunAs | ||
exit | ||
} | ||
|
||
# Ensure Pester is installed | ||
if (-not (Get-Module -ListAvailable -Name Pester)) { | ||
Write-Host "Pester not found. Installing Pester..." -ForegroundColor Yellow | ||
Install-Module -Name Pester -Force -SkipPublisherCheck | ||
} | ||
|
||
# Configure Pester | ||
$config = New-PesterConfiguration | ||
$config.Run.Path = $PSScriptRoot | ||
$config.Output.Verbosity = "Detailed" | ||
|
||
# Apply test filter if specified | ||
if ($TestType -ne 'All') { | ||
$config.Filter.Tag = $TestType | ||
} | ||
|
||
# Run tests | ||
Write-Host "Running $TestType tests..." -ForegroundColor Cyan | ||
Invoke-Pester -Configuration $config | ||
|
||
Write-Host "`nPress Enter to exit..." -ForegroundColor Green | ||
$null = Read-Host |