Repository navigation
Expand file tree
/
Copy pathBackup-DbaComputerCertificate.Tests.ps1
More file actions
71 lines (58 loc) 路 3.17 KB
/
Copy pathBackup-DbaComputerCertificate.Tests.ps1
File metadata and controls
71 lines (58 loc) 路 3.17 KB
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
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Backup-DbaComputerCertificate",
$PSDefaultParameterValues = $TestConfig.Defaults
)
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
It "Should have the expected parameters" {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"SecurePassword",
"InputObject",
"Path",
"FilePath",
"Type",
"EnableException"
)
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}
Describe $CommandName -Tag IntegrationTests -Skip:($PSVersionTable.PSVersion.Major -gt 5) {
# Skip IntegrationTests on pwsh because we need code changes (X509Certificate is immutable on this platform. Use the equivalent constructor instead.)
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
# For all the backups that we want to clean up after the test, we create a directory that we can delete at the end.
# Other files can be written there as well, maybe we change the name of that variable later. But for now we focus on backups.
$backupPath = "$($TestConfig.Temp)\$CommandName-$(Get-Random)"
$null = New-Item -Path $backupPath -ItemType Directory
# Explain what needs to be set up for the test:
# To test certificate backup, we need a certificate installed on the computer.
# Set variables. They are available in all the It blocks.
$certThumbprint = "29C469578D6C6211076A09CEE5C5797EEA0C2713"
$certPath = "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt"
# Create the objects.
$null = Add-DbaComputerCertificate -Path $certPath
# 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
# Cleanup all created object.
$null = Remove-DbaComputerCertificate -Thumbprint $certThumbprint -ErrorAction SilentlyContinue
# Remove the backup directory.
Remove-Item -Path $backupPath -Recurse
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}
Context "Certificate is backed up properly" {
It "Returns the proper results" {
$backupResult = Get-DbaComputerCertificate -Thumbprint $certThumbprint | Backup-DbaComputerCertificate -Path $backupPath
$backupResult.Name | Should -Match "$certThumbprint.cer"
}
}
}