3
require 'puppet/defaults'
4
require 'puppet/indirector'
5
require 'puppet/indirector/memory'
7
describe Puppet::Indirector::Terminus, :'fails_on_ruby_1.9.2' => true do
9
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
10
@indirection = stub 'indirection', :name => :my_stuff, :register_terminus_type => nil
11
Puppet::Indirector::Indirection.stubs(:instance).with(:my_stuff).returns(@indirection)
12
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
17
@terminus_class = Class.new(@abstract_terminus) do
22
@terminus = @terminus_class.new
25
describe Puppet::Indirector::Terminus do
27
it "should provide a method for setting terminus class documentation" do
28
@terminus_class.should respond_to(:desc)
31
it "should support a class-level name attribute" do
32
@terminus_class.should respond_to(:name)
35
it "should support a class-level indirection attribute" do
36
@terminus_class.should respond_to(:indirection)
39
it "should support a class-level terminus-type attribute" do
40
@terminus_class.should respond_to(:terminus_type)
43
it "should support a class-level model attribute" do
44
@terminus_class.should respond_to(:model)
47
it "should accept indirection instances as its indirection" do
48
indirection = stub 'indirection', :is_a? => true, :register_terminus_type => nil
49
proc { @terminus_class.indirection = indirection }.should_not raise_error
50
@terminus_class.indirection.should equal(indirection)
53
it "should look up indirection instances when only a name has been provided" do
54
indirection = mock 'indirection'
55
Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(indirection)
56
@terminus_class.indirection = :myind
57
@terminus_class.indirection.should equal(indirection)
60
it "should fail when provided a name that does not resolve to an indirection" do
61
Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(nil)
62
proc { @terminus_class.indirection = :myind }.should raise_error(ArgumentError)
64
# It shouldn't overwrite our existing one (or, more normally, it shouldn't set
66
@terminus_class.indirection.should equal(@indirection)
70
describe Puppet::Indirector::Terminus, " when creating terminus classes" do
71
it "should associate the subclass with an indirection based on the subclass constant" do
72
@terminus.indirection.should equal(@indirection)
75
it "should set the subclass's type to the abstract terminus name" do
76
@terminus.terminus_type.should == :abstract
79
it "should set the subclass's name to the indirection name" do
80
@terminus.name.should == :term_type
83
it "should set the subclass's model to the indirection model" do
84
@indirection.expects(:model).returns :yay
85
@terminus.model.should == :yay
89
describe Puppet::Indirector::Terminus, " when a terminus instance" do
91
it "should return the class's name as its name" do
92
@terminus.name.should == :term_type
95
it "should return the class's indirection as its indirection" do
96
@terminus.indirection.should equal(@indirection)
99
it "should set the instances's type to the abstract terminus type's name" do
100
@terminus.terminus_type.should == :abstract
103
it "should set the instances's model to the indirection's model" do
104
@indirection.expects(:model).returns :yay
105
@terminus.model.should == :yay
110
# LAK: This could reasonably be in the Indirection instances, too. It doesn't make
111
# a whole heckuva lot of difference, except that with the instance loading in
112
# the Terminus base class, we have to have a check to see if we're already
113
# instance-loading a given terminus class type.
114
describe Puppet::Indirector::Terminus, " when managing terminus classes" do
115
it "should provide a method for registering terminus classes" do
116
Puppet::Indirector::Terminus.should respond_to(:register_terminus_class)
119
it "should provide a method for returning terminus classes by name and type" do
120
terminus = stub 'terminus_type', :name => :abstract, :indirection_name => :whatever
121
Puppet::Indirector::Terminus.register_terminus_class(terminus)
122
Puppet::Indirector::Terminus.terminus_class(:whatever, :abstract).should equal(terminus)
125
it "should set up autoloading for any terminus class types requested" do
126
Puppet::Indirector::Terminus.expects(:instance_load).with(:test2, "puppet/indirector/test2")
127
Puppet::Indirector::Terminus.terminus_class(:test2, :whatever)
130
it "should load terminus classes that are not found" do
131
# Set up instance loading; it would normally happen automatically
132
Puppet::Indirector::Terminus.instance_load :test1, "puppet/indirector/test1"
134
Puppet::Indirector::Terminus.instance_loader(:test1).expects(:load).with(:yay)
135
Puppet::Indirector::Terminus.terminus_class(:test1, :yay)
138
it "should fail when no indirection can be found", :'fails_on_ruby_1.9.2' => true do
139
Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(nil)
141
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
147
@terminus = Class.new(@abstract_terminus) do
149
"MyIndirection::TestType"
152
}.should raise_error(ArgumentError)
155
it "should register the terminus class with the terminus base class", :'fails_on_ruby_1.9.2' => true do
156
Puppet::Indirector::Terminus.expects(:register_terminus_class).with do |type|
157
type.indirection_name == :my_indirection and type.name == :test_terminus
159
@indirection = stub 'indirection', :name => :my_indirection, :register_terminus_type => nil
160
Puppet::Indirector::Indirection.expects(:instance).with(:my_indirection).returns(@indirection)
162
@abstract_terminus = Class.new(Puppet::Indirector::Terminus) do
168
@terminus = Class.new(@abstract_terminus) do
170
"MyIndirection::TestTerminus"
176
describe Puppet::Indirector::Terminus, " when parsing class constants for indirection and terminus names" do
178
@subclass = mock 'subclass'
179
@subclass.stubs(:to_s).returns("TestInd::OneTwo")
180
@subclass.stubs(:mark_as_abstract_terminus)
181
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
184
it "should fail when anonymous classes are used" do
185
proc { Puppet::Indirector::Terminus.inherited(Class.new) }.should raise_error(Puppet::DevError)
188
it "should use the last term in the constant for the terminus class name" do
189
@subclass.expects(:name=).with(:one_two)
190
@subclass.stubs(:indirection=)
191
Puppet::Indirector::Terminus.inherited(@subclass)
194
it "should convert the terminus name to a downcased symbol" do
195
@subclass.expects(:name=).with(:one_two)
196
@subclass.stubs(:indirection=)
197
Puppet::Indirector::Terminus.inherited(@subclass)
200
it "should use the second to last term in the constant for the indirection name" do
201
@subclass.expects(:indirection=).with(:test_ind)
202
@subclass.stubs(:name=)
203
@subclass.stubs(:terminus_type=)
204
Puppet::Indirector::Memory.inherited(@subclass)
207
it "should convert the indirection name to a downcased symbol" do
208
@subclass.expects(:indirection=).with(:test_ind)
209
@subclass.stubs(:name=)
210
@subclass.stubs(:terminus_type=)
211
Puppet::Indirector::Memory.inherited(@subclass)
214
it "should convert camel case to lower case with underscores as word separators" do
215
@subclass.expects(:name=).with(:one_two)
216
@subclass.stubs(:indirection=)
218
Puppet::Indirector::Terminus.inherited(@subclass)
222
describe Puppet::Indirector::Terminus, " when creating terminus class types", :'fails_on_ruby_1.9.2' => true do
224
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
225
@subclass = Class.new(Puppet::Indirector::Terminus) do
227
"Puppet::Indirector::Terminus::MyTermType"
232
it "should set the name of the abstract subclass to be its class constant" do
233
@subclass.name.should equal(:my_term_type)
236
it "should mark abstract terminus types as such" do
237
@subclass.should be_abstract_terminus
240
it "should not allow instances of abstract subclasses to be created" do
241
proc { @subclass.new }.should raise_error(Puppet::DevError)
245
describe Puppet::Indirector::Terminus, " when listing terminus classes" do
246
it "should list the terminus files available to load" do
247
Puppet::Util::Autoload.any_instance.stubs(:files_to_load).returns ["/foo/bar/baz", "/max/runs/marathon"]
248
Puppet::Indirector::Terminus.terminus_classes('my_stuff').should == [:baz, :marathon]