|
| 1 | +$ModuleName = (Split-Path -leaf $MyInvocation.MyCommand.Path) -replace '\.[Tt][Ee][Ss][Tt][Ss].[Pp][Ss]1' |
| 2 | +$TestsFolder = 1..4 | |
| 3 | + foreach {$Path = $MyInvocation.MyCommand.Path } {$Path = Split-Path $Path} {$Path} |
| 4 | +$RootOfModule = Split-Path $TestsFolder |
| 5 | +$CurrentResourceModulePath = Join-Path $RootOfModule "DscResources/$ModuleName" |
| 6 | + |
| 7 | +Import-Module $CurrentResourceModulePath |
| 8 | + |
| 9 | +InModuleScope $ModuleName { |
| 10 | + Describe 'how Get-TargetResource reponds' { |
| 11 | + Context 'when automatic page file is configured' { |
| 12 | + mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_ComputerSystem'} -mockWith { |
| 13 | + return ([pscustomobject]@{AutomaticManagedPageFile = $true}) |
| 14 | + } |
| 15 | + mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_PageFileSetting'} -mockWith {} |
| 16 | + |
| 17 | + $result = Get-TargetResource -initialsize 4GB -MaximumSize 4GB -Ensure 'Present' |
| 18 | + |
| 19 | + It 'should call once Get-WmiObject Win32_ComputerSystem ' { |
| 20 | + Assert-MockCalled -commandName Get-WmiObject -times 1 -Exactly -parameterFilter { |
| 21 | + $Class -like 'Win32_ComputerSystem' |
| 22 | + } |
| 23 | + } |
| 24 | + It 'should not call Get-WmiObject Win32_PageFileSetting' { |
| 25 | + Assert-MockCalled -commandName Get-WmiObject -times 0 -Exactly -parameterFilter { |
| 26 | + $Class -like 'Win32_PageFileSetting' |
| 27 | + } |
| 28 | + } |
| 29 | + It "should return Ensure = 'Absent'" { |
| 30 | + $result['Ensure'] | should be ('Absent') |
| 31 | + } |
| 32 | + } |
| 33 | + Context 'when automatic page file not configured' { |
| 34 | + mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_ComputerSystem'} -mockWith { |
| 35 | + return ([pscustomobject]@{AutomaticManagedPageFile = $false}) |
| 36 | + } |
| 37 | + mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_PageFileSetting'} -mockWith { |
| 38 | + return ([pscustomobject]@{ |
| 39 | + InitialSize = (3GB/1MB) |
| 40 | + MaximumSize = (3GB/1MB) |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + $result = Get-TargetResource -initialsize 4GB -MaximumSize 4GB -Ensure 'Present' |
| 45 | + |
| 46 | + It 'should call once Get-WmiObject Win32_ComputerSystem ' { |
| 47 | + Assert-MockCalled -commandName Get-WmiObject -times 1 -Exactly -parameterFilter { |
| 48 | + $Class -like 'Win32_ComputerSystem' |
| 49 | + } |
| 50 | + } |
| 51 | + It 'should call once Get-WmiObject Win32_PageFileSetting' { |
| 52 | + Assert-MockCalled -commandName Get-WmiObject -times 1 -Exactly -parameterFilter { |
| 53 | + $Class -like 'Win32_PageFileSetting' |
| 54 | + } |
| 55 | + } |
| 56 | + It "should return Ensure = 'Present' with Intial and Maximum size at 3GB" { |
| 57 | + $result['Ensure'] | should be ('Present') |
| 58 | + $result['InitialSize'] | should be (3GB) |
| 59 | + $result['MaximumSize'] | should be (3GB) |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + Describe 'how Set-TargetResource responds' { |
| 66 | + |
| 67 | + Context 'when Ensure is set to Absent and AutomaticPageFile is set' { |
| 68 | + Mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_ComputerSystem'} -mockWith { |
| 69 | + $r = [pscustomobject]@{ |
| 70 | + AutomaticManagedPageFile = $true |
| 71 | + } | Add-Member -MemberType ScriptMethod -Name Put -Value { |
| 72 | + $global:PutWasCalled = $true |
| 73 | + $global:PutValue = $this |
| 74 | + } -PassThru |
| 75 | + return ($r) |
| 76 | + } |
| 77 | + $global:PutValue = $null |
| 78 | + $global:PutWasCalled = $False |
| 79 | + Set-TargetResource -initialsize 4GB -MaximumSize 4GB -Ensure 'Absent' |
| 80 | + |
| 81 | + It 'should not call put' { |
| 82 | + $global:PutWasCalled | should be ($false) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + Context 'when Ensure is set to Absent and AutomaticPageFile is not set' { |
| 87 | + Mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_ComputerSystem'} -mockWith { |
| 88 | + $r = [pscustomobject]@{ |
| 89 | + AutomaticManagedPageFile = $false |
| 90 | + } | Add-Member -MemberType ScriptMethod -Name Put -Value { |
| 91 | + $global:PutWasCalled = $true |
| 92 | + $global:PutValue = $this |
| 93 | + } -PassThru |
| 94 | + return ($r) |
| 95 | + } |
| 96 | + $global:PutValue = $null |
| 97 | + $global:PutWasCalled = $False |
| 98 | + Set-TargetResource -initialsize 4GB -MaximumSize 4GB -Ensure 'Absent' |
| 99 | + |
| 100 | + It 'should call put' { |
| 101 | + $global:PutWasCalled | should be ($true) |
| 102 | + } |
| 103 | + It 'should set AutomaticManagedPageFile set to $true' { |
| 104 | + $global:PutValue.AutomaticManagedPageFile | should be ($true) |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + Context 'when Ensure is set to Absent and AutomaticPageFile is not set' { |
| 109 | + Mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_ComputerSystem'} -mockWith { |
| 110 | + $r = [pscustomobject]@{ AutomaticManagedPageFile = $false |
| 111 | + } | |
| 112 | + Add-Member -MemberType ScriptMethod -Name Put -Value { |
| 113 | + $global:Win32_ComputerPutWasCalled = $true |
| 114 | + $global:Win32_ComputerPutValue = $this |
| 115 | + } -PassThru |
| 116 | + return ($r) |
| 117 | + } |
| 118 | + Mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_PageFileSetting'} -mockWith { |
| 119 | + $r = [pscustomobject]@{ |
| 120 | + InitialSize = 0 |
| 121 | + MaximumSize = 0 |
| 122 | + } | Add-Member -MemberType ScriptMethod -Name Put -Value { |
| 123 | + $global:Win32_PageFileSettingPutWasCalled = $true |
| 124 | + $global:Win32_PageFileSettingPutValue = $this |
| 125 | + } -PassThru |
| 126 | + return ($r) |
| 127 | + } |
| 128 | + |
| 129 | + $global:Win32_ComputerPutValue = $null |
| 130 | + $global:Win32_ComputerPutWasCalled = $False |
| 131 | + $global:Win32_PageFileSettingPutValue = $null |
| 132 | + $global:Win32_PageFileSettingPutWasCalled = $False |
| 133 | + |
| 134 | + Set-TargetResource -initialsize 4GB -MaximumSize 4GB -Ensure 'Absent' |
| 135 | + |
| 136 | + It 'should call put to Win32_ComputerSystem' { |
| 137 | + $global:Win32_ComputerPutWasCalled | should be ($true) |
| 138 | + } |
| 139 | + It 'should set AutomaticManagedPageFile set to $true' { |
| 140 | + $global:Win32_ComputerPutValue.AutomaticManagedPageFile | should be ($true) |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + Context 'when Ensure is set to Present and AutomaticPageFile is not set' { |
| 145 | + Mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_ComputerSystem'} -mockWith { |
| 146 | + $r = [pscustomobject]@{ AutomaticManagedPageFile = $false |
| 147 | + } | |
| 148 | + Add-Member -MemberType ScriptMethod -Name Put -Value { |
| 149 | + $global:Win32_ComputerPutWasCalled = $true |
| 150 | + $global:PutVWin32_ComputerPutValuealue = $this |
| 151 | + } -PassThru |
| 152 | + return ($r) |
| 153 | + } |
| 154 | + Mock -commandName Get-WmiObject -parameterFilter {$Class -like 'Win32_PageFileSetting'} -mockWith { |
| 155 | + $r = [pscustomobject]@{ |
| 156 | + InitialSize = 0 |
| 157 | + MaximumSize = 0 |
| 158 | + } | Add-Member -MemberType ScriptMethod -Name Put -Value { |
| 159 | + $global:Win32_PageFileSettingPutWasCalled = $true |
| 160 | + $global:Win32_PageFileSettingPutValue = $this |
| 161 | + } -PassThru |
| 162 | + return ($r) |
| 163 | + } |
| 164 | + |
| 165 | + $global:Win32_ComputerPutValue = $null |
| 166 | + $global:Win32_ComputerPutWasCalled = $False |
| 167 | + $global:Win32_PageFileSettingPutValue = $null |
| 168 | + $global:Win32_PageFileSettingPutWasCalled = $False |
| 169 | + |
| 170 | + Set-TargetResource -initialsize 4GB -MaximumSize 4GB -Ensure 'Present' |
| 171 | + |
| 172 | + It 'should call put on Win32_PageFileSetting' { |
| 173 | + $global:PutWasCalled | should be ($true) |
| 174 | + } |
| 175 | + It 'should not set AutomaticManagedPageFile set to $true' { |
| 176 | + $global:Win32_ComputerPutValue.AutomaticManagedPageFile | should beNullOrEmpty |
| 177 | + } |
| 178 | + It 'should set Initial and Maximum size to 4 GB' { |
| 179 | + $global:Win32_PageFileSettingPutValue.InitialSize | should be (4gb/1mb) |
| 180 | + $global:Win32_PageFileSettingPutValue.MaximumSize | should be (4gb/1mb) |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + Get-Variable -Scope Global -Name Win32_ComputerPutValue | |
| 187 | + Remove-Variable -Scope Global -Force |
| 188 | + Get-Variable -Scope Global -Name Win32_ComputerPutWasCalled | |
| 189 | + Remove-Variable -Scope Global -Force |
| 190 | + Get-Variable -Scope Global -Name Win32_PageFileSettingPutValue | |
| 191 | + Remove-Variable -Scope Global -Force |
| 192 | + Get-Variable -Scope Global -Name Win32_PageFileSettingPutWasCalled | |
| 193 | + Remove-Variable -Scope Global -Force |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | + |
0 commit comments