[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Luke Kanies luke at puppetlabs.com
Tue May 10 08:15:11 UTC 2011


The following commit has been merged in the experimental branch:
commit 07a7a68a25eb9b21189751c27f90f972224ea533
Author: Luke Kanies <luke at puppetlabs.com>
Date:   Tue Apr 12 21:01:09 2011 -0700

    Fixing Facts pson methods more resilient
    
    They were currently failing if any values were nil,
    which happened a lot.
    
    We also prefer not to include nil values, since it muddies
    the json unnecessarily.
    
    Reviewed-by: Daniel Pittman <daniel at puppetlabs.com>
    Signed-off-by: Luke Kanies <luke at puppetlabs.com>

diff --git a/lib/puppet/node/facts.rb b/lib/puppet/node/facts.rb
index 577b62b..2ff7156 100755
--- a/lib/puppet/node/facts.rb
+++ b/lib/puppet/node/facts.rb
@@ -61,18 +61,22 @@ class Puppet::Node::Facts
 
   def self.from_pson(data)
     result = new(data['name'], data['values'])
-    result.timestamp = Time.parse(data['timestamp'])
-    result.expiration = Time.parse(data['expiration'])
+    result.timestamp = Time.parse(data['timestamp']) if data['timestamp']
+    result.expiration = Time.parse(data['expiration']) if data['expiration']
     result
   end
 
   def to_pson(*args)
-    {
-      'expiration' => expiration,
-      'name' => name,
-      'timestamp' => timestamp,
-      'values' => strip_internal,
-    }.to_pson(*args)
+    result = {
+      'document_type' => "Puppet::Node::Facts",
+      'data' => {}
+    }
+
+    result['data']['name'] = name
+    result['data']['expiration'] = expiration if expiration
+    result['data']['timestamp'] = timestamp if timestamp
+    result['data']['values'] = strip_internal
+    result.to_pson(*args)
   end
 
   # Add internal data to the facts for storage.
diff --git a/spec/unit/node/facts_spec.rb b/spec/unit/node/facts_spec.rb
index a130ae3..3c7c9d0 100755
--- a/spec/unit/node/facts_spec.rb
+++ b/spec/unit/node/facts_spec.rb
@@ -110,7 +110,11 @@ describe Puppet::Node::Facts, "when indirecting" do
       end
 
       it "should accept properly formatted pson" do
-        pson = %Q({"name": "foo", "expiration": "#{@expiration}", "timestamp": "#{@timestamp}", "values": {"a": "1", "b": "2", "c": "3"}})
+        facts = Puppet::Node::Facts.new("foo")
+        facts.values = {"a" => "1", "b" => "2", "c" => "3"}
+        facts.expiration = Time.now
+        #pson = %Q({"document_type": "Puppet::Node::Facts", "data: {"name": "foo", "expiration": "#{@expiration}", "timestamp": "#{@timestamp}", "values": {"a": "1", "b": "2", "c": "3"}}})
+        pson = %Q({"data": {"name":"foo", "expiration":"#{@expiration}", "timestamp": "#{@timestamp}", "values":{"a":"1","b":"2","c":"3"}}, "document_type":"Puppet::Node::Facts"})
         format = Puppet::Network::FormatHandler.format('pson')
         facts = format.intern(Puppet::Node::Facts,pson)
         facts.name.should == 'foo'
@@ -125,6 +129,20 @@ describe Puppet::Node::Facts, "when indirecting" do
         pson = PSON.parse(facts.to_pson)
         pson.should == {"name"=>"foo", "timestamp"=>@timestamp.to_s, "expiration"=>@expiration.to_s, "values"=>{"a"=>1, "b"=>2, "c"=>3}}
       end
+
+      it "should not include nil values" do
+        facts = Puppet::Node::Facts.new("foo", {'a' => 1, 'b' => 2, 'c' => 3})
+        pson = PSON.parse(facts.to_pson)
+        pson.should_not be_include("expiration")
+      end
+
+      it "should be able to handle nil values" do
+        pson = %Q({"name": "foo", "values": {"a": "1", "b": "2", "c": "3"}})
+        format = Puppet::Network::FormatHandler.format('pson')
+        facts = format.intern(Puppet::Node::Facts,pson)
+        facts.name.should == 'foo'
+        facts.expiration.should be_nil
+      end
     end
   end
 end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list