diff options
author | Jesse Wolfe <jes5199@gmail.com> | 2011-01-10 18:30:42 -0800 |
---|---|---|
committer | Paul Berry <paul@puppetlabs.com> | 2011-01-11 11:41:46 -0800 |
commit | a002231f45339da9b152162c2f48126d95e2e246 (patch) | |
tree | cf51e75f954847fda264b561a7a3acdfa5869dda | |
parent | e6e88e7df7fa1cbec1400d73cb7ba9428c420be0 (diff) | |
download | puppet-a002231f45339da9b152162c2f48126d95e2e246.tar.gz puppet-a002231f45339da9b152162c2f48126d95e2e246.tar.xz puppet-a002231f45339da9b152162c2f48126d95e2e246.zip |
(#5171) Made filebucket able to perform diffs
It is now possible to ask the filebucket to diff two files using a URL
of the form:
https://puppet/production/file_bucket_file/md5/{first file hash}?diff_with={second file hash}
The returned diff is a string, the output of the "diff" command.
Paired-with: Paul Berry <paul@puppetlabs.com>
-rw-r--r-- | lib/puppet/indirector/file_bucket_file/file.rb | 11 | ||||
-rw-r--r-- | lib/puppet/indirector/indirection.rb | 2 | ||||
-rw-r--r-- | lib/puppet/network/http/handler.rb | 6 | ||||
-rwxr-xr-x | spec/unit/indirector/file_bucket_file/file_spec.rb | 45 | ||||
-rwxr-xr-x | spec/unit/network/http/handler_spec.rb | 6 |
5 files changed, 67 insertions, 3 deletions
diff --git a/lib/puppet/indirector/file_bucket_file/file.rb b/lib/puppet/indirector/file_bucket_file/file.rb index 318858aaf..9d9cee793 100644 --- a/lib/puppet/indirector/file_bucket_file/file.rb +++ b/lib/puppet/indirector/file_bucket_file/file.rb @@ -15,7 +15,16 @@ module Puppet::FileBucketFile def find( request ) checksum, path = request_to_checksum_and_path( request ) - find_by_checksum( checksum, request.options ) + file = find_by_checksum( checksum, request.options ) + + if file && request.options[:diff_with] + hash_protocol = sumtype(checksum) + file2 = find_by_checksum( "{#{hash_protocol}}#{request.options[:diff_with]}", request.options ) + raise "could not find diff_with #{request.options[:diff_with]}" unless file2 + return `diff #{path_for(file).inspect}/contents #{path_for(file2).inspect}/contents` + end + + file end def save( request ) diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 309eed7b6..a010c4e40 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -191,7 +191,7 @@ class Puppet::Indirector::Indirection # Otherwise, return the result from the terminus, caching if appropriate. if ! request.ignore_terminus? and result = terminus.find(request) - result.expiration ||= self.expiration + result.expiration ||= self.expiration if result.respond_to?(:expiration) if cache? and request.use_cache? Puppet.info "Caching #{self.name} for #{request.key}" cache.save request(:save, result, *args) diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 61ae2d2fc..f22498b70 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -109,7 +109,11 @@ module Puppet::Network::HTTP::Handler format = format_to_use(request) set_content_type(response, format) - set_response(response, result.render(format)) + if result.respond_to?(:render) + set_response(response, result.render(format)) + else + set_response(response, result) + end end # Execute our search. diff --git a/spec/unit/indirector/file_bucket_file/file_spec.rb b/spec/unit/indirector/file_bucket_file/file_spec.rb index aa3ade6b6..7bf02b5b3 100755 --- a/spec/unit/indirector/file_bucket_file/file_spec.rb +++ b/spec/unit/indirector/file_bucket_file/file_spec.rb @@ -13,6 +13,51 @@ describe Puppet::FileBucketFile::File do Puppet::FileBucketFile::File.doc.should be_instance_of(String) end + describe "non-stubbing tests" do + include PuppetSpec::Files + + before do + Puppet[:bucketdir] = tmpdir('bucketdir') + end + + describe "when diffing files" do + def save_bucket_file(contents) + bucket_file = Puppet::FileBucket::File.new(contents) + bucket_file.save + bucket_file.checksum_data + end + + it "should generate an empty string if there is no diff" do + checksum = save_bucket_file("I'm the contents of a file") + Puppet::FileBucket::File.find("md5/#{checksum}", :diff_with => checksum).should == '' + end + + it "should generate a proper diff if there is a diff" do + checksum1 = save_bucket_file("foo\nbar\nbaz") + checksum2 = save_bucket_file("foo\nbiz\nbaz") + diff = Puppet::FileBucket::File.find("md5/#{checksum1}", :diff_with => checksum2) + diff.should == <<HERE +2c2 +< bar +--- +> biz +HERE + end + + it "should raise an exception if the hash to diff against isn't found" do + checksum = save_bucket_file("whatever") + bogus_checksum = "d1bf072d0e2c6e20e3fbd23f022089a1" + lambda { Puppet::FileBucket::File.find("md5/#{checksum}", :diff_with => bogus_checksum) }.should raise_error "could not find diff_with #{bogus_checksum}" + end + + it "should return nil if the hash to diff from isn't found" do + checksum = save_bucket_file("whatever") + bogus_checksum = "d1bf072d0e2c6e20e3fbd23f022089a1" + Puppet::FileBucket::File.find("md5/#{bogus_checksum}", :diff_with => checksum).should == nil + end + end + end + describe "when initializing" do it "should use the filebucket settings section" do Puppet.settings.expects(:use).with(:filebucket) diff --git a/spec/unit/network/http/handler_spec.rb b/spec/unit/network/http/handler_spec.rb index 76a9c5530..cdbce41f7 100755 --- a/spec/unit/network/http/handler_spec.rb +++ b/spec/unit/network/http/handler_spec.rb @@ -209,6 +209,12 @@ describe Puppet::Network::HTTP::Handler do @handler.do_find(@irequest, @request, @response) end + it "should pass the result through without rendering it if the result is a string" do + @model_class.stubs(:find).returns "foo" + @handler.expects(:set_response).with(@response, "foo") + @handler.do_find(@irequest, @request, @response) + end + it "should use the default status when a model find call succeeds" do @handler.expects(:set_response).with { |response, body, status| status.nil? } @handler.do_find(@irequest, @request, @response) |