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

« back to all changes in this revision

Viewing changes to spec/unit/application/puppet.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/puppet'
 
6
 
 
7
describe "Puppet" do
 
8
    before :each do
 
9
        @puppet = Puppet::Application[:puppet]
 
10
        Puppet::Util::Log.stubs(:newdestination)
 
11
        Puppet::Util::Log.stubs(:level=)
 
12
    end
 
13
 
 
14
    [:debug,:loadclasses,:verbose,:use_nodes,:detailed_exitcodes].each do |option|
 
15
        it "should declare handle_#{option} method" do
 
16
            @puppet.should respond_to("handle_#{option}".to_sym)
 
17
        end
 
18
 
 
19
        it "should store argument value when calling handle_#{option}" do
 
20
            @puppet.options.expects(:[]=).with(option, 'arg')
 
21
            @puppet.send("handle_#{option}".to_sym, 'arg')
 
22
        end
 
23
    end
 
24
 
 
25
    it "should set the code to the provided code when :execute is used" do
 
26
        @puppet.options.expects(:[]=).with(:code, 'arg')
 
27
        @puppet.send("handle_execute".to_sym, 'arg')
 
28
    end
 
29
 
 
30
    it "should ask Puppet::Application to parse Puppet configuration file" do
 
31
        @puppet.should_parse_config?.should be_true
 
32
    end
 
33
 
 
34
    describe "when applying options" do
 
35
 
 
36
        it "should set the log destination with --logdest" do
 
37
            Puppet::Log.expects(:newdestination).with("console")
 
38
 
 
39
            @puppet.handle_logdest("console")
 
40
        end
 
41
 
 
42
        it "should put the logset options to true" do
 
43
            @puppet.options.expects(:[]=).with(:logset,true)
 
44
 
 
45
            @puppet.handle_logdest("console")
 
46
        end
 
47
    end
 
48
 
 
49
    describe "during setup" do
 
50
 
 
51
        before :each do
 
52
            Puppet::Log.stubs(:newdestination)
 
53
            Puppet.stubs(:trap)
 
54
            Puppet::Log.stubs(:level=)
 
55
            Puppet.stubs(:parse_config)
 
56
            Puppet::Network::Client.dipper.stubs(:new)
 
57
            STDIN.stubs(:read)
 
58
 
 
59
            @puppet.options.stubs(:[]).with(any_parameters)
 
60
        end
 
61
 
 
62
        it "should set show_diff on --noop" do
 
63
            Puppet.stubs(:[]=)
 
64
            Puppet.stubs(:[]).with(:config)
 
65
            Puppet.stubs(:[]).with(:noop).returns(true)
 
66
 
 
67
            Puppet.expects(:[]=).with(:show_diff, true)
 
68
 
 
69
            @puppet.run_setup
 
70
        end
 
71
 
 
72
        it "should set console as the log destination if logdest option wasn't provided" do
 
73
            Puppet::Log.expects(:newdestination).with(:console)
 
74
 
 
75
            @puppet.run_setup
 
76
        end
 
77
 
 
78
        it "should set INT trap" do
 
79
            @puppet.expects(:trap).with(:INT)
 
80
 
 
81
            @puppet.run_setup
 
82
        end
 
83
 
 
84
        it "should set log level to debug if --debug was passed" do
 
85
            @puppet.options.stubs(:[]).with(:debug).returns(true)
 
86
 
 
87
            Puppet::Log.expects(:level=).with(:debug)
 
88
 
 
89
            @puppet.run_setup
 
90
        end
 
91
 
 
92
        it "should set log level to info if --verbose was passed" do
 
93
            @puppet.options.stubs(:[]).with(:verbose).returns(true)
 
94
 
 
95
            Puppet::Log.expects(:level=).with(:info)
 
96
 
 
97
            @puppet.run_setup
 
98
        end
 
99
 
 
100
        it "should print puppet config if asked to in Puppet config" do
 
101
            @puppet.stubs(:exit)
 
102
            Puppet.settings.stubs(:print_configs?).returns(true)
 
103
 
 
104
            Puppet.settings.expects(:print_configs)
 
105
 
 
106
            @puppet.run_setup
 
107
        end
 
108
 
 
109
        it "should exit after printing puppet config if asked to in Puppet config" do
 
110
            Puppet.settings.stubs(:print_configs?).returns(true)
 
111
 
 
112
            lambda { @puppet.run_setup }.should raise_error(SystemExit)
 
113
        end
 
114
 
 
115
    end
 
116
 
 
117
    describe "when executing" do
 
118
 
 
119
        it "should dispatch to parseonly if parseonly is set" do
 
120
            @puppet.stubs(:options).returns({})
 
121
            Puppet.stubs(:[]).with(:parseonly).returns(true)
 
122
 
 
123
            @puppet.get_command.should == :parseonly
 
124
        end
 
125
 
 
126
        it "should dispatch to 'apply' if it was called with 'apply'" do
 
127
            @puppet.options[:catalog] = "foo"
 
128
 
 
129
            @puppet.get_command.should == :apply
 
130
        end
 
131
 
 
132
        it "should dispatch to main if parseonly is not set" do
 
133
            @puppet.stubs(:options).returns({})
 
134
            Puppet.stubs(:[]).with(:parseonly).returns(false)
 
135
 
 
136
            @puppet.get_command.should == :main
 
137
        end
 
138
 
 
139
        describe "the parseonly command" do
 
140
            before :each do
 
141
                Puppet.stubs(:[]).with(:environment)
 
142
                Puppet.stubs(:[]).with(:manifest).returns("site.pp")
 
143
                @interpreter = stub_everything
 
144
                Puppet.stubs(:err)
 
145
                @puppet.stubs(:exit)
 
146
                @puppet.options.stubs(:[]).with(:code).returns "some code"
 
147
                Puppet::Parser::Interpreter.stubs(:new).returns(@interpreter)
 
148
            end
 
149
 
 
150
            it "should delegate to the Puppet Parser" do
 
151
 
 
152
                @interpreter.expects(:parser)
 
153
 
 
154
                @puppet.parseonly
 
155
            end
 
156
 
 
157
            it "should exit with exit code 0 if no error" do
 
158
                @puppet.expects(:exit).with(0)
 
159
 
 
160
                @puppet.parseonly
 
161
            end
 
162
 
 
163
            it "should exit with exit code 1 if error" do
 
164
                @interpreter.stubs(:parser).raises(Puppet::ParseError)
 
165
 
 
166
                @puppet.expects(:exit).with(1)
 
167
 
 
168
                @puppet.parseonly
 
169
            end
 
170
 
 
171
        end
 
172
 
 
173
        describe "the main command" do
 
174
            before :each do
 
175
                Puppet.stubs(:[])
 
176
                Puppet.stubs(:[]).with(:trace).returns(true)
 
177
 
 
178
                @puppet.options.stubs(:[])
 
179
 
 
180
                @facts = stub_everything 'facts'
 
181
                Puppet::Node::Facts.stubs(:find).returns(@facts)
 
182
 
 
183
                @node = stub_everything 'node'
 
184
                Puppet::Node.stubs(:find).returns(@node)
 
185
 
 
186
                @catalog = stub_everything 'catalog'
 
187
                @catalog.stubs(:to_ral).returns(@catalog)
 
188
                Puppet::Resource::Catalog.stubs(:find).returns(@catalog)
 
189
 
 
190
                STDIN.stubs(:read)
 
191
 
 
192
                @transaction = stub_everything 'transaction'
 
193
                @catalog.stubs(:apply).returns(@transaction)
 
194
 
 
195
                @puppet.stubs(:exit)
 
196
            end
 
197
 
 
198
            it "should set the code to run from --code" do
 
199
                @puppet.options.stubs(:[]).with(:code).returns("code to run")
 
200
                Puppet.expects(:[]=).with(:code,"code to run")
 
201
 
 
202
                @puppet.main
 
203
            end
 
204
 
 
205
            it "should set the code to run from STDIN if no arguments" do
 
206
                ARGV.stubs(:length).returns(0)
 
207
                STDIN.stubs(:read).returns("code to run")
 
208
 
 
209
                Puppet.expects(:[]=).with(:code,"code to run")
 
210
 
 
211
                @puppet.main
 
212
            end
 
213
 
 
214
            it "should set the manifest if some files are passed on command line" do
 
215
                ARGV.stubs(:length).returns(1)
 
216
                ARGV.stubs(:shift).returns("site.pp")
 
217
 
 
218
                Puppet.expects(:[]=).with(:manifest,"site.pp")
 
219
 
 
220
                @puppet.main
 
221
            end
 
222
 
 
223
            it "should collect the node facts" do
 
224
                Puppet::Node::Facts.expects(:find).returns(@facts)
 
225
 
 
226
                @puppet.main
 
227
            end
 
228
 
 
229
            it "should find the node" do
 
230
                Puppet::Node.expects(:find).returns(@node)
 
231
 
 
232
                @puppet.main
 
233
            end
 
234
 
 
235
            it "should raise an error if we can't find the node" do
 
236
                Puppet::Node.expects(:find).returns(nil)
 
237
 
 
238
                lambda { @puppet.main }.should raise_error
 
239
            end
 
240
 
 
241
            it "should merge in our node the loaded facts" do
 
242
                @facts.stubs(:values).returns("values")
 
243
 
 
244
                @node.expects(:merge).with("values")
 
245
 
 
246
                @puppet.main
 
247
            end
 
248
 
 
249
            it "should load custom classes if loadclasses" do
 
250
                @puppet.options.stubs(:[]).with(:loadclasses).returns(true)
 
251
                Puppet.stubs(:[]).with(:classfile).returns("/etc/puppet/classes.txt")
 
252
                FileTest.stubs(:exists?).with("/etc/puppet/classes.txt").returns(true)
 
253
                FileTest.stubs(:readable?).with("/etc/puppet/classes.txt").returns(true)
 
254
                File.stubs(:read).with("/etc/puppet/classes.txt").returns("class")
 
255
 
 
256
                @node.expects(:classes=)
 
257
 
 
258
                @puppet.main
 
259
            end
 
260
 
 
261
            it "should compile the catalog" do
 
262
                Puppet::Resource::Catalog.expects(:find).returns(@catalog)
 
263
 
 
264
                @puppet.main
 
265
            end
 
266
 
 
267
            it "should transform the catalog to ral" do
 
268
 
 
269
                @catalog.expects(:to_ral).returns(@catalog)
 
270
 
 
271
                @puppet.main
 
272
            end
 
273
 
 
274
            it "should finalize the catalog" do
 
275
                @catalog.expects(:finalize)
 
276
 
 
277
                @puppet.main
 
278
            end
 
279
 
 
280
            it "should apply the catalog" do
 
281
                @catalog.expects(:apply)
 
282
 
 
283
                @puppet.main
 
284
            end
 
285
 
 
286
            it "should generate a report if not noop" do
 
287
                Puppet.stubs(:[]).with(:noop).returns(false)
 
288
                @puppet.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
 
289
                metrics = stub 'metrics', :[] => { :total => 10, :failed => 0}
 
290
                report = stub 'report', :metrics => metrics
 
291
                @transaction.stubs(:report).returns(report)
 
292
 
 
293
                @transaction.expects(:generate_report)
 
294
 
 
295
                @puppet.main
 
296
            end
 
297
 
 
298
            describe "with detailed_exitcodes" do
 
299
                before :each do
 
300
                    Puppet.stubs(:[]).with(:noop).returns(false)
 
301
                    @puppet.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
 
302
                end
 
303
 
 
304
                it "should exit with exit code of 2 if changes" do
 
305
                    report = stub 'report', :metrics => { "changes" => {:total => 1}, "resources" => {:failed => 0} }
 
306
                    @transaction.stubs(:generate_report).returns(report)
 
307
                    @transaction.stubs(:report).returns(report)
 
308
                    @puppet.expects(:exit).with(2)
 
309
 
 
310
                    @puppet.main
 
311
                end
 
312
 
 
313
                it "should exit with exit code of 4 if failures" do
 
314
                    report = stub 'report', :metrics => { "changes" => {:total => 0}, "resources" => {:failed => 1} }
 
315
                    @transaction.stubs(:generate_report).returns(report)
 
316
                    @transaction.stubs(:report).returns(report)
 
317
                    @puppet.expects(:exit).with(4)
 
318
 
 
319
                    @puppet.main
 
320
                end
 
321
 
 
322
                it "should exit with exit code of 6 if changes and failures" do
 
323
                    report = stub 'report', :metrics => { "changes" => {:total => 1}, "resources" => {:failed => 1} }
 
324
                    @transaction.stubs(:generate_report).returns(report)
 
325
                    @transaction.stubs(:report).returns(report)
 
326
                    @puppet.expects(:exit).with(6)
 
327
 
 
328
                    @puppet.main
 
329
                end
 
330
            end
 
331
 
 
332
        end
 
333
 
 
334
        describe "the 'apply' command" do
 
335
            confine "PSON library is missing; cannot test applying catalogs" => Puppet.features.pson?
 
336
 
 
337
            before do
 
338
                #Puppet::Resource::Catalog.stubs(:pson_create).returns Puppet::Resource::Catalog.new
 
339
                PSON.stubs(:parse).returns Puppet::Resource::Catalog.new
 
340
            end
 
341
 
 
342
            it "should read the catalog in from disk if a file name is provided" do
 
343
                @puppet.options[:catalog] = "/my/catalog.pson"
 
344
 
 
345
                File.expects(:read).with("/my/catalog.pson").returns "something"
 
346
 
 
347
                @puppet.apply
 
348
            end
 
349
 
 
350
            it "should read the catalog in from stdin if '-' is provided" do
 
351
                @puppet.options[:catalog] = "-"
 
352
 
 
353
                $stdin.expects(:read).returns "something"
 
354
 
 
355
                @puppet.apply
 
356
            end
 
357
 
 
358
            it "should deserialize the catalog from pson" do
 
359
                @puppet.options[:catalog] = "/my/catalog.pson"
 
360
 
 
361
                File.expects(:read).returns "something"
 
362
                PSON.expects(:parse).with("something").returns Puppet::Resource::Catalog.new
 
363
 
 
364
                @puppet.apply
 
365
            end
 
366
 
 
367
            it "should fail helpfully if deserializing fails" do
 
368
                @puppet.options[:catalog] = "/my/catalog.pson"
 
369
 
 
370
                File.expects(:read).returns "something"
 
371
                PSON.expects(:parse).raises ArgumentError
 
372
 
 
373
                lambda { @puppet.apply }.should raise_error(Puppet::Error)
 
374
            end
 
375
 
 
376
            it "should convert plain data structures into a catalog if deserialization does not do so" do
 
377
                @puppet.options[:catalog] = "/my/catalog.pson"
 
378
 
 
379
                File.expects(:read).returns "something"
 
380
                PSON.expects(:parse).with("something").returns({:foo => "bar"})
 
381
                Puppet::Resource::Catalog.expects(:pson_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new)
 
382
 
 
383
                @puppet.apply
 
384
            end
 
385
 
 
386
            it "should convert the catalog to a RAL catalog and use a Configurer instance to apply it" do
 
387
                @puppet.options[:catalog] = "/my/catalog.pson"
 
388
 
 
389
                File.expects(:read).returns "something"
 
390
 
 
391
                catalog = Puppet::Resource::Catalog.new
 
392
                PSON.expects(:parse).returns catalog
 
393
 
 
394
                catalog.expects(:to_ral).returns "mycatalog"
 
395
 
 
396
                configurer = stub 'configurer'
 
397
                Puppet::Configurer.expects(:new).returns configurer
 
398
 
 
399
                configurer.expects(:run).with(:catalog => "mycatalog")
 
400
 
 
401
                @puppet.apply
 
402
            end
 
403
        end
 
404
    end
 
405
end