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.
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.
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 '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 of pwsh from within the $PSPATH listed within the $PATH system variable.
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.
Use Case
Unix OS should be able to run unit tests for DSC resources
pwsh
supported platform to run unit tests for DSC resources.puppet-dev-tools
for ephemeral testing.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 toGet
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-canonicalizeDescribe the Solution You Would Like
The Unix OS running as test hardware will require
pwsh
to be installed and the $PSPATH added to $PATH.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.canonicalize
method attempts to start a Powershell instance on Unix to validate manifest values during catalog compilation. https://github.com/puppetlabs/ruby-pwsh/blob/fec0326118ddaf566f130ad7d0f3f695b4d2e380/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb#L48-L63pwsh
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 https://github.com/puppetlabs/ruby-pwsh/blob/fec0326118ddaf566f130ad7d0f3f695b4d2e380/lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb#L1076-L1080The 'Pwsh::Manager.pwsh_path' method should consistently split the $PATH system variable by ':' when the ruby interpreter is run on Unix OS.
;
instead of:
for Unix. This prevents the execution ofpwsh
from within the $PSPATH listed within the $PATH system variable.pwsh
executable will be dynamically found https://github.com/puppetlabs/ruby-pwsh/blob/fec0326118ddaf566f130ad7d0f3f695b4d2e380/lib/pwsh.rb#L378-L386Pwsh::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.
https://github.com/puppetlabs/ruby-pwsh/blob/fec0326118ddaf566f130ad7d0f3f695b4d2e380/lib/pwsh/util.rb#L12-L16