diff options
author | Luke Kanies <luke@madstop.com> | 2008-04-08 02:17:31 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-04-08 11:33:50 -0500 |
commit | f9881edd5d4b9f86c1da1c66fb12324d0f9c3c41 (patch) | |
tree | dd3d8fd6d93073559162d4c480d7cde95bdf1dfa /spec | |
parent | 4032a270d90ef93b39ed1df17ebe1040a11128b9 (diff) | |
download | puppet-f9881edd5d4b9f86c1da1c66fb12324d0f9c3c41.tar.gz puppet-f9881edd5d4b9f86c1da1c66fb12324d0f9c3c41.tar.xz puppet-f9881edd5d4b9f86c1da1c66fb12324d0f9c3c41.zip |
Adding a Request class to the Indirection layer. This
class is currently only used internally by the Indirection
instances, but I expect that I will soon be pushing it to
all of the terminus types.
I still need to fix a couple of tests that will get broken by
this commit.
Diffstat (limited to 'spec')
-rwxr-xr-x | spec/unit/indirector/indirection.rb | 211 | ||||
-rwxr-xr-x | spec/unit/indirector/request.rb | 40 |
2 files changed, 169 insertions, 82 deletions
diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb index 5338a4858..8768076c6 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -35,16 +35,6 @@ describe Puppet::Indirector::Indirection do @indirection.metaclass.included_modules.should include(mod) end - it "should allow specification of supported options" do - lambda { @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test, :options => [:one, :two]) }.should_not raise_error - end - - it "should define a new Struct class to support the specified options" do - struct = mock 'struct' - Struct.expects(:new).with(:one, :two) - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test, :options => [:one, :two]) - end - after do @indirection.delete if defined? @indirection end @@ -85,6 +75,35 @@ describe Puppet::Indirector::Indirection do end describe "and looking for a model instance" do + it "should create a request with the indirection name, the sought-after name, the :find method, and any passed arguments" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).with(@indirection.name, @name, :find, {:one => :two}).returns request + + @indirection.stubs(:check_authorization) + @terminus.stubs(:find) + + @indirection.find(@name, :one => :two) + end + + it "should let the :select_terminus method choose the terminus if the method is defined" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).returns request + + # Define the method, so our respond_to? hook matches. + class << @indirection + def select_terminus(request) + end + end + + @indirection.expects(:select_terminus).with(request).returns :test_terminus + + @indirection.stubs(:check_authorization) + @terminus.expects(:find) + + @indirection.find(@name) + + end + it "should let the appropriate terminus perform the lookup" do @terminus.expects(:find).with(@name).returns(@instance) @indirection.find(@name).should == @instance @@ -197,18 +216,39 @@ describe Puppet::Indirector::Indirection do end describe "and storing a model instance" do - it "should let the appropriate terminus store the instance" do - @terminus.expects(:save).with(@instance).returns(@instance) - @indirection.save(@instance).should == @instance - end + it "should create a request with the indirection name, the instance's name, the :save method, and any passed arguments" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).with(@indirection.name, @instance.name, :save, {:one => :two}).returns request - it "should check authorization" do - @indirection.expects(:check_authorization).with(:save, :test_terminus, [@instance]) + @indirection.stubs(:check_authorization) @terminus.stubs(:save) + @indirection.save(@instance, :one => :two) + end + + it "should let the :select_terminus method choose the terminus if the method is defined" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).returns request + + # Define the method, so our respond_to? hook matches. + class << @indirection + def select_terminus(request) + end + end + + @indirection.expects(:select_terminus).with(request).returns :test_terminus + + @indirection.stubs(:check_authorization) + @terminus.expects(:save) + @indirection.save(@instance) end + it "should let the appropriate terminus store the instance" do + @terminus.expects(:save).with(@instance).returns(@instance) + @indirection.save(@instance).should == @instance + end + describe "when caching is enabled" do before do @indirection.cache_class = :cache_terminus @@ -226,6 +266,34 @@ describe Puppet::Indirector::Indirection do end describe "and removing a model instance" do + it "should create a request with the indirection name, the name of the instance being destroyed, the :destroy method, and any passed arguments" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).with(@indirection.name, "me", :destroy, {:one => :two}).returns request + + @indirection.stubs(:check_authorization) + @terminus.stubs(:destroy) + + @indirection.destroy("me", :one => :two) + end + + it "should let the :select_terminus method choose the terminus if the method is defined" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).returns request + + # Define the method, so our respond_to? hook matches. + class << @indirection + def select_terminus(request) + end + end + + @indirection.expects(:select_terminus).with(request).returns :test_terminus + + @indirection.stubs(:check_authorization) + @terminus.expects(:destroy) + + @indirection.destroy(@name) + end + it "should delegate the instance removal to the appropriate terminus" do @terminus.expects(:destroy).with(@name) @indirection.destroy(@name) @@ -256,6 +324,34 @@ describe Puppet::Indirector::Indirection do end describe "and searching for multiple model instances" do + it "should create a request with the indirection name, the search key, the :search method, and any passed arguments" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).with(@indirection.name, "me", :search, {:one => :two}).returns request + + @indirection.stubs(:check_authorization) + @terminus.stubs(:search) + + @indirection.search("me", :one => :two) + end + + it "should let the :select_terminus method choose the terminus if the method is defined" do + request = mock 'request' + Puppet::Indirector::Request.expects(:new).returns request + + # Define the method, so our respond_to? hook matches. + class << @indirection + def select_terminus(request) + end + end + + @indirection.expects(:select_terminus).with(request).returns :test_terminus + + @indirection.stubs(:check_authorization) + @terminus.expects(:search) + + @indirection.search("me") + end + it "should let the appropriate terminus find the matching instances" do @terminus.expects(:search).with(@name).returns(@instance) @indirection.search(@name).should == @instance @@ -277,50 +373,59 @@ describe Puppet::Indirector::Indirection do @indirection.find("/my/key") end + it "should pass the request to the terminus's authorization method" do + request = stub 'request', :options => {:node => "yayhost"} + Puppet::Indirector::Request.expects(:new).returns(request) + @terminus.expects(:authorized?).with(request).returns(true) + @terminus.stubs(:find) + + @indirection.find("/my/key", :node => "mynode") + end + it "should fail while finding instances if authorization returns false" do - @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(false) + @terminus.expects(:authorized?).returns(false) @terminus.stubs(:find) proc { @indirection.find("/my/key", :node => "mynode") }.should raise_error(ArgumentError) end it "should continue finding instances if authorization returns true" do - @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(true) + @terminus.expects(:authorized?).returns(true) @terminus.stubs(:find) @indirection.find("/my/key", :node => "mynode") end it "should fail while saving instances if authorization returns false" do - @terminus.expects(:authorized?).with(:save, @instance, :node => "mynode").returns(false) + @terminus.expects(:authorized?).returns(false) @terminus.stubs(:save) proc { @indirection.save(@instance, :node => "mynode") }.should raise_error(ArgumentError) end it "should continue saving instances if authorization returns true" do - @terminus.expects(:authorized?).with(:save, @instance, :node => "mynode").returns(true) + @terminus.expects(:authorized?).returns(true) @terminus.stubs(:save) @indirection.save(@instance, :node => "mynode") end it "should fail while destroying instances if authorization returns false" do - @terminus.expects(:authorized?).with(:destroy, "/my/key", :node => "mynode").returns(false) + @terminus.expects(:authorized?).returns(false) @terminus.stubs(:destroy) proc { @indirection.destroy("/my/key", :node => "mynode") }.should raise_error(ArgumentError) end it "should continue destroying instances if authorization returns true" do - @terminus.expects(:authorized?).with(:destroy, @instance, :node => "mynode").returns(true) + @terminus.expects(:authorized?).returns(true) @terminus.stubs(:destroy) @indirection.destroy(@instance, :node => "mynode") end it "should fail while searching for instances if authorization returns false" do - @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(false) + @terminus.expects(:authorized?).returns(false) @terminus.stubs(:search) proc { @indirection.search("/my/key", :node => "mynode") }.should raise_error(ArgumentError) end it "should continue searching for instances if authorization returns true" do - @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(true) + @terminus.expects(:authorized?).returns(true) @terminus.stubs(:search) @indirection.search("/my/key", :node => "mynode") end @@ -365,15 +470,11 @@ describe Puppet::Indirector::Indirection do @terminus_class = stub 'terminus class', :new => @terminus Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :default).returns(@terminus_class) end - - it "should have a method for choosing the appropriate terminus class" it "should fail if no terminus class can be picked" do proc { @indirection.terminus_class }.should raise_error(Puppet::DevError) end - it "should use the select_terminus hook if one is available" - it "should choose the default terminus class if one is specified" do @indirection.terminus_class = :default @indirection.terminus_class.should equal(:default) @@ -440,60 +541,6 @@ describe Puppet::Indirector::Indirection do end end - describe "when a select_terminus hook is available" do - before do - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - - # And provide a select_terminus hook - @indirection.meta_def(:select_terminus) do |uri| - :other - end - - @terminus = mock 'terminus' - @terminus_class = stub 'terminus class', :new => @terminus - - @other_terminus = mock 'other_terminus' - @other_terminus_class = stub 'other_terminus_class', :new => @other_terminus - - @cache_terminus = mock 'cache_terminus' - @cache_terminus_class = stub 'cache_terminus_class', :new => @cache_terminus - - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class) - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :other).returns(@other_terminus_class) - Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache).returns(@cache_terminus_class) - - # Set it to a default type. - @indirection.terminus_class = :foo - - @uri = "full://url/path" - @result = stub 'result', :expiration => 1, :expired? => false - end - - it "should use the terminus name provided by passing the key to the :select_terminus hook when finding instances" do - # Set up the expectation - @other_terminus.expects(:find).with(@uri).returns(@result) - - @indirection.find(@uri) - end - - it "should pass all arguments to the :select_terminus hook" do - @indirection.expects(:select_terminus).with(@uri, :node => "johnny").returns(:other) - @other_terminus.stubs(:find) - - @indirection.find(@uri, :node => "johnny") - end - - it "should pass the original key to the terminus rather than a modified key" do - # This is the same test as before - @other_terminus.expects(:find).with(@uri).returns(@result) - @indirection.find(@uri) - end - - after do - @indirection.delete if defined? @indirection - end - end - describe "when managing terminus instances" do before do @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) diff --git a/spec/unit/indirector/request.rb b/spec/unit/indirector/request.rb new file mode 100755 index 000000000..ee60f600f --- /dev/null +++ b/spec/unit/indirector/request.rb @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' +require 'puppet/indirector/request' + +describe Puppet::Indirector::Request do + describe "when initializing" do + it "should require an indirection name, a key, and a method" do + lambda { Puppet::Indirector::Request.new }.should raise_error(ArgumentError) + end + + it "should support options specified as a hash" do + lambda { Puppet::Indirector::Request.new(:ind, :key, :method, :one => :two) }.should_not raise_error(ArgumentError) + end + + it "should support nil options" do + lambda { Puppet::Indirector::Request.new(:ind, :key, :method, nil) }.should_not raise_error(ArgumentError) + end + + it "should support unspecified options" do + lambda { Puppet::Indirector::Request.new(:ind, :key, :method) }.should_not raise_error(ArgumentError) + end + + it "should fail if options are specified as anything other than nil or a hash" do + lambda { Puppet::Indirector::Request.new(:ind, :key, :method, [:one, :two]) }.should raise_error(ArgumentError) + end + + it "should use an empty options hash if nil was provided" do + Puppet::Indirector::Request.new(:ind, :key, :method, nil).options.should == {} + end + end + + it "should look use the Indirection class to return the appropriate indirection" do + ind = mock 'indirection' + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns ind + request = Puppet::Indirector::Request.new(:myind, :key, :method) + + request.indirection.should equal(ind) + end +end |