From e8caf135a4a378d54bf62f8c37064ee3ccc508e9 Mon Sep 17 00:00:00 2001 From: Rick Bradley Date: Wed, 2 Apr 2008 00:06:30 -0500 Subject: making search work over REST, w/ unit & integration specs --- lib/puppet/indirector/rest.rb | 4 +-- lib/puppet/network/http/handler.rb | 2 +- spec/integration/indirector/rest.rb | 63 ++++++++++++++++++++++++++++++++++--- spec/unit/indirector/rest.rb | 2 +- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index 690c79632..0c86b2706 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -17,8 +17,6 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus def search(key, options = {}) network_results = network_fetch("#{indirection.name}s/#{key}") raise YAML.load(network_results) if network_results =~ %r{--- !ruby/exception} - decoded_results = network_results.collect do |result| - indirection.model.from_yaml(result) - end + decoded_results = YAML.load(network_results.to_s).collect {|result| indirection.model.from_yaml(result) } end end diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index f226ae133..9e6c28512 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -32,7 +32,7 @@ module Puppet::Network::HTTP::Handler def do_search(request, response) args = params(request) - result = model.search(args).collect {|obj| obj.to_yaml } + result = model.search(args).collect {|result| result.to_yaml }.to_yaml encode_result(request, response, result) end diff --git a/spec/integration/indirector/rest.rb b/spec/integration/indirector/rest.rb index f8084e39a..b9e9513c4 100644 --- a/spec/integration/indirector/rest.rb +++ b/spec/integration/indirector/rest.rb @@ -166,15 +166,70 @@ describe Puppet::Indirector::REST do end end - describe "when saving a model instance over REST" do - it "needs more specs" + describe "when searching for model instances over REST" do + describe "when matching model instances can be found" do + before :each do + @model_instances = [ Puppet::TestIndirectedFoo.new(23), Puppet::TestIndirectedFoo.new(24) ] + @mock_model = stub('faked model', :search => @model_instances) + Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:model).returns(@mock_model) + end + + it "should not fail" do + lambda { Puppet::TestIndirectedFoo.search('bar') }.should_not raise_error + end + + it 'should return all matching results' do + Puppet::TestIndirectedFoo.search('bar').length.should == @model_instances.length + end + + it 'should return model instances' do + Puppet::TestIndirectedFoo.search('bar').each do |result| + result.class.should == Puppet::TestIndirectedFoo + end + end + + it 'should return the instance of the model class associated with the provided lookup key' do + Puppet::TestIndirectedFoo.search('bar').collect(&:value).should == @model_instances.collect(&:value) + end + + it 'should set a version timestamp on model instances' do + pending("Luke looking at why this version magic might not be working") do + Puppet::TestIndirectedFoo.search('bar').each do |result| + result.version.should_not be_nil + end + end + end + end + + describe "when no matching model instance can be found" do + before :each do + @mock_model = stub('faked model', :find => nil) + Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:model).returns(@mock_model) + end + + it "should return nil" do + Puppet::TestIndirectedFoo.find('bar').should be_nil + end + end + + describe "when an exception is encountered in looking up a model instance" do + before :each do + @mock_model = stub('faked model') + @mock_model.stubs(:find).raises(RuntimeError) + Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:model).returns(@mock_model) + end + + it "should raise an exception" do + lambda { Puppet::TestIndirectedFoo.find('bar') }.should raise_error(RuntimeError) + end + end end - describe "when searching for model instances over REST" do + describe "when destroying a model instance over REST" do it "needs more specs" end - describe "when destroying a model instance over REST" do + describe "when saving a model instance over REST" do it "needs more specs" end diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb index 791ef862a..6f4285765 100755 --- a/spec/unit/indirector/rest.rb +++ b/spec/unit/indirector/rest.rb @@ -69,7 +69,7 @@ describe Puppet::Indirector::REST do describe "when doing a search" do before :each do - @result = [1, 2] + @result = [1, 2].to_yaml @searcher.stubs(:network_fetch).returns(@result) @model.stubs(:from_yaml).returns(@instance) end -- cgit