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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/application/puppet'
describe "Puppet" do
before :each do
@puppet = Puppet::Application[:puppet]
Puppet::Util::Log.stubs(:newdestination)
Puppet::Util::Log.stubs(:level=)
end
[:debug,:loadclasses,:verbose,:use_nodes,:detailed_exitcodes].each do |option|
it "should declare handle_#{option} method" do
@puppet.should respond_to("handle_#{option}".to_sym)
end
it "should store argument value when calling handle_#{option}" do
@puppet.options.expects(:[]=).with(option, 'arg')
@puppet.send("handle_#{option}".to_sym, 'arg')
end
end
it "should set the code to the provided code when :execute is used" do
@puppet.options.expects(:[]=).with(:code, 'arg')
@puppet.send("handle_execute".to_sym, 'arg')
end
it "should ask Puppet::Application to parse Puppet configuration file" do
@puppet.should_parse_config?.should be_true
end
describe "when applying options" do
it "should set the log destination with --logdest" do
Puppet::Log.expects(:newdestination).with("console")
@puppet.handle_logdest("console")
end
it "should put the logset options to true" do
@puppet.options.expects(:[]=).with(:logset,true)
@puppet.handle_logdest("console")
end
end
describe "during setup" do
before :each do
Puppet::Log.stubs(:newdestination)
Puppet.stubs(:trap)
Puppet::Log.stubs(:level=)
Puppet.stubs(:parse_config)
Puppet::Network::Client.dipper.stubs(:new)
STDIN.stubs(:read)
@puppet.options.stubs(:[]).with(any_parameters)
end
it "should set show_diff on --noop" do
Puppet.stubs(:[]=)
Puppet.stubs(:[]).with(:config)
Puppet.stubs(:[]).with(:noop).returns(true)
Puppet.expects(:[]=).with(:show_diff, true)
@puppet.run_setup
end
it "should set console as the log destination if logdest option wasn't provided" do
Puppet::Log.expects(:newdestination).with(:console)
@puppet.run_setup
end
it "should set INT trap" do
@puppet.expects(:trap).with(:INT)
@puppet.run_setup
end
it "should set log level to debug if --debug was passed" do
@puppet.options.stubs(:[]).with(:debug).returns(true)
Puppet::Log.expects(:level=).with(:debug)
@puppet.run_setup
end
it "should set log level to info if --verbose was passed" do
@puppet.options.stubs(:[]).with(:verbose).returns(true)
Puppet::Log.expects(:level=).with(:info)
@puppet.run_setup
end
it "should print puppet config if asked to in Puppet config" do
@puppet.stubs(:exit)
Puppet.settings.stubs(:print_configs?).returns(true)
Puppet.settings.expects(:print_configs)
@puppet.run_setup
end
it "should exit after printing puppet config if asked to in Puppet config" do
Puppet.settings.stubs(:print_configs?).returns(true)
lambda { @puppet.run_setup }.should raise_error(SystemExit)
end
end
describe "when executing" do
it "should dispatch to parseonly if parseonly is set" do
@puppet.stubs(:options).returns({})
Puppet.stubs(:[]).with(:parseonly).returns(true)
@puppet.get_command.should == :parseonly
end
it "should dispatch to 'apply' if it was called with 'apply'" do
@puppet.options[:catalog] = "foo"
@puppet.get_command.should == :apply
end
it "should dispatch to main if parseonly is not set" do
@puppet.stubs(:options).returns({})
Puppet.stubs(:[]).with(:parseonly).returns(false)
@puppet.get_command.should == :main
end
describe "the parseonly command" do
before :each do
Puppet.stubs(:[]).with(:environment)
Puppet.stubs(:[]).with(:manifest).returns("site.pp")
@interpreter = stub_everything
Puppet.stubs(:err)
@puppet.stubs(:exit)
Puppet::Parser::Interpreter.stubs(:new).returns(@interpreter)
end
it "should delegate to the Puppet Parser" do
@interpreter.expects(:parser)
@puppet.parseonly
end
it "should exit with exit code 0 if no error" do
@puppet.expects(:exit).with(0)
@puppet.parseonly
end
it "should exit with exit code 1 if error" do
@interpreter.stubs(:parser).raises(Puppet::ParseError)
@puppet.expects(:exit).with(1)
@puppet.parseonly
end
end
describe "the main command" do
before :each do
Puppet.stubs(:[])
Puppet.stubs(:[]).with(:trace).returns(true)
@puppet.options.stubs(:[])
@facts = stub_everything 'facts'
Puppet::Node::Facts.stubs(:find).returns(@facts)
@node = stub_everything 'node'
Puppet::Node.stubs(:find).returns(@node)
@catalog = stub_everything 'catalog'
@catalog.stubs(:to_ral).returns(@catalog)
Puppet::Resource::Catalog.stubs(:find).returns(@catalog)
STDIN.stubs(:read)
@transaction = stub_everything 'transaction'
@catalog.stubs(:apply).returns(@transaction)
@puppet.stubs(:exit)
end
it "should set the code to run from --code" do
@puppet.options.stubs(:[]).with(:code).returns("code to run")
Puppet.expects(:[]=).with(:code,"code to run")
@puppet.main
end
it "should set the code to run from STDIN if no arguments" do
ARGV.stubs(:length).returns(0)
STDIN.stubs(:read).returns("code to run")
Puppet.expects(:[]=).with(:code,"code to run")
@puppet.main
end
it "should set the manifest if some files are passed on command line" do
ARGV.stubs(:length).returns(1)
ARGV.stubs(:shift).returns("site.pp")
Puppet.expects(:[]=).with(:manifest,"site.pp")
@puppet.main
end
it "should collect the node facts" do
Puppet::Node::Facts.expects(:find).returns(@facts)
@puppet.main
end
it "should find the node" do
Puppet::Node.expects(:find).returns(@node)
@puppet.main
end
it "should raise an error if we can't find the node" do
Puppet::Node.expects(:find).returns(nil)
lambda { @puppet.main }.should raise_error
end
it "should merge in our node the loaded facts" do
@facts.stubs(:values).returns("values")
@node.expects(:merge).with("values")
@puppet.main
end
it "should load custom classes if loadclasses" do
@puppet.options.stubs(:[]).with(:loadclasses).returns(true)
Puppet.stubs(:[]).with(:classfile).returns("/etc/puppet/classes.txt")
FileTest.stubs(:exists?).with("/etc/puppet/classes.txt").returns(true)
FileTest.stubs(:readable?).with("/etc/puppet/classes.txt").returns(true)
File.stubs(:read).with("/etc/puppet/classes.txt").returns("class")
@node.expects(:classes=)
@puppet.main
end
it "should compile the catalog" do
Puppet::Resource::Catalog.expects(:find).returns(@catalog)
@puppet.main
end
it "should transform the catalog to ral" do
@catalog.expects(:to_ral).returns(@catalog)
@puppet.main
end
it "should finalize the catalog" do
@catalog.expects(:finalize)
@puppet.main
end
it "should apply the catalog" do
@catalog.expects(:apply)
@puppet.main
end
it "should generate a report if not noop" do
Puppet.stubs(:[]).with(:noop).returns(false)
@puppet.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
metrics = stub 'metrics', :[] => { :total => 10, :failed => 0}
report = stub 'report', :metrics => metrics
@transaction.stubs(:report).returns(report)
@transaction.expects(:generate_report)
@puppet.main
end
describe "with detailed_exitcodes" do
before :each do
Puppet.stubs(:[]).with(:noop).returns(false)
@puppet.options.stubs(:[]).with(:detailed_exitcodes).returns(true)
end
it "should exit with exit code of 2 if changes" do
report = stub 'report', :metrics => { "changes" => {:total => 1}, "resources" => {:failed => 0} }
@transaction.stubs(:generate_report).returns(report)
@transaction.stubs(:report).returns(report)
@puppet.expects(:exit).with(2)
@puppet.main
end
it "should exit with exit code of 4 if failures" do
report = stub 'report', :metrics => { "changes" => {:total => 0}, "resources" => {:failed => 1} }
@transaction.stubs(:generate_report).returns(report)
@transaction.stubs(:report).returns(report)
@puppet.expects(:exit).with(4)
@puppet.main
end
it "should exit with exit code of 6 if changes and failures" do
report = stub 'report', :metrics => { "changes" => {:total => 1}, "resources" => {:failed => 1} }
@transaction.stubs(:generate_report).returns(report)
@transaction.stubs(:report).returns(report)
@puppet.expects(:exit).with(6)
@puppet.main
end
end
end
describe "the 'apply' command" do
before do
#Puppet::Resource::Catalog.stubs(:json_create).returns Puppet::Resource::Catalog.new
JSON.stubs(:parse).returns Puppet::Resource::Catalog.new
end
it "should read the catalog in from disk if a file name is provided" do
@puppet.options[:catalog] = "/my/catalog.json"
File.expects(:read).with("/my/catalog.json").returns "something"
@puppet.apply
end
it "should read the catalog in from stdin if '-' is provided" do
@puppet.options[:catalog] = "-"
$stdin.expects(:read).returns "something"
@puppet.apply
end
it "should deserialize the catalog from json" do
@puppet.options[:catalog] = "/my/catalog.json"
File.expects(:read).returns "something"
JSON.expects(:parse).with("something").returns Puppet::Resource::Catalog.new
@puppet.apply
end
it "should fail helpfully if deserializing fails" do
@puppet.options[:catalog] = "/my/catalog.json"
File.expects(:read).returns "something"
JSON.expects(:parse).raises ArgumentError
lambda { @puppet.apply }.should raise_error(Puppet::Error)
end
it "should convert plain data structures into a catalog if deserialization does not do so" do
@puppet.options[:catalog] = "/my/catalog.json"
File.expects(:read).returns "something"
JSON.expects(:parse).with("something").returns({:foo => "bar"})
Puppet::Resource::Catalog.expects(:json_create).with({:foo => "bar"}).returns(Puppet::Resource::Catalog.new)
@puppet.apply
end
it "should convert the catalog to a RAL catalog and use a Configurer instance to apply it" do
@puppet.options[:catalog] = "/my/catalog.json"
File.expects(:read).returns "something"
catalog = Puppet::Resource::Catalog.new
JSON.expects(:parse).returns catalog
catalog.expects(:to_ral).returns "mycatalog"
configurer = stub 'configurer'
Puppet::Configurer.expects(:new).returns configurer
configurer.expects(:run).with(:catalog => "mycatalog")
@puppet.apply
end
end
end
end
|