Skip to content

Commit cae8c4d

Browse files
Tests - Case sensitive lookups and backup cleanup in seven test files (#10670)
1 parent 9728737 commit cae8c4d

7 files changed

Lines changed: 93 additions & 23 deletions

tests/Connect-DbaInstance.Tests.ps1

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,16 +929,33 @@ Describe $CommandName -Tag IntegrationTests {
929929
# runs against a path that does not exist on the machine running the tests and silently does
930930
# nothing. The backup was then left behind on every remote instance, and because this file
931931
# runs early in the suite, every later test file reported it as a leftover of its own.
932-
$results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp
932+
# Each backup gets its own file name, because two backups within the same minute share the
933+
# default name and the second one appends to the first.
934+
$backupFiles = @()
935+
$results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp -FilePath "msdb_$(Get-Random).bak"
933936
if ($results.FullName) {
934-
Remove-Item -Path $results.FullName -ErrorAction SilentlyContinue
937+
$backupFiles += $results.FullName
935938
}
936939

937-
$results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp -WarningVariable warn
940+
$results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp -FilePath "msdb_$(Get-Random).bak" -WarningVariable warn
938941
$warn | Should -BeNullOrEmpty
939942

940943
if ($results.FullName) {
941-
Remove-Item -Path $results.FullName -ErrorAction SilentlyContinue
944+
$backupFiles += $results.FullName
945+
}
946+
947+
# Right after the backup the share can still hold the file open for a moment, and a silently
948+
# failed removal left it behind once. Retry a few times and then insist that it is gone.
949+
foreach ($backupFile in $backupFiles) {
950+
$attempts = 0
951+
while ((Test-Path -Path $backupFile) -and $attempts -lt 5) {
952+
$attempts++
953+
Remove-Item -Path $backupFile -ErrorAction SilentlyContinue
954+
if (Test-Path -Path $backupFile) {
955+
Start-Sleep -Seconds 1
956+
}
957+
}
958+
Test-Path -Path $backupFile | Should -BeFalse
942959
}
943960
}
944961
}

tests/Copy-DbaDbViewData.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ Describe $CommandName -Tag IntegrationTests {
175175
}
176176

177177
It "Copy data using a query that relies on the default source database" {
178-
$result = Copy-DbaDbViewData -SqlInstance $TestConfig.InstanceCopy1 -Database tempdb -View dbatoolsci_view_example -Query "SELECT TOP (1) Id FROM dbo.dbatoolsci_view_example4 ORDER BY Id DESC" -DestinationTable dbatoolsci_example3 -Truncate
178+
$result = Copy-DbaDbViewData -SqlInstance $TestConfig.InstanceCopy1 -Database tempdb -View dbatoolsci_view_example -Query "SELECT TOP (1) id FROM dbo.dbatoolsci_view_example4 ORDER BY id DESC" -DestinationTable dbatoolsci_example3 -Truncate
179179
$result.RowsCopied | Should -Be 1
180180
}
181181

182182
It "Copy data using a query that uses a 3 part query" {
183-
$result = Copy-DbaDbViewData -SqlInstance $TestConfig.InstanceCopy1 -Database tempdb -View dbatoolsci_view_example -Query "SELECT TOP (1) Id FROM tempdb.dbo.dbatoolsci_view_example4 ORDER BY Id DESC" -DestinationTable dbatoolsci_example3 -Truncate
183+
$result = Copy-DbaDbViewData -SqlInstance $TestConfig.InstanceCopy1 -Database tempdb -View dbatoolsci_view_example -Query "SELECT TOP (1) id FROM tempdb.dbo.dbatoolsci_view_example4 ORDER BY id DESC" -DestinationTable dbatoolsci_example3 -Truncate
184184
$result.RowsCopied | Should -Be 1
185185
}
186186
}

tests/Disable-DbaForceNetworkEncryption.Tests.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ Describe $CommandName -Tag UnitTests {
2121
}
2222

2323
Describe $CommandName -Tag IntegrationTests {
24+
BeforeAll {
25+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
26+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
27+
28+
# Remember the state of the instance, so that AfterAll can put it back.
29+
$forceEncryptionBefore = (Get-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle).ForceEncryption
30+
31+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
32+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
33+
}
34+
35+
AfterAll {
36+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
37+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
38+
39+
if ($forceEncryptionBefore) {
40+
$null = Enable-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle
41+
}
42+
43+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
44+
}
45+
2446
Context "When disabling force network encryption" {
2547
It "Returns results with ForceEncryption set to false" {
2648
$results = Disable-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle -EnableException

tests/Enable-DbaForceNetworkEncryption.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Describe $CommandName -Tag IntegrationTests {
2525
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
2626
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
2727

28+
# Remember the state of the instance, so that AfterAll can put it back. The registry value survives everything
29+
# except an explicit Disable, so without this the instance stays with force encryption after the test.
30+
$forceEncryptionBefore = (Get-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle).ForceEncryption
31+
2832
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
2933
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
3034
}
@@ -33,6 +37,10 @@ Describe $CommandName -Tag IntegrationTests {
3337
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
3438
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
3539

40+
if (-not $forceEncryptionBefore) {
41+
$null = Disable-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle
42+
}
43+
3644
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
3745
}
3846

tests/Measure-DbaBackupThroughput.Tests.ps1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@ Describe $CommandName -Tag IntegrationTests {
5151

5252
$null = Remove-DbaDatabase -SqlInstance $TestConfig.InstanceSingle -Database $testDb
5353

54-
# Remove the backup directory.
55-
Remove-Item -Path $backupPath -Recurse
54+
# Remove the backup directory. The backup was written seconds ago and the share can still hold the
55+
# file open for a moment, which once made a single Remove-Item leave the whole folder behind.
56+
# Retry a few times and let the last attempt report its error.
57+
$attempts = 0
58+
while ((Test-Path -Path $backupPath) -and $attempts -lt 5) {
59+
$attempts++
60+
Remove-Item -Path $backupPath -Recurse -ErrorAction SilentlyContinue
61+
if (Test-Path -Path $backupPath) {
62+
Start-Sleep -Seconds 1
63+
}
64+
}
65+
if (Test-Path -Path $backupPath) {
66+
Remove-Item -Path $backupPath -Recurse
67+
}
5668

5769
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
5870
}
@@ -64,4 +76,4 @@ Describe $CommandName -Tag IntegrationTests {
6476
$testResults | Should -Not -BeNullOrEmpty
6577
}
6678
}
67-
}
79+
}

tests/New-DbaDatabase.Tests.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ Describe $CommandName -Tag IntegrationTests {
273273
$serverMulti1.Databases[$newDbName].FileGroups["PRIMARY"].Files["$($newDbName)_PRIMARY"].Growth | Should -Be 65536
274274
$serverMulti1.Databases[$newDbName].FileGroups["PRIMARY"].Files["$($newDbName)_PRIMARY"].GrowthType | Should -Be "KB"
275275

276-
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_log"].Size | Should -Be 32768
277-
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_log"].MaxSize | Should -Be 524288
278-
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_log"].Growth | Should -Be 32768
279-
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_log"].GrowthType | Should -Be "KB"
276+
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_Log"].Size | Should -Be 32768
277+
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_Log"].MaxSize | Should -Be 524288
278+
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_Log"].Growth | Should -Be 32768
279+
$serverMulti1.Databases[$newDbName].LogFiles["$($newDbName)_Log"].GrowthType | Should -Be "KB"
280280

281281
$serverMulti1.Databases[$newDbName].FileGroups["$($newDbName)_MainData"].Files[0].Size | Should -Be 65536
282282
$serverMulti1.Databases[$newDbName].FileGroups["$($newDbName)_MainData"].Files[0].MaxSize | Should -Be 524288
@@ -288,10 +288,10 @@ Describe $CommandName -Tag IntegrationTests {
288288
$serverMulti2.Databases[$newDbName].FileGroups["PRIMARY"].Files["$($newDbName)_PRIMARY"].Growth | Should -Be 65536
289289
$serverMulti2.Databases[$newDbName].FileGroups["PRIMARY"].Files["$($newDbName)_PRIMARY"].GrowthType | Should -Be "KB"
290290

291-
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_log"].Size | Should -Be 32768
292-
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_log"].MaxSize | Should -Be 524288
293-
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_log"].Growth | Should -Be 32768
294-
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_log"].GrowthType | Should -Be "KB"
291+
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_Log"].Size | Should -Be 32768
292+
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_Log"].MaxSize | Should -Be 524288
293+
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_Log"].Growth | Should -Be 32768
294+
$serverMulti2.Databases[$newDbName].LogFiles["$($newDbName)_Log"].GrowthType | Should -Be "KB"
295295

296296
$serverMulti2.Databases[$newDbName].FileGroups["$($newDbName)_MainData"].Files[0].Size | Should -Be 65536
297297
$serverMulti2.Databases[$newDbName].FileGroups["$($newDbName)_MainData"].Files[0].MaxSize | Should -Be 524288
@@ -313,8 +313,8 @@ Describe $CommandName -Tag IntegrationTests {
313313
$serverMulti1.Databases[$bug6780DbName].FileGroups["PRIMARY"].Files["$($bug6780DbName)_PRIMARY"].Growth | Should -Be $serverMulti1.Databases["model"].FileGroups["PRIMARY"].Files["modeldev"].Growth
314314
$serverMulti1.Databases[$bug6780DbName].FileGroups["PRIMARY"].Files["$($bug6780DbName)_PRIMARY"].GrowthType | Should -Be $serverMulti1.Databases["model"].FileGroups["PRIMARY"].Files["modeldev"].GrowthType
315315

316-
$serverMulti1.Databases[$bug6780DbName].LogFiles["$($bug6780DbName)_log"].Growth | Should -Be $serverMulti1.Databases["model"].LogFiles["modellog"].Growth
317-
$serverMulti1.Databases[$bug6780DbName].LogFiles["$($bug6780DbName)_log"].GrowthType | Should -Be $serverMulti1.Databases["model"].LogFiles["modellog"].GrowthType
316+
$serverMulti1.Databases[$bug6780DbName].LogFiles["$($bug6780DbName)_Log"].Growth | Should -Be $serverMulti1.Databases["model"].LogFiles["modellog"].Growth
317+
$serverMulti1.Databases[$bug6780DbName].LogFiles["$($bug6780DbName)_Log"].GrowthType | Should -Be $serverMulti1.Databases["model"].LogFiles["modellog"].GrowthType
318318

319319
# also check the randomDb since it was created without any additional params
320320
$serverMulti1.Databases[$($randomDb.Name)].FileGroups["PRIMARY"].Files["$($randomDb.Name)"].Growth | Should -Be $serverMulti1.Databases["model"].FileGroups["PRIMARY"].Files["modeldev"].Growth

tests/Sync-DbaLoginPermission.Tests.ps1

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ Describe $CommandName -Tag UnitTests {
2626

2727
Describe $CommandName -Tag IntegrationTests {
2828
BeforeAll {
29+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
30+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
31+
2932
$tempguid = [guid]::newguid()
3033
$DBUserName = "dbatoolssci_$($tempguid.guid)"
3134
$CreateTestUser = @"
3235
CREATE LOGIN [$DBUserName]
3336
WITH PASSWORD = '$($tempguid.guid)';
34-
USE Master;
37+
USE master;
3538
CREATE USER [$DBUserName] FOR LOGIN [$DBUserName]
3639
WITH DEFAULT_SCHEMA = dbo;
3740
GRANT VIEW ANY DEFINITION to [$DBUserName];
@@ -43,10 +46,18 @@ GRANT VIEW ANY DEFINITION to [$DBUserName];
4346
CREATE LOGIN [$DBUserName]
4447
WITH PASSWORD = '$($tempguid.guid)';
4548
"@
49+
50+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
51+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
4652
}
4753
AfterAll {
54+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
55+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
56+
4857
$DropTestUser = "DROP LOGIN [$DBUserName]"
4958
Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti1, $TestConfig.InstanceMulti2 -Query $DropTestUser -Database master
59+
60+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
5061
}
5162

5263
Context "Command execution and functionality" {
@@ -59,14 +70,14 @@ CREATE LOGIN [$DBUserName]
5970
It "Should execute against active nodes" {
6071
# Creates the user on
6172
Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti2 -Query $CreateTestLogin
62-
$results = Sync-DbaLoginPermission -Source $TestConfig.InstanceMulti1 -Destination $TestConfig.InstanceMulti2 -Login $DBUserName -ExcludeLogin 'NotaLogin' -WarningVariable $warn
63-
$results.Status | Should -Be 'Successful'
73+
$results = Sync-DbaLoginPermission -Source $TestConfig.InstanceMulti1 -Destination $TestConfig.InstanceMulti2 -Login $DBUserName -ExcludeLogin "NotaLogin" -WarningVariable warn
74+
$results.Status | Should -Be "Successful"
6475
$warn | Should -BeNullOrEmpty
6576
}
6677

6778
# The copy failes on Appveyor with: Failed to create or use STIG schema on APPVYR-WIN\sql2017
6879
It "Should have copied the user permissions of $DBUserName" -Skip:$env:appveyor {
69-
$permissionsAfter = Get-DbaUserPermission -SqlInstance $TestConfig.InstanceMulti2 -Database master | Where-Object { $_.member -eq $DBUserName -and $_.permission -eq 'VIEW ANY DEFINITION' }
80+
$permissionsAfter = Get-DbaUserPermission -SqlInstance $TestConfig.InstanceMulti2 -Database master | Where-Object { $_.member -eq $DBUserName -and $_.permission -eq "VIEW ANY DEFINITION" }
7081
$permissionsAfter.member | Should -Be $DBUserName
7182
}
7283
}

0 commit comments

Comments
 (0)