|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| - |
16 |
| -$ResourceName = ((Split-Path $MyInvocation.MyCommand.Path -Leaf) -split '_')[0] |
17 |
| -$ResourceFile = (Get-DscResource -Name $ResourceName).Path |
18 |
| - |
19 |
| -$TestsPath = (split-path -path $MyInvocation.MyCommand.Path -Parent) |
20 |
| -$ResourceFile = Get-ChildItem -Recurse $TestsPath\.. -File | Where-Object {$_.name -eq "$ResourceName.psm1"} |
21 |
| - |
22 |
| -Import-Module -Name $ResourceFile.FullName |
23 |
| - |
24 |
| - |
25 | 15 | #---------------------------------#
|
26 | 16 | # Pester tests for cChocoConfig #
|
27 | 17 | #---------------------------------#
|
28 | 18 | Describe "Testing cChocoConfig" {
|
| 19 | + BeforeAll { |
| 20 | + $ModuleUnderTest = "cChocoConfig" |
29 | 21 |
|
30 |
| - Context "Test-TargetResource" { |
| 22 | + Import-Module $PSScriptRoot\..\DSCResources\$($ModuleUnderTest)\$($ModuleUnderTest).psm1 -Force |
31 | 23 |
|
32 |
| - mock -ModuleName cChocoConfig -CommandName Get-Content -MockWith {'<?xml version="1.0" encoding="utf-8"?> |
33 |
| -<chocolatey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
34 |
| - <config> |
35 |
| - <add key="commandExecutionTimeoutSeconds" value="1339" description="Default timeout for command execution. for infinite (starting in 0.10.4)." /> |
36 |
| - <add key="proxy" value="" description="Explicit proxy location. Available in 0.9.9.9+." /> |
37 |
| - </config> |
38 |
| - <sources> |
39 |
| - <source id="chocolatey" value="https://chocolatey.org/api/v2/" disabled="false" bypassProxy="false" selfService="false" adminOnly="false" priority="0" /> |
40 |
| - </sources> |
41 |
| -</chocolatey>' |
42 |
| - } -Verifiable |
| 24 | + if (-not $env:ChocolateyInstall -and -not (Test-Path $env:ChocolateyInstall -ErrorAction SilentlyContinue)) { |
| 25 | + # Chocolatey doesn't need to be installed for these tests, but the resource tests for it |
| 26 | + $env:ChocolateyInstall = "C:\ProgramData\chocolatey" |
| 27 | + } |
43 | 28 |
|
44 |
| - it 'Test-TargetResource returns true when Present and Configured.' { |
45 |
| - Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Present' -Value '1339' | Should be $true |
| 29 | + if (-not (Get-Command choco -ErrorAction SilentlyContinue)) { |
| 30 | + function global:choco {} |
46 | 31 | }
|
47 | 32 |
|
48 |
| - it 'Test-TargetResource returns false when Present and Not configured' { |
49 |
| - Test-TargetResource -ConfigName 'proxy' -Ensure 'Present' -Value 'http://myproxy.url' | Should be $false |
| 33 | + Mock Get-Item -ModuleName $ModuleUnderTest -ParameterFilter { |
| 34 | + $Path.StartsWith($env:ChocolateyInstall) |
| 35 | + } -MockWith { |
| 36 | + $true |
50 | 37 | }
|
51 | 38 |
|
52 |
| - it 'Test-TargetResource returns false when Present and Unknown' { |
53 |
| - Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value 'MyValue' | Should be $false |
| 39 | + Mock Get-ChildItem -ModuleName $ModuleUnderTest -ParameterFilter { |
| 40 | + $Path -eq (Join-Path $env:ChocolateyInstall "config") |
| 41 | + } -MockWith { |
| 42 | + @{ |
| 43 | + Name = "chocolatey.config" |
| 44 | + FullName = Join-Path $env:ChocolateyInstall "config/chocolatey.config" |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + AfterAll { |
| 50 | + Remove-Module $ModuleUnderTest |
| 51 | + } |
| 52 | + |
| 53 | + Context "Test-TargetResource" { |
| 54 | + BeforeAll { |
| 55 | + Mock Get-Content -ModuleName $ModuleUnderTest -ParameterFilter { |
| 56 | + $Path.EndsWith('chocolatey.config') |
| 57 | + } -MockWith { |
| 58 | + '<?xml version="1.0" encoding="utf-8"?> |
| 59 | + <chocolatey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 60 | + <config> |
| 61 | + <add key="commandExecutionTimeoutSeconds" value="1339" description="Default timeout for command execution. for infinite (starting in 0.10.4)." /> |
| 62 | + <add key="proxy" value="" description="Explicit proxy location. Available in 0.9.9.9+." /> |
| 63 | + </config> |
| 64 | + <sources> |
| 65 | + <source id="chocolatey" value="https://chocolatey.org/api/v2/" disabled="false" bypassProxy="false" selfService="false" adminOnly="false" priority="0" /> |
| 66 | + </sources> |
| 67 | + </chocolatey>' |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + It 'Test-TargetResource returns true when Present and Configured.' { |
| 72 | + Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Present' -Value '1339' | Should -Be $true |
54 | 73 | }
|
55 | 74 |
|
56 |
| - it 'Test-TargetResource throws when Present and no value' { |
| 75 | + It 'Test-TargetResource returns false when Present and Not configured' { |
| 76 | + Test-TargetResource -ConfigName 'proxy' -Ensure 'Present' -Value 'http://myproxy.url' | Should -Be $false |
| 77 | + } |
| 78 | + |
| 79 | + It 'Test-TargetResource returns false when Present and Unknown' { |
| 80 | + Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value 'MyValue' | Should -Be $false |
| 81 | + } |
| 82 | + |
| 83 | + It 'Test-TargetResource throws when Present and no value' { |
57 | 84 | { Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' } | Should -Throw "Missing parameter 'Value' when ensuring config is present!"
|
58 | 85 | }
|
59 | 86 |
|
60 |
| - it 'Test-TargetResource throws when Present and no value' { |
| 87 | + It 'Test-TargetResource throws when Present and no value' { |
61 | 88 | { Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value '' } | Should -Throw "Missing parameter 'Value' when ensuring config is present!"
|
62 | 89 | }
|
63 | 90 |
|
64 |
| - it 'Test-TargetResource throws when Present and no value' { |
| 91 | + It 'Test-TargetResource throws when Present and no value' { |
65 | 92 | { Test-TargetResource -ConfigName 'MyParam' -Ensure 'Present' -Value $null } | Should -Throw "Missing parameter 'Value' when ensuring config is present!"
|
66 | 93 | }
|
67 | 94 |
|
68 |
| - it 'Test-TargetResource returns false when Absent and Configured' { |
69 |
| - Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Absent' | Should be $false |
| 95 | + It 'Test-TargetResource returns false when Absent and Configured' { |
| 96 | + Test-TargetResource -ConfigName 'commandExecutionTimeoutSeconds' -Ensure 'Absent' | Should -Be $false |
70 | 97 | }
|
71 | 98 |
|
72 |
| - it 'Test-TargetResource returns true when Absent and Not configured' { |
73 |
| - Test-TargetResource -ConfigName 'proxy' -Ensure 'Absent' | Should be $true |
| 99 | + It 'Test-TargetResource returns true when Absent and Not configured' { |
| 100 | + Test-TargetResource -ConfigName 'proxy' -Ensure 'Absent' | Should -Be $true |
74 | 101 | }
|
75 | 102 |
|
76 |
| - it 'Test-TargetResource returns true when Absent and Unknown' { |
77 |
| - Test-TargetResource -ConfigName 'MyParam' -Ensure 'Absent' | Should be $true |
| 103 | + It 'Test-TargetResource returns true when Absent and Unknown' { |
| 104 | + Test-TargetResource -ConfigName 'MyParam' -Ensure 'Absent' | Should -Be $true |
78 | 105 | }
|
79 |
| - |
80 | 106 | }
|
81 | 107 |
|
82 | 108 | Context "Set-TargetResource" {
|
83 |
| - |
84 |
| - InModuleScope -ModuleName cChocoConfig -ScriptBlock { |
85 |
| - function choco {} |
86 |
| - mock choco {} |
| 109 | + BeforeAll { |
| 110 | + Mock choco -ModuleName $ModuleUnderTest |
87 | 111 | }
|
88 | 112 |
|
89 |
| - Set-TargetResource -ConfigName "TestConfig" -Ensure "Present" -Value "MyValue" |
| 113 | + Context "Setting a config value when Present" { |
| 114 | + BeforeAll { |
| 115 | + Set-TargetResource -ConfigName "TestConfig" -Ensure "Present" -Value "MyValue" |
| 116 | + } |
90 | 117 |
|
91 |
| - it "Present - Should have called choco, with set" { |
92 |
| - Assert-MockCalled -CommandName choco -ModuleName cChocoConfig -ParameterFilter { |
93 |
| - $args -contains "'MyValue'" |
| 118 | + It "Present - Should have called choco, to set the specified ConfigName with the specified Value" { |
| 119 | + Assert-MockCalled choco -ModuleName $ModuleUnderTest -ParameterFilter { |
| 120 | + $args[0] -eq 'config' -and |
| 121 | + $args -match "\bset\b" -and |
| 122 | + $args -match "'MyValue'" |
| 123 | + } -Scope Context |
94 | 124 | }
|
95 | 125 | }
|
96 | 126 |
|
97 |
| - Set-TargetResource -ConfigName "TestConfig" -Ensure "Absent" |
| 127 | + Context "Removing a config value when Absent" { |
| 128 | + BeforeAll { |
| 129 | + Set-TargetResource -ConfigName "TestConfig" -Ensure "Absent" |
| 130 | + } |
98 | 131 |
|
99 |
| - it "Absent - Should have called choco, with unset" { |
100 |
| - Assert-MockCalled -CommandName choco -ModuleName cChocoConfig -ParameterFilter { |
101 |
| - $args -contains "unset" |
| 132 | + It "Absent - Should have called choco, to unset the specified ConfigName" { |
| 133 | + Assert-MockCalled choco -ModuleName $ModuleUnderTest -ParameterFilter { |
| 134 | + $args[0] -eq "config" -and |
| 135 | + $args -match "\bunset\b" -and |
| 136 | + $args -match "'TestConfig'" |
| 137 | + } -Scope Context |
102 | 138 | }
|
103 | 139 | }
|
104 | 140 | }
|
|
0 commit comments