From b77fd8cc2f2885be67677bc5da623c2145fc2b38 Mon Sep 17 00:00:00 2001 From: Andreas Jordan Date: Sat, 8 Aug 2026 18:31:43 +0200 Subject: [PATCH] Tests - Stop leaving backup files behind in the lab Three test files left backups behind, and because the environment check reports every file it finds after every test file, one leftover from the 34th file made 709 of 742 files in a full run look like they had left something in the lab. Connect-DbaInstance backed msdb up without a path, so the backup landed in the default backup folder of the instance. That is a path on the SQL Server, so the Remove-Item that was already there ran against a path that does not exist on the machine running the tests and silently did nothing. It backs up to the shared temp folder now, where the cleanup can actually reach it. Copy-DbaDatabase already said in a comment that the backups go to a directory that gets deleted at the end, but the variable pointed at the shared temp folder itself and nothing deleted anything. It now creates a directory per run and removes it in AfterAll. Invoke-DbaDbMirroring left the full backup and the log backup it uses to seed the mirror. Verified in the lab: the three files pass and the environment check reports no leftovers. (do Connect-DbaInstance, Copy-DbaDatabase, Invoke-DbaDbMirroring) Co-Authored-By: Claude Opus 5 (1M context) --- tests/Connect-DbaInstance.Tests.ps1 | 9 +++++++-- tests/Copy-DbaDatabase.Tests.ps1 | 7 ++++++- tests/Invoke-DbaDbMirroring.Tests.ps1 | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/Connect-DbaInstance.Tests.ps1 b/tests/Connect-DbaInstance.Tests.ps1 index e033ee52d8c5..0e72fee1cac0 100644 --- a/tests/Connect-DbaInstance.Tests.ps1 +++ b/tests/Connect-DbaInstance.Tests.ps1 @@ -526,12 +526,17 @@ Describe $CommandName -Tag IntegrationTests { It "clones when using Backup-DabInstace" { $server = Connect-DbaInstance -SqlInstance $TestConfig.InstanceMulti1 -Database tempdb - $results = Backup-DbaDatabase -SqlInstance $server -Database msdb + # The backups have to go to the shared temp folder. Without a path they land in the default + # backup folder of the instance, which is a path on the SQL Server, so the Remove-Item below + # runs against a path that does not exist on the machine running the tests and silently does + # nothing. The backup was then left behind on every remote instance, and because this file + # runs early in the suite, every later test file reported it as a leftover of its own. + $results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp if ($results.FullName) { Remove-Item -Path $results.FullName -ErrorAction SilentlyContinue } - $results = Backup-DbaDatabase -SqlInstance $server -Database msdb -WarningVariable warn + $results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp -WarningVariable warn $warn | Should -BeNullOrEmpty if ($results.FullName) { diff --git a/tests/Copy-DbaDatabase.Tests.ps1 b/tests/Copy-DbaDatabase.Tests.ps1 index 0dc9a6fa1a48..70a7b7012ba6 100644 --- a/tests/Copy-DbaDatabase.Tests.ps1 +++ b/tests/Copy-DbaDatabase.Tests.ps1 @@ -55,8 +55,9 @@ Describe $CommandName -Tag IntegrationTests { # For all the backups that we want to clean up after the test, we create a directory that we can delete at the end. # Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups. - $NetworkPath = $TestConfig.Temp $random = Get-Random + $NetworkPath = Join-Path -Path $TestConfig.Temp -ChildPath "dbatoolsci_copydatabase$random" + $null = New-Item -Path $NetworkPath -ItemType Directory -Force $backuprestoredb = "dbatoolsci_backuprestore$random" $backuprestoredb2 = "dbatoolsci_backuprestoreother$random" $detachattachdb = "dbatoolsci_detachattach$random" @@ -106,6 +107,10 @@ Describe $CommandName -Tag IntegrationTests { } Remove-DbaDatabase @splatRemoveSupport -ErrorAction SilentlyContinue + # The backups taken during the tests stay behind otherwise, and every test file that runs + # afterwards then reports them as leftovers of its own. + Remove-Item -Path $NetworkPath -Recurse -Force -ErrorAction SilentlyContinue + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } diff --git a/tests/Invoke-DbaDbMirroring.Tests.ps1 b/tests/Invoke-DbaDbMirroring.Tests.ps1 index 56069471bfd1..b2ffe700553c 100644 --- a/tests/Invoke-DbaDbMirroring.Tests.ps1 +++ b/tests/Invoke-DbaDbMirroring.Tests.ps1 @@ -58,6 +58,10 @@ Describe $CommandName -Tag IntegrationTests { $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceCopy1, $TestConfig.InstanceCopy2 -Database $dbName $null = Remove-DbaEndpoint -SqlInstance $TestConfig.InstanceCopy1, $TestConfig.InstanceCopy2 -EndPoint $endpointName + # Seeding the mirror leaves the full backup and the log backup in the shared folder, and every + # test file that runs afterwards then reports them as leftovers of its own. + Get-ChildItem -Path $TestConfig.Temp -Filter "$dbName*" | Remove-Item -Force -ErrorAction SilentlyContinue + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") }