[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:45 UTC 2010


The following commit has been merged in the upstream branch:
commit 1c5b67d3156873cf3f97aca4d8ca3c6707fc089f
Author: Rein Henrichs <rein at puppetlabs.com>
Date:   Tue Jun 22 15:46:56 2010 -0700

    [#4055] Refactor of abstract Couch terminus, more specs
    
    * Cleaner implementation of abstract Couch terminus
    * More thoroughly tested facts Couch terminus

diff --git a/lib/puppet/indirector/couch.rb b/lib/puppet/indirector/couch.rb
index b328f5c..dc3e495 100644
--- a/lib/puppet/indirector/couch.rb
+++ b/lib/puppet/indirector/couch.rb
@@ -4,37 +4,43 @@ class Puppet::Indirector::Couch < Puppet::Indirector::Terminus
     # The CouchRest database instance. One database instance per Puppet runtime
     # should be sufficient.
     #
-    def self.db
-        @db ||= CouchRest.database! Puppet[:couchdb_url]
-    end
-
+    def self.db; @db ||= CouchRest.database! Puppet[:couchdb_url] end
     def db; self.class.db end
 
     def find(request)
-        attributes_of db.get(id_for(request))
-    rescue RestClient::ResourceNotFound
-        Puppet.debug "No couchdb document with id: #{id_for(request)}"
-        return nil
+        attributes_of get(request)
     end
 
     # Create or update the couchdb document with the request's data hash.
     #
+    def save(request)
+        raise ArgumentError, "PUT does not accept options" unless request.options.empty?
+        update(request) || create(request)
+    end
+
+    private
+
     # RKH:TODO: Do not depend on error handling, check if the document exists
     # first. (Does couchrest support this?)
     #
-    def save(request)
-        raise ArgumentError, "PUT does not accept options" unless request.options.empty?
+    def get(request)
+        db.get(id_for(request))
+    rescue RestClient::ResourceNotFound
+        Puppet.debug "No couchdb document with id: #{id_for(request)}"
+        return nil
+    end
 
-        # Try to find an existing document.
-        doc = db.get(id_for(request))
-        doc.merge(hash_from(request))
+    def update(request)
+        doc = get request
+        return unless doc
+        doc.merge!(hash_from(request))
         doc.save
-    rescue RestClient::ResourceNotFound
-        # Document does not yet exist, create it
-        db.save_doc hash_from(request)
+        return true
     end
 
-    private
+    def create(request)
+        db.save_doc hash_from(request)
+    end
 
     # The attributes hash that is serialized to CouchDB as JSON. It includes
     # metadata that is used to help aggregate data in couchdb. Add
@@ -51,7 +57,7 @@ class Puppet::Indirector::Couch < Puppet::Indirector::Terminus
     # instance that is returned by save.
     #
     def attributes_of(response)
-        response.reject{|k,v| k =~ /^(_rev|puppet_)/ }
+        response && response.reject{|k,v| k =~ /^(_rev|puppet_)/ }
     end
 
     def document_type_for(request)
diff --git a/spec/unit/indirector/facts/couch.rb b/spec/unit/indirector/facts/couch.rb
index acc28b9..409cb50 100644
--- a/spec/unit/indirector/facts/couch.rb
+++ b/spec/unit/indirector/facts/couch.rb
@@ -10,29 +10,85 @@ describe Puppet::Node::Facts::Couch do
         @mock_db = mock('couch db')
         mock_document = CouchRest::Document.new(:_id => fake_request.key, :facts => fake_request.values)
         mock_document.stubs(:database).returns(@mock_db)
-        @mock_db.stubs(:get).with('test.local').returns(mock_document)
+        @mock_db.stubs(:get).with(fake_request.key).returns(mock_document)
         Puppet::Node::Facts::Couch.stubs(:db).returns(@mock_db)
     end
 
     subject { Puppet::Node::Facts::Couch }
 
     describe "#find" do
-        it "should find the request by key" do
-            @mock_db.expects(:get).with(fake_request.key).returns({'_id' => fake_request.key, 'facts' => fake_request.instance.values})
-            subject.new.find(fake_request).should == fake_request.instance
+        describe "when the node document exists" do
+            it "should find the request by key" do
+                @mock_db.expects(:get).with(fake_request.key).returns({'_id' => fake_request.key, 'facts' => fake_request.instance.values})
+                subject.new.find(fake_request).should == fake_request.instance
+            end
+        end
+
+        describe "when the node document does not exist" do
+            before do
+                @mock_db.expects(:get).
+                    with(fake_request.key).
+                    raises(RestClient::ResourceNotFound)
+            end
+
+            it "should return nil" do
+                subject.new.find(fake_request).should be_nil
+            end
+
+            it "should send Puppet a debug message" do
+                Puppet.expects(:debug).with("No couchdb document with id: test.local")
+                subject.new.find(fake_request).should be_nil
+            end
+
         end
     end
 
     describe "#save" do
+        describe "with options" do
+            subject do
+                lambda { Puppet::Node::Facts::Couch.new.save(fake_request([1])) }
+            end
+
+            it { should raise_error(ArgumentError, "PUT does not accept options") }
+        end
+
         it "should save the json to the CouchDB database" do
             @mock_db.expects(:save_doc).at_least_once.returns({'ok' => true })
             subject.new.save(fake_request)
         end
+
+        describe "when the document exists" do
+            before do
+                @doc = CouchRest::Document.new(:_id => fake_request.key, :facts => fake_request.instance.values)
+                @mock_db.expects(:get).with(fake_request.key).returns(@doc)
+            end
+
+            it "saves the document" do
+                @doc.expects(:save)
+                subject.new.save(fake_request)
+            end
+
+        end
+
+        describe "when the document does not exist" do
+            before do
+                @mock_db.expects(:get).
+                    with(fake_request.key).
+                    raises(RestClient::ResourceNotFound)
+            end
+
+            it "saves the document" do
+                @mock_db.expects(:save_doc)
+                subject.new.save(fake_request)
+            end
+
+        end
+
     end
 
-    def fake_request
+    def fake_request(options={})
         facts = YAML.load_file(File.join(PuppetSpec::FIXTURE_DIR, 'yaml', 'test.local.yaml'))
-        Struct.new(:instance, :key, :options).new(facts, facts.name, {})
+        Struct.new(:instance, :key, :options).new(facts, facts.name, options)
     end
     private :fake_request
 

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list