summaryrefslogtreecommitdiffstats
path: root/test/util/loadedfile.rb
blob: 801ba5b2a92d5e108184cfdcbe72d002fb4b6a57 (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
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
#!/usr/bin/env ruby

$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppet'
require 'puppet/util/loadedfile'
require 'puppettest'

class TestLoadedFile < Test::Unit::TestCase
	include PuppetTest
    def test_file
        Puppet[:filetimeout] = 0
        file = nil
        path = tempfile()
        File.open(path, "w") { |f| f.puts "yayness" }
        assert_nothing_raised {
            file = Puppet::Util::LoadedFile.new(path)
        }

        assert(!file.changed?, "File incorrectly returned changed")

        File.open(path, "w") { |f| f.puts "booness" }
        #file.tstamp = File.stat(path).ctime - 5
        new = File.stat(path).ctime - 5
        file.tstamp = new

        assert(file.changed?, "File did not catch change")
    end

    def test_timeout
        Puppet[:filetimeout] = 50
        path = tempfile()

        File.open(path, "w") { |f| f.puts "yay" }
        file = nil
        assert_nothing_raised {
            file = Puppet::Util::LoadedFile.new(path)
        }

        assert_nothing_raised {
            assert(!file.changed?,
                "File thought it changed immediately")
        }

        sleep 1
        File.open(path, "w") { |f| f.puts "yay" }
        #file.tstamp = File.stat(path).ctime - 5

        assert(!file.changed?,
            "File was marked as changed too soon")

        Puppet[:filetimeout] = 0
        assert(file.changed?,
            "File was not marked as changed soon enough")
    end

    def test_stamp
        file = tempfile()
        File.open(file, "w") { |f| f.puts "" }
        obj = nil
        assert_nothing_raised {
            obj = Puppet::Util::LoadedFile.new(file)
        }

        # Make sure we don't refresh
        Puppet[:filetimeout] = 50

        stamp = File.stat(file).ctime

        assert_equal(stamp, obj.stamp)

        sleep 1
        # Now change the file, and make sure the stamp doesn't update yet
        File.open(file, "w") { |f| f.puts "" }
        assert_equal(stamp, obj.stamp,
            "File prematurely refreshed")

        Puppet[:filetimeout] = 0
        assert_equal(File.stat(file).ctime, obj.stamp,
            "File did not refresh")
    end

    # Testing #394.
    def test_changed_missing_file
        file = tempfile()
        File.open(file, "w") { |f| f.puts "" }
        obj = nil
        assert_nothing_raised {
            obj = Puppet::Util::LoadedFile.new(file)
        }
        Puppet[:filetimeout] = -10

        assert_nothing_raised {
            obj.changed?
        }

        # Now remove the file
        File.unlink(file)

        assert_nothing_raised("removed file threw an error") {
            assert(obj.changed?, "File was not considered changed when missing")
        }
    end

    # Make sure negative values always result in change notifications.
    def test_negative_always_changes
        file = tempfile()
        File.open(file, "w") { |f| f.puts "" }
        obj = nil
        assert_nothing_raised {
            obj = Puppet::Util::LoadedFile.new(file)
        }

        assert(! obj.changed?, "file with no change considered changed")
        # Now set a negative value
        Puppet[:filetimeout] = -1

        assert(obj.changed?, "negative file timeout did not disable checking")
    end
end