summaryrefslogtreecommitdiffstats
path: root/spec/unit/util/loadedfile_spec.rb
blob: 083120e20fa8ce2aeda3db07936f718fb8a5cb50 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'tempfile'
require 'puppet/util/loadedfile'

describe Puppet::Util::LoadedFile do
    before(:each) do
        @f = Tempfile.new('loadedfile_test')
        @f.puts "yayness"
        @f.flush

        @loaded = Puppet::Util::LoadedFile.new(@f.path)

        fake_ctime = Time.now - (2 * Puppet[:filetimeout])
        @stat = stub('stat', :ctime => fake_ctime)
        @fake_now = Time.now + (2 * Puppet[:filetimeout])
    end

    it "should recognize when the file has not changed" do
        # Use fake "now" so that we can be sure changed? actually checks, without sleeping
        # for Puppet[:filetimeout] seconds.
        Time.stubs(:now).returns(@fake_now)
        @loaded.changed?.should == false
    end

    it "should recognize when the file has changed" do
        # Fake File.stat so we don't have to depend on the filesystem granularity. Doing a flush()
        # just didn't do the job.
        File.stubs(:stat).returns(@stat)
        # Use fake "now" so that we can be sure changed? actually checks, without sleeping
        # for Puppet[:filetimeout] seconds.
        Time.stubs(:now).returns(@fake_now)
        @loaded.changed?.should be_an_instance_of(Time)
    end

    it "should not catch a change until the timeout has elapsed" do
        # Fake File.stat so we don't have to depend on the filesystem granularity. Doing a flush()
        # just didn't do the job.
        File.stubs(:stat).returns(@stat)
        @loaded.changed?.should be(false)
        # Use fake "now" so that we can be sure changed? actually checks, without sleeping
        # for Puppet[:filetimeout] seconds.
        Time.stubs(:now).returns(@fake_now)
        @loaded.changed?.should_not be(false)
    end

    it "should consider a file changed when that file is missing" do
        @f.close!
        # Use fake "now" so that we can be sure changed? actually checks, without sleeping
        # for Puppet[:filetimeout] seconds.
        Time.stubs(:now).returns(@fake_now)
        @loaded.changed?.should_not be(false)
    end

    it "should disable checking if Puppet[:filetimeout] is negative" do
        Puppet[:filetimeout] = -1
        @loaded.changed?.should_not be(false)
    end

    after(:each) do
        @f.close
    end
end