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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
require 'spec_helper'
describe Mixlib::ShellOut do
subject { shell_cmd }
let(:shell_cmd) { Mixlib::ShellOut.new('apt-get install chef') }
context 'when instantiating' do
it "should set the command" do
subject.command.should eql("apt-get install chef")
end
context 'with default settings' do
its(:cwd) { should be_nil }
its(:user) { should be_nil }
its(:group) { should be_nil }
its(:umask) { should be_nil }
its(:timeout) { should eql(600) }
its(:valid_exit_codes) { should eql([0]) }
its(:live_stream) { should be_nil }
it "should set default environmental variables" do
shell_cmd.environment.should == {"LC_ALL" => "C"}
end
end
context 'when setting accessors' do
subject { shell_cmd.send(accessor) }
let(:shell_cmd) { blank_shell_cmd.tap(&with_overrides) }
let(:blank_shell_cmd) { Mixlib::ShellOut.new('apt-get install chef') }
let(:with_overrides) { lambda { |shell_cmd| shell_cmd.send("#{accessor}=", value) } }
context 'when setting user' do
let(:accessor) { :user }
let(:value) { 'root' }
it "should set the user" do
should eql(value)
end
context 'with an integer value for user' do
let(:value) { 0 }
it "should use the user-supplied uid" do
shell_cmd.uid.should eql(value)
end
end
context 'with string value for user' do
let(:value) { username }
let(:username) { user_info.name }
let(:expected_uid) { user_info.uid }
let(:user_info) { Etc.getpwent }
it "should compute the uid of the user", :unix_only => true do
shell_cmd.uid.should eql(expected_uid)
end
end
end
context 'when setting group' do
let(:accessor) { :group }
let(:value) { 'wheel' }
it "should set the group" do
should eql(value)
end
context 'with integer value for group' do
let(:value) { 0 }
it "should use the user-supplied gid" do
shell_cmd.gid.should eql(value)
end
end
context 'with string value for group' do
let(:value) { groupname }
let(:groupname) { group_info.name }
let(:expected_gid) { group_info.gid }
let(:group_info) { Etc.getgrent }
it "should compute the gid of the user", :unix_only => true do
shell_cmd.gid.should eql(expected_gid)
end
end
end
context 'when setting the umask' do
let(:accessor) { :umask }
context 'with octal integer' do
let(:value) { 007555}
it 'should set the umask' do
should eql(value)
end
end
context 'with decimal integer' do
let(:value) { 2925 }
it 'should sets the umask' do
should eql(005555)
end
end
context 'with string' do
let(:value) { '7777' }
it 'should sets the umask' do
should eql(007777)
end
end
end
context 'when setting read timeout' do
let(:accessor) { :timeout }
let(:value) { 10 }
it 'should set the read timeout' do
should eql(value)
end
end
context 'when setting valid exit codes' do
let(:accessor) { :valid_exit_codes }
let(:value) { [0, 23, 42] }
it "should set the valid exit codes" do
should eql(value)
end
end
context 'when setting a live stream' do
let(:accessor) { :live_stream }
let(:value) { stream }
let(:stream) { StringIO.new }
it "should set the live stream" do
should eql(value)
end
end
end
context "with options hash" do
let(:shell_cmd) { Mixlib::ShellOut.new("brew install couchdb", options) }
let(:options) { { :cwd => cwd, :user => user, :group => group, :umask => umask,
:timeout => timeout, :environment => environment, :returns => valid_exit_codes, :live_stream => stream } }
let(:cwd) { '/tmp' }
let(:user) { 'toor' }
let(:group) { 'wheel' }
let(:umask) { '2222' }
let(:timeout) { 5 }
let(:environment) { { 'RUBY_OPTS' => '-w' } }
let(:valid_exit_codes) { [ 0, 1, 42 ] }
let(:stream) { StringIO.new }
it "should set the working directory" do
shell_cmd.cwd.should eql(cwd)
end
it "should set the user" do
shell_cmd.user.should eql(user)
end
it "should set the group" do
shell_cmd.group.should eql(group)
end
it "should set the umask" do
shell_cmd.umask.should eql(002222)
end
it "should set the timout" do
shell_cmd.timeout.should eql(timeout)
end
it "should add environment settings to the default" do
shell_cmd.environment.should eql({'LC_ALL' => 'C', 'RUBY_OPTS' => '-w'})
end
context 'when setting custom environments' do
context 'when setting the :env option' do
let(:options) { { :env => environment } }
it "should also set the enviroment" do
shell_cmd.environment.should eql({'LC_ALL' => 'C', 'RUBY_OPTS' => '-w'})
end
end
context 'when :environment is set to nil' do
let(:options) { { :environment => nil } }
it "should not set any environment" do
shell_cmd.environment.should == {}
end
end
context 'when :env is set to nil' do
let(:options) { { :env => nil } }
it "should not set any environment" do
shell_cmd.environment.should eql({})
end
end
end
it "should set valid exit codes" do
shell_cmd.valid_exit_codes.should eql(valid_exit_codes)
end
it "should set the live stream" do
shell_cmd.live_stream.should eql(stream)
end
context 'with an invalid option' do
let(:options) { { :frab => :job } }
let(:invalid_option_exception) { Mixlib::ShellOut::InvalidCommandOption }
let(:exception_message) { "option ':frab' is not a valid option for Mixlib::ShellOut" }
it "should raise InvalidCommandOPtion" do
lambda { shell_cmd }.should raise_error(invalid_option_exception, exception_message)
end
end
end
context "with array of command and args" do
context 'without options' do
let(:shell_cmd) { Mixlib::ShellOut.new('ruby', '-e', %q{'puts "hello"'}) }
it "should set the command to the array of command and args" do
shell_cmd.command.should eql(['ruby', '-e', %q{'puts "hello"'}])
end
end
context 'with options' do
let(:shell_cmd) { Mixlib::ShellOut.new('ruby', '-e', %q{'puts "hello"'}, options) }
let(:options) { {:cwd => '/tmp', :user => 'nobody'} }
it "should set the command to the array of command and args" do
shell_cmd.command.should eql(['ruby', '-e', %q{'puts "hello"'}])
end
it "should evaluate the options" do
shell_cmd.cwd.should == '/tmp'
shell_cmd.user.should == 'nobody'
end
end
end
end
context 'when executing the command' do
let(:shell_cmd) { Mixlib::ShellOut.new(cmd) }
let(:executed_cmd) { shell_cmd.tap(&:run_command) }
let(:stdout) { executed_cmd.stdout }
let(:stderr) { executed_cmd.stderr }
let(:chomped_stdout) { stdout.chomp }
let(:exit_status) { executed_cmd.status.exitstatus }
let(:dir) { Dir.mktmpdir }
let(:ruby_eval) { lambda { |code| "ruby -e '#{code}'" } }
let(:dump_file) { "#{dir}/out.txt" }
let(:dump_file_content) { stdout; IO.read(dump_file) }
context 'with a current working directory' do
subject { File.expand_path(chomped_stdout) }
let(:fully_qualified_cwd) { File.expand_path(cwd) }
let(:shell_cmd) { Mixlib::ShellOut.new(cmd, :cwd => cwd) }
context 'when running under Unix', :unix_only => true do
let(:cwd) { '/bin' }
let(:cmd) { 'pwd' }
it "should chdir to the working directory" do
should eql(fully_qualified_cwd)
end
end
context 'when running under Windows', :windows_only => true do
let(:cwd) { Dir.tmpdir }
let(:cmd) { 'echo %cd%' }
it "should chdir to the working directory" do
should eql(fully_qualified_cwd)
end
end
end
context "with a live stream" do
let(:stream) { StringIO.new }
let(:shell_cmd) { Mixlib::ShellOut.new(%q{ruby -e 'puts "hello"'}, :live_stream => stream) }
it "should copy the child's stdout to the live stream" do
shell_cmd.run_command
stream.string.should eql("hello#{LINE_ENDING}")
end
end
context "when running different types of command" do
context 'with spaces in the path' do
subject { chomped_stdout }
let(:shell_cmd) { Mixlib::ShellOut.new(script_name) }
let(:script) { open_file.tap(&write_file).tap(&:close).tap(&make_executable) }
let(:file_name) { "#{dir}/blah blah.cmd" }
let(:script_name) { "\"#{script.path}\"" }
let(:open_file) { File.open(file_name, 'w') }
let(:write_file) { lambda { |f| f.write(script_content) } }
let(:make_executable) { lambda { |f| File.chmod(0755, f.path) } }
context 'when running under Unix', :unix_only => true do
let(:script_content) { 'echo blah' }
it 'should execute' do
should eql('blah')
end
end
context 'when running under Windows', :windows_only => true do
let(:script_content) { '@echo blah' }
it 'should execute' do
should eql('blah')
end
end
end
context 'with lots of long arguments' do
subject { chomped_stdout }
# This number was chosen because it seems to be an actual maximum
# in Windows--somewhere around 6-7K of command line
let(:echotext) { 10000.upto(11340).map(&:to_s).join(' ') }
let(:cmd) { "echo #{echotext}" }
it 'should execute' do
should eql(echotext)
end
end
context 'with special characters' do
subject { stdout }
let(:special_characters) { '<>&|&&||;' }
let(:cmd) { "ruby -e 'print \"#{special_characters}\"'" }
it 'should execute' do
should eql(special_characters)
end
end
context 'with backslashes' do
subject { stdout }
let(:backslashes) { %q{\\"\\\\} }
let(:cmd) { ruby_eval.call("print \"#{backslashes}\"") }
it 'should execute' do
should eql("\"\\")
end
end
context 'with pipes' do
let(:input_script) { "STDOUT.sync = true; STDERR.sync = true; print true; STDERR.print false" }
let(:output_script) { "print STDIN.read.length" }
let(:cmd) { ruby_eval.call(input_script) + " | " + ruby_eval.call(output_script) }
it 'should execute' do
stdout.should eql('4')
end
it 'should handle stderr' do
stderr.should eql('false')
end
end
context 'with file pipes' do
let(:code) { "STDOUT.sync = true; STDERR.sync = true; print true; STDERR.print false" }
let(:cmd) { ruby_eval.call(code) + " > #{dump_file}" }
it 'should execute' do
stdout.should eql('')
end
it 'should handle stderr' do
stderr.should eql('false')
end
it 'should write to file pipe' do
dump_file_content.should eql('true')
end
end
context 'with stdout and stderr file pipes' do
let(:code) { "STDOUT.sync = true; STDERR.sync = true; print true; STDERR.print false" }
let(:cmd) { ruby_eval.call(code) + " > #{dump_file} 2>&1" }
it 'should execute' do
stdout.should eql('')
end
it 'should write to file pipe' do
dump_file_content.should eql('truefalse')
end
end
context 'with &&' do
subject { stdout }
let(:cmd) { ruby_eval.call('print "foo"') + ' && ' + ruby_eval.call('print "bar"') }
it 'should execute' do
should eql('foobar')
end
end
context 'with ||' do
let(:cmd) { ruby_eval.call('print "foo"; exit 1') + ' || ' + ruby_eval.call('print "bar"') }
it 'should execute' do
stdout.should eql('foobar')
end
it 'should exit with code 0' do
exit_status.should eql(0)
end
end
end
context "when handling process exit codes" do
let(:cmd) { ruby_eval.call("exit #{exit_code}") }
context 'with normal exit status' do
let(:exit_code) { 0 }
it "should not raise error" do
lambda { executed_cmd.error! }.should_not raise_error
end
it "should set the exit status of the command" do
exit_status.should eql(exit_code)
end
end
context 'with nonzero exit status' do
let(:exit_code) { 2 }
let(:exception_message_format) { Regexp.escape(executed_cmd.format_for_exception) }
it "should raise ShellCommandFailed" do
lambda { executed_cmd.error! }.should raise_error(Mixlib::ShellOut::ShellCommandFailed)
end
it "includes output with exceptions from #error!" do
begin
executed_cmd.error!
rescue Mixlib::ShellOut::ShellCommandFailed => e
e.message.should match(exception_message_format)
end
end
it "should set the exit status of the command" do
exit_status.should eql(exit_code)
end
end
context 'with valid exit codes' do
let(:shell_cmd) { Mixlib::ShellOut.new(cmd, :returns => valid_exit_codes) }
let(:cmd) { ruby_eval.call("exit #{exit_code}" ) }
context 'when exiting with valid code' do
let(:valid_exit_codes) { 42 }
let(:exit_code) { 42 }
it "should not raise error" do
lambda { executed_cmd.error! }.should_not raise_error
end
it "should set the exit status of the command" do
exit_status.should eql(exit_code)
end
end
context 'when exiting with invalid code' do
let(:valid_exit_codes) { [ 0, 1, 42 ] }
let(:exit_code) { 2 }
it "should raise ShellCommandFailed" do
lambda { executed_cmd.error! }.should raise_error(Mixlib::ShellOut::ShellCommandFailed)
end
it "should set the exit status of the command" do
exit_status.should eql(exit_code)
end
end
context 'when exiting with invalid code 0' do
let(:valid_exit_codes) { 42 }
let(:exit_code) { 0 }
it "should raise ShellCommandFailed" do
lambda { executed_cmd.error! }.should raise_error(Mixlib::ShellOut::ShellCommandFailed)
end
it "should set the exit status of the command" do
exit_status.should eql(exit_code)
end
end
end
describe "#invalid!" do
let(:exit_code) { 0 }
it "should raise ShellCommandFailed" do
lambda { executed_cmd.invalid!("I expected this to exit 42, not 0") }.should raise_error(Mixlib::ShellOut::ShellCommandFailed)
end
end
end
context "when handling the subprocess" do
context 'with STDOUT and STDERR' do
let(:two_time) { 'STDERR.puts :hello; STDOUT.puts :world' }
let(:cmd) { ruby_eval.call(two_time) }
# We could separate this into two examples, but we want to make
# sure that stderr and stdout gets collected without stepping
# on each other.
it "should collect all of STDOUT and STDERR" do
stderr.should eql("hello#{LINE_ENDING}")
stdout.should eql("world#{LINE_ENDING}")
end
end
context 'with forking subprocess that does not close stdout and stderr' do
let(:evil_forker) { "exit if fork; 10.times { sleep 1 }" }
let(:cmd) { ruby_eval.call(evil_forker) }
it "should not hang" do
proc do
Timeout.timeout(2) do
executed_cmd
end
end.should_not raise_error
end
end
context 'with subprocess that takes longer than timeout' do
let(:shell_cmd) { Mixlib::ShellOut.new(cmd, :timeout => 0.1) }
let(:cmd) { ruby_eval.call('sleep 2') }
it "should raise CommandTimeout" do
lambda { executed_cmd }.should raise_error(Mixlib::ShellOut::CommandTimeout)
end
end
context 'with subprocess that exceeds buffersize' do
let(:chatty) { 'print("X" * 16 * 1024); print("." * 1024)' }
let(:cmd) { ruby_eval.call(chatty) }
it "should still reads all of the output" do
stdout.should match(/X{16384}\.{1024}/)
end
end
context 'with subprocess that returns nothing' do
let(:cmd) { ruby_eval.call('exit 0') }
it 'should return an empty string for stdout' do
stdout.should eql('')
end
it 'should return an empty string for stderr' do
stderr.should eql('')
end
end
context 'with subprocess that closes stdout and continues writing to stderr' do
let(:half_and_half) { "STDOUT.close; sleep 0.5; STDERR.puts :win" }
let(:cmd) { ruby_eval.call(half_and_half) }
it 'should not hang or lose outupt' do
stderr.should eql("win#{LINE_ENDING}")
end
end
context 'with subprocess that closes stderr and continues writing to stdout' do
let(:half_and_half) { "STDERR.close; sleep 0.5; STDOUT.puts :win" }
let(:cmd) { ruby_eval.call(half_and_half) }
it 'should not hang or lose outupt' do
stdout.should eql("win#{LINE_ENDING}")
end
end
context 'with subprocess writing lots of data to both stdout and stderr' do
let(:cmd) { ruby_eval.call(chatty) }
let(:expected_output_with) { lambda { |chr| (chr * 20_000) + "#{LINE_ENDING}" + (chr * 20_000) + "#{LINE_ENDING}" } }
context 'when writing to STDOUT first' do
let(:chatty) { %q{puts "f" * 20_000; STDERR.puts "u" * 20_000; puts "f" * 20_000; STDERR.puts "u" * 20_000} }
it "should not deadlock" do
stdout.should eql(expected_output_with.call('f'))
stderr.should eql(expected_output_with.call('u'))
end
end
context 'when writing to STDERR first' do
let(:chatty) { %q{STDERR.puts "u" * 20_000; puts "f" * 20_000; STDERR.puts "u" * 20_000; puts "f" * 20_000} }
it "should not deadlock" do
stdout.should eql(expected_output_with.call('f'))
stderr.should eql(expected_output_with.call('u'))
end
end
end
context 'when subprocess writes, pauses, then continues writing' do
subject { stdout }
let(:stop_and_go) { %q{puts "before"; sleep 0.5; puts "after"} }
let(:cmd) { ruby_eval.call(stop_and_go) }
it 'should not hang or lose output' do
should eql("before#{LINE_ENDING}after#{LINE_ENDING}")
end
end
it "doesn't hang or lose output when a process pauses before writing" do
late_arrival = %q{ruby -e 'sleep 0.5;puts "missed_the_bus"'}
cmd = Mixlib::ShellOut.new(late_arrival)
cmd.run_command
cmd.stdout.should == "missed_the_bus#{LINE_ENDING}"
end
it "uses the C locale by default" do
cmd = Mixlib::ShellOut.new(ECHO_LC_ALL)
cmd.run_command
cmd.stdout.strip.should == 'C'
end
it "does not set any locale when the user gives LC_ALL => nil" do
# kinda janky
cmd = Mixlib::ShellOut.new(ECHO_LC_ALL, :environment => {"LC_ALL" => nil})
cmd.run_command
if !ENV['LC_ALL'] && windows?
expected = "%LC_ALL%"
else
expected = ENV['LC_ALL'].to_s.strip
end
cmd.stdout.strip.should == expected
end
it "uses the requested locale" do
cmd = Mixlib::ShellOut.new(ECHO_LC_ALL, :environment => {"LC_ALL" => 'es'})
cmd.run_command
cmd.stdout.strip.should == 'es'
end
it "recovers the error message when exec fails" do
cmd = Mixlib::ShellOut.new("fuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu")
lambda {cmd.run_command}.should raise_error(Errno::ENOENT)
end
it "closes stdin on the child process so it knows not to wait for any input" do
cmd = Mixlib::ShellOut.new(%q{ruby -e 'print STDIN.eof?.to_s'})
cmd.run_command
cmd.stdout.should == "true"
end
it "doesn't hang when STDOUT is closed before STDERR" do
# Regression test:
# We need to ensure that stderr is removed from the list of file
# descriptors that we attempt to select() on in the case that:
# a) STDOUT closes first
# b) STDERR closes
# c) The program does not exit for some time after (b) occurs.
# Otherwise, we will attempt to read from the closed STDOUT pipe over and
# over again and generate lots of garbage, which will not be collected
# since we have to turn GC off to avoid segv.
cmd = Mixlib::ShellOut.new(%q{ruby -e 'STDOUT.puts "F" * 4096; STDOUT.close; sleep 0.1; STDERR.puts "foo"; STDERR.close; sleep 0.1; exit'})
cmd.run_command
unclosed_pipes = cmd.send(:open_pipes)
unclosed_pipes.should be_empty
end
end
end
it "formats itself for exception messages" do
cmd = Mixlib::ShellOut.new %q{ruby -e 'STDERR.puts "msg_in_stderr"; puts "msg_in_stdout"'}
cmd.run_command
cmd.format_for_exception.split("\n")[0].should == %q{---- Begin output of ruby -e 'STDERR.puts "msg_in_stderr"; puts "msg_in_stdout"' ----}
cmd.format_for_exception.split("\n")[1].should == %q{STDOUT: msg_in_stdout}
cmd.format_for_exception.split("\n")[2].should == %q{STDERR: msg_in_stderr}
cmd.format_for_exception.split("\n")[3].should == %q{---- End output of ruby -e 'STDERR.puts "msg_in_stderr"; puts "msg_in_stdout"' ----}
end
end
|