summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Edwards <adamed@opscode.com>2014-10-29 13:45:24 -0700
committerAdam Edwards <adamed@opscode.com>2014-10-29 13:45:24 -0700
commit6fb1fc6ceb7f1c3110bb3238764da655348bafdb (patch)
treed1d08ce2566818f4c311af4398d4784f879be973
parent0294c96e38b5cd9fcb36c3f16016bc31c4404921 (diff)
parentafd7a24739d2c637458af24a2dc19d26b3a3aa82 (diff)
downloadchef-6fb1fc6ceb7f1c3110bb3238764da655348bafdb.tar.gz
Merge pull request #2236 from opscode/adamedx/dsc-params-11-stable
Port Issue #2209: DSC parameters should be passed even when there is no config data file
-rw-r--r--lib/chef/provider/dsc_script.rb5
-rw-r--r--spec/functional/resource/dsc_script_spec.rb139
2 files changed, 94 insertions, 50 deletions
diff --git a/lib/chef/provider/dsc_script.rb b/lib/chef/provider/dsc_script.rb
index 9bb2b5f364..b88cb78fbe 100644
--- a/lib/chef/provider/dsc_script.rb
+++ b/lib/chef/provider/dsc_script.rb
@@ -61,7 +61,7 @@ class Chef
def define_resource_requirements
requirements.assert(:run) do |a|
err = [
- 'Could not find Dsc on the system',
+ 'Could not find PowerShell DSC support on the system',
powershell_info_str,
"Powershell 4.0 or higher was not detected on your system and is required to use the dsc_script resource.",
]
@@ -97,9 +97,8 @@ class Chef
end
def get_augmented_configuration_flags(configuration_data_path)
- updated_flags = nil
+ updated_flags = @dsc_resource.flags.nil? ? {} : @dsc_resource.flags.dup
if configuration_data_path
- updated_flags = @dsc_resource.flags.nil? ? {} : @dsc_resource.flags.dup
Chef::Util::PathHelper.validate_path(configuration_data_path)
updated_flags[:configurationdata] = configuration_data_path
end
diff --git a/spec/functional/resource/dsc_script_spec.rb b/spec/functional/resource/dsc_script_spec.rb
index fa13296c02..a736949c6b 100644
--- a/spec/functional/resource/dsc_script_spec.rb
+++ b/spec/functional/resource/dsc_script_spec.rb
@@ -81,17 +81,28 @@ describe Chef::Resource::DscScript, :windows_powershell_dsc_only do
let(:test_registry_value) { 'Registration' }
let(:test_registry_data1) { 'LL927' }
let(:test_registry_data2) { 'LL928' }
- let(:dsc_code) { <<-EOH
+ let(:reg_key_name_param_name) { 'testregkeyname' }
+ let(:reg_key_value_param_name) { 'testregvaluename' }
+ let(:registry_embedded_parameters) { "$#{reg_key_name_param_name} = '#{test_registry_key}';$#{reg_key_value_param_name} = '#{test_registry_value}'"}
+ let(:dsc_reg_code) { <<-EOH
+ #{registry_embedded_parameters}
Registry "ChefRegKey"
{
- Key = '#{test_registry_key}'
- ValueName = '#{test_registry_value}'
+ Key = $#{reg_key_name_param_name}
+ ValueName = $#{reg_key_value_param_name}
ValueData = '#{test_registry_data}'
Ensure = 'Present'
}
EOH
}
+ let(:dsc_code) { dsc_reg_code }
+ let(:dsc_reg_script) { <<-EOH
+ param($testregkeyname, $testregvaluename)
+ #{dsc_reg_code}
+EOH
+ }
+
let(:dsc_user_prefix) { 'dsc' }
let(:dsc_user_suffix) { 'chefx' }
let(:dsc_user) {"#{dsc_user_prefix}_usr_#{dsc_user_suffix}" }
@@ -175,7 +186,7 @@ environment "whatsmydir"
Ensure = 'Present'
}
EOH
- }
+}
let(:dsc_config_name) {
dsc_test_resource_base.name
@@ -227,41 +238,79 @@ environment 'removethis'
EOH
removal_resource.run_action(:run)
end
- let(:dsc_code) { dsc_environment_config }
- it 'should not raise an exception if the cwd is not etc' do
- dsc_test_resource.cwd(dsc_environment_no_fail_not_etc_directory)
- expect {dsc_test_resource.run_action(:run)}.not_to raise_error
- end
- 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
- dsc_test_resource.run_action(:run)
- rescue Chef::Exceptions::PowershellCmdletException => e
- expect(e.message).to match(exception_message_signature)
+ describe 'when the DSC configuration contains code that raises an exception if cwd has a specific value' do
+ let(:dsc_code) { dsc_environment_config }
+ it 'should not raise an exception if the cwd is not etc' do
+ dsc_test_resource.cwd(dsc_environment_no_fail_not_etc_directory)
+ expect {dsc_test_resource.run_action(:run)}.not_to raise_error
+ end
+
+ 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
+ dsc_test_resource.run_action(:run)
+ rescue Chef::Exceptions::PowershellCmdletException => e
+ expect(e.message).to match(exception_message_signature)
+ end
end
end
end
shared_examples_for 'a parameterized DSC configuration script' do
- context 'when specifying environment variables in the environment attribute' do
- let(:dsc_user_prefix_code) { dsc_user_prefix_env_code }
- let(:dsc_user_suffix_code) { dsc_user_suffix_env_code }
- it_behaves_like 'a dsc_script with configuration that uses environment variables'
+ let(:dsc_user_prefix_code) { dsc_user_prefix_env_code }
+ let(:dsc_user_suffix_code) { dsc_user_suffix_env_code }
+ it_behaves_like 'a dsc_script with configuration that uses environment variables'
+ end
+
+ shared_examples_for 'a dsc_script without configuration data that takes parameters' do
+ context 'when configuration data is not specified' do
+
+ before(:each) do
+ test_key_resource = Chef::Resource::RegistryKey.new(test_registry_key, dsc_test_run_context)
+ test_key_resource.recursive(true)
+ test_key_resource.run_action(:delete_key)
+ end
+
+ after(:each) do
+ test_key_resource = Chef::Resource::RegistryKey.new(test_registry_key, dsc_test_run_context)
+ test_key_resource.recursive(true)
+ test_key_resource.run_action(:delete_key)
+ end
+
+ let(:test_registry_data) { test_registry_data1 }
+ let(:dsc_parameterized_env_param_value) { "val" + Random::rand.to_s }
+
+ it 'should have a default value of nil for the configuration_data attribute' do
+ expect(dsc_test_resource.configuration_data).to eql(nil)
+ end
+
+ it 'should have a default value of nil for the configuration_data_path attribute' do
+ expect(dsc_test_resource.configuration_data_script).to eql(nil)
+ end
+
+ let(:dsc_test_resource) { dsc_resource_from_path }
+ let(:registry_embedded_parameters) { '' }
+ let(:dsc_code) { dsc_reg_script }
+
+ 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})
+ 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)
+ end
end
end
shared_examples_for 'a dsc_script with configuration data' do
- context 'when using the configuration_data attribute' do
- let(:configuration_data_attribute) { 'configuration_data' }
- it_behaves_like 'a dsc_script with configuration data set via an attribute'
- end
+ let(:configuration_data_attribute) { 'configuration_data' }
+ it_behaves_like 'a dsc_script with configuration data set via an attribute'
- context 'when using the configuration_data_script attribute' do
- let(:configuration_data_attribute) { 'configuration_data_script' }
- it_behaves_like 'a dsc_script with configuration data set via an attribute'
- end
+ let(:configuration_data_attribute) { 'configuration_data_script' }
+ it_behaves_like 'a dsc_script with configuration data set via an attribute'
end
shared_examples_for 'a dsc_script with configuration data set via an attribute' do
@@ -282,33 +331,28 @@ EOH
end
shared_examples_for 'a dsc_script with configuration data that takes parameters' do
- context 'when script code takes parameters for configuration' 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}"}}
- it 'does not directly contain the user name' do
- configuration_script_content = ::File.open(dsc_test_resource.command) do | file |
- file.read
- end
- expect(configuration_script_content.include?(dsc_user)).to be(false)
+ 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}"}}
+ it 'does not directly contain the user name' do
+ configuration_script_content = ::File.open(dsc_test_resource.command) do | file |
+ file.read
end
- it_behaves_like 'a dsc_script with configuration data'
+ expect(configuration_script_content.include?(dsc_user)).to be(false)
end
-
+ it_behaves_like 'a dsc_script with configuration data'
end
shared_examples_for 'a dsc_script with configuration data that uses environment variables' do
- context 'when script code uses environment variables' do
- let(:dsc_user_code) { dsc_user_env_code }
+ 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
- expect(configuration_script_content.include?(dsc_user)).to be(false)
+ it 'does not directly contain the user name' do
+ configuration_script_content = ::File.open(dsc_test_resource.command) do | file |
+ file.read
end
- it_behaves_like 'a dsc_script with configuration data'
+ expect(configuration_script_content.include?(dsc_user)).to be(false)
end
+ it_behaves_like 'a dsc_script with configuration data'
end
context 'when supplying configuration through the configuration attribute' do
@@ -333,5 +377,6 @@ EOH
it_behaves_like 'a dsc_script with configuration data'
it_behaves_like 'a dsc_script with configuration data that uses environment variables'
it_behaves_like 'a dsc_script with configuration data that takes parameters'
+ it_behaves_like 'a dsc_script without configuration data that takes parameters'
end
end