Skip to content

Commit ea30031

Browse files
Enhance integration test setup and cleanup steps
Expanded the BeforeAll and AfterAll blocks in the integration test style guide to include detailed setup and cleanup procedures for availability groups and databases. Added comments for clarity and ensured proper handling of test artifacts and PowerShell parameter defaults.
1 parent 5b8068a commit ea30031

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

.aitools/prompts/style.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,59 @@ Describe $CommandName -Tag UnitTests {
3232
3333
Describe $CommandName -Tag IntegrationTests {
3434
BeforeAll {
35-
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
35+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
36+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
37+
38+
# For all the backups that we want to clean up after the test, we create a directory that we can delete at the end.
39+
# Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups.
40+
$backupPath = "$($TestConfig.Temp)\$CommandName-$(Get-Random)"
41+
$null = New-Item -Path $backupPath -ItemType Directory
42+
43+
# Explain what needs to be set up for the test:
44+
# To add a database to an availablity group, we need an availability group and a database that has been backed up.
45+
# For negative tests, we need a database without a backup and a non existing database.
46+
47+
# Set variables. They are available in all the It blocks.
48+
$agName = "addagdb_group"
49+
$existingDbWithBackup = "dbWithBackup"
50+
$existingDbWithoutBackup = "dbWithoutBackup"
51+
$nonexistingDb = "dbdoesnotexist"
52+
53+
# Create the objects.
54+
$splat = @{
55+
Primary = $TestConfig.instance3
56+
Name = $agName
57+
ClusterType = "None"
58+
FailoverMode = "Manual"
59+
Certificate = "dbatoolsci_AGCert"
60+
}
61+
$null = New-DbaAvailabilityGroup @splat
62+
63+
$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithBackup
64+
$null = Backup-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup -Path $backupPath
65+
66+
$null = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $existingDbWithoutBackup
67+
68+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
69+
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
3670
}
3771
3872
AfterAll {
39-
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
73+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
74+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
75+
76+
# Cleanup all created object.
77+
$null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agName
78+
$null = Get-DbaEndpoint -SqlInstance $TestConfig.instance3 -Type DatabaseMirroring | Remove-DbaEndpoint
79+
$null = Remove-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $existingDbWithBackup, $existingDbWithoutBackup
80+
81+
# Remove the backup directory.
82+
Remove-Item -Path $backupPath -Recurse
83+
84+
# As this is the last block we do not need to reset the $PSDefaultParameterValues.
4085
}
86+
87+
# Integration tests here
4188
}
4289
```
4390

0 commit comments

Comments
 (0)