@@ -32,12 +32,59 @@ Describe $CommandName -Tag UnitTests {
32
32
33
33
Describe $CommandName -Tag IntegrationTests {
34
34
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')
36
70
}
37
71
38
72
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.
40
85
}
86
+
87
+ # Integration tests here
41
88
}
42
89
```
43
90
0 commit comments