From 07a7a68a25eb9b21189751c27f90f972224ea533 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 12 Apr 2011 21:01:09 -0700 Subject: 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 Signed-off-by: Luke Kanies --- spec/unit/node/facts_spec.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'spec/unit/node') diff --git a/spec/unit/node/facts_spec.rb b/spec/unit/node/facts_spec.rb index a130ae3f8..3c7c9d08b 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 -- cgit