diff --git a/lib/puppet/faces/indirector.rb b/lib/puppet/faces/indirector.rb index f72260017..7e4e0f00f 100644 --- a/lib/puppet/faces/indirector.rb +++ b/lib/puppet/faces/indirector.rb @@ -1,94 +1,94 @@ require 'puppet' require 'puppet/faces' class Puppet::Faces::Indirector < Puppet::Faces option "--terminus TERMINUS" do desc "REVISIT: You can select a terminus, which has some bigger effect that we should describe in this file somehow." end def self.indirections Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort end def self.terminus_classes(indirection) Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort end def call_indirection_method(method, *args) - options = args.pop + options = args.last options.has_key?(:terminus) and set_terminus(options[:terminus]) begin result = indirection.__send__(method, *args) rescue => detail puts detail.backtrace if Puppet[:trace] raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" end indirection.reset_terminus_class return result end action :destroy do when_invoked { |*args| call_indirection_method(:destroy, *args) } end action :find do when_invoked { |*args| call_indirection_method(:find, *args) } end action :save do when_invoked { |*args| call_indirection_method(:save, *args) } end action :search do when_invoked { |*args| call_indirection_method(:search, *args) } end # Print the configuration for the current terminus class action :info do when_invoked do |*args| options = args.pop options.has_key?(:terminus) and set_terminus(options[:terminus]) if t = indirection.terminus_class puts "Run mode '#{Puppet.run_mode.name}': #{t}" else $stderr.puts "No default terminus class for run mode '#{Puppet.run_mode.name}'" end indirection.reset_terminus_class end end attr_accessor :from def indirection_name @indirection_name || name.to_sym end # Here's your opportunity to override the indirection name. By default it # will be the same name as the face. def set_indirection_name(name) @indirection_name = name end # Return an indirection associated with a face, if one exists; # One usually does. def indirection unless @indirection @indirection = Puppet::Indirector::Indirection.instance(indirection_name) @indirection or raise "Could not find terminus for #{indirection_name}" end @indirection end def set_terminus(from) begin indirection.terminus_class = from rescue => detail raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{self.class.terminus_classes(indirection.name).join(", ") }" end end end diff --git a/spec/unit/faces/indirector_spec.rb b/spec/unit/faces/indirector_spec.rb index 218694bd9..3ed64bc01 100755 --- a/spec/unit/faces/indirector_spec.rb +++ b/spec/unit/faces/indirector_spec.rb @@ -1,56 +1,60 @@ #!/usr/bin/env ruby require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') require 'puppet/faces/indirector' describe Puppet::Faces::Indirector do subject do instance = Puppet::Faces::Indirector.new(:test, '0.0.1') indirection = stub('indirection', :name => :stub_indirection, :reset_terminus_class => nil) instance.stubs(:indirection).returns indirection instance end it "should be able to return a list of indirections" do Puppet::Faces::Indirector.indirections.should be_include("catalog") end it "should be able to return a list of terminuses for a given indirection" do Puppet::Faces::Indirector.terminus_classes(:catalog).should be_include("compiler") end describe "as an instance" do it "should be able to determine its indirection" do # Loading actions here an get, um, complicated Puppet::Faces.stubs(:load_actions) Puppet::Faces::Indirector.new(:catalog, '0.0.1').indirection.should equal(Puppet::Resource::Catalog.indirection) end end [:find, :search, :save, :destroy].each do |method| it "should define a '#{method}' action" do Puppet::Faces::Indirector.should be_action(method) end - it "should call the indirection method when the '#{method}' action is invoked" do - subject.indirection.expects(method).with(:test, "myargs") + it "should call the indirection method with options when the '#{method}' action is invoked" do + subject.indirection.expects(method).with(:test, "myargs", {}) subject.send(method, :test, "myargs") end + it "should forward passed options" do + subject.indirection.expects(method).with(:test, "action", {'one'=>'1'}) + subject.send(method, :test, 'action', {'one'=>'1'}) + end end it "should be able to override its indirection name" do subject.set_indirection_name :foo subject.indirection_name.should == :foo end it "should be able to set its terminus class" do subject.indirection.expects(:terminus_class=).with(:myterm) subject.set_terminus(:myterm) end it "should define a class-level 'info' action" do Puppet::Faces::Indirector.should be_action(:info) end end