~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to spec/unit/network/format_handler.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../spec_helper'
 
4
 
 
5
require 'puppet/network/format_handler'
 
6
 
 
7
class FormatTester
 
8
    extend Puppet::Network::FormatHandler
 
9
end
 
10
 
 
11
describe Puppet::Network::FormatHandler do
 
12
    after do
 
13
        formats = Puppet::Network::FormatHandler.instance_variable_get("@formats")
 
14
        formats.each do |name, format|
 
15
            formats.delete(name) unless format.is_a?(Puppet::Network::Format)
 
16
        end
 
17
    end
 
18
 
 
19
    it "should be able to list supported formats" do
 
20
        FormatTester.should respond_to(:supported_formats)
 
21
    end
 
22
 
 
23
    it "should include all supported formats" do
 
24
        one = stub 'supported', :supported? => true, :name => :one, :weight => 1
 
25
        two = stub 'supported', :supported? => false, :name => :two, :weight => 1
 
26
        three = stub 'supported', :supported? => true, :name => :three, :weight => 1
 
27
        four = stub 'supported', :supported? => false, :name => :four, :weight => 1
 
28
        Puppet::Network::FormatHandler.stubs(:formats).returns [:one, :two, :three, :four]
 
29
        Puppet::Network::FormatHandler.stubs(:format).with(:one).returns one
 
30
        Puppet::Network::FormatHandler.stubs(:format).with(:two).returns two
 
31
        Puppet::Network::FormatHandler.stubs(:format).with(:three).returns three
 
32
        Puppet::Network::FormatHandler.stubs(:format).with(:four).returns four
 
33
        result = FormatTester.supported_formats
 
34
        result.length.should == 2
 
35
        result.should be_include(:one)
 
36
        result.should be_include(:three)
 
37
    end
 
38
 
 
39
    it "should return the supported formats in decreasing order of weight" do
 
40
        one = stub 'supported', :supported? => true, :name => :one, :weight => 1
 
41
        two = stub 'supported', :supported? => true, :name => :two, :weight => 6
 
42
        three = stub 'supported', :supported? => true, :name => :three, :weight => 2
 
43
        four = stub 'supported', :supported? => true, :name => :four, :weight => 8
 
44
        Puppet::Network::FormatHandler.stubs(:formats).returns [:one, :two, :three, :four]
 
45
        Puppet::Network::FormatHandler.stubs(:format).with(:one).returns one
 
46
        Puppet::Network::FormatHandler.stubs(:format).with(:two).returns two
 
47
        Puppet::Network::FormatHandler.stubs(:format).with(:three).returns three
 
48
        Puppet::Network::FormatHandler.stubs(:format).with(:four).returns four
 
49
        FormatTester.supported_formats.should == [:four, :two, :three, :one]
 
50
    end
 
51
 
 
52
    it "should always put the preferred serialization format first if it is supported" do
 
53
        one = stub 'supported', :supported? => true, :name => :one, :weight => 1
 
54
        two = stub 'supported', :supported? => true, :name => :two, :weight => 6
 
55
 
 
56
        Puppet.settings.expects(:value).with(:preferred_serialization_format).returns :one
 
57
        Puppet::Network::FormatHandler.stubs(:formats).returns [:one, :two]
 
58
        Puppet::Network::FormatHandler.stubs(:format).with(:one).returns one
 
59
        Puppet::Network::FormatHandler.stubs(:format).with(:two).returns two
 
60
        FormatTester.supported_formats.should == [:one, :two]
 
61
    end
 
62
 
 
63
    it "should return the first format as the default format" do
 
64
        FormatTester.expects(:supported_formats).returns [:one, :two]
 
65
        FormatTester.default_format.should == :one
 
66
    end
 
67
 
 
68
    it "should be able to use a protected format for better logging on errors" do
 
69
        Puppet::Network::FormatHandler.should respond_to(:protected_format)
 
70
    end
 
71
 
 
72
    it "should delegate all methods from the informative format to the specified format" do
 
73
        format = mock 'format'
 
74
        format.stubs(:name).returns(:myformat)
 
75
        Puppet::Network::FormatHandler.expects(:format).twice.with(:myformat).returns format
 
76
 
 
77
        format.expects(:render).with("foo").returns "yay"
 
78
        Puppet::Network::FormatHandler.protected_format(:myformat).render("foo").should == "yay"
 
79
    end
 
80
 
 
81
    it "should provide better logging if a failure is encountered when delegating from the informative format to the real format" do
 
82
        format = mock 'format'
 
83
        format.stubs(:name).returns(:myformat)
 
84
        Puppet::Network::FormatHandler.expects(:format).twice.with(:myformat).returns format
 
85
 
 
86
        format.expects(:render).with("foo").raises "foo"
 
87
        lambda { Puppet::Network::FormatHandler.protected_format(:myformat).render("foo") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
 
88
    end
 
89
 
 
90
    it "should raise an error if we couldn't find a format by name or mime-type" do
 
91
        Puppet::Network::FormatHandler.stubs(:format).with(:myformat).returns nil
 
92
        lambda { Puppet::Network::FormatHandler.protected_format(:myformat) }.should raise_error
 
93
    end
 
94
 
 
95
    describe "when using formats" do
 
96
        before do
 
97
            @format = mock 'format'
 
98
            @format.stubs(:supported?).returns true
 
99
            @format.stubs(:name).returns :my_format
 
100
            Puppet::Network::FormatHandler.stubs(:format).with(:my_format).returns @format
 
101
            Puppet::Network::FormatHandler.stubs(:mime).with("text/myformat").returns @format
 
102
            Puppet::Network::Format.stubs(:===).returns false
 
103
            Puppet::Network::Format.stubs(:===).with(@format).returns true
 
104
        end
 
105
 
 
106
        it "should be able to test whether a format is supported" do
 
107
            FormatTester.should respond_to(:support_format?)
 
108
        end
 
109
 
 
110
        it "should use the Format to determine whether a given format is supported" do
 
111
            @format.expects(:supported?).with(FormatTester)
 
112
            FormatTester.support_format?(:my_format)
 
113
        end
 
114
 
 
115
        it "should be able to convert from a given format" do
 
116
            FormatTester.should respond_to(:convert_from)
 
117
        end
 
118
 
 
119
        it "should call the format-specific converter when asked to convert from a given format" do
 
120
            @format.expects(:intern).with(FormatTester, "mydata")
 
121
            FormatTester.convert_from(:my_format, "mydata")
 
122
        end
 
123
 
 
124
        it "should call the format-specific converter when asked to convert from a given format by mime-type" do
 
125
            @format.expects(:intern).with(FormatTester, "mydata")
 
126
            FormatTester.convert_from("text/myformat", "mydata")
 
127
        end
 
128
 
 
129
        it "should call the format-specific converter when asked to convert from a given format by format instance" do
 
130
            @format.expects(:intern).with(FormatTester, "mydata")
 
131
            FormatTester.convert_from(@format, "mydata")
 
132
        end
 
133
 
 
134
        it "should raise a FormatError when an exception is encountered when converting from a format" do
 
135
            @format.expects(:intern).with(FormatTester, "mydata").raises "foo"
 
136
            lambda { FormatTester.convert_from(:my_format, "mydata") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
 
137
        end
 
138
 
 
139
        it "should be able to use a specific hook for converting into multiple instances" do
 
140
            @format.expects(:intern_multiple).with(FormatTester, "mydata")
 
141
 
 
142
            FormatTester.convert_from_multiple(:my_format, "mydata")
 
143
        end
 
144
 
 
145
        it "should raise a FormatError when an exception is encountered when converting multiple items from a format" do
 
146
            @format.expects(:intern_multiple).with(FormatTester, "mydata").raises "foo"
 
147
            lambda { FormatTester.convert_from_multiple(:my_format, "mydata") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
 
148
        end
 
149
 
 
150
        it "should be able to use a specific hook for rendering multiple instances" do
 
151
            @format.expects(:render_multiple).with("mydata")
 
152
 
 
153
            FormatTester.render_multiple(:my_format, "mydata")
 
154
        end
 
155
 
 
156
        it "should raise a FormatError when an exception is encountered when rendering multiple items into a format" do
 
157
            @format.expects(:render_multiple).with("mydata").raises "foo"
 
158
            lambda { FormatTester.render_multiple(:my_format, "mydata") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
 
159
        end
 
160
    end
 
161
 
 
162
    describe "when managing formats" do
 
163
        it "should have a method for defining a new format" do
 
164
            Puppet::Network::FormatHandler.should respond_to(:create)
 
165
        end
 
166
 
 
167
        it "should create a format instance when asked" do
 
168
            format = stub 'format', :name => :foo
 
169
            Puppet::Network::Format.expects(:new).with(:foo).returns format
 
170
            Puppet::Network::FormatHandler.create(:foo)
 
171
        end
 
172
 
 
173
        it "should instance_eval any block provided when creating a format" do
 
174
            format = stub 'format', :name => :instance_eval
 
175
            format.expects(:yayness)
 
176
            Puppet::Network::Format.expects(:new).returns format
 
177
            Puppet::Network::FormatHandler.create(:instance_eval) do
 
178
                yayness
 
179
            end
 
180
        end
 
181
 
 
182
        it "should be able to retrieve a format by name" do
 
183
            format = Puppet::Network::FormatHandler.create(:by_name)
 
184
            Puppet::Network::FormatHandler.format(:by_name).should equal(format)
 
185
        end
 
186
 
 
187
        it "should be able to retrieve formats by name irrespective of case and class" do
 
188
            format = Puppet::Network::FormatHandler.create(:by_name)
 
189
            Puppet::Network::FormatHandler.format(:By_Name).should equal(format)
 
190
        end
 
191
 
 
192
        it "should be able to retrieve a format by mime type" do
 
193
            format = Puppet::Network::FormatHandler.create(:by_name, :mime => "foo/bar")
 
194
            Puppet::Network::FormatHandler.mime("foo/bar").should equal(format)
 
195
        end
 
196
 
 
197
        it "should be able to retrieve a format by mime type irrespective of case" do
 
198
            format = Puppet::Network::FormatHandler.create(:by_name, :mime => "foo/bar")
 
199
            Puppet::Network::FormatHandler.mime("Foo/Bar").should equal(format)
 
200
        end
 
201
 
 
202
        it "should be able to return all formats" do
 
203
            one = stub 'one', :name => :one
 
204
            two = stub 'two', :name => :two
 
205
            Puppet::Network::Format.expects(:new).with(:one).returns(one)
 
206
            Puppet::Network::Format.expects(:new).with(:two).returns(two)
 
207
 
 
208
            Puppet::Network::FormatHandler.create(:one)
 
209
            Puppet::Network::FormatHandler.create(:two)
 
210
 
 
211
            list = Puppet::Network::FormatHandler.formats
 
212
            list.should be_include(:one)
 
213
            list.should be_include(:two)
 
214
        end
 
215
    end
 
216
 
 
217
    describe "when an instance" do
 
218
        it "should be able to test whether a format is supported" do
 
219
            FormatTester.new.should respond_to(:support_format?)
 
220
        end
 
221
 
 
222
        it "should be able to convert to a given format" do
 
223
            FormatTester.new.should respond_to(:render)
 
224
        end
 
225
 
 
226
        it "should be able to get a format mime-type" do
 
227
            FormatTester.new.should respond_to(:mime)
 
228
        end
 
229
 
 
230
        it "should raise a FormatError when a rendering error is encountered" do
 
231
            format = stub 'rendering format', :supported? => true, :name => :foo
 
232
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
233
 
 
234
            tester = FormatTester.new
 
235
            format.expects(:render).with(tester).raises "eh"
 
236
 
 
237
            lambda { tester.render(:foo) }.should raise_error(Puppet::Network::FormatHandler::FormatError)
 
238
        end
 
239
 
 
240
        it "should call the format-specific converter when asked to convert to a given format" do
 
241
            format = stub 'rendering format', :supported? => true, :name => :foo
 
242
 
 
243
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
244
 
 
245
            tester = FormatTester.new
 
246
            format.expects(:render).with(tester).returns "foo"
 
247
 
 
248
            tester.render(:foo).should == "foo"
 
249
        end
 
250
 
 
251
        it "should call the format-specific converter when asked to convert to a given format by mime-type" do
 
252
            format = stub 'rendering format', :supported? => true, :name => :foo
 
253
            Puppet::Network::FormatHandler.stubs(:mime).with("text/foo").returns format
 
254
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
255
 
 
256
            tester = FormatTester.new
 
257
            format.expects(:render).with(tester).returns "foo"
 
258
 
 
259
            tester.render("text/foo").should == "foo"
 
260
        end
 
261
 
 
262
        it "should call the format converter when asked to convert to a given format instance" do
 
263
            format = stub 'rendering format', :supported? => true, :name => :foo
 
264
            Puppet::Network::Format.stubs(:===).with(format).returns(true)
 
265
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
266
 
 
267
            tester = FormatTester.new
 
268
            format.expects(:render).with(tester).returns "foo"
 
269
 
 
270
            tester.render(format).should == "foo"
 
271
        end
 
272
 
 
273
        it "should render to the default format if no format is provided when rendering" do
 
274
            format = stub 'rendering format', :supported? => true, :name => :foo
 
275
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
276
 
 
277
            FormatTester.expects(:default_format).returns :foo
 
278
            tester = FormatTester.new
 
279
 
 
280
            format.expects(:render).with(tester)
 
281
            tester.render
 
282
        end
 
283
 
 
284
        it "should call the format-specific converter when asked for the mime-type of a given format" do
 
285
            format = stub 'rendering format', :supported? => true, :name => :foo
 
286
 
 
287
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
288
 
 
289
            tester = FormatTester.new
 
290
            format.expects(:mime).returns "text/foo"
 
291
 
 
292
            tester.mime(:foo).should == "text/foo"
 
293
        end
 
294
 
 
295
        it "should return the default format mime-type if no format is provided" do
 
296
            format = stub 'rendering format', :supported? => true, :name => :foo
 
297
            Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
 
298
 
 
299
            FormatTester.expects(:default_format).returns :foo
 
300
            tester = FormatTester.new
 
301
 
 
302
            format.expects(:mime).returns "text/foo"
 
303
            tester.mime.should == "text/foo"
 
304
        end
 
305
    end
 
306
end