summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn McCrae <john.mccrae@progress.com>2023-04-27 21:25:39 +0600
committerGitHub <noreply@github.com>2023-04-27 21:25:39 +0600
commitad35bf42c057ea5832497b07f75e608ffd3f3409 (patch)
treec3caf03e465e829efd0054fe61dd292479ede3a8
parentc17f7114baa0c50a7e9a6c2f2aba23e780df5971 (diff)
parent481aa812b0e25d47ded9504a326cf67f3967c161 (diff)
downloadchef-ad35bf42c057ea5832497b07f75e608ffd3f3409.tar.gz
Merge pull request #13722 from chef/jfm/chef17_adhoc_pagefile
[chef-17] 15 of X - Correcting Pagefile spec errors in AdHoc
-rw-r--r--lib/chef/resource/windows_pagefile.rb49
-rw-r--r--spec/functional/resource/windows_pagefile_spec.rb35
2 files changed, 59 insertions, 25 deletions
diff --git a/lib/chef/resource/windows_pagefile.rb b/lib/chef/resource/windows_pagefile.rb
index 6fb63f2f3b..32f1151b13 100644
--- a/lib/chef/resource/windows_pagefile.rb
+++ b/lib/chef/resource/windows_pagefile.rb
@@ -88,7 +88,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
@@ -149,10 +149,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
@@ -164,13 +166,17 @@ 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
@@ -225,12 +231,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
@@ -240,14 +248,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