diff options
-rwxr-xr-x | lib/puppet/type/exec.rb | 14 | ||||
-rwxr-xr-x | spec/unit/type/exec_spec.rb | 16 |
2 files changed, 20 insertions, 10 deletions
diff --git a/lib/puppet/type/exec.rb b/lib/puppet/type/exec.rb index 4458bf081..be0ece023 100755 --- a/lib/puppet/type/exec.rb +++ b/lib/puppet/type/exec.rb @@ -229,19 +229,17 @@ module Puppet newparam(:timeout) do desc "The maximum time the command should take. If the command takes longer than the timeout, the command is considered to have failed - and will be stopped. Use any negative number to disable the timeout. + and will be stopped. Use 0 to disable the timeout. The time is specified in seconds." munge do |value| value = value.shift if value.is_a?(Array) - if value.is_a?(String) - unless value =~ /^[-\d.]+$/ - raise ArgumentError, "The timeout must be a number." - end - Float(value) - else - value + begin + value = Float(value) + rescue ArgumentError => e + raise ArgumentError, "The timeout must be a number." end + [value, 0.0].max end defaultto 300 diff --git a/spec/unit/type/exec_spec.rb b/spec/unit/type/exec_spec.rb index 07ad1df4c..e155506b0 100755 --- a/spec/unit/type/exec_spec.rb +++ b/spec/unit/type/exec_spec.rb @@ -336,7 +336,7 @@ describe Puppet::Type.type(:exec) do end describe "when setting timeout" do - [-3.5, -1, 0, 0.1, 1, 10, 4294967295].each do |valid| + [0, 0.1, 1, 10, 4294967295].each do |valid| it "should accept '#{valid}' as valid" do @exec[:timeout] = valid @exec[:timeout].should == valid @@ -348,7 +348,7 @@ describe Puppet::Type.type(:exec) do end end - ['1/2', '1_000_000', '+12', '', 'foo'].each do |invalid| + ['1/2', '', 'foo', '5foo'].each do |invalid| it "should reject '#{invalid}' as invalid" do expect { @exec[:timeout] = invalid }. should raise_error Puppet::Error, /The timeout must be a number/ @@ -366,6 +366,18 @@ describe Puppet::Type.type(:exec) do sleep_exec = Puppet::Type.type(:exec).new(:name => 'sleep 1', :path => ['/bin'], :timeout => '0.2') lambda { sleep_exec.refresh }.should raise_error Puppet::Error, "Command exceeded timeout" end + + it "should convert timeout to a float" do + resource = Puppet::Type.type(:exec).new :command => "/bin/false", :timeout => "12" + resource[:timeout].should be_a(Float) + resource[:timeout].should == 12.0 + end + + it "should munge negative timeouts to 0.0" do + resource = Puppet::Type.type(:exec).new :command => "/bin/false", :timeout => "-12.0" + resource.parameter(:timeout).value.should be_a(Float) + resource.parameter(:timeout).value.should == 0.0 + end end describe "when setting tries" do |