diff options
author | Luke Kanies <luke@madstop.com> | 2005-07-11 18:45:31 +0000 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2005-07-11 18:45:31 +0000 |
commit | d14dc32d1800e61a4b509ab8410c848ae472bdd1 (patch) | |
tree | 0acde8366233d2779d68abde39f4ba8747ca8b52 | |
parent | 96f3980a57f8fd24aff801420a0813bad9bb20d7 (diff) | |
download | puppet-d14dc32d1800e61a4b509ab8410c848ae472bdd1.tar.gz puppet-d14dc32d1800e61a4b509ab8410c848ae472bdd1.tar.xz puppet-d14dc32d1800e61a4b509ab8410c848ae472bdd1.zip |
fixing storage class; it was not actually correctly retrieving state from disk
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@355 980ebf18-57e1-0310-9a29-db15c13687c0
-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 |