summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn McCrae <john.mccrae@progress.com>2022-06-21 10:51:00 -0700
committerJohn McCrae <john.mccrae@progress.com>2022-06-24 11:23:33 -0700
commit2227d02c8d99ee4c0e4978f1c824860ebb8b48bc (patch)
tree6cdf6027eca3f30095c273492f180fe0f815c561
parent326af7f58592cef4c6e997296ec50c4eccb39826 (diff)
downloadchef-2227d02c8d99ee4c0e4978f1c824860ebb8b48bc.tar.gz
Refactored code to be more readable and changed a test to correct for starting out with Auto-Managed being false by default
Signed-off-by: John McCrae <john.mccrae@progress.com>
-rw-r--r--lib/chef/resource/windows_pagefile.rb33
-rw-r--r--spec/functional/resource/windows_pagefile_spec.rb10
2 files changed, 29 insertions, 14 deletions
diff --git a/lib/chef/resource/windows_pagefile.rb b/lib/chef/resource/windows_pagefile.rb
index ca80d5b2ab..80cbfc05c5 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
@@ -165,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
@@ -226,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
diff --git a/spec/functional/resource/windows_pagefile_spec.rb b/spec/functional/resource/windows_pagefile_spec.rb
index bb30c0e054..a947d5b40e 100644
--- a/spec/functional/resource/windows_pagefile_spec.rb
+++ b/spec/functional/resource/windows_pagefile_spec.rb
@@ -40,9 +40,19 @@ 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
+
describe "Setting Up Pagefile Management" do
context "Disable 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)