From 08e3777720e33a6983d757f7e071a784bad35a0b Mon Sep 17 00:00:00 2001 From: ReeceGoding <67124261+ReeceGoding@users.noreply.github.com> Date: Sat, 21 Feb 2026 19:45:42 +0000 Subject: [PATCH] Adds many tests for #10183. Four of these fail. Although a lot of tests are added here, the bottom line is that this adds four failing tests. The four that fail check that we have the following documentation-claimed behaviors: * When `-Verify` is not specified, we should enable verify. * When `-Checksum` is not specified, but the instance has it on by default, we should enable it in the job. * When `-Checksum` is not specified, but the instance has it off by default, we should enable it in the job. * When `-Compress` is not specified, but the instance has it on by default, we should enable it in the job. We can fix these once we agree how. **Somebody will have to tell me how we want this done**. Most of this is mentioned in #10183. This adds a test context for "Defaults for unspecified parameters are as documentation says", including three tests * "Should have Verify parameter set to Y in backup jobs", which fails as #10183 predicts * "Should have StartTime set to 011500 in backup schedule", which passes * "Should have BackupLocation parameter set to instance default in backup jobs", which passes. The "Checksum tests when instance defaults to checksum on" and "Checksum tests when instance defaults to checksum off" contexts are expanded to test for `-CheckSum` on, off, and not specified. To accommodate this, we now uninstall the maintenance solution after each test in these contexts. In both contexts, only the "not specified" test fails. This is as #10183 predicts. Similarly, the two contexts "Compression tests when instance defaults to compression on" and "Compression tests when instance defaults to compression off" have been added and they test for `-Compress` on, off, and not specified. Among these, only the test where compression is turned on by default for the instance but not specified in the install command fails. --- .../Install-DbaMaintenanceSolution.Tests.ps1 | 686 +++++++++++++++++- 1 file changed, 653 insertions(+), 33 deletions(-) diff --git a/tests/Install-DbaMaintenanceSolution.Tests.ps1 b/tests/Install-DbaMaintenanceSolution.Tests.ps1 index fb8dde631b8c..1669ac61a84f 100644 --- a/tests/Install-DbaMaintenanceSolution.Tests.ps1 +++ b/tests/Install-DbaMaintenanceSolution.Tests.ps1 @@ -727,6 +727,129 @@ Describe $CommandName -Tag IntegrationTests { } } + # Both CheckSum and Compress are special, so not in this context. + # Aside from those, the documentation claims we have defaults for + # Verify, BackupLocation, and StartTime. + Context "Defaults for unspecified parameters are as documentation says" { + # Note the lack of the usual AfterEach block. + # We test schedules in one place here, so our usual trick is not applicable. + + BeforeAll { + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + # Clean up any leftover test databases from previous runs + $oldTestDbs = Get-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Name -like "dbatoolsci_maintenancesolution_*" + if ($oldTestDbs) { + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Database $oldTestDbs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + # We could test that the default database is master, + # but that is currently enforced at the PowerShell level + # so it could just be a unit test. + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + AutoScheduleJobs = "WeeklyFull" + } + $installResult = Install-DbaMaintenanceSolution @splatInstall + + # Verify installation succeeded before running tests + # Skip tests if installation failed (eg. due to event log limitations on AppVeyor or SQL Agent not running) + $script:installationSucceeded = $false + if ($installResult) { + $splatJobCheck = @{ + SqlInstance = $TestConfig.InstanceMulti2 + } + $fullBackupJob = Get-DbaAgentJob @splatJobCheck | Where-Object Name -eq "DatabaseBackup - USER_DATABASES - FULL" + if ($fullBackupJob) { + $script:installationSucceeded = $true + } + } + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + + AfterAll { + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Database $testDbName -Confirm:$false + $jobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($jobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $jobs.Name -Confirm:$false + } + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + + It "Should have Verify parameter set to Y in backup jobs" { + if (-not $script:installationSucceeded) { + Set-ItResult -Skipped -Because "Installation failed" + return + } + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Verify = 'Y'" + } + + It "Should have StartTime set to 011500 in backup schedule" { + if (-not $script:installationSucceeded) { + Set-ItResult -Skipped -Because "Installation failed" + return + } + $schedule = Get-DbaAgentSchedule -SqlInstance $TestConfig.InstanceMulti2 + # We do not make any promises about job names, + # so checking for times is all we can do. + $schedule.ActiveStartTimeOfDay | + Where-Object { $_.Hours -eq 1 -and $_.Minutes -eq 15 } | + Should -Not -BeNullOrEmpty + } + + It "Should have BackupLocation parameter set to instance default in backup jobs" { + if (-not $script:installationSucceeded) { + Set-ItResult -Skipped -Because "Installation failed" + return + } + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $backupLocationSetting = (Get-DbaDefaultPath -SqlInstance $TestConfig.InstanceMulti2).Backup + $jobStep.Command | Should -BeLike "*@Directory = *$backupLocationSetting*" + } + } + # This case is special. We try to make the install fail. Context "Backup to Nul with Verify on" { BeforeAll { @@ -789,27 +912,57 @@ Describe $CommandName -Tag IntegrationTests { BackupLocation = "NUL" EnableException = $true } - $installResult = { Install-DbaMaintenanceSolution @splatInstall } | Should -Throw -ExpectedMessage '*NUL*' + $installResult = { Install-DbaMaintenanceSolution @splatInstall } | Should -Throw -ExpectedMessage "*NUL*" } } - # Ola checks sys.configurations in dbo.DatabaseBackup. - # This should not impact the Agent Jobs, but it will not hurt to check. + # Our documentation claims that we always turn CheckSum on unless + # it is deliberately turned off, so we must test that. + # However, Ola checks sys.configurations in dbo.DatabaseBackup. + # This should not impact the Agent Jobs, but we should make sure that + # there is no conflict. Context "Checksum tests when instance defaults to checksum on" { AfterEach { + # Notice that we are uninstalling after each test. Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti2 -Query $jobStep.Command -NoExec -EnableException + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName } BeforeAll { $PSDefaultParameterValues["*-Dba*:EnableException"] = $true $checksumSettingInitial = (Get-DbaSpConfigure -SqlInstance $TestConfig.InstanceMulti2 -ConfigName BackupChecksumDefault).ConfiguredValue - + # Throws if we already have the setting as we want it to be, so SilentlyContinue instead. $splatConfigure = @{ SqlInstance = $TestConfig.InstanceMulti2 - ConfigName = 'BackupChecksumDefault' + ConfigName = "BackupChecksumDefault" Value = 1 - WarningAction = 'SilentlyContinue' + WarningAction = "SilentlyContinue" EnableException = $false } Set-DbaSpConfigure @splatConfigure @@ -846,17 +999,13 @@ Describe $CommandName -Tag IntegrationTests { $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + # We uninstall the scripts after each run, + # so need it installed first. $splatInstall = @{ SqlInstance = $TestConfig.InstanceMulti2 Database = $testDbName InstallJobs = $true ReplaceExisting = $true - CleanupTime = 168 - ChangeBackupType = $false - Compress = $false - CopyOnly = $false - Verify = $false - CheckSum = $false } $installResult = Install-DbaMaintenanceSolution @splatInstall @@ -885,12 +1034,11 @@ Describe $CommandName -Tag IntegrationTests { $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $jobs.Name -Confirm:$false } - # Throws if we already have the setting as we want it to be, so SilentlyContinue instead. $splatConfigure = @{ SqlInstance = $TestConfig.InstanceMulti2 - ConfigName = 'BackupChecksumDefault' + ConfigName = "BackupChecksumDefault" Value = $checksumSettingInitial - WarningAction = 'SilentlyContinue' + WarningAction = "SilentlyContinue" EnableException = $false } Set-DbaSpConfigure @splatConfigure @@ -898,11 +1046,34 @@ Describe $CommandName -Tag IntegrationTests { $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } - It "Should have CheckSum parameter set to N in backup jobs" { - if (-not $script:installationSucceeded) { - Set-ItResult -Skipped -Because "Installation failed" - return + It "Should have CheckSum parameter set to Y in backup jobs when we ask for it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + CheckSum = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@CheckSum = 'Y'" + } + + It "Should have CheckSum parameter set to N in backup jobs when we refuse it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + CheckSum = $false } + Install-DbaMaintenanceSolution @splatInstall + $splatJobStep = @{ SqlInstance = $TestConfig.InstanceMulti2 Job = "DatabaseBackup - USER_DATABASES - FULL" @@ -910,11 +1081,57 @@ Describe $CommandName -Tag IntegrationTests { $jobStep = Get-DbaAgentJobStep @splatJobStep $jobStep.Command | Should -Match "@CheckSum = 'N'" } + + # Our documentation says we always turn CheckSum on unless + # we deliberately ask to not have it. + It "Should have CheckSum parameter set to Y in backup jobs when we do not specify it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@CheckSum = 'Y'" + } } + # See previous context. Context "Checksum tests when instance defaults to checksum off" { AfterEach { Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti2 -Query $jobStep.Command -NoExec -EnableException + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName } BeforeAll { @@ -924,9 +1141,9 @@ Describe $CommandName -Tag IntegrationTests { # Throws if we already have the setting as we want it to be, so SilentlyContinue instead. $splatConfigure = @{ SqlInstance = $TestConfig.InstanceMulti2 - ConfigName = 'BackupChecksumDefault' + ConfigName = "BackupChecksumDefault" Value = 0 - WarningAction = 'SilentlyContinue' + WarningAction = "SilentlyContinue" EnableException = $false } Set-DbaSpConfigure @splatConfigure @@ -963,17 +1180,13 @@ Describe $CommandName -Tag IntegrationTests { $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + # We uninstall the scripts after each run, + # so need it installed first. $splatInstall = @{ SqlInstance = $TestConfig.InstanceMulti2 Database = $testDbName InstallJobs = $true ReplaceExisting = $true - CleanupTime = 168 - ChangeBackupType = $false - Compress = $false - CopyOnly = $false - Verify = $false - CheckSum = $true } $installResult = Install-DbaMaintenanceSolution @splatInstall @@ -1004,9 +1217,9 @@ Describe $CommandName -Tag IntegrationTests { $splatConfigure = @{ SqlInstance = $TestConfig.InstanceMulti2 - ConfigName = 'BackupChecksumDefault' + ConfigName = "BackupChecksumDefault" Value = $checksumSettingInitial - WarningAction = 'SilentlyContinue' + WarningAction = "SilentlyContinue" EnableException = $false } Set-DbaSpConfigure @splatConfigure @@ -1014,11 +1227,53 @@ Describe $CommandName -Tag IntegrationTests { $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } - It "Should have CheckSum parameter set to Y in backup jobs" { - if (-not $script:installationSucceeded) { - Set-ItResult -Skipped -Because "Installation failed" - return + It "Should have CheckSum parameter set to Y in backup jobs when we ask for it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + CheckSum = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@CheckSum = 'Y'" + } + + It "Should have CheckSum parameter set to N in backup jobs when we refuse it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + CheckSum = $false + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@CheckSum = 'N'" + } + + # Our documentation says we always turn CheckSum on unless + # we deliberately ask to not have it. + It "Should have CheckSum parameter set to Y in backup jobs when we do not specify it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true } + Install-DbaMaintenanceSolution @splatInstall + $splatJobStep = @{ SqlInstance = $TestConfig.InstanceMulti2 Job = "DatabaseBackup - USER_DATABASES - FULL" @@ -1027,4 +1282,369 @@ Describe $CommandName -Tag IntegrationTests { $jobStep.Command | Should -Match "@CheckSum = 'Y'" } } + + # Same idea as the previous two contexts. + # However, this time we copy from sys.configurations. + Context "Compression tests when instance defaults to compression on" { + AfterEach { + # Notice that we are uninstalling after each test. + Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti2 -Query $jobStep.Command -NoExec -EnableException + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + } + + BeforeAll { + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $compressionSettingInitial = (Get-DbaSpConfigure -SqlInstance $TestConfig.InstanceMulti2 -ConfigName BackupChecksumDefault).ConfiguredValue + $splatConfigure = @{ + SqlInstance = $TestConfig.InstanceMulti2 + ConfigName = "DefaultBackupCompression" + Value = 1 + WarningAction = "SilentlyContinue" + EnableException = $false + } + Set-DbaSpConfigure @splatConfigure + + # Clean up any leftover test databases from previous runs + $oldTestDbs = Get-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Name -like "dbatoolsci_maintenancesolution_*" + if ($oldTestDbs) { + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Database $oldTestDbs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + + # We uninstall the scripts after each run, + # so need it installed first. + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + } + $installResult = Install-DbaMaintenanceSolution @splatInstall + + # Verify installation succeeded before running tests + # Skip tests if installation failed (eg. due to event log limitations on AppVeyor or SQL Agent not running) + $script:installationSucceeded = $false + if ($installResult) { + $splatJobCheck = @{ + SqlInstance = $TestConfig.InstanceMulti2 + } + $fullBackupJob = Get-DbaAgentJob @splatJobCheck | Where-Object Name -eq "DatabaseBackup - USER_DATABASES - FULL" + if ($fullBackupJob) { + $script:installationSucceeded = $true + } + } + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + + AfterAll { + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Database $testDbName -Confirm:$false + $jobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($jobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $jobs.Name -Confirm:$false + } + + # Throws if we already have the setting as we want it to be, so SilentlyContinue instead. + $splatConfigure = @{ + SqlInstance = $TestConfig.InstanceMulti2 + ConfigName = "DefaultBackupCompression" + Value = $compressionSettingInitial + WarningAction = "SilentlyContinue" + EnableException = $false + } + Set-DbaSpConfigure @splatConfigure + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + + It "Should have Compress parameter set to Y in backup jobs when we ask for it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + Compress = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Compress = 'Y'" + } + + It "Should have Compress parameter set to N in backup jobs when we refuse it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + Compress = $false + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Compress = 'N'" + } + + # Our documentation says we copy from the configuration + # of the instance when Compress is not specified. + It "Should have Compress parameter set to Y in backup jobs when we do not specify it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Compress = 'Y'" + } + } + + # See previous context. + Context "Compression tests when instance defaults to compression off" { + AfterEach { + # Notice that we are uninstalling after each test. + Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti2 -Query $jobStep.Command -NoExec -EnableException + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + } + + BeforeAll { + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $compressionSettingInitial = (Get-DbaSpConfigure -SqlInstance $TestConfig.InstanceMulti2 -ConfigName BackupChecksumDefault).ConfiguredValue + $splatConfigure = @{ + SqlInstance = $TestConfig.InstanceMulti2 + ConfigName = "DefaultBackupCompression" + Value = 0 + WarningAction = "SilentlyContinue" + EnableException = $false + } + Set-DbaSpConfigure @splatConfigure + + # Clean up any leftover test databases from previous runs + $oldTestDbs = Get-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Name -like "dbatoolsci_maintenancesolution_*" + if ($oldTestDbs) { + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Database $oldTestDbs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren jobs + $oldJobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($oldJobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $oldJobs.Name -Confirm:$false + } + + # Clean up any leftover Hallengren procedures in tempdb from the first Context + $cleanupTempdb = " + IF OBJECT_ID('dbo.CommandExecute', 'P') IS NOT NULL DROP PROCEDURE dbo.CommandExecute; + IF OBJECT_ID('dbo.DatabaseBackup', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseBackup; + IF OBJECT_ID('dbo.DatabaseIntegrityCheck', 'P') IS NOT NULL DROP PROCEDURE dbo.DatabaseIntegrityCheck; + IF OBJECT_ID('dbo.IndexOptimize', 'P') IS NOT NULL DROP PROCEDURE dbo.IndexOptimize; + IF OBJECT_ID('dbo.CommandLog', 'U') IS NOT NULL DROP TABLE dbo.CommandLog; + IF OBJECT_ID('dbo.Queue', 'U') IS NOT NULL DROP TABLE dbo.Queue; + IF OBJECT_ID('dbo.QueueDatabase', 'U') IS NOT NULL DROP TABLE dbo.QueueDatabase; + " + $splatCleanupTempdb = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = "tempdb" + Query = $cleanupTempdb + } + Invoke-DbaQuery @splatCleanupTempdb + + $testDbName = "dbatoolsci_maintenancesolution_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Name $testDbName + + # We uninstall the scripts after each run, + # so need it installed first. + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + } + $installResult = Install-DbaMaintenanceSolution @splatInstall + + # Verify installation succeeded before running tests + # Skip tests if installation failed (eg. due to event log limitations on AppVeyor or SQL Agent not running) + $script:installationSucceeded = $false + if ($installResult) { + $splatJobCheck = @{ + SqlInstance = $TestConfig.InstanceMulti2 + } + $fullBackupJob = Get-DbaAgentJob @splatJobCheck | Where-Object Name -eq "DatabaseBackup - USER_DATABASES - FULL" + if ($fullBackupJob) { + $script:installationSucceeded = $true + } + } + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + + AfterAll { + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceMulti2 -Database $testDbName -Confirm:$false + $jobs = Get-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 | Where-Object Description -match "hallengren" + if ($jobs) { + $null = Remove-DbaAgentJob -SqlInstance $TestConfig.InstanceMulti2 -Job $jobs.Name -Confirm:$false + } + + # Throws if we already have the setting as we want it to be, so SilentlyContinue instead. + $splatConfigure = @{ + SqlInstance = $TestConfig.InstanceMulti2 + ConfigName = "DefaultBackupCompression" + Value = $compressionSettingInitial + WarningAction = "SilentlyContinue" + EnableException = $false + } + Set-DbaSpConfigure @splatConfigure + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + + It "Should have Compress parameter set to Y in backup jobs when we ask for it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + Compress = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Compress = 'Y'" + } + + It "Should have Compress parameter set to N in backup jobs when we refuse it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + Compress = $false + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Compress = 'N'" + } + + # Our documentation says we copy from the configuration + # of the instance when Compress is not specified. + It "Should have Compress parameter set to N in backup jobs when we do not specify it" { + $splatInstall = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Database = $testDbName + InstallJobs = $true + ReplaceExisting = $true + } + Install-DbaMaintenanceSolution @splatInstall + + $splatJobStep = @{ + SqlInstance = $TestConfig.InstanceMulti2 + Job = "DatabaseBackup - USER_DATABASES - FULL" + } + $jobStep = Get-DbaAgentJobStep @splatJobStep + $jobStep.Command | Should -Match "@Compress = 'N'" + } + } }