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
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector/rest'
describe Puppet::Indirector::REST do
before do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@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
def self.to_s
"This::Is::A::Test::Class"
end
end
@searcher = @rest_class.new
end
describe "when locating the REST connection" do
before do
Puppet.settings.stubs(:value).returns("whatever")
end
it "should return the :server setting as the host" do
Puppet.settings.expects(:value).with(:server).returns "myserver"
@searcher.rest_connection_details[:host].should == "myserver"
end
it "should return the :masterport (as an Integer) as the port" do
Puppet.settings.expects(:value).with(:masterport).returns "1234"
@searcher.rest_connection_details[:port].should == 1234
end
end
describe "when doing a network fetch" do
before :each do
Net::HTTP.stubs(:start).returns('result')
@details = { :host => '127.0.0.1', :port => 34343 }
@searcher.stubs(:rest_connection_details).returns(@details)
end
it "should accept a path" do
lambda { @search.network_fetch('foo') }.should_not raise_error(ArgumentError)
end
it "should require a path" do
lambda { @searcher.network_fetch }.should raise_error(ArgumentError)
end
it "should look up connection details" do
@searcher.expects(:rest_connection_details).returns(@details)
@searcher.network_fetch('foo')
end
it "should use the GET http method" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = mock('mock http connection', :get => @mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_fetch('foo')
end
it "should use the appropriate remote server" do
Net::HTTP.expects(:start).with {|host, port| host == @details[:host] }
@searcher.network_fetch('foo')
end
it "should use the appropriate remote port" do
Net::HTTP.expects(:start).with {|host, port| port == @details[:port] }
@searcher.network_fetch('foo')
end
it "should use the provided path" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = stub('mock http connection')
@mock_connection.expects(:get).with('/foo').returns(@mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_fetch('foo')
end
it "should return the results of the GET request" do
@searcher.network_fetch('foo').should == 'result'
end
end
describe "when doing a network delete" do
before :each do
Net::HTTP.stubs(:start).returns('result')
@details = { :host => '127.0.0.1', :port => 34343 }
@searcher.stubs(:rest_connection_details).returns(@details)
end
it "should accept a path" do
lambda { @search.network_delete('foo') }.should_not raise_error(ArgumentError)
end
it "should require a path" do
lambda { @searcher.network_delete }.should raise_error(ArgumentError)
end
it "should look up connection details" do
@searcher.expects(:rest_connection_details).returns(@details)
@searcher.network_delete('foo')
end
it "should use the DELETE http method" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = mock('mock http connection', :delete => @mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_delete('foo')
end
it "should use the appropriate remote server" do
Net::HTTP.expects(:start).with {|host, port| host == @details[:host] }
@searcher.network_delete('foo')
end
it "should use the appropriate remote port" do
Net::HTTP.expects(:start).with {|host, port| port == @details[:port] }
@searcher.network_delete('foo')
end
it "should use the provided path" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = stub('mock http connection')
@mock_connection.expects(:delete).with('/foo').returns(@mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_delete('foo')
end
it "should return the results of the DELETE request" do
@searcher.network_delete('foo').should == 'result'
end
end
describe "when doing a network put" do
before :each do
Net::HTTP.stubs(:start).returns('result')
@details = { :host => '127.0.0.1', :port => 34343 }
@data = { :foo => 'bar' }
@searcher.stubs(:rest_connection_details).returns(@details)
end
it "should accept a path and data" do
lambda { @search.network_put('foo', @data) }.should_not raise_error(ArgumentError)
end
it "should require a path and data" do
lambda { @searcher.network_put('foo') }.should raise_error(ArgumentError)
end
it "should look up connection details" do
@searcher.expects(:rest_connection_details).returns(@details)
@searcher.network_put('foo', @data)
end
it "should use the appropriate remote server" do
Net::HTTP.expects(:start).with {|host, port| host == @details[:host] }
@searcher.network_put('foo', @data)
end
it "should use the appropriate remote port" do
Net::HTTP.expects(:start).with {|host, port| port == @details[:port] }
@searcher.network_put('foo', @data)
end
it "should use the PUT http method" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = mock('mock http connection', :put => @mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_put('foo', @data)
end
it "should use the provided path" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = stub('mock http connection')
@mock_connection.expects(:put).with {|path, data| path == '/foo' }.returns(@mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_put('foo', @data)
end
it "should use the provided data" do
@mock_result = stub('mock result', :body => 'result')
@mock_connection = stub('mock http connection')
@mock_connection.expects(:put).with {|path, data| data == @data }.returns(@mock_result)
@searcher.stubs(:network).yields(@mock_connection)
@searcher.network_put('foo', @data)
end
it "should return the results of the PUT request" do
@searcher.network_put('foo', @data).should == 'result'
end
end
describe "when doing a find" do
before :each do
@result = { :foo => 'bar'}.to_yaml
@searcher.stubs(:network_fetch).returns(@result) # neuter the network connection
@model.stubs(:from_yaml).returns(@instance)
@request = stub 'request', :key => 'foo'
end
it "should look up the model instance over the network" do
@searcher.expects(:network_fetch).returns(@result)
@searcher.find(@request)
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(@request)
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(@request)
end
it "should deserialize result data to a Model instance" do
@model.expects(:from_yaml)
@searcher.find(@request)
end
it "should return the deserialized Model instance" do
@searcher.find(@request).should == @instance
end
it "should return nil when deserialized model instance is nil" do
@model.stubs(:from_yaml).returns(nil)
@searcher.find(@request).should be_nil
end
it "should generate an error when result data deserializes improperly" do
@model.stubs(:from_yaml).raises(ArgumentError)
lambda { @searcher.find(@request) }.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.find(@request) }.should raise_error(RuntimeError)
end
end
describe "when doing a search" do
before :each do
@result = [1, 2].to_yaml
@searcher.stubs(:network_fetch).returns(@result)
@model.stubs(:from_yaml).returns(@instance)
@request = stub 'request', :key => 'foo'
end
it "should look up the model data over the network" do
@searcher.expects(:network_fetch).returns(@result)
@searcher.search(@request)
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(@request)
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(@request)
end
it "should deserialize result data into a list of Model instances" do
@model.expects(:from_yaml).at_least(2)
@searcher.search(@request)
end
it "should generate an error when result data deserializes improperly" do
@model.stubs(:from_yaml).raises(ArgumentError)
lambda { @searcher.search(@request) }.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(@request) }.should raise_error(RuntimeError)
end
end
describe "when doing a destroy" do
before :each do
@result = true.to_yaml
@searcher.stubs(:network_delete).returns(@result) # neuter the network connection
@model.stubs(:from_yaml).returns(@instance)
@request = stub 'request', :key => 'foo'
end
it "should look up the model instance over the network" do
@searcher.expects(:network_delete).returns(@result)
@searcher.destroy(@request)
end
it "should look up the model instance using the named indirection" do
@searcher.expects(:network_delete).with {|path| path =~ %r{^#{@indirection.name.to_s}/} }.returns(@result)
@searcher.destroy(@request)
end
it "should look up the model instance using the provided key" do
@searcher.expects(:network_delete).with {|path| path =~ %r{/foo$} }.returns(@result)
@searcher.destroy(@request)
end
it "should deserialize result data" do
YAML.expects(:load).with(@result)
@searcher.destroy(@request)
end
it "should return deserialized result data" do
@searcher.destroy(@request).should == true
end
it "should generate an error when result data specifies an error" do
@searcher.stubs(:network_delete).returns(RuntimeError.new("bogus").to_yaml)
lambda { @searcher.destroy(@request) }.should raise_error(RuntimeError)
end
end
describe "when doing a save" do
before :each do
@result = { :foo => 'bar'}.to_yaml
@searcher.stubs(:network_put).returns(@result) # neuter the network connection
@model.stubs(:from_yaml).returns(@instance)
@request = stub 'request', :instance => @instance
end
it "should save the model instance over the network" do
@searcher.expects(:network_put).returns(@result)
@searcher.save(@request)
end
it "should save the model instance using the named indirection" do
@searcher.expects(:network_put).with do |path, data|
path =~ %r{^#{@indirection.name.to_s}/} and
data == @instance.to_yaml
end.returns(@result)
@searcher.save(@request)
end
it "should deserialize result data to a Model instance" do
@model.expects(:from_yaml)
@searcher.save(@request)
end
it "should return the resulting deserialized Model instance" do
@searcher.save(@request).should == @instance
end
it "should return nil when deserialized model instance is nil" do
@model.stubs(:from_yaml).returns(nil)
@searcher.save(@request).should be_nil
end
it "should generate an error when result data deserializes improperly" do
@model.stubs(:from_yaml).raises(ArgumentError)
lambda { @searcher.save(@request) }.should raise_error(ArgumentError)
end
it "should generate an error when result data specifies an error" do
@searcher.stubs(:network_put).returns(RuntimeError.new("bogus").to_yaml)
lambda { @searcher.save(@request) }.should raise_error(RuntimeError)
end
end
end
|