From 1dd6035831d8ae147434aa6786c442be45228870 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Fri, 1 Nov 2024 11:33:54 +0100 Subject: [PATCH] Update Backup-DbaDatabase.Tests.ps1 --- tests/Backup-DbaDatabase.Tests.ps1 | 318 +++++++++++++++++++---------- 1 file changed, 208 insertions(+), 110 deletions(-) diff --git a/tests/Backup-DbaDatabase.Tests.ps1 b/tests/Backup-DbaDatabase.Tests.ps1 index 1ffcfdf525..7a186ba79a 100644 --- a/tests/Backup-DbaDatabase.Tests.ps1 +++ b/tests/Backup-DbaDatabase.Tests.ps1 @@ -1,40 +1,54 @@ -$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") +#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"} +param( + $ModuleName = "dbatools", + $PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults +) + Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan -$global:TestConfig = Get-TestConfig -Describe "$CommandName Unit Tests" -Tag 'UnitTests' { - Context "Validate parameters" { - [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') } - [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', 'Path', 'FilePath', 'ReplaceInName', 'NoAppendDbNameInPath', 'CopyOnly', 'Type', 'InputObject', 'CreateFolder', 'FileCount', 'CompressBackup', 'Checksum', 'Verify', 'MaxTransferSize', 'BlockSize', 'BufferCount', 'AzureBaseUrl', 'AzureCredential', 'NoRecovery', 'BuildPath', 'WithFormat', 'Initialize', 'SkipTapeHeader', 'TimeStampFormat', 'IgnoreFileChecks', 'OutputScriptOnly', 'EnableException', 'EncryptionAlgorithm', 'EncryptionCertificate', 'IncrementPrefix', 'Description' - $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters - It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0 - } +Describe "Backup-DbaDatabase Unit Tests" -Tag "UnitTests" { + BeforeAll { + $command = Get-Command Backup-DbaDatabase + $expected = @( + 'SqlInstance', 'SqlCredential', 'Database', 'ExcludeDatabase', + 'Path', 'FilePath', 'ReplaceInName', 'NoAppendDbNameInPath', + 'CopyOnly', 'Type', 'InputObject', 'CreateFolder', 'FileCount', + 'CompressBackup', 'Checksum', 'Verify', 'MaxTransferSize', + 'BlockSize', 'BufferCount', 'AzureBaseUrl', 'AzureCredential', + 'NoRecovery', 'BuildPath', 'WithFormat', 'Initialize', + 'SkipTapeHeader', 'TimeStampFormat', 'IgnoreFileChecks', + 'OutputScriptOnly', 'EnableException', 'EncryptionAlgorithm', + 'EncryptionCertificate', 'IncrementPrefix', 'Description' + ) + $expected += $TestConfig.CommonParameters } -} - -Describe "$commandname Integration Tests" -Tags "IntegrationTests" { - Context "Properly restores a database on the local drive using Path" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory C:\temp\backups - It "Should return a database name, specifically master" { - ($results.DatabaseName -contains 'master') | Should -Be $true + Context "Parameter validation" { + It "Has parameter: <_>" -ForEach $expected { + $command | Should -HaveParameter $PSItem } - It "Should return successful restore" { - $results.ForEach{ $_.BackupComplete | Should -Be $true } + + It "Should have exactly $($expected.Count) parameters" { + $params = $command.Parameters.Values.Name + Compare-Object -ReferenceObject $expected -DifferenceObject $params | Should -BeNullOrEmpty } } +} +Describe "Backup-DbaDatabase Integration Tests" -Tag "IntegrationTests" { BeforeAll { $DestBackupDir = 'C:\Temp\backups' $random = Get-Random $DestDbRandom = "dbatools_ci_backupdbadatabase$random" + if (-Not(Test-Path $DestBackupDir)) { New-Item -Type Container -Path $DestBackupDir } + Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database "dbatoolsci_singlerestore" | Remove-DbaDatabase -Confirm:$false Get-DbaDatabase -SqlInstance $TestConfig.instance2 -Database $DestDbRandom | Remove-DbaDatabase -Confirm:$false } + AfterAll { Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database "dbatoolsci_singlerestore" | Remove-DbaDatabase -Confirm:$false Get-DbaDatabase -SqlInstance $TestConfig.instance2 -Database $DestDbRandom | Remove-DbaDatabase -Confirm:$false @@ -42,15 +56,36 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { Remove-Item "$DestBackupDir\*" -Force -Recurse } } - Context "Should not backup if database and exclude match" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master -Exclude master + + Context "When backing up all databases to local drive using Path parameter" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory C:\temp\backups + } + + It "Returns master database in results" { + $results.DatabaseName | Should -Contain 'master' + } + + It "Reports successful backup completion" { + $results.ForEach{ $PSItem.BackupComplete | Should -BeTrue } + } + } + + Context "When backing up database that matches exclude filter" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master -Exclude master + } + It "Should not return object" { $results | Should -Be $null } } - Context "No database found to backup should raise warning and null output" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database AliceDoesntDBHereAnyMore -WarningVariable warnvar 3> $null + Context "When attempting to back up non-existent database" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database AliceDoesntDBHereAnyMore -WarningVariable warnvar 3> $null + } + It "Should not return object" { $results | Should -Be $null } @@ -59,8 +94,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Database should backup 1 database" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master + Context "When backing up single database master" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master + } + It "Database backup object count Should Be 1" { $results.DatabaseName.Count | Should -Be 1 $results.BackupComplete | Should -Be $true @@ -70,16 +108,22 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Database should backup 2 databases" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master, msdb + Context "When backing up multiple specified databases" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master, msdb + } + It "Database backup object count Should Be 2" { $results.DatabaseName.Count | Should -Be 2 $results.BackupComplete | Should -Be @($true, $true) } } - Context "Should take path and filename" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master -BackupFileName 'PesterTest.bak' + Context "When specifying custom backup path and filename" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database master -BackupFileName 'PesterTest.bak' + } + It "Should report it has backed up to the path with the correct name" { $results.Fullname | Should -BeLike "$DestBackupDir*PesterTest.bak" } @@ -88,8 +132,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Database parameter works when using pipes (fixes #5044)" { - $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 | Backup-DbaDatabase -Database master -BackupFileName PesterTest.bak -BackupDirectory $DestBackupDir + Context "When piping database objects with Database parameter" { + BeforeAll { + $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 | Backup-DbaDatabase -Database master -BackupFileName PesterTest.bak -BackupDirectory $DestBackupDir + } + It "Should report it has backed up to the path with the correct name" { $results.Fullname | Should -BeLike "$DestBackupDir*PesterTest.bak" } @@ -98,8 +145,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "ExcludeDatabase parameter works when using pipes (fixes #5044)" { - $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 | Backup-DbaDatabase -ExcludeDatabase master, tempdb, msdb, model + Context "When piping database objects with ExcludeDatabase parameter" { + BeforeAll { + $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 | Backup-DbaDatabase -ExcludeDatabase master, tempdb, msdb, model + } + It "Should report it has backed up to the path with the correct name" { $results.DatabaseName | Should -Not -Contain master $results.DatabaseName | Should -Not -Contain tempdb @@ -108,31 +158,43 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Handling backup paths that don't exist" { - $MissingPathTrailing = "$DestBackupDir\Missing1\Awol2\" - $MissingPath = "$DestBackupDir\Missing1\Awol2" - $null = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $MissingPath -WarningVariable warnvar *>$null + Context "When backup path does not exist" { + BeforeAll { + $MissingPathTrailing = "$DestBackupDir\Missing1\Awol2\" + $MissingPath = "$DestBackupDir\Missing1\Awol2" + $null = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $MissingPath -WarningVariable warnvar *>$null + } + It "Should warn and fail if path doesn't exist and BuildPath not set" { $warnvar | Should -BeLike "*$MissingPath*" } - # $MissingPathTrailing has a trailing slash but we normalize the path before doing the actual backup - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $MissingPathTrailing -BuildPath + + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $MissingPathTrailing -BuildPath + } + It "Should have backed up to $MissingPath" { $results.BackupFolder | Should -Be "$MissingPath" $results.Path | Should -Not -BeLike '*\\*' } } - Context "CreateFolder switch should append the databasename to the backup path" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -CreateFolder + Context "When using CreateFolder switch with single database" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -CreateFolder + } + It "Should have appended master to the backup path" { $results.BackupFolder | Should -Be "$DestBackupDir\master" } } - Context "CreateFolder switch should append the databasename to the backup path even when striping" { - $backupPaths = "$DestBackupDir\stripewithdb1", "$DestBackupDir\stripewithdb2" - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $backupPaths -CreateFolder + Context "When using CreateFolder switch with striped backups" { + BeforeAll { + $backupPaths = "$DestBackupDir\stripewithdb1", "$DestBackupDir\stripewithdb2" + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $backupPaths -CreateFolder + } + It "Should have appended master to all backup paths" { foreach ($path in $results.BackupFolder) { ($results.BackupFolder | Sort-Object) | Should -Be ($backupPaths | Sort-Object | ForEach-Object { [IO.Path]::Combine($_, 'master') }) @@ -140,9 +202,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } + Context "When providing fully qualified path that overrides backup folder" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory c:\temp -BackupFileName "$DestBackupDir\PesterTest2.bak" + } - Context "A fully qualified path should override a backupfolder" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory c:\temp -BackupFileName "$DestBackupDir\PesterTest2.bak" It "Should report backed up to $DestBackupDir" { $results.FullName | Should -BeLike "$DestBackupDir\PesterTest2.bak" $results.BackupFolder | Should Not Be 'c:\temp' @@ -152,12 +216,13 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Should stripe if multiple backupfolders specified" { - $backupPaths = "$DestBackupDir\stripe1", "$DestBackupDir\stripe2", "$DestBackupDir\stripe3" - $null = New-Item -Path $backupPaths -ItemType Directory - + Context "When specifying multiple backup folders for striping" { + BeforeAll { + $backupPaths = "$DestBackupDir\stripe1", "$DestBackupDir\stripe2", "$DestBackupDir\stripe3" + $null = New-Item -Path $backupPaths -ItemType Directory + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $backupPaths + } - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $backupPaths It "Should have created 3 backups" { $results.BackupFilesCount | Should -Be 3 } @@ -171,21 +236,27 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { [IO.Path]::GetExtension($path) | Should -Be '.bak' } } - # Assure that striping logic favours -BackupDirectory and not -Filecount - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $backupPaths -FileCount 2 + + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $backupPaths -FileCount 2 + } + It "Should have created 3 backups, even when FileCount is different" { $results.BackupFilesCount | Should -Be 3 } } - Context "Should stripe on filecount > 1" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -FileCount 3 + Context "When using FileCount parameter for striping" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -FileCount 3 + } + It "Should have created 3 backups" { $results.BackupFilesCount | Should -Be 3 } } - Context "Should build filenames properly" { + Context "When generating backup filenames" { It "Should have 1 period in file extension" { foreach ($path in $results.BackupFile) { [IO.Path]::GetExtension($path) | Should -Not -BeLike '*..*' @@ -193,9 +264,12 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Should prefix the filenames when IncrementPrefix set" { - $fileCount = 3 - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -FileCount $fileCount -IncrementPrefix + Context "When using IncrementPrefix parameter" { + BeforeAll { + $fileCount = 3 + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -FileCount $fileCount -IncrementPrefix + } + It "Should have created 3 backups" { $results.BackupFilesCount | Should -Be 3 } @@ -206,9 +280,12 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Should Backup to default path if none specified" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupFileName 'PesterTest.bak' - $DefaultPath = (Get-DbaDefaultPath -SqlInstance $TestConfig.instance1).Backup + Context "When no backup path is specified" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupFileName 'PesterTest.bak' + $DefaultPath = (Get-DbaDefaultPath -SqlInstance $TestConfig.instance1).Backup + } + It "Should report it has backed up to the path with the corrrect name" { $results.Fullname | Should -BeLike "$DefaultPath*PesterTest.bak" } @@ -217,7 +294,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Test backup verification" { + Context "When performing backup verification" { It "Should perform a full backup and verify it" { $b = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -Type full -Verify $b.BackupComplete | Should -Be $True @@ -236,27 +313,35 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Backup can pipe to restore" { - $null = Restore-DbaDatabase -SqlInstance $TestConfig.instance1 -Path "$($TestConfig.appveyorlabrepo)\singlerestore\singlerestore.bak" -DatabaseName "dbatoolsci_singlerestore" - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database "dbatoolsci_singlerestore" | Restore-DbaDatabase -SqlInstance $TestConfig.instance2 -DatabaseName $DestDbRandom -TrustDbBackupHistory -ReplaceDbNameInFile + Context "When piping backup to restore operation" { + BeforeAll { + $null = Restore-DbaDatabase -SqlInstance $TestConfig.instance1 -Path "$($TestConfig.appveyorlabrepo)\singlerestore\singlerestore.bak" -DatabaseName "dbatoolsci_singlerestore" + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -BackupDirectory $DestBackupDir -Database "dbatoolsci_singlerestore" | Restore-DbaDatabase -SqlInstance $TestConfig.instance2 -DatabaseName $DestDbRandom -TrustDbBackupHistory -ReplaceDbNameInFile + } + It "Should return successful restore" { $results.RestoreComplete | Should -Be $true } } - Context "Test Backup-DbaDatabase can take pipe input" { - $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master | Backup-DbaDatabase -confirm:$false -WarningVariable warnvar 3> $null + Context "When taking database input from pipeline" { + BeforeAll { + $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master | Backup-DbaDatabase -confirm:$false -WarningVariable warnvar 3> $null + } + It "Should not warn" { $warnvar | Should -BeNullOrEmpty } It "Should Complete Successfully" { $results.BackupComplete | Should -Be $true } - } - Context "Should handle NUL as an input path" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupFileName NUL + Context "When using NUL as backup destination" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupFileName NUL + } + It "Should return succesful backup" { $results.BackupComplete | Should -Be $true } @@ -265,8 +350,11 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Should only output a T-SQL String if OutputScriptOnly specified" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupFileName c:\notexists\file.bak -OutputScriptOnly + Context "When using OutputScriptOnly parameter" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupFileName c:\notexists\file.bak -OutputScriptOnly + } + It "Should return a string" { $results.GetType().ToString() | Should -Be 'System.String' } @@ -275,9 +363,10 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { } } - Context "Should handle an encrypted database when compression is specified" { - $sqlencrypt = - @" + Context "When backing up encrypted database with compression" { + BeforeAll { + $sqlencrypt = + @" CREATE MASTER KEY ENCRYPTION BY PASSWORD = ''; go CREATE CERTIFICATE MyServerCert WITH SUBJECT = 'My DEK Certificate'; @@ -285,9 +374,9 @@ go CREATE DATABASE encrypted go "@ - $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $sqlencrypt -Database Master - $createdb = - @" + $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $sqlencrypt -Database Master + $createdb = + @" CREATE DATABASE ENCRYPTION KEY WITH ALGORITHM = AES_128 ENCRYPTION BY SERVER CERTIFICATE MyServerCert; @@ -296,73 +385,82 @@ ALTER DATABASE encrypted SET ENCRYPTION ON; GO "@ - $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $createdb -Database encrypted + $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $createdb -Database encrypted + } + It "Should compress an encrypted db" { $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance2 -Database encrypted -Compress Invoke-Command2 -ComputerName $TestConfig.instance2 -ScriptBlock { Remove-Item -Path $args[0] } -ArgumentList $results.FullName $results.script | Should -BeLike '*D, COMPRESSION,*' } - Remove-DbaDatabase -SqlInstance $TestConfig.instance2 -Database encrypted -confirm:$false - $sqldrop = - @" + + AfterAll { + Remove-DbaDatabase -SqlInstance $TestConfig.instance2 -Database encrypted -confirm:$false + $sqldrop = + @" drop certificate MyServerCert go drop master key go "@ - $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $sqldrop -Database Master + $null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $sqldrop -Database Master + } } - Context "Custom TimeStamp" { - # Test relies on DateFormat bobob returning bobob as the values aren't interpreted, check here in case .Net rules change - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -TimeStampFormat bobob + Context "When using custom TimeStamp format" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master -BackupDirectory $DestBackupDir -TimeStampFormat bobob + } + It "Should apply the corect custom Timestamp" { ($results | Where-Object { $_.BackupPath -like '*bobob*' }).count | Should -Be $results.count } } - Context "Test Backup templating" { - $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master, msdb -BackupDirectory $DestBackupDir\dbname\instancename\backuptype\ -BackupFileName dbname-backuptype.bak -ReplaceInName -BuildPath + Context "When using backup filename templating" { + BeforeAll { + $results = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master, msdb -BackupDirectory $DestBackupDir\dbname\instancename\backuptype\ -BackupFileName dbname-backuptype.bak -ReplaceInName -BuildPath + } + It "Should have replaced the markers" { $results[0].BackupPath | Should -BeLike "$DestBackupDir\master\$(($TestConfig.instance1).split('\')[1])\Full\master-Full.bak" $results[1].BackupPath | Should -BeLike "$DestBackupDir\msdb\$(($TestConfig.instance1).split('\')[1])\Full\msdb-Full.bak" } } - Context "Test Backup templating when db object piped in issue 8100" { - $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master,msdb | Backup-DbaDatabase -BackupDirectory $DestBackupDir\db2\dbname\instancename\backuptype\ -BackupFileName dbname-backuptype.bak -ReplaceInName -BuildPath + Context "When piping database objects with filename templating" { + BeforeAll { + $results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master,msdb | Backup-DbaDatabase -BackupDirectory $DestBackupDir\db2\dbname\instancename\backuptype\ -BackupFileName dbname-backuptype.bak -ReplaceInName -BuildPath + } + It "Should have replaced the markers" { $results[0].BackupPath | Should -BeLike "$DestBackupDir\db2\master\$(($TestConfig.instance1).split('\')[1])\Full\master-Full.bak" $results[1].BackupPath | Should -BeLike "$DestBackupDir\db2\msdb\$(($TestConfig.instance1).split('\')[1])\Full\msdb-Full.bak" } } - Context "Test Backup Encryption with Certificate" { - $securePass = ConvertTo-SecureString "estBackupDir\master\script:instance1).split('\')[1])\Full\master-Full.bak" -AsPlainText -Force - New-DbaDbMasterKey -SqlInstance $TestConfig.instance2 -Database Master -SecurePassword $securePass -confirm:$false -ErrorAction SilentlyContinue - $cert = New-DbaDbCertificate -SqlInstance $TestConfig.instance2 -Database master -Name BackupCertt -Subject BackupCertt - $encBackupResults = Backup-DbaDatabase -SqlInstance $TestConfig.instance2 -Database master -EncryptionAlgorithm AES128 -EncryptionCertificate BackupCertt -BackupFileName 'encryptiontest.bak' -Description "Encrypted backup" - Invoke-Command2 -ComputerName $TestConfig.instance2 -ScriptBlock { Remove-Item -Path $args[0] } -ArgumentList $encBackupResults.FullName + Context "When using backup encryption with certificate" { + BeforeAll { + $securePass = ConvertTo-SecureString "estBackupDir\master\script:instance1).split('\')[1])\Full\master-Full.bak" -AsPlainText -Force + New-DbaDbMasterKey -SqlInstance $TestConfig.instance2 -Database Master -SecurePassword $securePass -confirm:$false -ErrorAction SilentlyContinue + $cert = New-DbaDbCertificate -SqlInstance $TestConfig.instance2 -Database master -Name BackupCertt -Subject BackupCertt + $encBackupResults = Backup-DbaDatabase -SqlInstance $TestConfig.instance2 -Database master -EncryptionAlgorithm AES128 -EncryptionCertificate BackupCertt -BackupFileName 'encryptiontest.bak' -Description "Encrypted backup" + Invoke-Command2 -ComputerName $TestConfig.instance2 -ScriptBlock { Remove-Item -Path $args[0] } -ArgumentList $encBackupResults.FullName + } + It "Should encrypt the backup" { $encBackupResults.EncryptorType | Should Be "CERTIFICATE" $encBackupResults.KeyAlgorithm | Should Be "aes_128" } - Remove-DbaDbCertificate -SqlInstance $TestConfig.instance2 -Database master -Certificate BackupCertt -Confirm:$false - Remove-DbaDbMasterKey -SqlInstance $TestConfig.instance2 -Database Master -confirm:$false - } - # Context "Test Backup Encryption with Asymmetric Key" { - # $key = New-DbaDbAsymmetricKey -SqlInstance $TestConfig.instance2 -Database master -Name BackupKey - # $encBackupResults = Backup-DbaDatabase -SqlInstance $TestConfig.instance2 -Database master -EncryptionAlgorithm AES128 -EncryptionKey BackupKey - # It "Should encrypt the backup" { - # $encBackupResults.EncryptorType | Should Be "CERTIFICATE" - # $encBackupResults.KeyAlgorithm | Should Be "aes_128" - # } - # remove-DbaDbCertificate -SqlInstance $TestConfig.instance2 -Database master -Certificate BackupCertt -Confirm:$false - # } + AfterAll { + Remove-DbaDbCertificate -SqlInstance $TestConfig.instance2 -Database master -Certificate BackupCertt -Confirm:$false + Remove-DbaDbMasterKey -SqlInstance $TestConfig.instance2 -Database Master -confirm:$false + } + } if ($env:azurepasswd) { - Context "Azure works" { + Context "When backing up to Azure storage" { BeforeAll { Get-DbaDatabase -SqlInstance $TestConfig.instance2 -Database "dbatoolsci_azure" | Remove-DbaDatabase -Confirm:$false $server = Connect-DbaInstance -SqlInstance $TestConfig.instance2