From fd7871d866d66800027cb26ce367f7ca6d809ae6 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Sun, 18 Oct 2026 19:26:36 -0500 Subject: [PATCH 1/2] test: extract shared setup/cleanup for DNS record integration tests SonarCloud flagged the BeforeAll/AfterAll blocks of the Get- and Remove-PiHoleDnsRecord integration tests as duplicated (8% duplication on new code, failing the quality gate). Move config loading and record cleanup into tests/DnsControl/DnsRecordTestHelpers.ps1 and use it from the Add, Get and Remove test files. Co-Authored-By: Claude Opus 5.5 --- .../Add-PiHoleDnsRecord.Integration.Tests.ps1 | 20 ++------ tests/DnsControl/DnsRecordTestHelpers.ps1 | 51 +++++++++++++++++++ .../Get-PiHoleDnsRecord.Integration.Tests.ps1 | 19 ++----- ...move-PiHoleDnsRecord.Integration.Tests.ps1 | 18 ++----- 4 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 tests/DnsControl/DnsRecordTestHelpers.ps1 diff --git a/tests/DnsControl/Add-PiHoleDnsRecord.Integration.Tests.ps1 b/tests/DnsControl/Add-PiHoleDnsRecord.Integration.Tests.ps1 index a1a26d1..bcbb31c 100644 --- a/tests/DnsControl/Add-PiHoleDnsRecord.Integration.Tests.ps1 +++ b/tests/DnsControl/Add-PiHoleDnsRecord.Integration.Tests.ps1 @@ -15,6 +15,7 @@ $script:ConfigAvailable = Test-Path (Join-Path (Split-Path $PSScriptRoot -Parent Describe 'Add-PiHoleDnsRecord (Integration)' -Tag 'Integration' { BeforeAll { Import-Module .\PiHoleShell\PiHoleShell.psm1 -Force + . (Join-Path $PSScriptRoot 'DnsRecordTestHelpers.ps1') $script:AHostname = 'piholeshell-test-a.example.com' $script:AIPv4 = '192.0.2.201' @@ -24,26 +25,11 @@ Describe 'Add-PiHoleDnsRecord (Integration)' -Tag 'Integration' { $script:CnameTarget = 'piholeshell-test-a.example.com' $script:CnameTtl = 300 - $configPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'IntegrationConfig.local.ps1' - if (Test-Path $configPath) { - . $configPath - $script:PiHoleServer = $PiHoleServer - $script:PiHoleToken = $PiHoleToken - $script:PiHoleIgnoreSsl = $PiHoleIgnoreSsl - - # Defensive cleanup in case a previous failed run left these behind - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AaaaIPv6 $script:AaaaHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - } + Initialize-DnsRecordTest -Hosts "$script:AIPv4 $script:AHostname", "$script:AaaaIPv6 $script:AaaaHostname" -CnameRecords "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" } AfterAll { - if ($script:PiHoleServer) { - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AaaaIPv6 $script:AaaaHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - } + Clear-DnsRecordTestData } It 'adds an A record and returns a formatted object' -Skip:(-not $script:ConfigAvailable) { diff --git a/tests/DnsControl/DnsRecordTestHelpers.ps1 b/tests/DnsControl/DnsRecordTestHelpers.ps1 new file mode 100644 index 0000000..2045727 --- /dev/null +++ b/tests/DnsControl/DnsRecordTestHelpers.ps1 @@ -0,0 +1,51 @@ +# Shared setup/cleanup for the *-PiHoleDnsRecord integration tests. Dot-source this from a +# Describe block's BeforeAll, then call Initialize-DnsRecordTest. + +function Initialize-DnsRecordTest { + # Loads tests/IntegrationConfig.local.ps1 into $script:PiHoleServer/PiHoleToken/PiHoleIgnoreSsl + # and removes any of the given test records a previous failed run left behind. Leaves + # $script:PiHoleServer unset if the config file is missing. + param ( + [string[]]$Hosts = @(), + [string[]]$CnameRecords = @() + ) + + $script:TestRecords = @{ Hosts = $Hosts; CnameRecords = $CnameRecords } + + $configPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'IntegrationConfig.local.ps1' + if (-not (Test-Path $configPath)) { + return + } + + . $configPath + $script:PiHoleServer = $PiHoleServer + $script:PiHoleToken = $PiHoleToken + $script:PiHoleIgnoreSsl = $PiHoleIgnoreSsl + + Clear-DnsRecordTestData +} + +function Clear-DnsRecordTestData { + # Removes the records passed to Initialize-DnsRecordTest without complaining if they aren't + # there, so it's safe to call in AfterAll even after a test already removed them + if (-not $script:PiHoleServer) { + return + } + + $removeParams = @{ + PiHoleServer = $script:PiHoleServer + Password = $script:PiHoleToken + IgnoreSsl = $script:PiHoleIgnoreSsl + Restart = $false + Confirm = $false + ErrorAction = 'SilentlyContinue' + WarningAction = 'SilentlyContinue' + } + + foreach ($value in $script:TestRecords.Hosts) { + Remove-PiHoleConfigArrayItem @removeParams -Element 'dns/hosts' -Value $value | Out-Null + } + foreach ($value in $script:TestRecords.CnameRecords) { + Remove-PiHoleConfigArrayItem @removeParams -Element 'dns/cnameRecords' -Value $value | Out-Null + } +} diff --git a/tests/DnsControl/Get-PiHoleDnsRecord.Integration.Tests.ps1 b/tests/DnsControl/Get-PiHoleDnsRecord.Integration.Tests.ps1 index 637ef28..d93f18f 100644 --- a/tests/DnsControl/Get-PiHoleDnsRecord.Integration.Tests.ps1 +++ b/tests/DnsControl/Get-PiHoleDnsRecord.Integration.Tests.ps1 @@ -13,6 +13,7 @@ $script:ConfigAvailable = Test-Path (Join-Path (Split-Path $PSScriptRoot -Parent Describe 'Get-PiHoleDnsRecord (Integration)' -Tag 'Integration' { BeforeAll { Import-Module .\PiHoleShell\PiHoleShell.psm1 -Force + . (Join-Path $PSScriptRoot 'DnsRecordTestHelpers.ps1') $script:AHostname = 'piholeshell-test-get-a.example.com' $script:AIPv4 = '192.0.2.203' @@ -20,27 +21,15 @@ Describe 'Get-PiHoleDnsRecord (Integration)' -Tag 'Integration' { $script:CnameTarget = 'piholeshell-test-get-a.example.com' $script:CnameTtl = 300 - $configPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'IntegrationConfig.local.ps1' - if (Test-Path $configPath) { - . $configPath - $script:PiHoleServer = $PiHoleServer - $script:PiHoleToken = $PiHoleToken - $script:PiHoleIgnoreSsl = $PiHoleIgnoreSsl - - # Defensive cleanup in case a previous failed run left these behind - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - + Initialize-DnsRecordTest -Hosts "$script:AIPv4 $script:AHostname" -CnameRecords "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" + if ($script:PiHoleServer) { Add-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false | Out-Null Add-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false | Out-Null } } AfterAll { - if ($script:PiHoleServer) { - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - } + Clear-DnsRecordTestData } It 'returns both the A and CNAME test records, correctly typed' -Skip:(-not $script:ConfigAvailable) { diff --git a/tests/DnsControl/Remove-PiHoleDnsRecord.Integration.Tests.ps1 b/tests/DnsControl/Remove-PiHoleDnsRecord.Integration.Tests.ps1 index 2845ae3..0a7177a 100644 --- a/tests/DnsControl/Remove-PiHoleDnsRecord.Integration.Tests.ps1 +++ b/tests/DnsControl/Remove-PiHoleDnsRecord.Integration.Tests.ps1 @@ -13,6 +13,7 @@ $script:ConfigAvailable = Test-Path (Join-Path (Split-Path $PSScriptRoot -Parent Describe 'Remove-PiHoleDnsRecord (Integration)' -Tag 'Integration' { BeforeAll { Import-Module .\PiHoleShell\PiHoleShell.psm1 -Force + . (Join-Path $PSScriptRoot 'DnsRecordTestHelpers.ps1') $script:AHostname = 'piholeshell-test-remove-a.example.com' $script:AIPv4 = '192.0.2.202' @@ -20,24 +21,11 @@ Describe 'Remove-PiHoleDnsRecord (Integration)' -Tag 'Integration' { $script:CnameTarget = 'piholeshell-test-remove-a.example.com' $script:CnameTtl = 300 - $configPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'IntegrationConfig.local.ps1' - if (Test-Path $configPath) { - . $configPath - $script:PiHoleServer = $PiHoleServer - $script:PiHoleToken = $PiHoleToken - $script:PiHoleIgnoreSsl = $PiHoleIgnoreSsl - - # Defensive cleanup in case a previous failed run left these behind - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null - } + Initialize-DnsRecordTest -Hosts "$script:AIPv4 $script:AHostname" -CnameRecords "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" } AfterAll { - if ($script:PiHoleServer) { - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/hosts' -Value "$script:AIPv4 $script:AHostname" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - Remove-PiHoleConfigArrayItem -PiHoleServer $script:PiHoleServer -Password $script:PiHoleToken -IgnoreSsl $script:PiHoleIgnoreSsl -Element 'dns/cnameRecords' -Value "$script:CnameHostname,$script:CnameTarget,$script:CnameTtl" -Restart $false -Confirm:$false -ErrorAction SilentlyContinue | Out-Null - } + Clear-DnsRecordTestData } It 'removes an A record and returns a formatted object' -Skip:(-not $script:ConfigAvailable) { From b06a6ee5c84b18a54b2662a5366d14a6ebaaa88c Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Sun, 18 Oct 2026 21:21:51 -0500 Subject: [PATCH 2/2] chore: re-trigger SonarCloud analysis Co-Authored-By: Claude Opus 5.5