diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-07-21 20:11:13 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-07-21 20:11:13 +0000 |
| commit | 310b3a11eec563c4687041f9ae1a0b894571bc05 (patch) | |
| tree | ebaf6941383ae7c2616f59258c0e95c83eb21044 | |
| parent | c8537a51c61290c9ba32d57fed31b389dbbdeb29 (diff) | |
| download | puppet-310b3a11eec563c4687041f9ae1a0b894571bc05.tar.gz puppet-310b3a11eec563c4687041f9ae1a0b894571bc05.tar.xz puppet-310b3a11eec563c4687041f9ae1a0b894571bc05.zip | |
Adding timeout functionality to the ParsedFile class, in preparation to adding config reloading to the Config class.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1419 980ebf18-57e1-0310-9a29-db15c13687c0
| -rw-r--r-- | lib/puppet/config.rb | 11 | ||||
| -rwxr-xr-x | lib/puppet/parsedfile.rb | 44 | ||||
| -rwxr-xr-x | test/other/config.rb | 3 | ||||
| -rwxr-xr-x | test/other/parsedfile.rb | 29 |
4 files changed, 69 insertions, 18 deletions
diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb index 701ee5942..f70201cee 100644 --- a/lib/puppet/config.rb +++ b/lib/puppet/config.rb @@ -92,9 +92,11 @@ class Config end # Remove all set values. - def clear + def clear(exceptcli = false) @config.each { |name, obj| - obj.clear + unless exceptcli and obj.setbycli + obj.clear + end } @used = [] end @@ -216,7 +218,6 @@ class Config # Parse a configuration file. def parse(file) text = nil - @file = file begin text = File.read(file) @@ -468,8 +469,8 @@ Generated on #{Time.now}. } topbucket = Puppet::TransBucket.new - if defined? @file and @file - topbucket.name = @file + if defined? @file.file and @file.file + topbucket.name = @file.file else topbucket.name = "configtop" end diff --git a/lib/puppet/parsedfile.rb b/lib/puppet/parsedfile.rb index e32a2daef..c5168b2b1 100755 --- a/lib/puppet/parsedfile.rb +++ b/lib/puppet/parsedfile.rb @@ -4,35 +4,55 @@ require 'puppet' module Puppet + class NoSuchFile < Puppet::Error; end class ParsedFile attr_reader :file + Puppet.config.setdefaults(:puppet, + :filetimeout => [ 15, + "The minimum time to wait between checking for updates in + configuration files." + ] + ) + # Determine whether the file has changed and thus whether it should # be reparsed def changed? - tmp = self.stamp - retval = false - if tmp != @stamp - retval = true - @stamp = tmp - end - @statted = Time.now + # Don't actually stat the file more often than filetimeout. + if Time.now - @statted > Puppet[:filetimeout] + tmp = stamp() - return retval + if tmp == @tstamp + return false + else + @tstamp = tmp + return true + end + else + return false + end end # Create the file. Must be passed the file path. def initialize(file) @file = file unless FileTest.exists?(@file) - raise Puppet::DevError, "Can not use a non-existent file for parsing" + raise Puppet::NoSuchFile, "Can not use a non-existent file for parsing" end - @stamp = self.stamp - @statted = Time.now + @tstamp = stamp() end + private + + # Provide a hook for setting the timestamp during testing, so we don't + # have to depend on the granularity of the filesystem. + attr_writer :tstamp + def stamp - File.stat(@file).ctime + @statted = Time.now + return File.stat(@file).ctime end end end + +# $Id$ diff --git a/test/other/config.rb b/test/other/config.rb index 9f941af4e..2b3c985a4 100755 --- a/test/other/config.rb +++ b/test/other/config.rb @@ -694,6 +694,9 @@ inttest = 27 "%s got created as wrong type" % value.inspect) end end + + def test_reparse + end end # $Id$ diff --git a/test/other/parsedfile.rb b/test/other/parsedfile.rb index e343e5478..2f87308ab 100755 --- a/test/other/parsedfile.rb +++ b/test/other/parsedfile.rb @@ -12,6 +12,7 @@ require 'test/unit' class TestParsedFile < Test::Unit::TestCase include TestPuppet def test_file + Puppet[:filetimeout] = 0 file = nil path = tempfile() File.open(path, "w") { |f| f.puts "yayness" } @@ -21,11 +22,37 @@ class TestParsedFile < Test::Unit::TestCase assert(!file.changed?, "File incorrectly returned changed") - sleep(1) + #sleep(1) File.open(path, "w") { |f| f.puts "booness" } + file.send("tstamp=".intern, File.stat(path).ctime - 5) 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::ParsedFile.new(path) + } + + assert_nothing_raised { file.changed? } + + File.open(path, "w") { |f| f.puts "yay" } + file.send("tstamp=".intern, 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 end # $Id$ |
