Description
Use Case
Unix OS should be able to run unit tests for DSC resources
- DSC resources are dependencies for the premium forge module CEM_windows
- Job hardware supporting CD4PE code jobs are usually Unix based.
- DSC unit testing requires complex setup of temporary Windows VM provisioning (via Vagrant, Terraform, other)
- The proposed changes would allow any
pwsh
supported platform to run unit tests for DSC resources.- This includes docker images like
puppet-dev-tools
for ephemeral testing.
- This includes docker images like
Currently unit tests for DSC resources must be performed on a Windows host as there is a dependency on PowerShell to execute the canonicalize
provider method during catalog compilation.
This often requires a complex Vagrant, Terraform, Build setup to provision short-term Windows VMs for ephemeral unit testing. This is a potentially complex and costly setup for users.
Issue Background
The canonicalize
method triggers a PowerShell process on agent or test hardware during catalog compilation to Get
current state of DSC resources on the target node.
The feature is implemented to negate unnecessary corrective actions due to case-mismatch. The provider is currently limited to PowerShell (Windows only).
See ref: https://www.puppet.com/docs/puppet/8/about_the_resource_api.html#provider_features-canonicalize
Describe the Solution You Would Like
-
The Unix OS running as test hardware will require
pwsh
to be installed and the $PSPATH added to $PATH.- See following for install process: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.4
-
The dsc_base_provider should utilise
pwsh
in the ps_manager method to ensure Unix platforms successfully execute the canonicalise method to normalise User data values supplied within the manifest.- The
canonicalize
method attempts to start a Powershell instance on Unix to validate manifest values during catalog compilation.
ruby-pwsh/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb
Lines 48 to 63 in fec0326
- It should use
pwsh
on Unix to inspect the local dsc state. Theinvoke-dscresource -method get
will return nil state and the catalog will compile manifest values as source of truth
ruby-pwsh/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb
Lines 1076 to 1080 in fec0326
- The
-
The 'Pwsh::Manager.pwsh_path' method should consistently split the $PATH system variable by ':' when the ruby interpreter is run on Unix OS.
- When unit testing via a Unix host, the rspec context fools the interpreter into thinking the File::PATH_SEPARATOR constant is Windows based
;
instead of:
for Unix. This prevents the execution ofpwsh
from within the $PSPATH listed within the $PATH system variable. - Ensuring the $PSPATH has been added to the $PATH system variable on the Unix OS, the
pwsh
executable will be dynamically found
Lines 378 to 386 in fec0326
- When unit testing via a Unix host, the rspec context fools the interpreter into thinking the File::PATH_SEPARATOR constant is Windows based
-
Pwsh::Util.on_windows? method should identify the OS running the Ruby interpreter not the synthetic context OS within rspec to ensure Unix conditional code is respected.
- File::ALT_SEPARATOR constant is set for Windows even if the OS is Unix when rspec context OS is Windows.
- This means a Unix host running rspec will not accurately return the correct boolean value for this method and follows Windows specific logic.
Lines 12 to 16 in fec0326