1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/network/server'
require 'puppet/indirector'
require 'puppet/indirector/rest'
# a fake class that will be indirected via REST
class Puppet::TestIndirectedFoo
extend Puppet::Indirector
indirects :test_indirected_foo, :terminus_setting => :test_indirected_foo_terminus
attr_reader :value
def initialize(value = 0)
@value = value
end
def self.from_yaml(yaml)
YAML.load(yaml)
end
def name
"bob"
end
end
# empty Terminus class -- this would normally have to be in a directory findable by the autoloader, but we short-circuit that below
class Puppet::TestIndirectedFoo::Rest < Puppet::Indirector::REST
end
describe Puppet::Indirector::REST do
before do
Puppet[:masterport] = 34343
Puppet[:server] = "localhost"
# Get a safe temporary file
@tmpfile = Tempfile.new("webrick_integration_testing")
@dir = @tmpfile.path + "_dir"
Puppet.settings[:confdir] = @dir
Puppet.settings[:vardir] = @dir
Puppet.settings[:server] = "127.0.0.1"
Puppet.settings[:masterport] = "34343"
Puppet.settings[:http_enable_post_connection_check] = false
Puppet::SSL::Host.ca_location = :local
Puppet::TestIndirectedFoo.terminus_class = :rest
end
after do
Puppet::Network::HttpPool.instance_variable_set("@ssl_host", nil)
Puppet.settings.clear
end
describe "when using webrick" do
before :each do
Puppet::Util::Cacher.invalidate
Puppet[:servertype] = 'webrick'
Puppet[:server] = '127.0.0.1'
Puppet[:certname] = '127.0.0.1'
ca = Puppet::SSL::CertificateAuthority.new
ca.generate(Puppet[:certname]) unless Puppet::SSL::Certificate.find(Puppet[:certname])
@params = { :address => "127.0.0.1", :port => 34343, :handlers => [ :test_indirected_foo ], :xmlrpc_handlers => [ :status ] }
@server = Puppet::Network::Server.new(@params)
@server.listen
# LAK:NOTE We need to have a fake model here so that our indirected methods get
# passed through REST; otherwise we'd be stubbing 'find', which would cause an immediate
# return.
@mock_model = stub('faked model', :name => "foo")
Puppet::Network::HTTP::WEBrickREST.any_instance.stubs(:model).returns(@mock_model)
end
describe "when finding a model instance over REST" do
describe "when a matching model instance can be found" do
before :each do
@model_instance = Puppet::TestIndirectedFoo.new(23)
@mock_model.stubs(:find).returns @model_instance
end
it "should not fail" do
lambda { Puppet::TestIndirectedFoo.find('bar') }.should_not raise_error
end
it 'should return an instance of the model class' do
Puppet::TestIndirectedFoo.find('bar').class.should == Puppet::TestIndirectedFoo
end
it 'should return the instance of the model class associated with the provided lookup key' do
Puppet::TestIndirectedFoo.find('bar').value.should == @model_instance.value
end
it 'should set an expiration on model instance' do
Puppet::TestIndirectedFoo.find('bar').expiration.should_not be_nil
end
it "should use a supported format" do
Puppet::TestIndirectedFoo.expects(:supported_formats).returns ["marshal"]
text = Marshal.dump(@model_instance)
@model_instance.expects(:render).with("marshal").returns text
Puppet::TestIndirectedFoo.find('bar')
end
end
describe "when no matching model instance can be found" do
before :each do
@mock_model = stub('faked model', :name => "foo", :find => nil)
Puppet::Network::HTTP::WEBrickREST.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', :name => "foo")
@mock_model.stubs(:find).raises(RuntimeError)
Puppet::Network::HTTP::WEBrickREST.any_instance.stubs(:model).returns(@mock_model)
end
it "should raise an exception" do
lambda { Puppet::TestIndirectedFoo.find('bar') }.should raise_error(Net::HTTPError)
end
end
end
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.stubs(:search).returns @model_instances
@mock_model.stubs(:render_multiple).returns @model_instances.to_yaml
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 { |i| i.value }.should == @model_instances.collect { |i| i.value }
end
end
describe "when no matching model instance can be found" do
before :each do
@mock_model = stub('faked model', :name => "foo", :find => nil)
Puppet::Network::HTTP::WEBrickREST.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::WEBrickREST.any_instance.stubs(:model).returns(@mock_model)
end
it "should raise an exception" do
lambda { Puppet::TestIndirectedFoo.find('bar') }.should raise_error(Net::HTTPError)
end
end
end
describe "when destroying a model instance over REST" do
describe "when a matching model instance can be found" do
before :each do
@mock_model.stubs(:destroy).returns true
end
it "should not fail" do
lambda { Puppet::TestIndirectedFoo.destroy('bar') }.should_not raise_error
end
it 'should return success' do
Puppet::TestIndirectedFoo.destroy('bar').should == true
end
end
describe "when no matching model instance can be found" do
before :each do
@mock_model.stubs(:destroy).returns false
end
it "should return failure" do
Puppet::TestIndirectedFoo.destroy('bar').should == false
end
end
describe "when an exception is encountered in destroying a model instance" do
before :each do
@mock_model.stubs(:destroy).raises(RuntimeError)
end
it "should raise an exception" do
lambda { Puppet::TestIndirectedFoo.destroy('bar') }.should raise_error(Net::HTTPError)
end
end
end
describe "when saving a model instance over REST" do
before :each do
@instance = Puppet::TestIndirectedFoo.new(42)
@mock_model.stubs(:save_object).returns @instance
@mock_model.stubs(:convert_from).returns @instance
Puppet::Network::HTTP::WEBrickREST.any_instance.stubs(:save_object).returns(@instance)
end
describe "when a successful save can be performed" do
before :each do
end
it "should not fail" do
lambda { @instance.save }.should_not raise_error
end
it 'should return an instance of the model class' do
@instance.save.class.should == Puppet::TestIndirectedFoo
end
it 'should return a matching instance of the model class' do
@instance.save.value.should == @instance.value
end
end
describe "when a save cannot be completed" do
before :each do
Puppet::Network::HTTP::WEBrickREST.any_instance.stubs(:save_object).returns(false)
end
it "should return failure" do
@instance.save.should == false
end
end
describe "when an exception is encountered in performing a save" do
before :each do
Puppet::Network::HTTP::WEBrickREST.any_instance.stubs(:save_object).raises(RuntimeError)
end
it "should raise an exception" do
lambda { @instance.save }.should raise_error(Net::HTTPError)
end
end
end
after :each do
@server.unlisten
end
end
describe "when using mongrel" do
confine "Mongrel is not available" => Puppet.features.mongrel?
before :each do
Puppet[:servertype] = 'mongrel'
@params = { :address => "127.0.0.1", :port => 34343, :handlers => [ :test_indirected_foo ] }
# Make sure we never get a cert, since mongrel can't speak ssl
Puppet::SSL::Certificate.stubs(:find).returns nil
# We stub ssl to be off, since mongrel can't speak ssl
Net::HTTP.any_instance.stubs(:use_ssl?).returns false
@server = Puppet::Network::Server.new(@params)
@server.listen
# LAK:NOTE We need to have a fake model here so that our indirected methods get
# passed through REST; otherwise we'd be stubbing 'find', which would cause an immediate
# return.
@mock_model = stub('faked model', :name => "foo")
Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:model).returns(@mock_model)
end
after do
@server.unlisten
end
describe "when finding a model instance over REST" do
describe "when a matching model instance can be found" do
before :each do
@model_instance = Puppet::TestIndirectedFoo.new(23)
@mock_model.stubs(:find).returns @model_instance
end
it "should not fail" do
lambda { Puppet::TestIndirectedFoo.find('bar') }.should_not raise_error
end
it 'should return an instance of the model class' do
Puppet::TestIndirectedFoo.find('bar').class.should == Puppet::TestIndirectedFoo
end
it 'should return the instance of the model class associated with the provided lookup key' do
Puppet::TestIndirectedFoo.find('bar').value.should == @model_instance.value
end
it 'should set an expiration on model instance' do
Puppet::TestIndirectedFoo.find('bar').expiration.should_not be_nil
end
it "should use a supported format" do
Puppet::TestIndirectedFoo.expects(:supported_formats).returns ["marshal"]
text = Marshal.dump(@model_instance)
@model_instance.expects(:render).with("marshal").returns text
Puppet::TestIndirectedFoo.find('bar')
end
end
describe "when no matching model instance can be found" do
before :each do
@mock_model.stubs(:find).returns nil
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.stubs(:find).raises(RuntimeError)
end
it "should raise an exception" do
lambda { Puppet::TestIndirectedFoo.find('bar') }.should raise_error(Net::HTTPError)
end
end
end
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.stubs(:search).returns @model_instances
@mock_model.stubs(:render_multiple).returns @model_instances.to_yaml
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 { |i| i.value }.should == @model_instances.collect { |i| i.value }
end
it 'should set an expiration on model instances' do
Puppet::TestIndirectedFoo.search('bar').each do |result|
result.expiration.should_not be_nil
end
end
end
describe "when no matching model instance can be found" do
before :each do
@mock_model.stubs(:search).returns nil
@mock_model.stubs(:render_multiple).returns nil.to_yaml
end
it "should return nil" do
Puppet::TestIndirectedFoo.search('bar').should be_nil
end
end
describe "when an exception is encountered in looking up a model instance" do
before :each do
@mock_model.stubs(:find).raises(RuntimeError)
end
it "should raise an exception" do
lambda { Puppet::TestIndirectedFoo.find('bar') }.should raise_error(Net::HTTPError)
end
end
end
describe "when destroying a model instance over REST" do
describe "when a matching model instance can be found" do
before :each do
@mock_model.stubs(:destroy).returns true
end
it "should not fail" do
lambda { Puppet::TestIndirectedFoo.destroy('bar') }.should_not raise_error
end
it 'should return success' do
Puppet::TestIndirectedFoo.destroy('bar').should == true
end
end
describe "when no matching model instance can be found" do
before :each do
@mock_model.stubs(:destroy).returns false
end
it "should return failure" do
Puppet::TestIndirectedFoo.destroy('bar').should == false
end
end
describe "when an exception is encountered in destroying a model instance" do
before :each do
@mock_model.stubs(:destroy).raises(RuntimeError)
end
it "should raise an exception" do
lambda { Puppet::TestIndirectedFoo.destroy('bar') }.should raise_error(Net::HTTPError)
end
end
end
describe "when saving a model instance over REST" do
before :each do
@instance = Puppet::TestIndirectedFoo.new(42)
@mock_model.stubs(:convert_from).returns @instance
# LAK:NOTE This stub is necessary to prevent the REST call from calling
# REST.save again, thus producing painful infinite recursion.
Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:save_object).returns(@instance)
end
describe "when a successful save can be performed" do
before :each do
end
it "should not fail" do
lambda { @instance.save }.should_not raise_error
end
it 'should return an instance of the model class' do
@instance.save.class.should == Puppet::TestIndirectedFoo
end
it 'should return a matching instance of the model class' do
@instance.save.value.should == @instance.value
end
end
describe "when a save cannot be completed" do
before :each do
Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:save_object).returns(false)
end
it "should return failure" do
@instance.save.should == false
end
end
describe "when an exception is encountered in performing a save" do
before :each do
Puppet::Network::HTTP::MongrelREST.any_instance.stubs(:save_object).raises(RuntimeError)
end
it "should raise an exception" do
lambda { @instance.save }.should raise_error(Net::HTTPError)
end
end
end
end
end
|