diff options
author | John McCrae <john.mccrae@progress.com> | 2022-06-25 10:11:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-25 10:11:18 -0700 |
commit | 620959013d601f751d0ccb285e513c4a6a394a6d (patch) | |
tree | 7af4783379ac73b64996dd804e9a30a972133050 | |
parent | 786715a03bfa2466800941d8edbbd5a558df8820 (diff) | |
parent | 87193ea7913b85330f5bbf45d02a022c65fb920e (diff) | |
download | chef-620959013d601f751d0ccb285e513c4a6a394a6d.tar.gz |
Merge pull request #13009 from chef/jfm/windows_pagefile_errors_in_adhoc
Signed-off-by: John McCrae <john.mccrae@progress.com>
-rw-r--r-- | lib/chef/resource/windows_pagefile.rb | 48 | ||||
-rw-r--r-- | spec/functional/resource/windows_pagefile_spec.rb | 35 |
2 files changed, 58 insertions, 25 deletions
diff --git a/lib/chef/resource/windows_pagefile.rb b/lib/chef/resource/windows_pagefile.rb index 03649e28aa..543170a70e 100644 --- a/lib/chef/resource/windows_pagefile.rb +++ b/lib/chef/resource/windows_pagefile.rb @@ -87,7 +87,7 @@ class Chef if automatic_managed set_automatic_managed unless automatic_managed? elsif automatic_managed == false - unset_automatic_managed if automatic_managed? + unset_automatic_managed else pagefile = clarify_pagefile_name initial_size = new_resource.initial_size @@ -148,10 +148,12 @@ class Chef def exists?(pagefile) @exists ||= begin logger.trace("Checking if #{pagefile} exists by running: Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($pagefile)} ") - cmd = "$page_file_name = '#{pagefile}';" - cmd << "$pagefile = Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($page_file_name)};" - cmd << "if ([string]::IsNullOrEmpty($pagefile)) { return $false } else { return $true }" - powershell_exec!(cmd).result + powershell_code = <<~CODE + $page_file_name = '#{pagefile}'; + $pagefile = Get-CimInstance Win32_PagefileSetting | Where-Object { $_.name -eq $($page_file_name)} + if ([string]::IsNullOrEmpty($pagefile)) { return $false } else { return $true } + CODE + powershell_exec!(powershell_code).result end end @@ -163,13 +165,16 @@ class Chef # @return [Boolean] def max_and_min_set?(pagefile, min, max) logger.trace("Checking if #{pagefile} has max and initial disk size values set") - cmd = "$page_file = '#{pagefile}';" - cmd << "$driveLetter = $page_file.split(':')[0];" - cmd << "$page_file_settings = Get-CimInstance -ClassName Win32_PageFileSetting -Filter \"SettingID='pagefile.sys @ $($driveLetter):'\" -Property * -ErrorAction Stop;" - cmd << "if ($page_file_settings.InitialSize -eq #{min} -and $page_file_settings.MaximumSize -eq #{max})" - cmd << "{ return $true }" - cmd << "else { return $false }" - powershell_exec!(cmd).result + powershell_code = <<-CODE + $page_file = '#{pagefile}'; + $driveLetter = $page_file.split(':')[0]; + $page_file_settings = Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -Property * -ErrorAction Stop; + if ($page_file_settings.InitialSize -eq #{min} -and $page_file_settings.MaximumSize -eq #{max}) + { return $true } + else + { return $false } + CODE + powershell_exec!(powershell_code).result end # create a pagefile @@ -224,12 +229,14 @@ class Chef # turn off automatic management of all pagefiles by Windows def unset_automatic_managed - converge_by("Turn off Automatically Managed on pagefiles") do - logger.trace("Running Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru") - powershell_exec! <<~EOH - $sys = Get-CimInstance Win32_ComputerSystem -Property * - Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru - EOH + if automatic_managed? + converge_by("Turn off Automatically Managed on pagefiles") do + logger.trace("Running Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru") + powershell_exec! <<~EOH + $sys = Get-CimInstance Win32_ComputerSystem -Property * + Set-CimInstance -InputObject $sys -Property @{AutomaticManagedPagefile=$false} -PassThru + EOH + end end end @@ -239,14 +246,13 @@ class Chef # @param [String] min the minimum size of the pagefile # @param [String] max the minimum size of the pagefile def set_custom_size(pagefile, min, max) + unset_automatic_managed converge_by("set #{pagefile} to InitialSize=#{min} & MaximumSize=#{max}") do logger.trace("Set-CimInstance -Property @{InitialSize = #{min} MaximumSize = #{max}") powershell_exec! <<~EOD $page_file = "#{pagefile}" $driveLetter = $page_file.split(':')[0] - Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{ - InitialSize = #{min} - MaximumSize = #{max}} + Get-CimInstance -ClassName Win32_PageFileSetting -Filter "SettingID='pagefile.sys @ $($driveLetter):'" -ErrorAction Stop | Set-CimInstance -Property @{InitialSize = #{min}; MaximumSize = #{max};} EOD end end diff --git a/spec/functional/resource/windows_pagefile_spec.rb b/spec/functional/resource/windows_pagefile_spec.rb index fd44c01973..9e4abc7e58 100644 --- a/spec/functional/resource/windows_pagefile_spec.rb +++ b/spec/functional/resource/windows_pagefile_spec.rb @@ -40,13 +40,32 @@ describe Chef::Resource::WindowsPagefile, :windows_only do new_resource end + def set_automatic_managed_to_false + powershell_code = <<~EOH + $computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges; + $computersys.AutomaticManagedPagefile = $False; + $computersys.Put(); + EOH + powershell_exec!(powershell_code) + end + + def set_automatic_managed_to_true + powershell_code = <<~EOH + $computersys = Get-WmiObject Win32_ComputerSystem -EnableAllPrivileges; + $computersys.AutomaticManagedPagefile = $True; + $computersys.Put(); + EOH + powershell_exec!(powershell_code) + end + describe "Setting Up Pagefile Management" do context "Disable Automatic Management" do - it "Disables Automatic Management" do + it "Verifies Automatic Management is Disabled" do + set_automatic_managed_to_false subject.path c_path subject.automatic_managed false subject.run_action(:set) - expect(subject).to be_updated_by_last_action + expect(subject).not_to be_updated_by_last_action end it "Enable Automatic Management " do @@ -55,6 +74,14 @@ describe Chef::Resource::WindowsPagefile, :windows_only do subject.run_action(:set) expect(subject).to be_updated_by_last_action end + + it "Disables Automatic Management" do + set_automatic_managed_to_true + subject.path c_path + subject.automatic_managed false + subject.run_action(:set) + expect(subject).to be_updated_by_last_action + end end end @@ -69,8 +96,8 @@ describe Chef::Resource::WindowsPagefile, :windows_only do context "Update a pagefile" do it "Changes a pagefile to use custom sizes" do subject.path c_path - subject.initial_size 20000 - subject.maximum_size 80000 + subject.initial_size 128 + subject.maximum_size 512 subject.run_action(:set) expect(subject).to be_updated_by_last_action end |