diff options
| -rw-r--r-- | spec/lib/matchers/json.rb | 111 | ||||
| -rwxr-xr-x | spec/unit/indirector/request_spec.rb | 15 | ||||
| -rwxr-xr-x | spec/unit/node/facts_spec.rb | 13 | ||||
| -rwxr-xr-x | spec/unit/node_spec.rb | 21 |
4 files changed, 136 insertions, 24 deletions
diff --git a/spec/lib/matchers/json.rb b/spec/lib/matchers/json.rb new file mode 100644 index 000000000..798e4cd21 --- /dev/null +++ b/spec/lib/matchers/json.rb @@ -0,0 +1,111 @@ +RSpec::Matchers.define :set_json_attribute do |*attributes| + def format + @format ||= Puppet::Network::FormatHandler.format('pson') + end + + chain :to do |value| + @value = value + end + + def json(instance) + PSON.parse(instance.to_pson) + end + + def attr_value(attrs, instance) + attrs = attrs.dup + hash = json(instance)['data'] + while attrs.length > 0 + name = attrs.shift + hash = hash[name] + end + hash + end + + match do |instance| + result = attr_value(attributes, instance) + if @value + result == @value + else + ! result.nil? + end + end + + failure_message_for_should do |instance| + if @value + "expected #{instance.inspect} to set #{attributes.inspect} to #{@value.inspect}; got #{attr_value(attributes, instance).inspect}" + else + "expected #{instance.inspect} to set #{attributes.inspect} but was nil" + end + end + + failure_message_for_should_not do |instance| + if @value + "expected #{instance.inspect} not to set #{attributes.inspect} to #{@value.inspect}" + else + "expected #{instance.inspect} not to set #{attributes.inspect} to nil" + end + end +end + +RSpec::Matchers.define :set_json_document_type_to do |type| + def format + @format ||= Puppet::Network::FormatHandler.format('pson') + end + + match do |instance| + json(instance)['document_type'] == type + end + + def json(instance) + PSON.parse(instance.to_pson) + end + + failure_message_for_should do |instance| + "expected #{instance.inspect} to set document_type to #{type.inspect}; got #{json(instance)['document_type'].inspect}" + end + + failure_message_for_should_not do |instance| + "expected #{instance.inspect} not to set document_type to #{type.inspect}" + end +end + +RSpec::Matchers.define :read_json_attribute do |attribute| + def format + @format ||= Puppet::Network::FormatHandler.format('pson') + end + + chain :from do |value| + @json = value + end + + chain :as do |as| + @value = as + end + + match do |klass| + raise "Must specify json with 'from'" unless @json + + @instance = format.intern(klass, @json) + if @value + @instance.send(attribute) == @value + else + ! @instance.send(attribute).nil? + end + end + + failure_message_for_should do |klass| + if @value + "expected #{klass} to read #{attribute} from #{@json} as #{@value.inspect}; got #{@instance.send(attribute).inspect}" + else + "expected #{klass} to read #{attribute} from #{@json} but was nil" + end + end + + failure_message_for_should_not do |klass| + if @value + "expected #{klass} not to set #{attribute} to #{@value}" + else + "expected #{klass} not to set #{attribute} to nil" + end + end +end diff --git a/spec/unit/indirector/request_spec.rb b/spec/unit/indirector/request_spec.rb index 4dabb3147..ba7dc815e 100755 --- a/spec/unit/indirector/request_spec.rb +++ b/spec/unit/indirector/request_spec.rb @@ -1,5 +1,6 @@ #!/usr/bin/env rspec require 'spec_helper' +require 'matchers/json' require 'puppet/indirector/request' describe Puppet::Indirector::Request do @@ -307,28 +308,24 @@ describe Puppet::Indirector::Request do end it "should produce a hash with the document_type set to 'request'" do - PSON.parse(@request.to_pson)["document_type"].should == "Puppet::Indirector::Request" - end - - it "should add its data under the 'data' attribute in the hash" do - PSON.parse(@request.to_pson)["data"].should be_instance_of(Hash) + @request.should set_json_document_type_to("Puppet::Indirector::Request") end it "should set the 'key'" do - PSON.parse(@request.to_pson)["data"]['key'].should == "foo" + @request.should set_json_attribute("key").to("foo") end it "should include an attribute for its indirection name" do - PSON.parse(@request.to_pson)["data"]['type'].should == "facts" + @request.should set_json_attribute("type").to("facts") end it "should include a 'method' attribute set to its method" do - PSON.parse(@request.to_pson)["data"]['method'].should == "find" + @request.should set_json_attribute("method").to("find") end it "should add all attributes under the 'attributes' attribute" do @request.ip = "127.0.0.1" - PSON.parse(@request.to_pson)["data"]['attributes']['ip'].should == "127.0.0.1" + @request.should set_json_attribute("attributes", "ip").to("127.0.0.1") end it "should add all options under the 'attributes' attribute" do diff --git a/spec/unit/node/facts_spec.rb b/spec/unit/node/facts_spec.rb index 3c7c9d08b..0a5948cfa 100755 --- a/spec/unit/node/facts_spec.rb +++ b/spec/unit/node/facts_spec.rb @@ -1,6 +1,6 @@ #!/usr/bin/env rspec require 'spec_helper' - +require 'matchers/json' require 'puppet/node/facts' describe Puppet::Node::Facts, "when indirecting" do @@ -126,14 +126,17 @@ describe Puppet::Node::Facts, "when indirecting" do Time.stubs(:now).returns(@timestamp) facts = Puppet::Node::Facts.new("foo", {'a' => 1, 'b' => 2, 'c' => 3}) facts.expiration = @expiration - 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}} + facts.to_pson.should == %Q[{"data":{"name":"foo","timestamp":"Thu Oct 28 11:16:31 -0700 2010","expiration":"Thu Oct 28 11:21:31 -0700 2010","values":{"a":1,"b":2,"c":3}},"document_type":"Puppet::Node::Facts"}] 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") + + # XXX:LAK For some reason this is resurrection the full instance, instead + # of just returning the hash. This code works, but I can't figure out what's + # going on. + newfacts = PSON.parse(facts.to_pson) + newfacts.expiration.should be_nil end it "should be able to handle nil values" do diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index af3110f45..e8f826dca 100755 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -1,5 +1,6 @@ #!/usr/bin/env rspec require 'spec_helper' +require 'matchers/json' describe Puppet::Node do describe "when managing its environment" do @@ -42,30 +43,30 @@ describe Puppet::Node do end it "should provide its name" do - PSON.parse(@node.to_pson)['data']['name'].should == "mynode" + @node.should set_json_attribute('name').to("mynode") end it "should include the classes if set" do @node.classes = %w{a b c} - PSON.parse(@node.to_pson)['data']['classes'].should == %w{a b c} + @node.should set_json_attribute("classes").to(%w{a b c}) end it "should not include the classes if there are none" do - PSON.parse(@node.to_pson)['data'].should_not be_include('classes') + @node.should_not set_json_attribute('classes') end it "should include parameters if set" do @node.parameters = {"a" => "b", "c" => "d"} - PSON.parse(@node.to_pson)['data']['parameters'].should == {"a" => "b", "c" => "d"} + @node.should set_json_attribute('parameters').to({"a" => "b", "c" => "d"}) end it "should not include the parameters if there are none" do - PSON.parse(@node.to_pson)['data'].should_not be_include('parameters') + @node.should_not set_json_attribute('parameters') end it "should include the environment" do @node.environment = "production" - PSON.parse(@node.to_pson)['data']['environment'].should == "production" + @node.should set_json_attribute('environment').to('production') end end @@ -80,22 +81,22 @@ describe Puppet::Node do end it "should set its name" do - from_json(@node.to_pson).name.should == "mynode" + Puppet::Node.should read_json_attribute('name').from(@node.to_pson).as("mynode") end it "should include the classes if set" do @node.classes = %w{a b c} - from_json(@node.to_pson).classes.should == %w{a b c} + Puppet::Node.should read_json_attribute('classes').from(@node.to_pson).as(%w{a b c}) end it "should include parameters if set" do @node.parameters = {"a" => "b", "c" => "d"} - from_json(@node.to_pson).parameters.should == {"a" => "b", "c" => "d"} + Puppet::Node.should read_json_attribute('parameters').from(@node.to_pson).as({"a" => "b", "c" => "d"}) end it "should include the environment" do @node.environment = "production" - from_json(@node.to_pson).environment.name.should == :production + Puppet::Node.should read_json_attribute('environment').from(@node.to_pson).as(Puppet::Node::Environment.new(:production)) end end end |
