diff options
| -rw-r--r-- | lib/puppet/indirector/rest.rb | 8 | ||||
| -rwxr-xr-x | spec/unit/indirector/rest.rb | 60 |
2 files changed, 60 insertions, 8 deletions
diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index 4c54183c6..690c79632 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -13,4 +13,12 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus raise YAML.load(network_result) if network_result =~ %r{--- !ruby/exception} decoded_result = indirection.model.from_yaml(network_result) end + + 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 + end end diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb index 56db8f509..791ef862a 100755 --- a/spec/unit/indirector/rest.rb +++ b/spec/unit/indirector/rest.rb @@ -6,8 +6,9 @@ require 'puppet/indirector/rest' describe Puppet::Indirector::REST do before do Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + @model = stub('model') + @instance = stub('model instance') + @indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model) Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) @rest_class = Class.new(Puppet::Indirector::REST) do @@ -21,12 +22,23 @@ describe Puppet::Indirector::REST do describe "when doing a find" do before :each do - @searcher.stubs(:network_fetch).returns({:foo => 'bar'}.to_yaml) # neuter the network connection + @result = { :foo => 'bar'}.to_yaml + @searcher.stubs(:network_fetch).returns(@result) # neuter the network connection @model.stubs(:from_yaml).returns(@instance) end it "should look up the model instance over the network" do - @searcher.expects(:network_fetch).returns({:foo => 'bar'}.to_yaml) + @searcher.expects(:network_fetch).returns(@result) + @searcher.find('foo') + end + + it "should look up the model instance using the named indirection" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{^#{@indirection.name.to_s}/} }.returns(@result) + @searcher.find('foo') + end + + it "should look up the model instance using the provided key" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{/foo$} }.returns(@result) @searcher.find('foo') end @@ -40,7 +52,7 @@ describe Puppet::Indirector::REST do end it "should return nil when deserialized model instance is nil" do - @model.stubs(:from_yaml).returns(@instance) + @model.stubs(:from_yaml).returns(nil) @searcher.find('foo').should be_nil end @@ -56,9 +68,41 @@ describe Puppet::Indirector::REST do end describe "when doing a search" do - it "should deserialize result data into a list of Model instances" - it "should generate an error when result data deserializes improperly" - it "should generate an error when result data specifies an error" + before :each do + @result = [1, 2] + @searcher.stubs(:network_fetch).returns(@result) + @model.stubs(:from_yaml).returns(@instance) + end + + it "should look up the model data over the network" do + @searcher.expects(:network_fetch).returns(@result) + @searcher.search('foo') + end + + it "should look up the model instance using the named indirection" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{^#{@indirection.name.to_s}s/} }.returns(@result) + @searcher.search('foo') + end + + it "should look up the model instance using the provided key" do + @searcher.expects(:network_fetch).with {|path| path =~ %r{/foo$} }.returns(@result) + @searcher.search('foo') + end + + it "should deserialize result data into a list of Model instances" do + @model.expects(:from_yaml).at_least(2) + @searcher.search('foo') + end + + it "should generate an error when result data deserializes improperly" do + @model.stubs(:from_yaml).raises(ArgumentError) + lambda { @searcher.search('foo') }.should raise_error(ArgumentError) + end + + it "should generate an error when result data specifies an error" do + @searcher.stubs(:network_fetch).returns(RuntimeError.new("bogus").to_yaml) + lambda { @searcher.search('foo') }.should raise_error(RuntimeError) + end end describe "when doing a save" do |
