@@ -40,58 +40,112 @@ Describe "Backup-DbaDbCertificate" -Tag "UnitTests" {
40
40
41
41
Describe " Backup-DbaDbCertificate" - Tag " IntegrationTests" {
42
42
BeforeAll {
43
+ $PSDefaultParameterValues [' *-Dba*:EnableException' ] = $true
44
+
43
45
$random = Get-Random
44
- $db1Name = " dbatoolscli_ $random "
45
- $db1 = New-DbaDatabase - SqlInstance $TestConfig .instance1 - Name $db1Name
46
+ $db1Name = " dbatoolscli_db1_ $random "
47
+ $db2Name = " dbatoolscli_db2_ $random "
46
48
$pw = ConvertTo-SecureString - String " GoodPass1234!" - AsPlainText - Force
47
- if (-not (Get-DbaDbMasterKey - SqlInstance $TestConfig.instance1 - Database $db1Name )) {
48
- $masterkey = New-DbaDbMasterKey - SqlInstance $TestConfig.instance1 - Database $db1Name - Password $pw - Confirm:$false
49
- }
50
49
51
- $cert = New-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Confirm:$false - Password $pw - Name dbatoolscli_cert1_$random
52
- $cert2 = New-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Confirm:$false - Password $pw - Name dbatoolscli_cert2_$random
50
+ $db1 = New-DbaDatabase - SqlInstance $TestConfig.instance1 - Name $db1Name
51
+ $null = New-DbaDbMasterKey - SqlInstance $TestConfig.instance1 - Database $db1Name - Password $pw
52
+
53
+ $db2 = New-DbaDatabase - SqlInstance $TestConfig.instance1 - Name $db2Name
54
+ $null = New-DbaDbMasterKey - SqlInstance $TestConfig.instance1 - Database $db2Name - Password $pw
55
+
56
+ $cert1 = New-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Password $pw - Name dbatoolscli_cert1_$random
57
+ $cert2 = New-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Password $pw - Name dbatoolscli_cert2_$random
58
+ $cert3 = New-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db2Name - Password $pw - Name dbatoolscli_cert3_$random
59
+
60
+ $PSDefaultParameterValues.Remove (' *-Dba*:EnableException' )
53
61
}
54
62
AfterAll {
55
- Remove-DbaDatabase - SqlInstance $TestConfig.instance1 - Database $db1Name - Confirm:$false
63
+ $PSDefaultParameterValues [' *-Dba*:EnableException' ] = $true
64
+
65
+ Remove-DbaDatabase - SqlInstance $TestConfig.instance1 - Database $db1Name , $db2Name
56
66
}
57
67
58
- Context " Can create and backup a database certificate" {
59
- It " backs up the db cert" {
60
- $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Certificate $cert.Name - Database $db1Name - EncryptionPassword $pw - DecryptionPassword $pw
61
- $null = Get-ChildItem - Path $results.Path - ErrorAction Ignore | Remove-Item - Confirm:$false - ErrorAction Ignore
62
- $results.Certificate | Should - Be $cert.Name
68
+ Context " Can backup a database certificate" {
69
+ BeforeAll {
70
+ $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Certificate $cert1.Name - EncryptionPassword $pw - DecryptionPassword $pw
71
+ }
72
+
73
+ AfterAll {
74
+ Remove-Item - Path $results.Path
75
+ Remove-Item - Path $results.Key
76
+ }
77
+
78
+ It " Returns results with proper data" {
79
+ $results.Certificate | Should - Be $cert1.Name
63
80
$results.Status | Should -Match " Success"
64
- $results.DatabaseID | Should - Be (Get-DbaDatabase - SqlInstance $TestConfig.instance1 - Database $db1Name ).ID
81
+ $results.DatabaseID | Should - Be $db1.ID
82
+ }
83
+ }
84
+
85
+ Context " Can backup a database certificate with a filename (see #9485)" {
86
+ BeforeAll {
87
+ $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Certificate $cert1.Name - EncryptionPassword $pw - DecryptionPassword $pw - FileBaseName " dbatoolscli_cert1_$random "
88
+ }
89
+
90
+ AfterAll {
91
+ Remove-Item - Path $results.Path
92
+ Remove-Item - Path $results.Key
65
93
}
66
94
67
- It " backs up the db cert with a filename (see #9485)" {
68
- $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Certificate $cert.Name - Database $db1Name - EncryptionPassword $pw - DecryptionPassword $pw - FileBaseName " dbatoolscli_cert1_$random "
69
- $results.Certificate | Should - Be $cert.Name
95
+ It " Returns results with proper data" {
96
+ $results.Certificate | Should - Be $cert1.Name
70
97
$results.Status | Should -Match " Success"
71
- $results.DatabaseID | Should - Be ( Get-DbaDatabase - SqlInstance $TestConfig .instance1 - Database $db1Name ) .ID
98
+ $results.DatabaseID | Should - Be $db1 .ID
72
99
[IO.Path ]::GetFileNameWithoutExtension($results.Path ) | Should - Be " dbatoolscli_cert1_$random "
73
- $null = Get-ChildItem - Path $results.Path - ErrorAction Ignore | Remove-Item - Confirm:$false - ErrorAction Ignore
74
100
}
101
+ }
75
102
76
- It " warns the caller if the cert cannot be found" {
103
+ Context " Warns the caller if the cert cannot be found" {
104
+ BeforeAll {
77
105
$invalidDBCertName = " dbatoolscli_invalidCertName"
78
106
$invalidDBCertName2 = " dbatoolscli_invalidCertName2"
79
- $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Certificate $invalidDBCertName , $invalidDBCertName2 , $cert2.Name - Database $db1Name - EncryptionPassword $pw - DecryptionPassword $pw - WarningVariable warnVariable 3> $null
80
- $null = Get-ChildItem - Path $results.Path - ErrorAction Ignore | Remove-Item - Confirm:$false - ErrorAction Ignore
81
- $warnVariable | Should - BeLike " *Database certificate(s) * not found*"
107
+ $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - Certificate $invalidDBCertName , $invalidDBCertName2 , $cert2.Name - EncryptionPassword $pw - DecryptionPassword $pw - WarningAction SilentlyContinue
108
+ }
109
+
110
+ AfterAll {
111
+ Remove-Item - Path $results.Path
112
+ Remove-Item - Path $results.Key
82
113
}
83
114
84
- It " backs up all db certs for a database" - Skip {
115
+ It " Does warn" {
116
+ $WarnVar | Should - BeLike " *Database certificate(s) * not found*"
117
+ }
118
+ }
119
+
120
+ Context " Backs up all db certs for a database" {
121
+ BeforeAll {
85
122
$results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - Database $db1Name - EncryptionPassword $pw - DecryptionPassword $pw
86
- $null = Get-ChildItem - Path $results.Path - ErrorAction Ignore | Remove-Item - Confirm:$false - ErrorAction Ignore
87
- $results.length | Should - Be 2
88
- $results.Certificate | Should - Be $cert.Name , $cert2.Name
89
123
}
90
124
91
- It " backs up all db certs for an instance " - Skip {
92
- $results = Backup-DbaDbCertificate - SqlInstance $TestConfig .instance1 - EncryptionPassword $pw
93
- $null = Get-ChildItem - Path $results.Path - ErrorAction Ignore | Remove-Item - Confirm: $false - ErrorAction Ignore
125
+ AfterAll {
126
+ Remove-Item - Path $results .Path
127
+ Remove-Item - Path $results.Key
94
128
}
95
129
130
+ It " Returns results with proper data" {
131
+ $results | Should - HaveCount 2
132
+ $results.Certificate | Should - Be $cert1.Name , $cert2.Name
133
+ }
134
+ }
135
+
136
+ Context " Backs up all db certs for an instance" {
137
+ BeforeAll {
138
+ $results = Backup-DbaDbCertificate - SqlInstance $TestConfig.instance1 - EncryptionPassword $pw - DecryptionPassword $pw
139
+ }
140
+
141
+ AfterAll {
142
+ Remove-Item - Path $results.Path
143
+ Remove-Item - Path $results.Key
144
+ }
145
+
146
+ It " Returns results with proper data" {
147
+ $results | Should - HaveCount 3
148
+ $results.Certificate | Should - Be $cert1.Name , $cert2.Name , $cert3.Name
149
+ }
96
150
}
97
151
}
0 commit comments