Skip to content

Refactor and enhance Pester tests batch #9755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
969c2d0
Refactor and improve test scripts for consistency
potatoqualitee Aug 8, 2025
6a7b7fa
Merge branch 'development' into testsupd
potatoqualitee Aug 8, 2025
25d4baf
Merge branch 'development' into testsupd
potatoqualitee Aug 8, 2025
b3c4cd3
Refactor and enhance integration test setup/teardown
potatoqualitee Aug 9, 2025
1f17830
Enhance formatter to preserve alignment and avoid unnecessary writes
potatoqualitee Aug 9, 2025
b07ac96
Improve file handling in Invoke-DbatoolsFormatter
potatoqualitee Aug 9, 2025
4b4c3b5
Improve formatting comparison in Invoke-DbatoolsFormatter
potatoqualitee Aug 9, 2025
05eb897
Refactor Invoke-DbatoolsFormatter for improved formatting
potatoqualitee Aug 9, 2025
21da25c
Add progress reporting to Invoke-DbatoolsFormatter
potatoqualitee Aug 9, 2025
74465ac
Merge branch 'development' into testsupd
potatoqualitee Aug 9, 2025
ce8e0fd
Merge remote-tracking branch 'origin/formatfix' into testsupd
potatoqualitee Aug 9, 2025
43ea064
Fix formatting and minor style issues in test scripts
potatoqualitee Aug 9, 2025
4bc808d
Merge branch 'development' into testsupd
potatoqualitee Aug 10, 2025
47d4537
replace from dev since no changes
potatoqualitee Aug 10, 2025
b1bc868
Refactor and clean up integration tests
potatoqualitee Aug 11, 2025
881e391
Handle missing backup files in test cleanup
potatoqualitee Aug 11, 2025
380fb4e
Update Backup-DbaDatabase tests to use IgnoreFileChecks
potatoqualitee Aug 11, 2025
9fe7433
Improve certificate backup test assertions
potatoqualitee Aug 11, 2025
3f1164a
Ah, this was the wrong branch, revert
potatoqualitee Aug 11, 2025
9dbdee1
fix afterall
potatoqualitee Aug 11, 2025
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
36 changes: 20 additions & 16 deletions tests/Add-DbaAgDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ param(
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe $CommandName -Tag "UnitTests" {
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"SqlInstance",
Expand All @@ -37,10 +37,10 @@ Describe $CommandName -Tag "UnitTests" {
}
}

Describe $CommandName -Tag "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# For all the backups that we want to clean up after the test, we create a directory that we can delete at the end.
# Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups.
Expand All @@ -52,41 +52,42 @@ Describe $CommandName -Tag "IntegrationTests" {
# For negative tests, we need a database without a backup and a non existing database.

# Set variables. They are available in all the It blocks.
$agName = "addagdb_group"
$existingDbWithBackup = "dbWithBackup"
$agName = "addagdb_group"
$existingDbWithBackup = "dbWithBackup"
$existingDbWithoutBackup = "dbWithoutBackup"
$nonexistingDb = "dbdoesnotexist"
$nonexistingDb = "dbdoesnotexist"

# Create the objects.
$splat = @{
$splatAg = @{
Primary = $TestConfig.instance3
Name = $agName
ClusterType = "None"
FailoverMode = "Manual"
Certificate = "dbatoolsci_AGCert"
Confirm = $false
}
$null = New-DbaAvailabilityGroup @splat
$null = New-DbaAvailabilityGroup @splatAg

$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithBackup
$null = Backup-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup -Path $backupPath

$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithoutBackup

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

AfterAll {
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Cleanup all created object.
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agName
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
$null = Remove-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup, $existingDbWithoutBackup

# Remove the backup directory.
Remove-Item -Path $backupPath -Recurse
Remove-Item -Path $backupPath -Recurse -ErrorAction SilentlyContinue

# As this is the last block we do not need to reset the $PSDefaultParameterValues.
}
Expand All @@ -95,12 +96,13 @@ Describe $CommandName -Tag "IntegrationTests" {
# We use the BeforeAll to run the test itself.
# Results are saved in $results.
BeforeAll {
$splat = @{
$splatAddAgDatabase = @{
SqlInstance = $TestConfig.instance3
AvailabilityGroup = $agName
Database = $existingDbWithBackup
Confirm = $false
}
$results = Add-DbaAgDatabase @splat
$results = Add-DbaAgDatabase @splatAddAgDatabase
}

# Always include this test to be sure that the command runs without warnings.
Expand All @@ -123,7 +125,8 @@ Describe $CommandName -Tag "IntegrationTests" {
Database = $existingDbWithoutBackup
# As we don't want an output, we suppress the warning.
# But we can still test the warning because WarningVariable is set globally to WarnVar.
WarningAction = 'SilentlyContinue'
WarningAction = "SilentlyContinue"
Confirm = $false
}
$results = Add-DbaAgDatabase @splatAddAgDb
}
Expand All @@ -143,7 +146,8 @@ Describe $CommandName -Tag "IntegrationTests" {
SqlInstance = $TestConfig.instance3
AvailabilityGroup = $agName
Database = $nonexistingDb
WarningAction = 'SilentlyContinue'
WarningAction = "SilentlyContinue"
Confirm = $false
}
$results = Add-DbaAgDatabase @splatAddAgDb
}
Expand Down
44 changes: 28 additions & 16 deletions tests/Add-DbaAgListener.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Add-DbaAgListener",
$ModuleName = "dbatools",
$CommandName = "Add-DbaAgListener",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe $CommandName -Tag "UnitTests" {
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"SqlInstance",
Expand All @@ -32,50 +32,62 @@ Describe $CommandName -Tag "UnitTests" {
}
}

Describe $CommandName -Tag "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# For all the backups that we want to clean up after the test, we create a directory that we can delete at the end.
# Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups.
$backupPath = "$($TestConfig.Temp)\$CommandName-$(Get-Random)"
$null = New-Item -Path $backupPath -ItemType Directory

# Explain what needs to be set up for the test:
# To add a listener to an availablity group, we need an availability group, an ip address and a port.
# TODO: Add some negative tests.

$agName = "addagdb_group"
# Set variables. They are available in all the It blocks.
$agName = "addagdb_group"
$listenerName = "listener"
$listenerIp = "127.0.20.1"
$listenerIp = "127.0.20.1"
$listenerPort = 14330

$splat = @{
# Create the objects.
$splatAg = @{
Primary = $TestConfig.instance3
Name = $agName
ClusterType = "None"
FailoverMode = "Manual"
Certificate = "dbatoolsci_AGCert"
}
$ag = New-DbaAvailabilityGroup @splat
$ag = New-DbaAvailabilityGroup @splatAg

$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

AfterAll {
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Cleanup all created objects.
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agName
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint

Remove-Item -Path $backupPath -Recurse
# Remove the backup directory.
Remove-Item -Path $backupPath -Recurse -ErrorAction SilentlyContinue

# As this is the last block we do not need to reset the $PSDefaultParameterValues.
}

Context "When creating a listener" {
BeforeAll {
$splat = @{
$splatListener = @{
Name = $listenerName
IPAddress = $listenerIp
Port = $listenerPort
}
$results = $ag | Add-DbaAgListener @splat
$results = $ag | Add-DbaAgListener @splatListener
}

It "Does not warn" {
Expand All @@ -86,4 +98,4 @@ Describe $CommandName -Tag "IntegrationTests" {
$results.PortNumber | Should -Be $listenerPort
}
}
} #$TestConfig.instance2 for appveyor
} #$TestConfig.instance2 for appveyor
64 changes: 44 additions & 20 deletions tests/Add-DbaAgReplica.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
$ModuleName = "dbatools",
$CommandName = "Add-DbaAgReplica",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe $CommandName -Tag "UnitTests" {
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"SqlInstance",
Expand Down Expand Up @@ -40,62 +40,86 @@ Describe $CommandName -Tag "UnitTests" {
}
}

Describe $CommandName -Tag "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Explain what needs to be set up for the test:
# To add an availability group replica, we need an availability group to work with.

# Set variables. They are available in all the It blocks.
$primaryAgName = "dbatoolsci_agroup"
$splat = @{

# Create the objects.
$splatPrimary = @{
Primary = $TestConfig.instance3
Name = $primaryAgName
ClusterType = "None"
FailoverMode = "Manual"
Certificate = "dbatoolsci_AGCert"
Confirm = $false
}
$primaryAg = New-DbaAvailabilityGroup @splat
$primaryAg = New-DbaAvailabilityGroup @splatPrimary
$replicaName = $primaryAg.PrimaryReplica

$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

AfterAll {
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Cleanup all created objects.
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $primaryAgName -Confirm:$false -ErrorAction SilentlyContinue
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint -Confirm:$false -ErrorAction SilentlyContinue

$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $primaryAgName
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
# As this is the last block we do not need to reset the $PSDefaultParameterValues.
}

Context "When adding AG replicas" {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

$replicaAgName = "dbatoolsci_add_replicagroup"
$splatRepAg = @{
Primary = $TestConfig.instance3
Name = $replicaAgName
ClusterType = "None"
FailoverMode = "Manual"
Certificate = "dbatoolsci_AGCert"
Confirm = $false
}
$replicaAg = New-DbaAvailabilityGroup @splatRepAg

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $replicaAgName
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Cleanup all created objects.
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $replicaAgName -Confirm:$false -ErrorAction SilentlyContinue
}

It "Returns results with proper data" {
$results = Get-DbaAgReplica -SqlInstance $TestConfig.instance3
$results.AvailabilityGroup | Should -Contain $replicaAgName
$results.Role | Should -Contain 'Primary'
$results.AvailabilityMode | Should -Contain 'SynchronousCommit'
$results.FailoverMode | Should -Contain 'Manual'
$results.Role | Should -Contain "Primary"
$results.AvailabilityMode | Should -Contain "SynchronousCommit"
$results.FailoverMode | Should -Contain "Manual"
}

It "Returns just one result for a specific replica" {
$results = Get-DbaAgReplica -SqlInstance $TestConfig.instance3 -Replica $replicaName -AvailabilityGroup $replicaAgName
$results.AvailabilityGroup | Should -Be $replicaAgName
$results.Role | Should -Be 'Primary'
$results.AvailabilityMode | Should -Be 'SynchronousCommit'
$results.FailoverMode | Should -Be 'Manual'
$results.Role | Should -Be "Primary"
$results.AvailabilityMode | Should -Be "SynchronousCommit"
$results.FailoverMode | Should -Be "Manual"
}
}
} #$TestConfig.instance2 for appveyor
} #$TestConfig.instance2 for appveyor
27 changes: 18 additions & 9 deletions tests/Add-DbaComputerCertificate.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
$ModuleName = "dbatools",
$CommandName = "Add-DbaComputerCertificate",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe $CommandName -Tag "UnitTests" {
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"ComputerName",
Expand All @@ -29,16 +29,25 @@ Describe $CommandName -Tag "UnitTests" {
}
}

Describe $CommandName -Tag "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

$certPath = "$($TestConfig.AppveyorLabRepo)\certificates\localhost.crt"
$certThumbprint = "29C469578D6C6211076A09CEE5C5797EEA0C2713"
}

AfterAll {
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

Context "Certificate is added properly" {
BeforeAll {
$certPath = "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt"
$certThumbprint = "29C469578D6C6211076A09CEE5C5797EEA0C2713"
$results = Add-DbaComputerCertificate -Path $certPath
}

AfterAll {
Remove-DbaComputerCertificate -Thumbprint $certThumbprint
Remove-DbaComputerCertificate -Thumbprint $certThumbprint -ErrorAction SilentlyContinue
}

It "Should show the proper thumbprint has been added" {
Expand All @@ -49,4 +58,4 @@ Describe $CommandName -Tag "IntegrationTests" {
$results.PSParentPath | Should -Be "Microsoft.PowerShell.Security\Certificate::LocalMachine\My"
}
}
}
}
Loading
Loading