diff --git a/tests/Get-DbaDbQueryStoreOption.Tests.ps1 b/tests/Get-DbaDbQueryStoreOption.Tests.ps1 index 4ab707ed7c8..95b32dd0cd6 100644 --- a/tests/Get-DbaDbQueryStoreOption.Tests.ps1 +++ b/tests/Get-DbaDbQueryStoreOption.Tests.ps1 @@ -37,10 +37,27 @@ Describe $CommandName -Tag IntegrationTests { $serverSingle = Connect-DbaInstance -SqlInstance $TestConfig.InstanceSingle + # Created here rather than in the Context that uses it, because the Contexts below read Query Store + # on model and leave a session parked in it, and the next CREATE DATABASE then fails with + # "Could not obtain exclusive lock on database 'model'". That is the leak of #10584, and until it + # is merged the only way past it is to create the database before anything has touched model. + $queryStoreDbName = "dbatoolsci_qso_$(Get-Random)" + $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Name $queryStoreDbName + $null = Set-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -State ReadWrite + # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings. $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } + AfterAll { + # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails. + $PSDefaultParameterValues["*-Dba*:EnableException"] = $true + + $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -ErrorAction SilentlyContinue + + $PSDefaultParameterValues.Remove("*-Dba*:EnableException") + } + Context "When a system database is named explicitly" { It "Warns about master and tempdb instead of silently returning nothing" { $resultsSystemDb = Get-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database master, tempdb -WarningVariable warnSystemDb -WarningAction SilentlyContinue @@ -76,23 +93,12 @@ Describe $CommandName -Tag IntegrationTests { BeforeAll { $PSDefaultParameterValues["*-Dba*:EnableException"] = $true - $queryStoreDbName = "dbatoolsci_qso_$(Get-Random)" - $null = New-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Name $queryStoreDbName - $null = Set-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -State ReadWrite - + # The database itself comes from the BeforeAll of the Describe, see the note there. $resultsFromSmo = Get-DbaDbQueryStoreOption -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName $PSDefaultParameterValues.Remove("*-Dba*:EnableException") } - AfterAll { - $PSDefaultParameterValues["*-Dba*:EnableException"] = $true - - $null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Database $queryStoreDbName -ErrorAction SilentlyContinue - - $PSDefaultParameterValues.Remove("*-Dba*:EnableException") - } - It "Leaves MaxPlansPerQuery and WaitStatsCaptureMode as the properties of the SMO object" { # Both are real properties of QueryStoreOptions. Adding them with Add-Member replaces the # property with a note property on the object of the caller - one that keeps its value even