diff options
author | Steven Danna <steve@opscode.com> | 2014-04-06 15:05:28 -0700 |
---|---|---|
committer | Bryan McLellan <btm@getchef.com> | 2014-05-02 12:19:40 -0700 |
commit | aa2b95e5515ad6d4855d0de6c72604f5d0ae04d0 (patch) | |
tree | d03e0ed38850f1e63977fb2b98150fcc26741714 | |
parent | 28f30348abf715c4d41ef3def5218c7e42d299ba (diff) | |
download | chef-aa2b95e5515ad6d4855d0de6c72604f5d0ae04d0.tar.gz |
[CHEF-5162] Support DelayedEvaluator for remote_file's source attribute
This commit adds the ability to pass a single DelayedEvaluator to the
source attribute of the remote_file resource. If more than one
DelayedEvalutor is passed, an error will be raised.
The source attribute now accepts:
- A single string argument,
- Multiple string arguments
- A single array argument
- A single delayed evaluator argument
Technically, it also accepts
- An array of arrays
but this case is not tested.
-rw-r--r-- | lib/chef/resource/remote_file.rb | 26 | ||||
-rw-r--r-- | spec/unit/resource/remote_file_spec.rb | 29 |
2 files changed, 49 insertions, 6 deletions
diff --git a/lib/chef/resource/remote_file.rb b/lib/chef/resource/remote_file.rb index 24d2562a9b..a10a0996a6 100644 --- a/lib/chef/resource/remote_file.rb +++ b/lib/chef/resource/remote_file.rb @@ -41,12 +41,24 @@ class Chef end def source(*args) - if not args.empty? - args = Array(args).flatten - validate_source(args) - @source = args - elsif self.instance_variable_defined?(:@source) == true - @source + arg = if args.empty? + nil + elsif args[0].is_a?(Chef::DelayedEvaluator) && args.count == 1 + args[0] + elsif args.any? {|a| a.is_a?(Chef::DelayedEvaluator)} && args.count > 1 + raise Exceptions::InvalidRemoteFileURI, "Only 1 DelayedEvaluator is allowed" + else + Array(args).flatten + end + ret = set_or_return(:source, + arg, + { :callbacks => { + :validate_source => method(:validate_source) + }}) + if ret.is_a? String + Array(ret) + else + ret end end @@ -107,6 +119,7 @@ class Chef private def validate_source(source) + source = Array(source).flatten raise ArgumentError, "#{resource_name} has an empty source" if source.empty? source.each do |src| unless absolute_uri?(src) @@ -114,6 +127,7 @@ class Chef "#{src.inspect} is not a valid `source` parameter for #{resource_name}. `source` must be an absolute URI or an array of URIs." end end + true end def absolute_uri?(source) diff --git a/spec/unit/resource/remote_file_spec.rb b/spec/unit/resource/remote_file_spec.rb index 643bc8ba21..8f1633119d 100644 --- a/spec/unit/resource/remote_file_spec.rb +++ b/spec/unit/resource/remote_file_spec.rb @@ -50,20 +50,49 @@ describe Chef::Resource::RemoteFile do @resource.source.should eql([ "http://opscode.com/" ]) end + it "should accept a delayed evalutator (string) for the remote file source" do + @resource.source Chef::DelayedEvaluator.new {"http://opscode.com/"} + @resource.source.should eql([ "http://opscode.com/" ]) + end + it "should accept an array of URIs for the remote file source" do @resource.source([ "http://opscode.com/", "http://puppetlabs.com/" ]) @resource.source.should eql([ "http://opscode.com/", "http://puppetlabs.com/" ]) end + it "should accept a delated evaluator (array) for the remote file source" do + @resource.source Chef::DelayedEvaluator.new { [ "http://opscode.com/", "http://puppetlabs.com/" ] } + @resource.source.should eql([ "http://opscode.com/", "http://puppetlabs.com/" ]) + end + it "should accept an multiple URIs as arguments for the remote file source" do @resource.source("http://opscode.com/", "http://puppetlabs.com/") @resource.source.should eql([ "http://opscode.com/", "http://puppetlabs.com/" ]) end + it "should only accept a single argument if a delayed evalutor is used" do + lambda { + @resource.source("http://opscode.com/", Chef::DelayedEvaluator.new {"http://opscode.com/"}) + }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) + end + + it "should only accept a single array item if a delayed evalutor is used" do + lambda { + @resource.source(["http://opscode.com/", Chef::DelayedEvaluator.new {"http://opscode.com/"}]) + }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) + end + it "does not accept a non-URI as the source" do lambda { @resource.source("not-a-uri") }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) end + it "does not accept a non-URI as the source when read from a delayed evaluator" do + lambda { + @resource.source(Chef::DelayedEvaluator.new {"not-a-uri"}) + @resource.source + }.should raise_error(Chef::Exceptions::InvalidRemoteFileURI) + end + it "should raise an exception when source is an empty array" do lambda { @resource.source([]) }.should raise_error(ArgumentError) end |