Skip to content

Commit 3a53e3a

Browse files
A pester 5 test as a blueprint (#9729)
Co-authored-by: Chrissy LeMaire <[email protected]>
1 parent 9de0399 commit 3a53e3a

File tree

5 files changed

+154
-56
lines changed

5 files changed

+154
-56
lines changed

private/testing/Get-TestConfig.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ function Get-TestConfig {
6161

6262
$config['CommonParameters'] = [System.Management.Automation.PSCmdlet]::CommonParameters
6363

64+
# We want the tests as readable as possible so we want to set Confirm globally to $false
65+
$config['Defaults']['*:Confirm'] = $false
66+
67+
# We use a global warning variable so that we can always test that the command does not write a warning
68+
$config['Defaults']['*:WarningVariable'] = 'WarnVar'
69+
6470
if (-not $config['Temp']) {
6571
$config['Temp'] = 'C:\Temp'
6672
}

tests/Add-DbaAgDatabase.Tests.ps1

Lines changed: 110 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
22
param(
3-
$ModuleName = "dbatools",
4-
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
3+
$ModuleName = "dbatools",
4+
# $TestConfig has to be set outside of the tests by running: $TestConfig = Get-TestConfig
5+
# This will set $TestConfig.Defaults with the parameter defaults, including:
6+
# * Confirm = $false
7+
# * WarningVariable = 'WarnVar'
8+
# So you don't have to use -Confirm:$false and you can always use $WarnVar to test for warnings.
9+
$PSDefaultParameterValues = $TestConfig.Defaults
510
)
611

7-
Describe "Add-DbaAgDatabase" -Tag "UnitTests" {
12+
Describe "Add-DbaAgDatabase UnitTests" -Tag "UnitTests" {
813
Context "Parameter validation" {
914
BeforeAll {
1015
$command = Get-Command Add-DbaAgDatabase
@@ -38,49 +43,122 @@ Describe "Add-DbaAgDatabase" -Tag "UnitTests" {
3843
}
3944
}
4045

41-
Describe "Add-DbaAgDatabase" -Tag "IntegrationTests" {
46+
Describe "Add-DbaAgDatabase IntegrationTests" -Tag "IntegrationTests" {
4247
BeforeAll {
43-
$null = Get-DbaProcess -SqlInstance $TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
44-
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance3
45-
$agname = "dbatoolsci_addagdb_agroup"
46-
$dbname = "dbatoolsci_addagdb_agroupdb"
47-
$newdbname = "dbatoolsci_addag_agroupdb_2"
48-
$server.Query("create database $dbname")
49-
$backup = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbname | Backup-DbaDatabase
50-
$splatNewAg = @{
51-
Primary = $TestConfig.instance3
52-
Name = $agname
53-
ClusterType = "None"
48+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
49+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
50+
51+
# Collect all the created files to be able to remove them in the AfterAll block.
52+
$filesToRemove = @( )
53+
54+
# Explain what needs to be set up for the test:
55+
# To add a database to an availablity group, we need an availability group and a database that has been backed up.
56+
# For negative tests, we need a database without a backup and a non existing database.
57+
58+
# Set variables. They are available in all the It blocks.
59+
$agName = "addagdb_group"
60+
$existingDbWithBackup = "dbWithBackup"
61+
$existingDbWithoutBackup = "dbWithoutBackup"
62+
$nonexistingDb = "dbdoesnotexist"
63+
64+
# Create the objects.
65+
$splat = @{
66+
Primary = $TestConfig.instance3
67+
Name = $agName
68+
ClusterType = "None"
5469
FailoverMode = "Manual"
55-
Database = $dbname
56-
Confirm = $false
57-
Certificate = "dbatoolsci_AGCert"
70+
Certificate = "dbatoolsci_AGCert"
5871
}
59-
$ag = New-DbaAvailabilityGroup @splatNewAg
72+
$null = New-DbaAvailabilityGroup @splat
73+
74+
$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithBackup
75+
$backup = Backup-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup -Path $TestConfig.Temp
76+
$filesToRemove += $backup.Path
77+
78+
$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithoutBackup
79+
80+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
81+
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
6082
}
6183

6284
AfterAll {
63-
$null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname -Confirm:$false
64-
$null = Remove-DbaDatabase -SqlInstance $server -Database $dbname, $newdbname -Confirm:$false
85+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
86+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
87+
88+
# Cleanup all created object.
89+
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agName
90+
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
91+
$null = Remove-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup, $existingDbWithoutBackup
92+
93+
# Remove all created files.
94+
Remove-Item -Path $filesToRemove
95+
96+
# As this is the last block we do not need to reset the $PSDefaultParameterValues.
6597
}
6698

6799
Context "When adding AG database" {
100+
# We use the BeforeAll to run the test itself.
101+
# Results are saved in $results.
102+
BeforeAll {
103+
$splat = @{
104+
SqlInstance = $TestConfig.instance3
105+
AvailabilityGroup = $agName
106+
Database = $existingDbWithBackup
107+
}
108+
$results = Add-DbaAgDatabase @splat
109+
}
110+
111+
# Always include this test to be sure that the command runs without warnings.
112+
It "Does not warn" {
113+
$WarnVar | Should -BeNullOrEmpty
114+
}
115+
116+
It "Returns proper results" {
117+
$results.AvailabilityGroup | Should -Be $agName
118+
$results.Name | Should -Be $existingDbWithBackup
119+
$results.IsJoined | Should -BeTrue
120+
}
121+
}
122+
123+
Context "When adding AG database that does not have a backup" {
68124
BeforeAll {
69-
$server.Query("create database $newdbname")
70-
$backup = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $newdbname | Backup-DbaDatabase
71125
$splatAddAgDb = @{
72-
SqlInstance = $TestConfig.instance3
73-
AvailabilityGroup = $agname
74-
Database = $newdbname
75-
Confirm = $false
126+
SqlInstance = $TestConfig.instance3
127+
AvailabilityGroup = $agName
128+
Database = $existingDbWithoutBackup
129+
# As we don't want an output, we suppress the warning.
130+
# But we can still test the warning because WarningVariable is set globally to WarnVar.
131+
WarningAction = 'SilentlyContinue'
76132
}
77133
$results = Add-DbaAgDatabase @splatAddAgDb
78134
}
79135

80-
It "Returns proper results" {
81-
$results.AvailabilityGroup | Should -Be $agname
82-
$results.Name | Should -Be $newdbname
83-
$results.IsJoined | Should -Be $true
136+
It "Does warn" {
137+
$WarnVar | Should -Match "Failed to add database $existingDbWithoutBackup to Availability Group $agName"
138+
}
139+
140+
It "Does not return results" {
141+
$results | Should -BeNullOrEmpty
142+
}
143+
}
144+
145+
Context "When adding AG database that does not exists" {
146+
BeforeAll {
147+
$splatAddAgDb = @{
148+
SqlInstance = $TestConfig.instance3
149+
AvailabilityGroup = $agName
150+
Database = $nonexistingDb
151+
WarningAction = 'SilentlyContinue'
152+
}
153+
$results = Add-DbaAgDatabase @splatAddAgDb
154+
}
155+
156+
It "Does warn" {
157+
$WarnVar | Should -Match ([regex]::Escape("Database [$nonexistingDb] is not found"))
158+
}
159+
160+
It "Does not return results" {
161+
$results | Should -BeNullOrEmpty
84162
}
85163
}
86-
} #$TestConfig.instance2 for appveyor
164+
} #$TestConfig.instance2 for appveyor

tests/Add-DbaAgListener.Tests.ps1

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
22
param(
3-
$ModuleName = "dbatools",
4-
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
3+
$ModuleName = "dbatools",
4+
$PSDefaultParameterValues = $TestConfig.Defaults
55
)
66

77
Describe "Add-DbaAgListener" -Tag "UnitTests" {
@@ -40,40 +40,53 @@ Describe "Add-DbaAgListener" -Tag "UnitTests" {
4040

4141
Describe "Add-DbaAgListener" -Tag "IntegrationTests" {
4242
BeforeAll {
43-
$agname = "dbatoolsci_ag_newlistener"
44-
$listenerName = 'dbatoolsci_listener'
45-
$splatNewAg = @{
46-
Primary = $TestConfig.instance3
47-
Name = $agname
48-
ClusterType = "None"
43+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
44+
45+
$filesToRemove = @( )
46+
47+
# To add a listener to an availablity group, we need an availability group, an ip address and a port.
48+
# TODO: Add some negative tests.
49+
50+
$agName = "addagdb_group"
51+
$listenerName = "listener"
52+
$listenerIp = "127.0.20.1"
53+
$listenerPort = 14330
54+
55+
$splat = @{
56+
Primary = $TestConfig.instance3
57+
Name = $agName
58+
ClusterType = "None"
4959
FailoverMode = "Manual"
50-
Confirm = $false
51-
Certificate = "dbatoolsci_AGCert"
60+
Certificate = "dbatoolsci_AGCert"
5261
}
53-
$ag = New-DbaAvailabilityGroup @splatNewAg
62+
$ag = New-DbaAvailabilityGroup @splat
63+
64+
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
5465
}
5566

5667
AfterAll {
57-
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agname -Confirm:$false
68+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
69+
70+
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agName
71+
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
5872
}
5973

6074
Context "When creating a listener" {
6175
BeforeAll {
62-
$splatAddListener = @{
63-
Name = $listenerName
64-
IPAddress = "127.0.20.1"
65-
Port = 14330
66-
Confirm = $false
76+
$splat = @{
77+
Name = $listenerName
78+
IPAddress = $listenerIp
79+
Port = $listenerPort
6780
}
68-
$results = $ag | Add-DbaAgListener @splatAddListener
81+
$results = $ag | Add-DbaAgListener @splat
6982
}
7083

71-
AfterAll {
72-
$null = Remove-DbaAgListener -SqlInstance $TestConfig.instance3 -Listener $listenerName -AvailabilityGroup $agname -Confirm:$false
84+
It "Does not warn" {
85+
$WarnVar | Should -BeNullOrEmpty
7386
}
7487

7588
It "Returns results with proper data" {
76-
$results.PortNumber | Should -Be 14330
89+
$results.PortNumber | Should -Be $listenerPort
7790
}
7891
}
7992
} #$TestConfig.instance2 for appveyor

tests/appveyor.pester.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ if (-not $Finalize) {
240240
# Import pester 5
241241
Import-Module pester -RequiredVersion 5.6.1
242242
Write-Host -Object "appveyor.pester: Running with Pester Version $((Get-Command Invoke-Pester -ErrorAction SilentlyContinue).Version)" -ForegroundColor DarkGreen
243+
$TestConfig = Get-TestConfig
243244
$Counter = 0
244245
foreach ($f in $AllTestsWithinScenario) {
245246
$Counter += 1

tests/dbatools.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ Describe "$ModuleName Tests missing" -Tag 'Tests' {
153153
Context "Every function should have tests" {
154154
foreach ($f in $functions) {
155155
It "$($f.basename) has a tests.ps1 file" {
156-
Test-Path "tests\$($f.basename).tests.ps1" | Should Be $true
156+
Test-Path "$ModulePath\tests\$($f.basename).tests.ps1" | Should Be $true
157157
}
158-
If (Test-Path "tests\$($f.basename).tests.ps1") {
158+
If (Test-Path "$ModulePath\tests\$($f.basename).tests.ps1") {
159159
It "$($f.basename) has validate parameters unit test" {
160-
$testFile = Get-Content "tests\$($f.basename).Tests.ps1" -Raw
160+
$testFile = Get-Content "$ModulePath\tests\$($f.basename).Tests.ps1" -Raw
161161
$hasValidation = $testFile -match 'Context "Validate parameters"' -or $testFile -match 'Context "Parameter validation"'
162162
$hasValidation | Should -Be $true -Because "Test file must have parameter validation"
163163
}

0 commit comments

Comments
 (0)