Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions tests/Connect-DbaInstance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -929,16 +929,33 @@ Describe $CommandName -Tag IntegrationTests {
# 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
# Each backup gets its own file name, because two backups within the same minute share the
# default name and the second one appends to the first.
$backupFiles = @()
$results = Backup-DbaDatabase -SqlInstance $server -Database msdb -Path $TestConfig.Temp -FilePath "msdb_$(Get-Random).bak"
if ($results.FullName) {
Remove-Item -Path $results.FullName -ErrorAction SilentlyContinue
$backupFiles += $results.FullName
}

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

if ($results.FullName) {
Remove-Item -Path $results.FullName -ErrorAction SilentlyContinue
$backupFiles += $results.FullName
}

# Right after the backup the share can still hold the file open for a moment, and a silently
# failed removal left it behind once. Retry a few times and then insist that it is gone.
foreach ($backupFile in $backupFiles) {
$attempts = 0
while ((Test-Path -Path $backupFile) -and $attempts -lt 5) {
$attempts++
Remove-Item -Path $backupFile -ErrorAction SilentlyContinue
if (Test-Path -Path $backupFile) {
Start-Sleep -Seconds 1
}
}
Test-Path -Path $backupFile | Should -BeFalse
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Copy-DbaDbViewData.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ Describe $CommandName -Tag IntegrationTests {
}

It "Copy data using a query that relies on the default source database" {
$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
$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
$result.RowsCopied | Should -Be 1
}

It "Copy data using a query that uses a 3 part query" {
$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
$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
$result.RowsCopied | Should -Be 1
}
}
22 changes: 22 additions & 0 deletions tests/Disable-DbaForceNetworkEncryption.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ Describe $CommandName -Tag UnitTests {
}

Describe $CommandName -Tag IntegrationTests {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Remember the state of the instance, so that AfterAll can put it back.
$forceEncryptionBefore = (Get-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle).ForceEncryption

# 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

if ($forceEncryptionBefore) {
$null = Enable-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle
}

$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

Context "When disabling force network encryption" {
It "Returns results with ForceEncryption set to false" {
$results = Disable-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle -EnableException
Expand Down
8 changes: 8 additions & 0 deletions tests/Enable-DbaForceNetworkEncryption.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Describe $CommandName -Tag IntegrationTests {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Remember the state of the instance, so that AfterAll can put it back. The registry value survives everything
# except an explicit Disable, so without this the instance stays with force encryption after the test.
$forceEncryptionBefore = (Get-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle).ForceEncryption

# 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")
}
Expand All @@ -33,6 +37,10 @@ Describe $CommandName -Tag IntegrationTests {
# 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

if (-not $forceEncryptionBefore) {
$null = Disable-DbaForceNetworkEncryption -SqlInstance $TestConfig.InstanceSingle
}

$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

Expand Down
18 changes: 15 additions & 3 deletions tests/Measure-DbaBackupThroughput.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,20 @@ Describe $CommandName -Tag IntegrationTests {

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

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

$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}
Expand All @@ -64,4 +76,4 @@ Describe $CommandName -Tag IntegrationTests {
$testResults | Should -Not -BeNullOrEmpty
}
}
}
}
20 changes: 10 additions & 10 deletions tests/New-DbaDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ Describe $CommandName -Tag IntegrationTests {
$serverMulti1.Databases[$newDbName].FileGroups["PRIMARY"].Files["$($newDbName)_PRIMARY"].Growth | Should -Be 65536
$serverMulti1.Databases[$newDbName].FileGroups["PRIMARY"].Files["$($newDbName)_PRIMARY"].GrowthType | Should -Be "KB"

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

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

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

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

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

# also check the randomDb since it was created without any additional params
$serverMulti1.Databases[$($randomDb.Name)].FileGroups["PRIMARY"].Files["$($randomDb.Name)"].Growth | Should -Be $serverMulti1.Databases["model"].FileGroups["PRIMARY"].Files["modeldev"].Growth
Expand Down
19 changes: 15 additions & 4 deletions tests/Sync-DbaLoginPermission.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ Describe $CommandName -Tag UnitTests {

Describe $CommandName -Tag IntegrationTests {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

$tempguid = [guid]::newguid()
$DBUserName = "dbatoolssci_$($tempguid.guid)"
$CreateTestUser = @"
CREATE LOGIN [$DBUserName]
WITH PASSWORD = '$($tempguid.guid)';
USE Master;
USE master;
CREATE USER [$DBUserName] FOR LOGIN [$DBUserName]
WITH DEFAULT_SCHEMA = dbo;
GRANT VIEW ANY DEFINITION to [$DBUserName];
Expand All @@ -43,10 +46,18 @@ GRANT VIEW ANY DEFINITION to [$DBUserName];
CREATE LOGIN [$DBUserName]
WITH PASSWORD = '$($tempguid.guid)';
"@

# 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

$DropTestUser = "DROP LOGIN [$DBUserName]"
Invoke-DbaQuery -SqlInstance $TestConfig.InstanceMulti1, $TestConfig.InstanceMulti2 -Query $DropTestUser -Database master

$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

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

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