summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn McCrae <john.mccrae@progress.com>2021-06-02 19:17:14 -0700
committerJohn McCrae <john.mccrae@progress.com>2021-06-02 19:17:14 -0700
commit8010189955828df2acb3b2182f0efdd511a67f61 (patch)
treec301c913d9a47918667db4a3fa91c774832e22d5
parentc6caf9c4346d854e6dfb6dabb9185c3c9f961b76 (diff)
downloadchef-8010189955828df2acb3b2182f0efdd511a67f61.tar.gz
Updated the Windows Pagefile resource to use PowerShell over WMI, added a corresponding test file
Signed-off-by: John McCrae <john.mccrae@progress.com>
-rw-r--r--lib/chef/resource/windows_pagefile.rb34
-rw-r--r--spec/functional/resource/windows_pagefile_spec.rb2
2 files changed, 17 insertions, 19 deletions
diff --git a/lib/chef/resource/windows_pagefile.rb b/lib/chef/resource/windows_pagefile.rb
index 69c93ee1ff..2764ff31f7 100644
--- a/lib/chef/resource/windows_pagefile.rb
+++ b/lib/chef/resource/windows_pagefile.rb
@@ -24,7 +24,7 @@ class Chef
provides(:windows_pagefile) { true }
- description "Use the **windows_pagefile** resource to configure pagefile settings on Windows. The resource can accept paths in the form of 'C:\pagefile.sys', 'f:\foo\pagefile.sys', 'h', etc."
+ description "Use the **windows_pagefile** resource to configure pagefile settings on Windows. We are introducing a potentially breaking change - we no longer require or use a full path to a pagefile. We only need the drive (C:\, E:\, etc) you want to manage a pagefile for."
introduced "14.0"
examples <<~DOC
**Set the system to manage pagefiles**:
@@ -83,10 +83,6 @@ class Chef
description: "Maximum size of the pagefile in megabytes."
action :set, description: "Configures the default pagefile, creating if it doesn't exist." do
- pagefile = clarify_pagefile_name
- initial_size = new_resource.initial_size
- maximum_size = new_resource.maximum_size
- system_managed = new_resource.system_managed
automatic_managed = new_resource.automatic_managed
if automatic_managed
@@ -94,6 +90,11 @@ class Chef
elsif automatic_managed == false
unset_automatic_managed if automatic_managed?
else
+ pagefile = clarify_pagefile_name
+ initial_size = new_resource.initial_size
+ maximum_size = new_resource.maximum_size
+ system_managed = new_resource.system_managed
+
# the method below is designed to raise an exception if the drive you are trying to create a pagefile for doesn't exist.
# PowerShell will happily let you create a pagefile called h:\pagefile.sys even though you don't have an H:\ drive.
@@ -114,29 +115,26 @@ class Chef
action :delete, description: "Deletes the specified pagefile." do
pagefile = clarify_pagefile_name
- delete(pagefile ) if exists?(pagefile )
+ delete(pagefile) if exists?(pagefile)
end
action_class do
private
+ # We are adding support for a number of possibilities for how users will express the drive and location they want the pagefile written to.
def clarify_pagefile_name
- if new_resource.path.length < 4
- (new_resource.path[0] + ":\\pagefile.sys")
- elsif new_resource.path !~ /^.:.*/
- "C:\\pagefile.sys"
- else
- validate_name
+ case new_resource.path
+ # user enters C, C:, C:\, C:\\
+ when /^[a-zA-Z]/
+ new_resource.path[0] + ":\\pagefile.sys"
+ # user enters C:\pagefile.sys OR c:\foo\bar\pagefile.sys as the path
+ when /^[a-zA-Z]:.*.sys/
new_resource.path
+ else
+ raise "#{new_resource.path} does not match the format DRIVE:\\path\\pagefile.sys for pagefiles. Example: C:\\pagefile.sys"
end
end
- def validate_name
- return if /^.:.*.sys/.match?(new_resource.path)
-
- raise "#{new_resource.path} does not match the format DRIVE:\\path\\pagefile.sys for pagefiles. Example: C:\\pagefile.sys"
- end
-
# raise an exception if the target drive location is invalid
def pagefile_drive_exist?(pagefile)
if ::Dir.exist?(pagefile[0] + ":\\") == false
diff --git a/spec/functional/resource/windows_pagefile_spec.rb b/spec/functional/resource/windows_pagefile_spec.rb
index c627b6708f..fd44c01973 100644
--- a/spec/functional/resource/windows_pagefile_spec.rb
+++ b/spec/functional/resource/windows_pagefile_spec.rb
@@ -21,7 +21,7 @@ require "chef/mixin/powershell_exec"
describe Chef::Resource::WindowsPagefile, :windows_only do
include Chef::Mixin::PowershellExec
- let(:c_path) { "c:\pagefile.sys" }
+ let(:c_path) { "c:\\" }
let(:e_path) { "e:\pagefile.sys" }
let(:run_context) do