@@ -239,6 +239,19 @@ def exec_stub(options = {})
239
239
expect ( dependencies . collect ( &:to_s ) ) . to eq ( [ Puppet ::Relationship . new ( tmp , execer ) . to_s ] )
240
240
end
241
241
242
+ it "should be able to autorequire files mentioned in the array command" do
243
+ foo = make_absolute ( '/bin/foo' )
244
+ catalog = Puppet ::Resource ::Catalog . new
245
+ tmp = Puppet ::Type . type ( :file ) . new ( :name => foo )
246
+ execer = Puppet ::Type . type ( :exec ) . new ( :name => 'test array' , :command => [ foo , 'bar' ] )
247
+
248
+ catalog . add_resource tmp
249
+ catalog . add_resource execer
250
+ dependencies = execer . autorequire ( catalog )
251
+
252
+ expect ( dependencies . collect ( &:to_s ) ) . to eq ( [ Puppet ::Relationship . new ( tmp , execer ) . to_s ] )
253
+ end
254
+
242
255
describe "when handling the path parameter" do
243
256
expect = %w{ one two three four }
244
257
{ "an array" => expect ,
@@ -346,7 +359,13 @@ def instance(path)
346
359
end
347
360
348
361
shared_examples_for "all exec command parameters" do |param |
349
- { "relative" => "example" , "absolute" => "/bin/example" } . sort . each do |name , command |
362
+ array_cmd = [ "/bin/example" , "*" ]
363
+ array_cmd = [ [ "/bin/example" , "*" ] ] if [ :onlyif , :unless ] . include? ( param )
364
+
365
+ commands = { "relative" => "example" , "absolute" => "/bin/example" }
366
+ commands [ "array" ] = array_cmd
367
+
368
+ commands . sort . each do |name , command |
350
369
describe "if command is #{ name } " do
351
370
before :each do
352
371
@param = param
@@ -379,45 +398,44 @@ def test(command, valid)
379
398
end
380
399
381
400
shared_examples_for "all exec command parameters that take arrays" do |param |
382
- describe "when given an array of inputs" do
383
- before :each do
384
- @test = Puppet ::Type . type ( :exec ) . new ( :name => @executable )
385
- end
401
+ [
402
+ %w{ one two three } ,
403
+ [ %w{ one -a } , %w{ two, -b } , 'three' ]
404
+ ] . each do |input |
405
+ context "when given #{ input . inspect } as input" do
406
+ let ( :resource ) { Puppet ::Type . type ( :exec ) . new ( :name => @executable ) }
386
407
387
- it "should accept the array when all commands return valid" do
388
- input = %w{ one two three }
389
- expect ( @test . provider ) . to receive ( :validatecmd ) . exactly ( input . length ) . times . and_return ( true )
390
- @test [ param ] = input
391
- expect ( @test [ param ] ) . to eq ( input )
392
- end
408
+ it "accepts the array when all commands return valid" do
409
+ input = %w{ one two three }
410
+ allow ( resource . provider ) . to receive ( :validatecmd ) . exactly ( input . length ) . times . and_return ( true )
411
+ resource [ param ] = input
412
+ expect ( resource [ param ] ) . to eq ( input )
413
+ end
393
414
394
- it "should reject the array when any commands return invalid" do
395
- input = %w{ one two three }
396
- expect ( @test . provider ) . to receive ( :validatecmd ) . with ( input . first ) . and_return ( false )
397
- input [ 1 ..-1 ] . each do |cmd |
398
- expect ( @test . provider ) . to receive ( :validatecmd ) . with ( cmd ) . and_return ( true )
415
+ it "rejects the array when any commands return invalid" do
416
+ input = %w{ one two three }
417
+ allow ( resource . provider ) . to receive ( :validatecmd ) . with ( input [ 0 ] ) . and_return ( true )
418
+ allow ( resource . provider ) . to receive ( :validatecmd ) . with ( input [ 1 ] ) . and_raise ( Puppet ::Error )
419
+
420
+ expect { resource [ param ] = input } . to raise_error ( Puppet ::ResourceError , /Parameter #{ param } failed/ )
399
421
end
400
- @test [ param ] = input
401
- expect ( @test [ param ] ) . to eq ( input )
402
- end
403
422
404
- it "should reject the array when all commands return invalid" do
405
- input = %w{ one two three }
406
- expect ( @test . provider ) . to receive ( :validatecmd ) . exactly ( input . length ) . times . and_return ( false )
407
- @test [ param ] = input
408
- expect ( @test [ param ] ) . to eq ( input )
423
+ it "stops at the first invalid command" do
424
+ input = %w{ one two three }
425
+ allow ( resource . provider ) . to receive ( :validatecmd ) . with ( input [ 0 ] ) . and_raise ( Puppet ::Error )
426
+
427
+ expect ( resource . provider ) . not_to receive ( :validatecmd ) . with ( input [ 1 ] )
428
+ expect ( resource . provider ) . not_to receive ( :validatecmd ) . with ( input [ 2 ] )
429
+ expect { resource [ param ] = input } . to raise_error ( Puppet ::ResourceError , /Parameter #{ param } failed/ )
430
+ end
409
431
end
410
432
end
411
433
end
412
434
413
435
describe "when setting command" do
414
436
subject { described_class . new ( :name => @command ) }
415
- it "fails when passed an Array" do
416
- expect { subject [ :command ] = [ ] } . to raise_error Puppet ::Error , /Command must be a String/
417
- end
418
-
419
437
it "fails when passed a Hash" do
420
- expect { subject [ :command ] = { } } . to raise_error Puppet ::Error , /Command must be a String/
438
+ expect { subject [ :command ] = { } } . to raise_error Puppet ::Error , /Command must be a String or Array<String> /
421
439
end
422
440
end
423
441
@@ -759,6 +777,35 @@ def instance(path)
759
777
end
760
778
end
761
779
780
+ context 'with an array of arrays with multiple items' do
781
+ before do
782
+ [ true , false ] . each do |check |
783
+ allow ( @test . provider ) . to receive ( :run ) . with ( [ @pass , '--flag' ] , check ) .
784
+ and_return ( [ 'test output' , @pass_status ] )
785
+ allow ( @test . provider ) . to receive ( :run ) . with ( [ @fail , '--flag' ] , check ) .
786
+ and_return ( [ 'test output' , @fail_status ] )
787
+ allow ( @test . provider ) . to receive ( :run ) . with ( [ @pass ] , check ) .
788
+ and_return ( [ 'test output' , @pass_status ] )
789
+ allow ( @test . provider ) . to receive ( :run ) . with ( [ @fail ] , check ) .
790
+ and_return ( [ 'test output' , @fail_status ] )
791
+ end
792
+ end
793
+ it "runs if all the commands exits non-zero" do
794
+ @test [ param ] = [ [ @fail , '--flag' ] , [ @fail ] , [ @fail , '--flag' ] ]
795
+ expect ( @test . check_all_attributes ) . to eq ( true )
796
+ end
797
+
798
+ it "does not run if one command exits zero" do
799
+ @test [ param ] = [ [ @pass , '--flag' ] , [ @pass ] , [ @fail , '--flag' ] ]
800
+ expect ( @test . check_all_attributes ) . to eq ( false )
801
+ end
802
+
803
+ it "does not run if all command exits zero" do
804
+ @test [ param ] = [ [ @pass , '--flag' ] , [ @pass ] , [ @pass , '--flag' ] ]
805
+ expect ( @test . check_all_attributes ) . to eq ( false )
806
+ end
807
+ end
808
+
762
809
it "should emit output to debug" do
763
810
Puppet ::Util ::Log . level = :debug
764
811
@test [ param ] = @fail
0 commit comments