[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 0.25.5-639-g8f94f35

test branch puppet-dev at googlegroups.com
Wed Jul 14 10:34:05 UTC 2010


The following commit has been merged in the upstream branch:
commit 2b5bd4927569c0a87ea32628cdc4303ff1e83853
Author: Luke Kanies <luke at puppetlabs.com>
Date:   Thu May 20 17:30:59 2010 -0700

    Fixing #3822 - checksums will be loaded from filebuckets
    
    If you have the following code or equivalent:
    
        file { "/foo": content => "{md5}foobar" }
    
    Puppet will attempt to pull the content associated with
    that file from whatever the default filebucket is for the
    resource in question.
    
    Signed-off-by: Luke Kanies <luke at puppetlabs.com>

diff --git a/lib/puppet/type/file/content.rb b/lib/puppet/type/file/content.rb
index ff139f2..7d54164 100755
--- a/lib/puppet/type/file/content.rb
+++ b/lib/puppet/type/file/content.rb
@@ -138,7 +138,6 @@ module Puppet
         end
 
         def write(file)
-            self.fail "Writing content that wasn't provided" unless actual_content || resource.parameter(:source)
             resource.parameter(:checksum).sum_stream { |sum|
                 each_chunk_from(actual_content || resource.parameter(:source)) { |chunk|
                     sum << chunk
@@ -151,27 +150,46 @@ module Puppet
             if source_or_content.is_a?(String)
                 yield source_or_content
             elsif source_or_content.nil?
-                nil
+                yield read_file_from_filebucket
             elsif source_or_content.local?
-                File.open(source_or_content.full_path, "r") do |src|
-                    while chunk = src.read(8192)
-                        yield chunk
-                    end
-                end
+                chunk_file_from_disk(source_or_content) { |chunk| yield chunk }
             else
-                request = Puppet::Indirector::Request.new(:file_content, :find, source_or_content.full_path)
-                connection = Puppet::Network::HttpPool.http_instance(source_or_content.server, source_or_content.port)
-                connection.request_get(indirection2uri(request), add_accept_encoding({"Accept" => "raw"})) do |response|
-                    case response.code
-                    when "404"; nil
-                    when /^2/;  uncompress(response) { |uncompressor| response.read_body { |chunk| yield uncompressor.uncompress(chunk) } }
-                    else
-                        # Raise the http error if we didn't get a 'success' of some kind.
-                        message = "Error %s on SERVER: %s" % [response.code, (response.body||'').empty? ? response.message : uncompress_body(response)]
-                        raise Net::HTTPError.new(message, response)
-                    end
+                chunk_file_from_source(source_or_content) { |chunk| yield chunk }
+            end
+        end
+
+        private
+
+        def chunk_file_from_disk(source_or_content)
+            File.open(source_or_content.full_path, "r") do |src|
+                while chunk = src.read(8192)
+                    yield chunk
+                end
+            end
+        end
+
+        def chunk_file_from_source(source_or_content)
+            request = Puppet::Indirector::Request.new(:file_content, :find, source_or_content.full_path)
+            connection = Puppet::Network::HttpPool.http_instance(source_or_content.server, source_or_content.port)
+            connection.request_get(indirection2uri(request), add_accept_encoding({"Accept" => "raw"})) do |response|
+                case response.code
+                when "404"; nil
+                when /^2/;  uncompress(response) { |uncompressor| response.read_body { |chunk| yield uncompressor.uncompress(chunk) } }
+                else
+                    # Raise the http error if we didn't get a 'success' of some kind.
+                    message = "Error %s on SERVER: %s" % [response.code, (response.body||'').empty? ? response.message : uncompress_body(response)]
+                    raise Net::HTTPError.new(message, response)
                 end
             end
         end
+
+        def read_file_from_filebucket
+            raise "Could not get filebucket from file" unless dipper = resource.bucket
+            sum = should.sub(/\{\w+\}/, '')
+
+            dipper.getfile(sum)
+        rescue => detail
+            fail "Could not retrieve content for #{should} from filebucket: #{detail}"
+        end
     end
 end
diff --git a/spec/unit/type/file/content.rb b/spec/unit/type/file/content.rb
index 2289215..f1001aa 100755
--- a/spec/unit/type/file/content.rb
+++ b/spec/unit/type/file/content.rb
@@ -13,10 +13,6 @@ describe content do
     end
 
     describe "when determining the checksum type" do
-        before do
-            @resource = Puppet::Type.type(:file).new :path => "/foo/bar"
-        end
-
         it "should use the type specified in the source checksum if a source is set" do
             @resource[:source] = "/foo"
             @resource.parameter(:source).expects(:checksum).returns "{md5lite}eh"
@@ -34,10 +30,6 @@ describe content do
     end
 
     describe "when determining the actual content to write" do
-        before do
-            @resource = Puppet::Type.type(:file).new :path => "/foo/bar"
-        end
-
         it "should use the set content if available" do
             @content = content.new(:resource => @resource)
             @content.should = "ehness"
@@ -254,8 +246,10 @@ describe content do
             @fh = stub_everything
         end
 
-        it "should fail if no actual content nor source exists" do
-            lambda { @content.write(@fh) }.should raise_error
+        it "should attempt to read from the filebucket if no actual content nor source exists" do
+            @content.should = "{md5}foo"
+            @content.resource.bucket.class.any_instance.stubs(:getfile).returns "foo"
+            @content.write(@fh)
         end
 
         describe "from actual content" do
@@ -274,6 +268,32 @@ describe content do
             end
         end
 
+        describe "from a file bucket" do
+            it "should fail if a file bucket cannot be retrieved" do
+                @content.should = "{md5}foo"
+                @content.resource.expects(:bucket).returns nil
+                lambda { @content.write(@fh) }.should raise_error(Puppet::Error)
+            end
+
+            it "should fail if the file bucket cannot find any content" do
+                @content.should = "{md5}foo"
+                bucket = stub 'bucket'
+                @content.resource.expects(:bucket).returns bucket
+                bucket.expects(:getfile).with("foo").raises "foobar"
+                lambda { @content.write(@fh) }.should raise_error(Puppet::Error)
+            end
+
+            it "should write the returned content to the file" do
+                @content.should = "{md5}foo"
+                bucket = stub 'bucket'
+                @content.resource.expects(:bucket).returns bucket
+                bucket.expects(:getfile).with("foo").returns "mycontent"
+
+                @fh.expects(:print).with("mycontent")
+                @content.write(@fh)
+            end
+        end
+
         describe "from local source" do
             before(:each) do
                 @content.stubs(:actual_content).returns(nil)
@@ -432,5 +452,8 @@ describe content do
                 @content.write(@fh)
             end
         end
+
+        describe "from a filebucket" do
+        end
     end
 end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list