diff options
author | Daniel Schauenberg <d@unwiredcouch.com> | 2021-01-07 09:26:12 +0100 |
---|---|---|
committer | Daniel Schauenberg <d@unwiredcouch.com> | 2021-01-07 09:27:09 +0100 |
commit | e50d6e907d6bebec70271e9bcca50459dad88548 (patch) | |
tree | 57bef79bd565b536404761031eea39ebc90babdd /spec/functional/resource/dsc_script_spec.rb | |
parent | d98533a4d677cfc5bf1a9a77ff9cdcc4e2543862 (diff) | |
parent | f2a9569d23af039bd69d2dd3adb1251f7da044e3 (diff) | |
download | chef-e50d6e907d6bebec70271e9bcca50459dad88548.tar.gz |
Merge remote-tracking branch 'origin/master' into mrtazz/pkgng-exit-code-fix
Signed-off-by: Daniel Schauenberg <d@unwiredcouch.com>
Diffstat (limited to 'spec/functional/resource/dsc_script_spec.rb')
-rw-r--r-- | spec/functional/resource/dsc_script_spec.rb | 247 |
1 files changed, 119 insertions, 128 deletions
diff --git a/spec/functional/resource/dsc_script_spec.rb b/spec/functional/resource/dsc_script_spec.rb index e2b58f6432..b22599266b 100644 --- a/spec/functional/resource/dsc_script_spec.rb +++ b/spec/functional/resource/dsc_script_spec.rb @@ -1,6 +1,6 @@ # # Author:: Adam Edwards (<adamed@chef.io>) -# Copyright:: Copyright 2014-2016, Chef Software, Inc. +# Copyright:: Copyright (c) Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,14 +17,13 @@ # require "spec_helper" -require "chef/mixin/powershell_out" -require "chef/mixin/shell_out" +require "chef/mixin/powershell_exec" require "chef/mixin/windows_architecture_helper" require "support/shared/integration/integration_helper" -describe Chef::Resource::DscScript, :windows_powershell_dsc_only do +describe Chef::Resource::DscScript, :windows_powershell_dsc_only, :ruby64_only do include Chef::Mixin::WindowsArchitectureHelper - include Chef::Mixin::PowershellOut + include Chef::Mixin::PowershellExec before(:all) do @temp_dir = ::Dir.mktmpdir("dsc-functional-test") # enable the HTTP listener if it is not already enabled needed by underlying DSC engine @@ -34,7 +33,7 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only do winrm create winrm/config/Listener?Address=*+Transport=HTTP } CODE - powershell_out!(winrm_code) + powershell_exec!(winrm_code) end after(:all) do @@ -65,10 +64,8 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only do end def delete_user(target_user) - begin - shell_out!("net user #{target_user} /delete") - rescue Mixlib::ShellOut::ShellCommandFailed - end + shell_out!("net user #{target_user} /delete") + rescue Mixlib::ShellOut::ShellCommandFailed end let(:dsc_env_variable) { "chefenvtest" } @@ -76,10 +73,11 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only do let(:env_value2) { "value2" } let(:dsc_test_run_context) do node = Chef::Node.new + node.consume_external_attrs(OHAI_SYSTEM.data, {}) # node[:languages][:powershell][:version] + node.automatic["os"] = "windows" node.automatic["platform"] = "windows" node.automatic["platform_version"] = "6.1" node.automatic["kernel"][:machine] = :x86_64 # Only 64-bit architecture is supported - node.automatic[:languages][:powershell][:version] = "4.0" empty_events = Chef::EventDispatch::Dispatcher.new Chef::RunContext.new(node, {}, empty_events) end @@ -104,7 +102,7 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only do ValueData = '#{test_registry_data}' Ensure = 'Present' } -EOH + EOH end let(:dsc_code) { dsc_reg_code } @@ -112,7 +110,7 @@ EOH <<-EOH param($testregkeyname, $testregvaluename) #{dsc_reg_code} -EOH + EOH end let(:dsc_user_prefix) { "dsc" } @@ -139,7 +137,7 @@ EOH $#{dsc_user_prefix_param_name}, $#{dsc_user_suffix_param_name} ) -EOH + EOH end let(:config_param_section) { "" } @@ -148,59 +146,59 @@ EOH let(:dsc_user_suffix_code) { dsc_user_suffix } let(:dsc_script_environment_attribute) { nil } let(:dsc_user_resources_code) do - <<-EOH - #{config_param_section} -node localhost -{ -$testuser = #{dsc_user_code} -$testpassword = ConvertTo-SecureString -String "jf9a8m49jrajf4#" -AsPlainText -Force -$testcred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $testuser, $testpassword - -User dsctestusercreate -{ - UserName = $testuser - Password = $testcred - Description = "DSC test user" - Ensure = "Present" - Disabled = $false - PasswordNeverExpires = $true - PasswordChangeRequired = $false -} -} -EOH + <<~EOH + #{config_param_section} + node localhost + { + $testuser = #{dsc_user_code} + $testpassword = ConvertTo-SecureString -String "jf9a8m49jrajf4#" -AsPlainText -Force + $testcred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $testuser, $testpassword + + User dsctestusercreate + { + UserName = $testuser + Password = $testcred + Description = "DSC test user" + Ensure = "Present" + Disabled = $false + PasswordNeverExpires = $true + PasswordChangeRequired = $false + } + } + EOH end let(:dsc_user_config_data) do - <<-EOH -@{ - AllNodes = @( - @{ - NodeName = "localhost"; - PSDscAllowPlainTextPassword = $true - } - ) -} + <<~EOH + @{ + AllNodes = @( + @{ + NodeName = "localhost"; + PSDscAllowPlainTextPassword = $true + } + ) + } -EOH + EOH end let(:dsc_environment_env_var_name) { "dsc_test_cwd" } - let(:dsc_environment_no_fail_not_etc_directory) { "#{ENV['systemroot']}\\system32" } - let(:dsc_environment_fail_etc_directory) { "#{ENV['systemroot']}\\system32\\drivers\\etc" } + let(:dsc_environment_no_fail_not_etc_directory) { "#{ENV["systemroot"]}\\system32" } + let(:dsc_environment_fail_etc_directory) { "#{ENV["systemroot"]}\\system32\\drivers\\etc" } let(:exception_message_signature) { "LL927-LL928" } let(:dsc_environment_config) do - <<-EOH -if (($pwd.path -eq '#{dsc_environment_fail_etc_directory}') -and (test-path('#{dsc_environment_fail_etc_directory}'))) -{ - throw 'Signature #{exception_message_signature}: Purposefully failing because cwd == #{dsc_environment_fail_etc_directory}' -} -environment "whatsmydir" -{ - Name = '#{dsc_environment_env_var_name}' - Value = $pwd.path - Ensure = 'Present' -} -EOH + <<~EOH + if (($pwd.path -eq '#{dsc_environment_fail_etc_directory}') -and (test-path('#{dsc_environment_fail_etc_directory}'))) + { + throw 'Signature #{exception_message_signature}: Purposefully failing because cwd == #{dsc_environment_fail_etc_directory}' + } + environment "whatsmydir" + { + Name = '#{dsc_environment_env_var_name}' + Value = $pwd.path + Ensure = 'Present' + } + EOH end let(:dsc_config_name) do @@ -235,7 +233,7 @@ EOH expect(dsc_test_resource.registry_key_exists?(test_registry_key)).to eq(false) dsc_test_resource.run_action(:run) expect(dsc_test_resource.registry_key_exists?(test_registry_key)).to eq(true) - expect(dsc_test_resource.registry_value_exists?(test_registry_key, { :name => test_registry_value, :type => :string, :data => test_registry_data })).to eq(true) + expect(dsc_test_resource.registry_value_exists?(test_registry_key, { name: test_registry_value, type: :string, data: test_registry_data })).to eq(true) end it_should_behave_like "a dsc_script resource with configuration affected by cwd" @@ -244,13 +242,13 @@ EOH shared_examples_for "a dsc_script resource with configuration affected by cwd" do after(:each) do removal_resource = Chef::Resource::DscScript.new(dsc_test_resource_name, dsc_test_run_context) - removal_resource.code <<-EOH -environment 'removethis' -{ - Name = '#{dsc_environment_env_var_name}' - Ensure = 'Absent' -} -EOH + removal_resource.code <<~EOH + environment 'removethis' + { + Name = '#{dsc_environment_env_var_name}' + Ensure = 'Absent' + } + EOH removal_resource.run_action(:run) end @@ -263,12 +261,9 @@ EOH it "should raise an exception if the cwd is etc" do dsc_test_resource.cwd(dsc_environment_fail_etc_directory) - expect { dsc_test_resource.run_action(:run) }.to raise_error(Chef::Exceptions::PowershellCmdletException) - begin + expect { dsc_test_resource.run_action(:run) - rescue Chef::Exceptions::PowershellCmdletException => e - expect(e.message).to match(exception_message_signature) - end + }.to raise_error(Chef::PowerShell::CommandFailed, /#{exception_message_signature}/) end end end @@ -311,11 +306,11 @@ EOH it "should set a registry key according to parameters passed to the configuration" do dsc_test_resource.configuration_name(config_name_value) - dsc_test_resource.flags({ :"#{reg_key_name_param_name}" => test_registry_key, :"#{reg_key_value_param_name}" => test_registry_value }) + dsc_test_resource.flags({ "#{reg_key_name_param_name}": test_registry_key, "#{reg_key_value_param_name}": test_registry_value }) expect(dsc_test_resource.registry_key_exists?(test_registry_key)).to eq(false) dsc_test_resource.run_action(:run) expect(dsc_test_resource.registry_key_exists?(test_registry_key)).to eq(true) - expect(dsc_test_resource.registry_value_exists?(test_registry_key, { :name => test_registry_value, :type => :string, :data => test_registry_data })).to eq(true) + expect(dsc_test_resource.registry_value_exists?(test_registry_key, { name: test_registry_value, type: :string, data: test_registry_data })).to eq(true) end end end @@ -348,11 +343,9 @@ EOH shared_examples_for "a dsc_script with configuration data that takes parameters" do let(:dsc_user_code) { dsc_user_param_code } let(:config_param_section) { config_params } - let(:config_flags) { { :"#{dsc_user_prefix_param_name}" => "#{dsc_user_prefix}", :"#{dsc_user_suffix_param_name}" => "#{dsc_user_suffix}" } } + let(:config_flags) { { "#{dsc_user_prefix_param_name}": (dsc_user_prefix).to_s, "#{dsc_user_suffix_param_name}": (dsc_user_suffix).to_s } } it "does not directly contain the user name" do - configuration_script_content = ::File.open(dsc_test_resource.command) do |file| - file.read - end + configuration_script_content = ::File.open(dsc_test_resource.command, &:read) expect(configuration_script_content.include?(dsc_user)).to be(false) end it_behaves_like "a dsc_script with configuration data" @@ -362,9 +355,7 @@ EOH let(:dsc_user_code) { dsc_user_env_code } it "does not directly contain the user name" do - configuration_script_content = ::File.open(dsc_test_resource.command) do |file| - file.read - end + configuration_script_content = ::File.open(dsc_test_resource.command, &:read) expect(configuration_script_content.include?(dsc_user)).to be(false) end it_behaves_like "a dsc_script with configuration data" @@ -410,46 +401,46 @@ EOH end let(:dsc_configuration_script) do - <<-MYCODE -cd c:\\ -configuration LCM -{ - param ($thumbprint) - localconfigurationmanager - { - RebootNodeIfNeeded = $false - ConfigurationMode = 'ApplyOnly' - CertificateID = $thumbprint - } -} -$cert = ls Cert:\\LocalMachine\\My\\ | - Where-Object {$_.Subject -match "ChefTest"} | - Select -first 1 - -if($cert -eq $null) { - $pfxpath = '#{self_signed_cert_path}' - $password = '' - $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxpath, $password, ([System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeyset)) - $store = New-Object System.Security.Cryptography.X509Certificates.X509Store "My", ([System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine) - $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) - $store.Add($cert) - $store.Close() -} - -lcm -thumbprint $cert.thumbprint -set-dsclocalconfigurationmanager -path ./LCM -$ConfigurationData = @" -@{ -AllNodes = @( - @{ - NodeName = "localhost"; - CertificateID = '$($cert.thumbprint)'; - }; -); -} -"@ -$ConfigurationData | out-file '#{configuration_data_path}' -force - MYCODE + <<~MYCODE + cd c:\\ + configuration LCM + { + param ($thumbprint) + localconfigurationmanager + { + RebootNodeIfNeeded = $false + ConfigurationMode = 'ApplyOnly' + CertificateID = $thumbprint + } + } + $cert = ls Cert:\\LocalMachine\\My\\ | + Where-Object {$_.Subject -match "ChefTest"} | + Select -first 1 + + if($cert -eq $null) { + $pfxpath = '#{self_signed_cert_path}' + $password = '' + $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($pfxpath, $password, ([System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeyset)) + $store = New-Object System.Security.Cryptography.X509Certificates.X509Store "My", ([System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine) + $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) + $store.Add($cert) + $store.Close() + } + + lcm -thumbprint $cert.thumbprint + set-dsclocalconfigurationmanager -path ./LCM + $ConfigurationData = @" + @{ + AllNodes = @( + @{ + NodeName = "localhost"; + CertificateID = '$($cert.thumbprint)'; + }; + ); + } + "@ + $ConfigurationData | out-file '#{configuration_data_path}' -force + MYCODE end let(:powershell_script_resource) do @@ -461,14 +452,14 @@ $ConfigurationData | out-file '#{configuration_data_path}' -force let(:dsc_script_resource) do dsc_test_resource_base.tap do |r| - r.code <<-EOF -User dsctestusercreate -{ - UserName = '#{dsc_user}' - Password = #{r.ps_credential('jf9a8m49jrajf4#')} - Ensure = "Present" -} -EOF + r.code <<~EOF + User dsctestusercreate + { + UserName = '#{dsc_user}' + Password = #{r.ps_credential("jf9a8m49jrajf4#")} + Ensure = "Present" + } + EOF r.configuration_data_script(configuration_data_path) end end |