Skip to content

Commit ad2a5b8

Browse files
More tests in the new pester 5 design (#9753)
1 parent c449253 commit ad2a5b8

17 files changed

+178
-221
lines changed

tests/Add-DbaAgDatabase.Tests.ps1

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
33
$ModuleName = "dbatools",
4-
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
4+
$CommandName = "Add-DbaAgDatabase",
55
# $TestConfig has to be set outside of the tests by running: $TestConfig = Get-TestConfig
66
# This will set $TestConfig.Defaults with the parameter defaults, including:
77
# * Confirm = $false
@@ -13,9 +13,9 @@ param(
1313
Describe $CommandName -Tag "UnitTests" {
1414
Context "Parameter validation" {
1515
BeforeAll {
16-
$command = Get-Command Add-DbaAgDatabase
17-
$expected = $TestConfig.CommonParameters
18-
$expected += @(
16+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
17+
$expectedParameters = $TestConfig.CommonParameters
18+
$expectedParameters += @(
1919
"SqlInstance",
2020
"SqlCredential",
2121
"AvailabilityGroup",
@@ -27,19 +27,12 @@ Describe $CommandName -Tag "UnitTests" {
2727
"SharedPath",
2828
"UseLastBackup",
2929
"AdvancedBackupParams",
30-
"EnableException",
31-
"Confirm",
32-
"WhatIf"
30+
"EnableException"
3331
)
3432
}
3533

36-
It "Has parameter: <_>" -ForEach $expected {
37-
$command | Should -HaveParameter $PSItem
38-
}
39-
40-
It "Should have exactly the number of expected parameters ($($expected.Count))" {
41-
$hasparms = $command.Parameters.Values.Name
42-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
34+
It "Should have the expected parameters" {
35+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
4336
}
4437
}
4538
}
@@ -49,8 +42,10 @@ Describe $CommandName -Tag "IntegrationTests" {
4942
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
5043
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
5144

52-
# Collect all the created files to be able to remove them in the AfterAll block.
53-
$filesToRemove = @( )
45+
# For all the backups that we want to clean up after the test, we create a directory that we can delete at the end.
46+
# Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups.
47+
$backupPath = "$($TestConfig.Temp)\$CommandName-$(Get-Random)"
48+
$null = New-Item -Path $backupPath -ItemType Directory
5449

5550
# Explain what needs to be set up for the test:
5651
# To add a database to an availablity group, we need an availability group and a database that has been backed up.
@@ -73,8 +68,7 @@ Describe $CommandName -Tag "IntegrationTests" {
7368
$null = New-DbaAvailabilityGroup @splat
7469

7570
$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithBackup
76-
$backup = Backup-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup -Path $TestConfig.Temp
77-
$filesToRemove += $backup.Path
71+
$null = Backup-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup -Path $backupPath
7872

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

@@ -91,8 +85,8 @@ Describe $CommandName -Tag "IntegrationTests" {
9185
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
9286
$null = Remove-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup, $existingDbWithoutBackup
9387

94-
# Remove all created files.
95-
Remove-Item -Path $filesToRemove
88+
# Remove the backup directory.
89+
Remove-Item -Path $backupPath -Recurse
9690

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

tests/Add-DbaAgListener.Tests.ps1

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
33
$ModuleName = "dbatools",
4-
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
4+
$CommandName = "Add-DbaAgListener",
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
77

88
Describe $CommandName -Tag "UnitTests" {
99
Context "Parameter validation" {
1010
BeforeAll {
11-
$command = Get-Command Add-DbaAgListener
12-
$expected = $TestConfig.CommonParameters
13-
$expected += @(
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
1414
"SqlInstance",
1515
"SqlCredential",
1616
"AvailabilityGroup",
@@ -22,19 +22,12 @@ Describe $CommandName -Tag "UnitTests" {
2222
"Dhcp",
2323
"Passthru",
2424
"InputObject",
25-
"EnableException",
26-
"Confirm",
27-
"WhatIf"
25+
"EnableException"
2826
)
2927
}
3028

31-
It "Has parameter: <_>" -ForEach $expected {
32-
$command | Should -HaveParameter $PSItem
33-
}
34-
35-
It "Should have exactly the number of expected parameters ($($expected.Count))" {
36-
$hasparms = $command.Parameters.Values.Name
37-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
29+
It "Should have the expected parameters" {
30+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
3831
}
3932
}
4033
}
@@ -43,7 +36,8 @@ Describe $CommandName -Tag "IntegrationTests" {
4336
BeforeAll {
4437
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
4538

46-
$filesToRemove = @( )
39+
$backupPath = "$($TestConfig.Temp)\$CommandName-$(Get-Random)"
40+
$null = New-Item -Path $backupPath -ItemType Directory
4741

4842
# To add a listener to an availablity group, we need an availability group, an ip address and a port.
4943
# TODO: Add some negative tests.
@@ -70,6 +64,8 @@ Describe $CommandName -Tag "IntegrationTests" {
7064

7165
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agName
7266
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
67+
68+
Remove-Item -Path $backupPath -Recurse
7369
}
7470

7571
Context "When creating a listener" {

tests/Add-DbaAgReplica.Tests.ps1

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ param(
88
Describe $CommandName -Tag "UnitTests" {
99
Context "Parameter validation" {
1010
BeforeAll {
11-
$command = Get-Command Add-DbaAgReplica
12-
$expected = $TestConfig.CommonParameters
13-
$expected += @(
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
1414
"SqlInstance",
1515
"SqlCredential",
1616
"Name",
@@ -30,40 +30,39 @@ Describe $CommandName -Tag "UnitTests" {
3030
"ConfigureXESession",
3131
"SessionTimeout",
3232
"InputObject",
33-
"EnableException",
34-
"Confirm",
35-
"WhatIf"
33+
"EnableException"
3634
)
3735
}
3836

39-
It "Has parameter: <_>" -ForEach $expected {
40-
$command | Should -HaveParameter $PSItem
41-
}
42-
43-
It "Should have exactly the number of expected parameters ($($expected.Count))" {
44-
$hasparms = $command.Parameters.Values.Name
45-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
37+
It "Should have the expected parameters" {
38+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
4639
}
4740
}
4841
}
4942

5043
Describe $CommandName -Tag "IntegrationTests" {
5144
BeforeAll {
45+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
46+
5247
$primaryAgName = "dbatoolsci_agroup"
53-
$splatPrimary = @{
48+
$splat = @{
5449
Primary = $TestConfig.instance3
5550
Name = $primaryAgName
5651
ClusterType = "None"
5752
FailoverMode = "Manual"
5853
Certificate = "dbatoolsci_AGCert"
59-
Confirm = $false
6054
}
61-
$primaryAg = New-DbaAvailabilityGroup @splatPrimary
55+
$primaryAg = New-DbaAvailabilityGroup @splat
6256
$replicaName = $primaryAg.PrimaryReplica
57+
58+
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
6359
}
6460

6561
AfterAll {
66-
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $primaryAgName -Confirm:$false
62+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
63+
64+
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $primaryAgName
65+
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
6766
}
6867

6968
Context "When adding AG replicas" {
@@ -75,11 +74,14 @@ Describe $CommandName -Tag "IntegrationTests" {
7574
ClusterType = "None"
7675
FailoverMode = "Manual"
7776
Certificate = "dbatoolsci_AGCert"
78-
Confirm = $false
7977
}
8078
$replicaAg = New-DbaAvailabilityGroup @splatRepAg
8179
}
8280

81+
AfterAll {
82+
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $replicaAgName
83+
}
84+
8385
It "Returns results with proper data" {
8486
$results = Get-DbaAgReplica -SqlInstance $TestConfig.instance3
8587
$results.AvailabilityGroup | Should -Contain $replicaAgName

tests/Add-DbaComputerCertificate.Tests.ps1

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ param(
88
Describe $CommandName -Tag "UnitTests" {
99
Context "Parameter validation" {
1010
BeforeAll {
11-
$command = Get-Command Add-DbaComputerCertificate
12-
$expected = $TestConfig.CommonParameters
13-
$expected += @(
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
1414
"ComputerName",
1515
"Credential",
1616
"SecurePassword",
@@ -19,19 +19,12 @@ Describe $CommandName -Tag "UnitTests" {
1919
"Store",
2020
"Folder",
2121
"Flag",
22-
"EnableException",
23-
"Confirm",
24-
"WhatIf"
22+
"EnableException"
2523
)
2624
}
2725

28-
It "Has parameter: <_>" -ForEach $expected {
29-
$command | Should -HaveParameter $PSItem
30-
}
31-
32-
It "Should have exactly the number of expected parameters ($($expected.Count))" {
33-
$hasparms = $command.Parameters.Values.Name
34-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
26+
It "Should have the expected parameters" {
27+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
3528
}
3629
}
3730
}
@@ -40,19 +33,20 @@ Describe $CommandName -Tag "IntegrationTests" {
4033
Context "Certificate is added properly" {
4134
BeforeAll {
4235
$certPath = "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt"
43-
$results = Add-DbaComputerCertificate -Path $certPath -Confirm:$false
36+
$certThumbprint = "29C469578D6C6211076A09CEE5C5797EEA0C2713"
37+
$results = Add-DbaComputerCertificate -Path $certPath
38+
}
39+
40+
AfterAll {
41+
Remove-DbaComputerCertificate -Thumbprint $certThumbprint
4442
}
4543

4644
It "Should show the proper thumbprint has been added" {
47-
$results.Thumbprint | Should -Be "29C469578D6C6211076A09CEE5C5797EEA0C2713"
45+
$results.Thumbprint | Should -Be $certThumbprint
4846
}
4947

5048
It "Should be in LocalMachine\My Cert Store" {
5149
$results.PSParentPath | Should -Be "Microsoft.PowerShell.Security\Certificate::LocalMachine\My"
5250
}
53-
54-
AfterAll {
55-
Remove-DbaComputerCertificate -Thumbprint 29C469578D6C6211076A09CEE5C5797EEA0C2713 -Confirm:$false
56-
}
5751
}
5852
}

tests/Add-DbaDbMirrorMonitor.Tests.ps1

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,29 @@ param(
88
Describe $CommandName -Tag "UnitTests" {
99
Context "Parameter validation" {
1010
BeforeAll {
11-
$command = Get-Command Add-DbaDbMirrorMonitor
12-
$expected = $TestConfig.CommonParameters
13-
$expected += @(
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
1414
"SqlInstance",
1515
"SqlCredential",
16-
"EnableException",
17-
"Confirm",
18-
"WhatIf"
16+
"EnableException"
1917
)
2018
}
2119

22-
It "Has parameter: <_>" -ForEach $expected {
23-
$command | Should -HaveParameter $PSItem
24-
}
25-
26-
It "Should have exactly the number of expected parameters ($($expected.Count))" {
27-
$hasparms = $command.Parameters.Values.Name
28-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
20+
It "Should have the expected parameters" {
21+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
2922
}
3023
}
3124
}
3225

3326
Describe $CommandName -Tag "IntegrationTests" {
34-
AfterAll {
35-
$null = Remove-DbaDbMirrorMonitor -SqlInstance $TestConfig.instance2 -WarningAction SilentlyContinue
36-
}
37-
3827
Context "When adding mirror monitor" {
3928
BeforeAll {
40-
$results = Add-DbaDbMirrorMonitor -SqlInstance $TestConfig.instance2 -WarningAction SilentlyContinue
29+
$results = Add-DbaDbMirrorMonitor -SqlInstance $TestConfig.instance2
30+
}
31+
32+
AfterAll {
33+
$null = Remove-DbaDbMirrorMonitor -SqlInstance $TestConfig.instance2
4134
}
4235

4336
It "Adds the mirror monitor" {

0 commit comments

Comments
 (0)