diff options
author | Adam Edwards <adamed@opscode.com> | 2014-08-28 06:10:48 -0700 |
---|---|---|
committer | Adam Edwards <adamed@opscode.com> | 2014-08-29 14:42:47 -0700 |
commit | 4c76b295ccd652012a281d2de3f043b44f9a23e7 (patch) | |
tree | ba912aa4064a59e039926598f2abef436f1c717d | |
parent | dd49506ed7572fffa5f093d92565e52320ae221e (diff) | |
download | chef-4c76b295ccd652012a281d2de3f043b44f9a23e7.tar.gz |
DSC: Added test for dsc_script provider
-rw-r--r-- | lib/chef/provider/dsc_script.rb | 4 | ||||
-rw-r--r-- | spec/unit/provider/dsc_script_spec.rb | 124 | ||||
-rw-r--r-- | spec/unit/util/dsc/configuration_generator_spec.rb | 4 |
3 files changed, 129 insertions, 3 deletions
diff --git a/lib/chef/provider/dsc_script.rb b/lib/chef/provider/dsc_script.rb index 122b7e6198..a1bb9c63a7 100644 --- a/lib/chef/provider/dsc_script.rb +++ b/lib/chef/provider/dsc_script.rb @@ -98,7 +98,9 @@ class Chef if @dsc_resource.command generator.configuration_document_from_script_path(@dsc_resource.command, configuration_name, configuration_flags, shellout_flags) else - generator.configuration_document_from_script_code(@dsc_resource.code, configuration_flags, shellout_flags) + # If code is also not provided, we mimic what the other script resources do (execute nothing) + Chef::Log.warn("Neither code or command were provided for dsc_resource[#{@dsc_resource.name}].") unless @dsc_resource.code + generator.configuration_document_from_script_code(@dsc_resource.code || '', configuration_flags, shellout_flags) end end diff --git a/spec/unit/provider/dsc_script_spec.rb b/spec/unit/provider/dsc_script_spec.rb new file mode 100644 index 0000000000..8c9064a6c1 --- /dev/null +++ b/spec/unit/provider/dsc_script_spec.rb @@ -0,0 +1,124 @@ +# +# Author:: Jay Mundrawala (<jdm@getchef.com>) +# +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef' +require 'chef/util/dsc/resource_info' +require 'spec_helper' + +describe Chef::Provider::DscScript do + + before(:each) do + @node = Chef::Node.new + @events = Chef::EventDispatch::Dispatcher.new + @run_context = Chef::RunContext.new(@node, {}, @events) + @resource = Chef::Resource::DscScript.new("script", @run_context) + @provider = Chef::Provider::DscScript.new(@resource, @run_context) + @provider.current_resource = @current_resource + end + + describe '#load_current_resource' do + it "describes the resource as converged if there were 0 DSC resources" do + @provider.stub(:run_configuration).with(:test).and_return([]) + @provider.load_current_resource + @provider.instance_variable_get('@resource_converged').should be_true + end + + it "describes the resource as not converged if there is 1 DSC resources that is converged" do + dsc_resource_info = Chef::Util::DSC::ResourceInfo.new('resource', false, ['nothing will change something']) + @provider.stub(:run_configuration).with(:test).and_return([dsc_resource_info]) + @provider.load_current_resource + @provider.instance_variable_get('@resource_converged').should be_true + end + + it "describes the resource as not converged if there is 1 DSC resources that is not converged" do + dsc_resource_info = Chef::Util::DSC::ResourceInfo.new('resource', true, ['will change something']) + @provider.stub(:run_configuration).with(:test).and_return([dsc_resource_info]) + @provider.load_current_resource + @provider.instance_variable_get('@resource_converged').should be_false + end + + it "describes the resource as not converged if there are any DSC resources that are not converged" do + dsc_resource_info1 = Chef::Util::DSC::ResourceInfo.new('resource', true, ['will change something']) + dsc_resource_info2 = Chef::Util::DSC::ResourceInfo.new('resource', false, ['nothing will change something']) + + @provider.stub(:run_configuration).with(:test).and_return([dsc_resource_info1, dsc_resource_info2]) + @provider.load_current_resource + @provider.instance_variable_get('@resource_converged').should be_false + end + + it "describes the resource as converged if all DSC resources that are converged" do + dsc_resource_info1 = Chef::Util::DSC::ResourceInfo.new('resource', false, ['nothing will change something']) + dsc_resource_info2 = Chef::Util::DSC::ResourceInfo.new('resource', false, ['nothing will change something']) + + @provider.stub(:run_configuration).with(:test).and_return([dsc_resource_info1, dsc_resource_info2]) + @provider.load_current_resource + @provider.instance_variable_get('@resource_converged').should be_true + end + end + + describe '#generate_configuration_document' do + # I think integration tests should cover these cases + + it 'uses configuration_document_from_script_path when a dsc script file is given' do + @provider.stub(:load_current_resource) + @resource.command("path_to_script") + generator = double('Chef::Util::DSC::ConfigurationGenerator') + generator.should_receive(:configuration_document_from_script_path) + Chef::Util::DSC::ConfigurationGenerator.stub(:new).and_return(generator) + @provider.send(:generate_configuration_document, 'tmp', nil) + end + + it 'uses configuration_document_from_script_code when a the dsc resource is given' do + @provider.stub(:load_current_resource) + @resource.code("ImADSCResource{}") + generator = double('Chef::Util::DSC::ConfigurationGenerator') + generator.should_receive(:configuration_document_from_script_code) + Chef::Util::DSC::ConfigurationGenerator.stub(:new).and_return(generator) + @provider.send(:generate_configuration_document, 'tmp', nil) + end + + it 'should noop if neither code or command are provided' do + @provider.stub(:load_current_resource) + generator = double('Chef::Util::DSC::ConfigurationGenerator') + generator.should_receive(:configuration_document_from_script_code).with('', anything(), anything()) + Chef::Util::DSC::ConfigurationGenerator.stub(:new).and_return(generator) + @provider.send(:generate_configuration_document, 'tmp', nil) + end + end + + describe 'action_run' do + it 'should converge the script if it is not converged' do + dsc_resource_info = Chef::Util::DSC::ResourceInfo.new('resource', true, ['will change something']) + @provider.stub(:run_configuration).with(:test).and_return([dsc_resource_info]) + @provider.stub(:run_configuration).with(:set) + + @provider.run_action(:run) + @resource.should be_updated + + end + + it 'should not converge if the script is already converged' do + @provider.stub(:run_configuration).with(:test).and_return([]) + + @provider.run_action(:run) + @resource.should_not be_updated + end + end +end + diff --git a/spec/unit/util/dsc/configuration_generator_spec.rb b/spec/unit/util/dsc/configuration_generator_spec.rb index 1d4ffd7572..1c6c57ac57 100644 --- a/spec/unit/util/dsc/configuration_generator_spec.rb +++ b/spec/unit/util/dsc/configuration_generator_spec.rb @@ -1,5 +1,5 @@ # -# Author:: Bryan McLellan <btm@loftninjas.org> +# Author:: Jay Mundrawala <jmundrawala@getchef.com> # Copyright:: Copyright (c) 2014 Chef Software, Inc. # License:: Apache License, Version 2.0 # @@ -163,7 +163,7 @@ describe Chef::Util::DSC::ConfigurationGenerator do dsc = @conf_man.send(:configuration_code, 'archive{}', 'hello') found_configuration = false dsc.split(';').each do |command| - if command.downcase =~ /\s*configuration\s+'hello'\s*\{\s*archive\s*\{\s*\}\s*\}\s*/ + if command.downcase =~ /\s*configuration\s+'hello'\s*\{\s*node\s+'localhost'\s*\{\s*archive\s*\{\s*\}\s*\}\s*\}\s*/ found_configuration = true end end |