Tests/Unit/MSFT_WaitForCertificateServices.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#region HEADER
$script:dscModuleName = 'CertificateDsc'
$script:dscResourceName = 'MSFT_WaitForCertificateServices'

# Unit Test Template Version: 1.2.4
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
    (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests'))
}

Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force

$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:dscModuleName `
    -DSCResourceName $script:dscResourceName `
    -ResourceType 'Mof' `
    -TestType Unit
#endregion HEADER

# Begin Testing
try
{
    InModuleScope $script:dscResourceName {
        $caServerFQDN = 'rootca.contoso.com'
        $caRootName = 'contoso-CA'
        $retryIntervalSec = 1
        $retryCount = 5

        $paramsCAOnline = @{
            CAServerFQDN     = $caServerFQDN
            CARootName       = $caRootName
            RetryIntervalSeconds = $retryIntervalSec
            RetryCount       = $retryCount
        }

        $ca = "$caServerFQDN\$caRootName"

        Describe 'MSFT_WaitForCertificateServices\Get-TargetResource' -Tag 'Get' {
            Context 'Online CA parameters passed' {
                $result = Get-TargetResource @paramsCAOnline -Verbose

                It 'Should return a hashtable' {
                    $result | Should -BeOfType System.Collections.Hashtable
                }

                It 'Should contain the same values passed as parameters' {
                    $result.CAServerFQDN         | Should -BeExactly $caServerFQDN
                    $result.CARootName           | Should -BeExactly $caRootName
                    $result.RetryIntervalSeconds | Should -Be $retryIntervalSec
                    $result.RetryCount           | Should -Be $retryCount
                }
            }
        }

        Describe 'MSFT_WaitForCertificateServices\Set-TargetResource' -Tag 'Set' {
            Context 'CA is online' {
                Mock `
                    -CommandName Test-CertificateAuthority `
                    -MockWith { $true } `
                    -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName }

                It 'Should not throw' {
                    { Set-TargetResource @paramsCAOnline -Verbose } | Should -Not -Throw
                }

                It 'Should call expected mocks' {
                    Assert-MockCalled `
                        -CommandName Test-CertificateAuthority `
                        -Exactly -Times 1
                }
            }

            Context 'CA is offline' {
                Mock `
                    -CommandName Test-CertificateAuthority `
                    -MockWith { $false } `
                    -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName }

                Mock `
                    -CommandName Start-Sleep `
                    -MockWith { } `
                    -ParameterFilter { $Seconds -eq $retryIntervalSec }

                $errorRecord = Get-InvalidOperationRecord `
                    -Message $($localizedData.CertificateAuthorityNotFoundAfterError -f $ca,$retryCount)

                It 'Should throw CANotFoundAfterError exception' {
                    { Set-TargetResource @paramsCAOnline -Verbose } | Should -Throw $errorRecord
                }

                It 'Should call expected mocks' {
                    Assert-MockCalled `
                        -CommandName Test-CertificateAuthority `
                        -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName } `
                        -Exactly -Times $retryCount

                    Assert-MockCalled `
                        -CommandName Start-Sleep `
                        -ParameterFilter { $Seconds -eq $retryIntervalSec } `
                        -Exactly -Times $retryCount
                }
            }
        }

        Describe 'MSFT_WaitForCertificateServices\Test-TargetResource' -Tag 'Test' {
            Context 'CA is online' {
                Mock `
                    -CommandName Test-CertificateAuthority `
                    -MockWith { $true } `
                    -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName }

                It 'Should not throw' {
                    { $script:result = Test-TargetResource @paramsCAOnline -Verbose } | Should -Not -Throw
                }

                It 'Should return true' {
                    $script:result | Should -Be $true
                }

                It 'Should call expected mocks' {
                    Assert-MockCalled `
                        -CommandName Test-CertificateAuthority `
                        -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName } `
                        -Exactly -Times 1
                }
            }

            Context 'CA is offline' {
                Mock `
                    -CommandName Test-CertificateAuthority `
                    -MockWith { $false } `
                    -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName }

                It 'Should not throw' {
                    { $script:result = Test-TargetResource @paramsCAOnline -Verbose } | Should -Not -Throw
                }

                It 'Should return false' {
                    $script:result | Should -Be $false
                }

                It 'Should call expected mocks' {
                    Assert-MockCalled `
                        -CommandName Test-CertificateAuthority `
                        -ParameterFilter { $CAServerFQDN -eq $caServerFQDN -and $CARootName -eq $caRootName } `
                        -Exactly -Times 1
                }
            }
        }
    }
}
finally
{
    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}