summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/indirection.rb
blob: 455b5dc672405b3a5edb8afc819f2a572c823c10 (plain)
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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/indirector'

describe Puppet::Indirector::Indirection do
    before do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        @terminus = stub 'terminus', :has_most_recent? => false
        @indirection.stubs(:terminus).returns(@terminus)
        @instance = stub 'instance', :version => nil, :version= => nil, :name => "whatever"
        @name = :mything
    end
  
    it "should not attempt to set a timestamp if the terminus cannot find the instance" do
        @terminus.expects(:find).with(@name).returns(nil)
        proc { @indirection.find(@name) }.should_not raise_error
    end
  
    it "should handle lookups of a model instance by letting the appropriate terminus perform the lookup" do
        @terminus.expects(:find).with(@name).returns(@instance)
        @indirection.find(@name).should == @instance
    end

    it "should handle removing model instances from a terminus letting the appropriate terminus remove the instance" do
        @terminus.expects(:destroy).with(@name).returns(@instance)
        @indirection.destroy(@name).should == @instance
    end
  
    it "should handle searching for model instances by letting the appropriate terminus find the matching instances" do
        @terminus.expects(:search).with(@name).returns(@instance)
        @indirection.search(@name).should == @instance
    end
  
    it "should handle storing a model instance by letting the appropriate terminus store the instance" do
        @terminus.expects(:save).with(@instance).returns(@instance)
        @indirection.save(@instance).should == @instance
    end
  
    it "should handle version lookups by letting the appropriate terminus perform the lookup" do
        @terminus.expects(:version).with(@name).returns(5)
        @indirection.version(@name).should == 5
    end

    it "should add versions to found instances that do not already have them" do
        @terminus.expects(:find).with(@name).returns(@instance)
        time = mock 'time'
        time.expects(:utc).returns(:mystamp)
        Time.expects(:now).returns(time)
        @instance.expects(:version=).with(:mystamp)
        @indirection.find(@name)
    end

    it "should add versions to saved instances that do not already have them" do
        time = mock 'time'
        time.expects(:utc).returns(:mystamp)
        Time.expects(:now).returns(time)
        @instance.expects(:version=).with(:mystamp)
        @terminus.stubs(:save)
        @indirection.save(@instance)
    end

    # We've already tested this, basically, but...
    it "should use the current time in UTC for versions" do
        @instance.expects(:version=).with do |time|
            time.utc?
        end
        @terminus.stubs(:save)
        @indirection.save(@instance)
    end

    after do
        @indirection.delete
        Puppet::Indirector::Indirection.clear_cache
    end
end

describe Puppet::Indirector::Indirection, " when initializing" do
    it "should keep a reference to the indirecting model" do
        model = mock 'model'
        @indirection = Puppet::Indirector::Indirection.new(model, :myind)
        @indirection.model.should equal(model)
    end

    it "should set the name" do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :myind)
        @indirection.name.should == :myind
    end

    it "should require indirections to have unique names" do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        proc { Puppet::Indirector::Indirection.new(:test) }.should raise_error(ArgumentError)
    end

    after do
        @indirection.delete if defined? @indirection
    end
end

describe Puppet::Indirector::Indirection, " when managing indirection instances" do
    it "should allow an indirection to be retrieved by name" do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        Puppet::Indirector::Indirection.instance(:test).should equal(@indirection)
    end

    it "should return nil when the named indirection has not been created" do
        Puppet::Indirector::Indirection.instance(:test).should be_nil
    end

    after do
        @indirection.delete if defined? @indirection
    end
end

describe Puppet::Indirector::Indirection, " when specifying terminus types" do
    before do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        @terminus = mock 'terminus'
        @terminus_class = stub 'terminus class', :new => @terminus
    end

    it "should allow specification of a terminus type" do
        @indirection.should respond_to(:terminus_class=)
    end

    it "should fail to redirect if no terminus type has been specified" do
        proc { @indirection.find("blah") }.should raise_error(Puppet::DevError)
    end

    it "should fail when the terminus class name is an empty string" do
        proc { @indirection.terminus_class = "" }.should raise_error(ArgumentError)
    end

    it "should fail when the terminus class name is nil" do
        proc { @indirection.terminus_class = nil }.should raise_error(ArgumentError)
    end

    it "should fail when the specified terminus class cannot be found" do
        Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil)
        proc { @indirection.terminus_class = :foo }.should raise_error(ArgumentError)
    end

    it "should select the specified terminus class if a terminus class name is provided" do
        Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(@terminus_class)
        @indirection.terminus(:foo).should equal(@terminus)
    end

    it "should use the configured terminus class if no terminus name is specified" do
        Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class)
        @indirection.terminus_class = :foo
        @indirection.terminus().should equal(@terminus)
    end

    after do
        @indirection.delete if defined? @indirection
    end
end

describe Puppet::Indirector::Indirection, " when managing terminus instances" do
    before do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        @terminus = mock 'terminus'
        @terminus_class = mock 'terminus class'
        Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class)
    end

    it "should create an instance of the chosen terminus class" do
        @terminus_class.stubs(:new).returns(@terminus)
        @indirection.terminus(:foo).should equal(@terminus)
    end

    it "should allow the clearance of cached terminus instances" do
        terminus1 = mock 'terminus1'
        terminus2 = mock 'terminus2'
        @terminus_class.stubs(:new).returns(terminus1, terminus2, ArgumentError)
        @indirection.terminus(:foo).should equal(terminus1)
        @indirection.class.clear_cache
        @indirection.terminus(:foo).should equal(terminus2)
    end

    # Make sure it caches the terminus.
    it "should return the same terminus instance each time for a given name" do
        @terminus_class.stubs(:new).returns(@terminus)
        @indirection.terminus(:foo).should equal(@terminus)
        @indirection.terminus(:foo).should equal(@terminus)
    end

    it "should not create a terminus instance until one is actually needed" do
        Puppet::Indirector.expects(:terminus).never
        indirection = Puppet::Indirector::Indirection.new(mock('model'), :lazytest)
    end

    after do
        @indirection.delete
        Puppet::Indirector::Indirection.clear_cache
    end
end

describe Puppet::Indirector::Indirection, " when deciding whether to cache" do
    before do
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        @terminus = mock 'terminus'
        @terminus_class = mock 'terminus class'
        @terminus_class.stubs(:new).returns(@terminus)
        Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :foo).returns(@terminus_class)
        @indirection.terminus_class = :foo
    end

    it "should provide a method for setting the cache terminus class" do
        @indirection.should respond_to(:cache_class=)
    end

    it "should fail to cache if no cache type has been specified" do
        proc { @indirection.cache }.should raise_error(Puppet::DevError)
    end

    it "should fail to set the cache class when the cache class name is an empty string" do
        proc { @indirection.cache_class = "" }.should raise_error(ArgumentError)
    end

    it "should fail to set the cache class when the cache class name is nil" do
        proc { @indirection.cache_class = nil }.should raise_error(ArgumentError)
    end

    it "should fail to set the cache class when the specified cache class cannot be found" do
        Puppet::Indirector::Terminus.expects(:terminus_class).with(:test, :foo).returns(nil)
        proc { @indirection.cache_class = :foo }.should raise_error(ArgumentError)
    end

    after do
        @indirection.delete
        Puppet::Indirector::Indirection.clear_cache
    end
end

module IndirectionCaching
    def setup
        Puppet.settings.stubs(:value).with("test_terminus").returns("test_terminus")
        @terminus_class = mock 'terminus_class'
        @terminus = mock 'terminus'
        @terminus_class.stubs(:new).returns(@terminus)
        @cache = mock 'cache'
        @cache_class = mock 'cache_class'
        Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :cache_terminus).returns(@cache_class)
        Puppet::Indirector::Terminus.stubs(:terminus_class).with(:test, :test_terminus).returns(@terminus_class)
        @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test)
        @indirection.terminus_class = :test_terminus
    end

    def teardown
        @indirection.delete
        Puppet::Indirector::Indirection.clear_cache
    end
end

describe Puppet::Indirector::Indirection, " when managing the cache terminus" do
    include IndirectionCaching

    it "should not create a cache terminus at initialization" do
        # This is weird, because all of the code is in the setup.  If we got
        # new() called on the cache class, we'd get an exception here.
    end

    it "should reuse the cache terminus" do
        @cache_class.expects(:new).returns(@cache)
        Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus")
        @indirection.cache_class = :cache_terminus
        @indirection.cache.should equal(@cache)
        @indirection.cache.should equal(@cache)
    end

    it "should remove the cache terminus when all other terminus instances are cleared" do
        cache2 = mock 'cache2'
        @cache_class.stubs(:new).returns(@cache, cache2)
        @indirection.cache_class = :cache_terminus
        @indirection.cache.should equal(@cache)
        @indirection.clear_cache
        @indirection.cache.should equal(cache2)
    end
end

describe Puppet::Indirector::Indirection, " when saving and using a cache" do
    include IndirectionCaching

    before do
        @indirection.cache_class = :cache_terminus
        @cache_class.expects(:new).returns(@cache)
        @name = "testing"
        @instance = stub 'instance', :version => 5, :name => @name
    end

    it "should not update the cache or terminus if the new object is not different" do
        @cache.expects(:has_most_recent?).with(@name, 5).returns(true)
        @indirection.save(@instance)
    end

    it "should update the original and the cache if the cached object is different" do
        @cache.expects(:has_most_recent?).with(@name, 5).returns(false)
        @terminus.expects(:save).with(@instance)
        @cache.expects(:save).with(@instance)
        @indirection.save(@instance)
    end
end

describe Puppet::Indirector::Indirection, " when finding and using a cache" do
    include IndirectionCaching

    before do
        @indirection.cache_class = :cache_terminus
        @cache_class.expects(:new).returns(@cache)
    end

    it "should return the cached object if the cache is up to date" do
        cached = mock 'cached object'

        name = "myobject"

        @terminus.expects(:version).with(name).returns(1)
        @cache.expects(:has_most_recent?).with(name, 1).returns(true)

        @cache.expects(:find).with(name).returns(cached)

        @indirection.find(name).should equal(cached)
    end

    it "should return the original object if the cache is not up to date" do
        real = stub 'real object', :version => 1

        name = "myobject"

        @cache.stubs(:save)
        @cache.expects(:has_most_recent?).with(name, 1).returns(false)
        @terminus.expects(:version).with(name).returns(1)

        @terminus.expects(:find).with(name).returns(real)

        @indirection.find(name).should equal(real)
    end

    it "should cache any newly returned objects" do
        real = stub 'real object', :version => 1

        name = "myobject"

        @terminus.expects(:version).with(name).returns(1)
        @cache.expects(:has_most_recent?).with(name, 1).returns(false)

        @terminus.expects(:find).with(name).returns(real)
        @cache.expects(:save).with(real)

        @indirection.find(name).should equal(real)
    end
end