Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions tests/DnsControl/Add-PiHoleDnsRecord.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions tests/DnsControl/DnsRecordTestHelpers.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
19 changes: 4 additions & 15 deletions tests/DnsControl/Get-PiHoleDnsRecord.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,23 @@ $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'
$script:CnameHostname = 'piholeshell-test-get-cname.example.com'
$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) {
Expand Down
18 changes: 3 additions & 15 deletions tests/DnsControl/Remove-PiHoleDnsRecord.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,19 @@ $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'
$script:CnameHostname = 'piholeshell-test-remove-cname.example.com'
$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) {
Expand Down
Loading