summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuke Kanies <luke@puppetlabs.com>2011-04-14 17:08:17 -0700
committerLuke Kanies <luke@puppetlabs.com>2011-04-14 17:08:17 -0700
commitd53c84de9985421e6c667f095a10574171ece159 (patch)
tree4b57f6e2852997faae127dd130c5f82f22c74c87 /spec
parente945cea00512d33c3d9d262b8285896febf946a3 (diff)
parente424740d78b8b72dc6bd7ebbbe27b237347d67f5 (diff)
downloadpuppet-d53c84de9985421e6c667f095a10574171ece159.tar.gz
puppet-d53c84de9985421e6c667f095a10574171ece159.tar.xz
puppet-d53c84de9985421e6c667f095a10574171ece159.zip
Merge branch 'tickets/next/7080-serializable_indirector_requests' into 2.7.x
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/matchers/json.rb111
-rwxr-xr-xspec/unit/indirector/request_spec.rb96
-rwxr-xr-xspec/unit/indirector/rest_spec.rb5
-rwxr-xr-xspec/unit/network/http/handler_spec.rb18
-rwxr-xr-xspec/unit/node/facts_spec.rb29
-rwxr-xr-xspec/unit/node_spec.rb64
-rwxr-xr-xspec/watchr.rb6
7 files changed, 313 insertions, 16 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 965d54188..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
@@ -300,4 +301,99 @@ describe Puppet::Indirector::Request do
lambda { @request.query_string }.should raise_error(ArgumentError)
end
end
+
+ describe "when converting to json" do
+ before do
+ @request = Puppet::Indirector::Request.new(:facts, :find, "foo")
+ end
+
+ it "should produce a hash with the document_type set to 'request'" do
+ @request.should set_json_document_type_to("Puppet::Indirector::Request")
+ end
+
+ it "should set the 'key'" do
+ @request.should set_json_attribute("key").to("foo")
+ end
+
+ it "should include an attribute for its indirection name" do
+ @request.should set_json_attribute("type").to("facts")
+ end
+
+ it "should include a 'method' attribute set to its method" do
+ @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"
+ @request.should set_json_attribute("attributes", "ip").to("127.0.0.1")
+ end
+
+ it "should add all options under the 'attributes' attribute" do
+ @request.options["opt"] = "value"
+ PSON.parse(@request.to_pson)["data"]['attributes']['opt'].should == "value"
+ end
+
+ it "should include the instance if provided" do
+ facts = Puppet::Node::Facts.new("foo")
+ @request.instance = facts
+ PSON.parse(@request.to_pson)["data"]['instance'].should be_instance_of(Puppet::Node::Facts)
+ end
+ end
+
+ describe "when converting from json" do
+ before do
+ @request = Puppet::Indirector::Request.new(:facts, :find, "foo")
+ @klass = Puppet::Indirector::Request
+ @format = Puppet::Network::FormatHandler.format('pson')
+ end
+
+ def from_json(json)
+ @format.intern(Puppet::Indirector::Request, json)
+ end
+
+ it "should set the 'key'" do
+ from_json(@request.to_pson).key.should == "foo"
+ end
+
+ it "should fail if no key is provided" do
+ json = PSON.parse(@request.to_pson)
+ json['data'].delete("key")
+ lambda { from_json(json.to_pson) }.should raise_error(ArgumentError)
+ end
+
+ it "should set its indirector name" do
+ from_json(@request.to_pson).indirection_name.should == :facts
+ end
+
+ it "should fail if no type is provided" do
+ json = PSON.parse(@request.to_pson)
+ json['data'].delete("type")
+ lambda { from_json(json.to_pson) }.should raise_error(ArgumentError)
+ end
+
+ it "should set its method" do
+ from_json(@request.to_pson).method.should == "find"
+ end
+
+ it "should fail if no method is provided" do
+ json = PSON.parse(@request.to_pson)
+ json['data'].delete("method")
+ lambda { from_json(json.to_pson) }.should raise_error(ArgumentError)
+ end
+
+ it "should initialize with all attributes and options" do
+ @request.ip = "127.0.0.1"
+ @request.options["opt"] = "value"
+ result = from_json(@request.to_pson)
+ result.options[:opt].should == "value"
+ result.ip.should == "127.0.0.1"
+ end
+
+ it "should set its instance as an instance if one is provided" do
+ facts = Puppet::Node::Facts.new("foo")
+ @request.instance = facts
+ result = from_json(@request.to_pson)
+ result.instance.should be_instance_of(Puppet::Node::Facts)
+ end
+ end
end
diff --git a/spec/unit/indirector/rest_spec.rb b/spec/unit/indirector/rest_spec.rb
index 513eb8352..5637080e8 100755
--- a/spec/unit/indirector/rest_spec.rb
+++ b/spec/unit/indirector/rest_spec.rb
@@ -233,6 +233,11 @@ describe Puppet::Indirector::REST do
params[s] = 'foo'
end
+ # The request special-cases this parameter, and it
+ # won't be passed on to the server, so we remove it here
+ # to avoid a failure.
+ params.delete('ip')
+
@request = Puppet::Indirector::Request.new(:foo, :find, "foo bar", params.merge(:environment => "myenv"))
@connection.expects(:post).with do |uri, body|
diff --git a/spec/unit/network/http/handler_spec.rb b/spec/unit/network/http/handler_spec.rb
index 83969c504..c709d82fe 100755
--- a/spec/unit/network/http/handler_spec.rb
+++ b/spec/unit/network/http/handler_spec.rb
@@ -240,34 +240,30 @@ describe Puppet::Network::HTTP::Handler do
describe "when performing head operation" do
before do
- @irequest = stub 'indirection_request', :method => :head, :indirection_name => "my_handler", :to_hash => {}, :key => "my_result", :model => @model_class
+ @handler.stubs(:model).with("my_handler").returns(stub 'model', :indirection => @model_class)
+ @handler.stubs(:http_method).with(@request).returns("HEAD")
+ @handler.stubs(:path).with(@request).returns("/production/my_handler/my_result")
+ @handler.stubs(:params).with(@request).returns({})
@model_class.stubs(:head).returns true
end
- it "should use the indirection request to find the model class" do
- @irequest.expects(:model).returns @model_class
-
- @handler.do_head(@irequest, @request, @response)
- end
-
it "should use the escaped request key" do
@model_class.expects(:head).with do |key, args|
key == "my_result"
end.returns true
- @handler.do_head(@irequest, @request, @response)
+ @handler.process(@request, @response)
end
it "should not generate a response when a model head call succeeds" do
@handler.expects(:set_response).never
- @handler.do_head(@irequest, @request, @response)
+ @handler.process(@request, @response)
end
it "should return a 404 when the model head call returns false" do
- @model_class.stubs(:name).returns "my name"
@handler.expects(:set_response).with { |response, body, status| status == 404 }
@model_class.stubs(:head).returns(false)
- @handler.do_head(@irequest, @request, @response)
+ @handler.process(@request, @response)
end
end
diff --git a/spec/unit/node/facts_spec.rb b/spec/unit/node/facts_spec.rb
index a130ae3f8..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
@@ -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'
@@ -122,8 +126,25 @@ 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})
+
+ # 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
+ 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
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb
index 7d813ba30..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
@@ -35,6 +36,69 @@ describe Puppet::Node do
node.environment.name.should == :bar
end
end
+
+ describe "when converting to json" do
+ before do
+ @node = Puppet::Node.new("mynode")
+ end
+
+ it "should provide its name" do
+ @node.should set_json_attribute('name').to("mynode")
+ end
+
+ it "should include the classes if set" do
+ @node.classes = %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
+ @node.should_not set_json_attribute('classes')
+ end
+
+ it "should include parameters if set" do
+ @node.parameters = {"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
+ @node.should_not set_json_attribute('parameters')
+ end
+
+ it "should include the environment" do
+ @node.environment = "production"
+ @node.should set_json_attribute('environment').to('production')
+ end
+ end
+
+ describe "when converting from json" do
+ before do
+ @node = Puppet::Node.new("mynode")
+ @format = Puppet::Network::FormatHandler.format('pson')
+ end
+
+ def from_json(json)
+ @format.intern(Puppet::Node, json)
+ end
+
+ it "should set its name" do
+ 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}
+ 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"}
+ 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"
+ Puppet::Node.should read_json_attribute('environment').from(@node.to_pson).as(Puppet::Node::Environment.new(:production))
+ end
+ end
end
describe Puppet::Node, "when initializing" do
diff --git a/spec/watchr.rb b/spec/watchr.rb
index 26919d1a1..c0f1d0257 100755
--- a/spec/watchr.rb
+++ b/spec/watchr.rb
@@ -85,7 +85,11 @@ def run_spec_files(files)
else
opts = File.readlines('spec/spec.opts').collect { |l| l.chomp }.join(" ")
end
- run_spec("rspec #{opts} --tty #{files.join(' ')}")
+ begin
+ run_spec("rspec #{opts} --tty #{files.join(' ')}")
+ rescue => detail
+ puts "Failed to load #{files}: #{detail}"
+ end
end
def run_all_tests