summaryrefslogtreecommitdiff
path: root/spec/unit/settings/duration_setting_spec.rb
blob: 532e50ae4ce0fbf194be23e3ae8d2a52f18840f1 (plain)
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
#!/usr/bin/env ruby
require 'spec_helper'

require 'puppet/settings'
require 'puppet/settings/duration_setting'

describe Puppet::Settings::DurationSetting do
  subject { described_class.new(:settings => mock('settings'), :desc => "test") }

  describe "when munging the setting" do
    it "should return the same value if given an integer" do
      subject.munge(5).should == 5
    end

    it "should return an integer if given a decimal string" do
      subject.munge("12").should == 12
    end

    it "should fail if given anything but a well-formed string or integer" do
      [ '', 'foo', '2 d', '2d ', true, Time.now, 8.3, [] ].each do |value|
        expect { subject.munge(value) }.to raise_error(Puppet::Settings::ValidationError)
      end
    end

    it "should parse strings with units of 'y', 'd', 'h', 'm', or 's'" do
      # Note: the year value won't jive with most methods of calculating
      # year due to the Julian calandar having 365.25 days in a year
      {
        '3y' => 94608000,
        '3d' => 259200,
        '3h' => 10800,
        '3m' => 180,
        '3s' => 3
      }.each do |value, converted_value|
        # subject.munge(value).should == converted_value
        subject.munge(value).should == converted_value
      end
    end

    # This is to support the `filetimeout` setting
    it "should allow negative values" do
      subject.munge(-1).should == -1
    end
  end
end