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

« back to all changes in this revision

Viewing changes to spec/unit/application/puppetdoc.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/application/puppetdoc'
 
6
 
 
7
describe "puppetdoc" do
 
8
    before :each do
 
9
        @puppetdoc = Puppet::Application[:puppetdoc]
 
10
        @puppetdoc.stubs(:puts)
 
11
        @puppetdoc.run_preinit
 
12
        Puppet::Util::Log.stubs(:newdestination)
 
13
        Puppet::Util::Log.stubs(:level=)
 
14
    end
 
15
 
 
16
    it "should ask Puppet::Application to not parse Puppet configuration file" do
 
17
        @puppetdoc.should_parse_config?.should be_false
 
18
    end
 
19
 
 
20
    it "should declare a other command" do
 
21
        @puppetdoc.should respond_to(:other)
 
22
    end
 
23
 
 
24
    it "should declare a rdoc command" do
 
25
        @puppetdoc.should respond_to(:rdoc)
 
26
    end
 
27
 
 
28
    it "should declare a trac command" do
 
29
        @puppetdoc.should respond_to(:trac)
 
30
    end
 
31
 
 
32
    it "should declare a fallback for unknown options" do
 
33
        @puppetdoc.should respond_to(:handle_unknown)
 
34
    end
 
35
 
 
36
    it "should declare a preinit block" do
 
37
        @puppetdoc.should respond_to(:run_preinit)
 
38
    end
 
39
 
 
40
    describe "in preinit" do
 
41
        it "should set references to []" do
 
42
            @puppetdoc.run_preinit
 
43
 
 
44
            @puppetdoc.options[:references].should == []
 
45
        end
 
46
 
 
47
        it "should init mode to text" do
 
48
            @puppetdoc.run_preinit
 
49
 
 
50
            @puppetdoc.options[:mode].should == :text
 
51
        end
 
52
 
 
53
        it "should init format to to_rest" do
 
54
            @puppetdoc.run_preinit
 
55
 
 
56
            @puppetdoc.options[:format].should == :to_rest
 
57
        end
 
58
    end
 
59
 
 
60
    describe "when handling options" do
 
61
        [:all, :outputdir, :verbose, :debug].each do |option|
 
62
            it "should declare handle_#{option} method" do
 
63
                @puppetdoc.should respond_to("handle_#{option}".to_sym)
 
64
            end
 
65
 
 
66
            it "should store argument value when calling handle_#{option}" do
 
67
                @puppetdoc.options.expects(:[]=).with(option, 'arg')
 
68
                @puppetdoc.send("handle_#{option}".to_sym, 'arg')
 
69
            end
 
70
        end
 
71
 
 
72
        it "should store the format if valid" do
 
73
            Puppet::Util::Reference.stubs(:method_defined?).with('to_format').returns(true)
 
74
 
 
75
            @puppetdoc.options.expects(:[]=).with(:format, 'to_format')
 
76
 
 
77
            @puppetdoc.handle_format('format')
 
78
        end
 
79
 
 
80
        it "should raise an error if the format is not valid" do
 
81
            Puppet::Util::Reference.stubs(:method_defined?).with('to_format').returns(false)
 
82
            lambda { @puppetdoc.handle_format('format') }
 
83
        end
 
84
 
 
85
        it "should store the mode if valid" do
 
86
            Puppet::Util::Reference.stubs(:modes).returns(stub('mode', :include? => true))
 
87
 
 
88
            @puppetdoc.options.expects(:[]=).with(:mode, :mode)
 
89
 
 
90
            @puppetdoc.handle_mode('mode')
 
91
        end
 
92
 
 
93
        it "should store the mode if :rdoc" do
 
94
            Puppet::Util::Reference.modes.stubs(:include?).with('rdoc').returns(false)
 
95
 
 
96
            @puppetdoc.options.expects(:[]=).with(:mode, :rdoc)
 
97
 
 
98
            @puppetdoc.handle_mode('rdoc')
 
99
        end
 
100
 
 
101
        it "should raise an error if the mode is not valid" do
 
102
            Puppet::Util::Reference.modes.stubs(:include?).with('unknown').returns(false)
 
103
            lambda { @puppetdoc.handle_mode('unknown') }
 
104
        end
 
105
 
 
106
        it "should list all references on list and exit" do
 
107
            reference = stubs 'reference'
 
108
            ref = stubs 'ref'
 
109
            Puppet::Util::Reference.stubs(:references).returns([reference])
 
110
 
 
111
            Puppet::Util::Reference.expects(:reference).with(reference).returns(ref)
 
112
            ref.expects(:doc)
 
113
            @puppetdoc.expects(:exit)
 
114
 
 
115
            @puppetdoc.handle_list(nil)
 
116
        end
 
117
 
 
118
        it "should add reference to references list with --reference" do
 
119
            @puppetdoc.options[:references] = [:ref1]
 
120
 
 
121
            @puppetdoc.handle_reference('ref2')
 
122
 
 
123
            @puppetdoc.options[:references].should == [:ref1,:ref2]
 
124
        end
 
125
    end
 
126
 
 
127
    describe "during setup" do
 
128
 
 
129
        before :each do
 
130
            Puppet::Log.stubs(:newdestination)
 
131
            ARGV.stubs(:size).returns(0)
 
132
        end
 
133
 
 
134
        it "should default to rdoc mode if there are command line arguments" do
 
135
            ARGV.stubs(:size).returns(1)
 
136
            @puppetdoc.stubs(:setup_rdoc)
 
137
 
 
138
            @puppetdoc.options.expects(:[]=).with(:mode,:rdoc)
 
139
 
 
140
            @puppetdoc.run_setup
 
141
        end
 
142
 
 
143
        it "should call setup_rdoc in rdoc mode" do
 
144
            @puppetdoc.options.stubs(:[]).with(:mode).returns(:rdoc)
 
145
 
 
146
            @puppetdoc.expects(:setup_rdoc)
 
147
 
 
148
            @puppetdoc.run_setup
 
149
        end
 
150
 
 
151
        it "should call setup_reference if not rdoc" do
 
152
            @puppetdoc.options.stubs(:[]).with(:mode).returns(:test)
 
153
 
 
154
            @puppetdoc.expects(:setup_reference)
 
155
 
 
156
            @puppetdoc.run_setup
 
157
        end
 
158
 
 
159
        describe "in non-rdoc mode" do
 
160
 
 
161
            it "should get all non-dynamic reference if --all" do
 
162
                @puppetdoc.options.stubs(:[]).with(:all).returns(true)
 
163
                @puppetdoc.options.stubs(:[]).with(:references).returns([])
 
164
                static = stub 'static', :dynamic? => false
 
165
                dynamic = stub 'dynamic', :dynamic? => true
 
166
                Reference.stubs(:reference).with(:static).returns(static)
 
167
                Reference.stubs(:reference).with(:dynamic).returns(dynamic)
 
168
                Reference.stubs(:references).returns([:static,:dynamic])
 
169
 
 
170
                @puppetdoc.options.stubs(:[]=).with(:references, [:static])
 
171
 
 
172
                @puppetdoc.setup_reference
 
173
            end
 
174
 
 
175
            it "should default to :type if no references" do
 
176
                @puppetdoc.options.stubs(:[]).with(:all).returns(false)
 
177
                array = stub 'array', :empty? => true
 
178
                @puppetdoc.options.stubs(:[]).with(:references).returns(array)
 
179
 
 
180
                array.expects(:<<).with(:type)
 
181
 
 
182
                @puppetdoc.setup_reference
 
183
            end
 
184
 
 
185
        end
 
186
 
 
187
        describe "in rdoc mode" do
 
188
 
 
189
            before :each do
 
190
                @puppetdoc.options.stubs(:[]).returns(false)
 
191
                Puppet.stubs(:[]=).with(:name, "puppetmasterd")
 
192
                Puppet.stubs(:parse_config)
 
193
                Puppet::Util::Log.stubs(:level=)
 
194
                Puppet::Util::Log.stubs(:newdestination)
 
195
            end
 
196
 
 
197
            describe "when there are unknown args" do
 
198
 
 
199
                it "should expand --modulepath if any" do
 
200
                    @puppetdoc.unknown_args = [ { :opt => "--modulepath", :arg => "path" } ]
 
201
                    Puppet.settings.stubs(:handlearg)
 
202
 
 
203
                    File.expects(:expand_path).with("path")
 
204
 
 
205
                    @puppetdoc.setup_rdoc
 
206
                end
 
207
 
 
208
                it "should expand --manifestdir if any" do
 
209
                    @puppetdoc.unknown_args = [ { :opt => "--manifestdir", :arg => "path" } ]
 
210
                    Puppet.settings.stubs(:handlearg)
 
211
 
 
212
                    File.expects(:expand_path).with("path")
 
213
 
 
214
                    @puppetdoc.setup_rdoc
 
215
                end
 
216
 
 
217
                it "should give them to Puppet.settings" do
 
218
                    @puppetdoc.unknown_args = [ { :opt => :option, :arg => :argument } ]
 
219
                    Puppet.settings.expects(:handlearg).with(:option,:argument)
 
220
 
 
221
                    @puppetdoc.setup_rdoc
 
222
                end
 
223
            end
 
224
 
 
225
            it "should pretend to be puppetmasterd" do
 
226
                Puppet.expects(:[]=).with(:name, "puppetmasterd")
 
227
 
 
228
                @puppetdoc.setup_rdoc
 
229
            end
 
230
 
 
231
            it "should parse puppet configuration" do
 
232
                Puppet.expects(:parse_config)
 
233
 
 
234
                @puppetdoc.setup_rdoc
 
235
            end
 
236
 
 
237
            it "should set log level to debug if --debug" do
 
238
                @puppetdoc.options.stubs(:[]).with(:debug).returns(true)
 
239
                Puppet::Util::Log.expects(:level=).with(:debug)
 
240
 
 
241
                @puppetdoc.setup_rdoc
 
242
            end
 
243
 
 
244
            it "should set log level to info if --verbose" do
 
245
                @puppetdoc.options.stubs(:[]).with(:verbose).returns(true)
 
246
                Puppet::Util::Log.expects(:level=).with(:info)
 
247
 
 
248
                @puppetdoc.setup_rdoc
 
249
            end
 
250
 
 
251
            it "should set log destination to console if --verbose" do
 
252
                @puppetdoc.options.stubs(:[]).with(:verbose).returns(true)
 
253
 
 
254
                Puppet::Util::Log.expects(:newdestination).with(:console)
 
255
 
 
256
                @puppetdoc.setup_rdoc
 
257
            end
 
258
 
 
259
            it "should set log destination to console if --debug" do
 
260
                @puppetdoc.options.stubs(:[]).with(:debug).returns(true)
 
261
 
 
262
                Puppet::Util::Log.expects(:newdestination).with(:console)
 
263
 
 
264
                @puppetdoc.setup_rdoc
 
265
            end
 
266
 
 
267
        end
 
268
 
 
269
    end
 
270
 
 
271
    describe "when running" do
 
272
        before :each do
 
273
        end
 
274
 
 
275
        describe "in trac mode" do
 
276
            it "should call trac for each reference" do
 
277
                ref = stub 'ref'
 
278
                Puppet::Util::Reference.stubs(:reference).with(:ref).returns(ref)
 
279
                @puppetdoc.options.stubs(:[]).with(:references).returns([:ref])
 
280
                @puppetdoc.options.stubs(:[]).with(:mode).returns(:trac)
 
281
 
 
282
                ref.expects(:trac)
 
283
 
 
284
                @puppetdoc.trac
 
285
            end
 
286
        end
 
287
 
 
288
        describe "in rdoc mode" do
 
289
            before :each do
 
290
                @puppetdoc.manifest = false
 
291
                Puppet.stubs(:info)
 
292
                Puppet.stubs(:[]).with(:trace).returns(false)
 
293
                Puppet.stubs(:[]).with(:modulepath).returns('modules')
 
294
                Puppet.stubs(:[]).with(:manifestdir).returns('manifests')
 
295
                @puppetdoc.options.stubs(:[]).with(:all).returns(false)
 
296
                @puppetdoc.options.stubs(:[]).with(:outputdir).returns('doc')
 
297
                Puppet.settings.stubs(:[]=).with(:document_all, false)
 
298
                Puppet.settings.stubs(:setdefaults)
 
299
                Puppet::Util::RDoc.stubs(:rdoc)
 
300
                @puppetdoc.stubs(:exit)
 
301
                File.stubs(:expand_path).with('modules').returns('modules')
 
302
                File.stubs(:expand_path).with('manifests').returns('manifests')
 
303
                @old = ARGV.dup
 
304
                ARGV.clear
 
305
            end
 
306
 
 
307
            after :each do
 
308
                ARGV << @old
 
309
            end
 
310
 
 
311
            it "should set document_all on --all" do
 
312
                @puppetdoc.options.expects(:[]).with(:all).returns(true)
 
313
                Puppet.settings.expects(:[]=).with(:document_all, true)
 
314
 
 
315
                @puppetdoc.rdoc
 
316
            end
 
317
 
 
318
            it "should call Puppet::Util::RDoc.rdoc in full mode" do
 
319
                Puppet::Util::RDoc.expects(:rdoc).with('doc', ['modules','manifests'])
 
320
                @puppetdoc.rdoc
 
321
            end
 
322
 
 
323
            it "should call Puppet::Util::RDoc.rdoc in full mode with outputdir set to doc if no --outputdir" do
 
324
                @puppetdoc.options.expects(:[]).with(:outputdir).returns(false)
 
325
                Puppet::Util::RDoc.expects(:rdoc).with('doc', ['modules','manifests'])
 
326
                @puppetdoc.rdoc
 
327
            end
 
328
 
 
329
            it "should call Puppet::Util::RDoc.manifestdoc in manifest mode" do
 
330
                @puppetdoc.manifest = true
 
331
                Puppet::Util::RDoc.expects(:manifestdoc)
 
332
                @puppetdoc.rdoc
 
333
            end
 
334
        end
 
335
 
 
336
        describe "in the other modes" do
 
337
            it "should get reference in given format" do
 
338
                reference = stub 'reference'
 
339
                @puppetdoc.options.stubs(:[]).with(:mode).returns(:none)
 
340
                @puppetdoc.options.stubs(:[]).with(:references).returns([:ref])
 
341
                Puppet::Util::Reference.expects(:reference).with(:ref).returns(reference)
 
342
                @puppetdoc.options.stubs(:[]).with(:format).returns(:format)
 
343
                @puppetdoc.stubs(:exit)
 
344
 
 
345
                reference.expects(:send).with { |format,contents| format == :format }.returns('doc')
 
346
                @puppetdoc.other
 
347
            end
 
348
        end
 
349
 
 
350
    end
 
351
end