diff options
-rw-r--r-- | lib/puppet/storage.rb | 40 | ||||
-rw-r--r-- | test/other/tc_state.rb | 36 |
2 files changed, 64 insertions, 12 deletions
diff --git a/lib/puppet/storage.rb b/lib/puppet/storage.rb index 28d593db0..5dac7dc35 100644 --- a/lib/puppet/storage.rb +++ b/lib/puppet/storage.rb @@ -4,26 +4,44 @@ module Puppet # a class for storing state class Storage include Singleton - @@state = Hash.new { |hash,key| - hash[key] = Hash.new(nil) - } - @@splitchar = "\t" def initialize self.class.load end + def Storage.clear + @@state = nil + Storage.init + end + + def Storage.init + @@state = Hash.new { |hash,key| + hash[key] = Hash.new(nil) + } + @@splitchar = "\t" + end + + self.init + def Storage.load - # XXX I should probably use a better default state dir - Puppet[:statefile] ||= "/var/tmp/puppetstate" - return unless File.exists?(Puppet[:statefile]) + if Puppet[:statefile].nil? + raise "Somehow the statefile is nil" + end + + unless File.exists?(Puppet[:statefile]) + Puppet.info "Statefile %s does not exist" % Puppet[:statefile] + return + end + Puppet.debug "Loading statefile %s" % Puppet[:statefile] File.open(Puppet[:statefile]) { |file| - file.gets { |line| + file.each { |line| myclass, key, value = line.split(@@splitchar) - @@state[myclass][key] = Marshal::load(value) + @@state[eval(myclass)][key] = Marshal::load(value) } } + + Puppet.debug "Loaded state is %s" % @@state.inspect end def Storage.state(myclass) @@ -39,7 +57,7 @@ module Puppet begin Puppet.recmkdir(Puppet[:statefile]) Puppet.info "Creating state directory %s" % - File.basename(Puppet[:statefile]) + File.dirname(Puppet[:statefile]) rescue => detail Puppet.err "Could not create state file: %s" % detail return @@ -58,6 +76,8 @@ module Puppet } } } + + Puppet.debug "Stored state is %s" % @@state.inspect end end end diff --git a/test/other/tc_state.rb b/test/other/tc_state.rb index 3720f6286..f295a0061 100644 --- a/test/other/tc_state.rb +++ b/test/other/tc_state.rb @@ -9,12 +9,20 @@ require 'test/unit' # $Id$ +class StorageTestingClass +end + class TestStorage < Test::Unit::TestCase def setup Puppet[:loglevel] = :debug if __FILE__ == $0 Puppet[:statefile] = "/var/tmp/puppetteststate" end + def teardown + system("rm -f %s" % Puppet[:statefile]) + Puppet::Storage.clear + end + def test_simple state = nil assert_nothing_raised { @@ -28,6 +36,12 @@ class TestStorage < Test::Unit::TestCase assert_nothing_raised { Puppet::Storage.store } + + # clear the memory, so we're sure we're hitting the state file + assert_nothing_raised { + Puppet::Storage.clear + Puppet::Storage.init + } assert_nothing_raised { Puppet::Storage.load } @@ -54,7 +68,25 @@ class TestStorage < Test::Unit::TestCase assert(state) end - def teardown - system("rm -f %s" % Puppet[:statefile]) + def test_update + state = Puppet::Storage.state(StorageTestingClass) + state["testing"] = "yayness" + Puppet::Storage.store + assert(FileTest.exists?(Puppet[:statefile])) + end + + def test_hashstorage + state = Puppet::Storage.state(StorageTestingClass) + hash = { + :yay => "boo", + :rah => "foo" + } + state["testing"] = hash + Puppet::Storage.store + Puppet::Storage.clear + Puppet::Storage.init + Puppet::Storage.load + state = Puppet::Storage.state(StorageTestingClass) + assert_equal(hash, state["testing"]) end end |