-
-
Notifications
You must be signed in to change notification settings - Fork 831
/
Copy pathBackup-DbaDbMasterKey.Tests.ps1
90 lines (80 loc) · 3.4 KB
/
Backup-DbaDbMasterKey.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
param(
$ModuleName = "dbatools",
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
)
Describe "Backup-DbaDbMasterKey" -Tag "UnitTests" {
Context "Parameter validation" {
BeforeAll {
$command = Get-Command Backup-DbaDbMasterKey
$expected = $TestConfig.CommonParameters
$expected += @(
"SqlInstance",
"SqlCredential",
"Credential",
"Database",
"ExcludeDatabase",
"SecurePassword",
"Path",
"FileBaseName",
"InputObject",
"EnableException",
"WhatIf",
"Confirm"
)
}
It "Has parameter: <_>" -ForEach $expected {
$command | Should -HaveParameter $PSItem
}
It "Should have exactly the number of expected parameters ($($expected.Count))" {
$hasparms = $command.Parameters.Values.Name
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
}
}
}
Describe "Backup-DbaDbMasterKey" -Tag "IntegrationTests" {
Context "Can backup a database master key" {
BeforeAll {
$instance = $TestConfig.instance1
$database = "tempdb"
$password = ConvertTo-SecureString -String "GoodPass1234!" -AsPlainText -Force
if (-not (Get-DbaDbMasterKey -SqlInstance $instance -Database $database)) {
$null = New-DbaDbMasterKey -SqlInstance $instance -Database $database -Password $password -Confirm:$false
}
}
AfterAll {
Get-DbaDbMasterKey -SqlInstance $instance -Database $database | Remove-DbaDbMasterKey -Confirm:$false
}
It "Backs up the database master key" {
$splatBackup = @{
SqlInstance = $instance
Database = $database
SecurePassword = $password
Confirm = $false
}
$results = Backup-DbaDbMasterKey @splatBackup
$results | Should -Not -BeNullOrEmpty
$results.Database | Should -Be $database
$results.Status | Should -Be "Success"
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $instance -Database $database).ID
$null = Remove-Item -Path $results.Path -ErrorAction SilentlyContinue -Confirm:$false
}
It "Backs up the database master key with a specific filename (see #9484)" {
$random = Get-Random
$splatBackup = @{
SqlInstance = $instance
Database = $database
SecurePassword = $password
FileBaseName = "dbatoolscli_dbmasterkey_$random"
Confirm = $false
}
$results = Backup-DbaDbMasterKey @splatBackup
$results | Should -Not -BeNullOrEmpty
$results.Database | Should -Be $database
$results.Status | Should -Be "Success"
$results.DatabaseID | Should -Be (Get-DbaDatabase -SqlInstance $instance -Database $database).ID
[IO.Path]::GetFileNameWithoutExtension($results.Path) | Should -Be "dbatoolscli_dbmasterkey_$random"
$null = Remove-Item -Path $results.Path -ErrorAction SilentlyContinue -Confirm:$false
}
}
}