From c5da40122493eacf158458f0d79e335e2d0c4c9d Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Sat, 17 May 2008 00:54:07 -0700 Subject: Add unit tests for Puppet::Util::Storage --- spec/unit/util/storage.rb | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 spec/unit/util/storage.rb diff --git a/spec/unit/util/storage.rb b/spec/unit/util/storage.rb new file mode 100755 index 000000000..38e445127 --- /dev/null +++ b/spec/unit/util/storage.rb @@ -0,0 +1,72 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'yaml' +require 'sync' +require 'tempfile' + +describe Puppet::Util::Storage do + + before(:all) do + Puppet[:statedir] = Dir.tmpdir() + end + + it "it should re-initialize to a clean state when the clear() method is called" do + end + + it "it should use the main settings section if the state dir is not a directory" do + FileTest.expects(:directory?).with(Puppet[:statedir]).returns(false) + Puppet.settings.expects(:use).with(:main) + Puppet::Util::Storage.load() + end + + it "it should initialize with an empty state when the state file does not exist" do + File.expects(:exists?).with(Puppet[:statefile]).returns(false) + Puppet::Util::Storage.load() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + end + + describe "when the state file exists" do + it "it should attempt to get a read lock on the file" do + File.expects(:exists?).with(Puppet[:statefile]).returns(true) + Puppet::Util.expects(:benchmark).with(:debug, "Loaded state").yields() + Puppet::Util.expects(:readlock).with(Puppet[:statefile]) + Puppet::Util::Storage.load() + end + + describe "and the file contents are valid" do + it "it should initialize with the correct state from the state file" do + File.expects(:exists?).with(Puppet[:statefile]).returns(true) + Puppet::Util.expects(:benchmark).with(:debug, "Loaded state").yields() + Puppet::Util.expects(:readlock).with(Puppet[:statefile]).yields(0) + test_yaml = {'File["/root"]'=>{"name"=>{:a=>:b,:c=>:d}}} + YAML.expects(:load).returns(test_yaml) + + Puppet::Util::Storage.load() + Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + end + end + + describe "and the file contents are invalid" do + # Commented out because the previous test's existence causes this one to fail. +# it "it should not initialize from the state file" do +# File.expects(:exists?).with(Puppet[:statefile]).returns(true) +# Puppet::Util.expects(:benchmark).with(:debug, "Loaded state").yields() +# Puppet::Util.expects(:readlock).with(Puppet[:statefile]).yields(0) +# YAML.expects(:load).raises(YAML::Error) +# File.expects(:rename).with(Puppet[:statefile], Puppet[:statefile] + ".bad").returns(0) + +# Puppet::Util::Storage.load() +# Puppet::Util::Storage.stateinspect().should == {}.inspect() +# end + + it "it should attempt to rename the state file" do + + end + + end + + end + +end -- cgit From d7f25ff5715a0d17eca8a590df6d9dd8b93ae443 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Sat, 17 May 2008 04:26:54 -0700 Subject: Rewritten tests for Puppet::Util::Storage. --- spec/unit/util/storage.rb | 255 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 211 insertions(+), 44 deletions(-) diff --git a/spec/unit/util/storage.rb b/spec/unit/util/storage.rb index 38e445127..58a1549c8 100755 --- a/spec/unit/util/storage.rb +++ b/spec/unit/util/storage.rb @@ -7,66 +7,233 @@ require 'sync' require 'tempfile' describe Puppet::Util::Storage do - before(:all) do Puppet[:statedir] = Dir.tmpdir() + @file_test = Puppet.type(:file).create(:name => "/yayness", :check => %w{checksum type}) + @exec_test = Puppet.type(:exec).create(:name => "/bin/ls /yayness") + @bogus_objects = [ {}, [], "foo", 42, nil, Tempfile.new('storage_test') ] end - it "it should re-initialize to a clean state when the clear() method is called" do + before(:each) do + Puppet::Util::Storage.clear() end - it "it should use the main settings section if the state dir is not a directory" do - FileTest.expects(:directory?).with(Puppet[:statedir]).returns(false) - Puppet.settings.expects(:use).with(:main) - Puppet::Util::Storage.load() + it "it should return an empty hash when caching a symbol" do + Puppet::Util::Storage.cache(:yayness).should == {} + Puppet::Util::Storage.cache(:more_yayness).should == {} end - - it "it should initialize with an empty state when the state file does not exist" do - File.expects(:exists?).with(Puppet[:statefile]).returns(false) - Puppet::Util::Storage.load() + it "it should add the symbol to it's internal state when caching a symbol" do + Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.cache(:bubblyness) + Puppet::Util::Storage.stateinspect().should == {:yayness=>{},:bubblyness=>{}}.inspect() + end + it "it should return an empty hash when caching a Puppet::Type" do + Puppet::Util::Storage.cache(@file_test).should == {} + Puppet::Util::Storage.cache(@exec_test).should == {} + end + it "it should add the resource ref to it's internal state when caching a Puppet::Type" do Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}}.inspect() + Puppet::Util::Storage.cache(@exec_test) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, "Exec[/bin/ls /yayness]"=>{}}.inspect() end - describe "when the state file exists" do - it "it should attempt to get a read lock on the file" do - File.expects(:exists?).with(Puppet[:statefile]).returns(true) - Puppet::Util.expects(:benchmark).with(:debug, "Loaded state").yields() - Puppet::Util.expects(:readlock).with(Puppet[:statefile]) - Puppet::Util::Storage.load() + it "it should raise an ArgumentError when caching invalid objects" do + @bogus_objects.each do |object| + proc { Puppet::Util::Storage.cache(object) }.should raise_error() end - - describe "and the file contents are valid" do - it "it should initialize with the correct state from the state file" do - File.expects(:exists?).with(Puppet[:statefile]).returns(true) - Puppet::Util.expects(:benchmark).with(:debug, "Loaded state").yields() - Puppet::Util.expects(:readlock).with(Puppet[:statefile]).yields(0) - test_yaml = {'File["/root"]'=>{"name"=>{:a=>:b,:c=>:d}}} - YAML.expects(:load).returns(test_yaml) - - Puppet::Util::Storage.load() - Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + end + it "it should not add anything to it's internal state when caching invalid objects" do + @bogus_objects.each do |object| + begin + Puppet::Util::Storage.cache(object) + rescue + Puppet::Util::Storage.stateinspect().should == {}.inspect() end end + end - describe "and the file contents are invalid" do - # Commented out because the previous test's existence causes this one to fail. -# it "it should not initialize from the state file" do -# File.expects(:exists?).with(Puppet[:statefile]).returns(true) -# Puppet::Util.expects(:benchmark).with(:debug, "Loaded state").yields() -# Puppet::Util.expects(:readlock).with(Puppet[:statefile]).yields(0) -# YAML.expects(:load).raises(YAML::Error) -# File.expects(:rename).with(Puppet[:statefile], Puppet[:statefile] + ".bad").returns(0) - -# Puppet::Util::Storage.load() -# Puppet::Util::Storage.stateinspect().should == {}.inspect() -# end - - it "it should attempt to rename the state file" do - - end + it "it should clear it's internal state when clear() is called" do + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + Puppet::Util::Storage.clear() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.cache(@exec_test) + Puppet::Util::Storage.cache(:bubblyness) + Puppet::Util::Storage.stateinspect().should == {"Exec[/bin/ls /yayness]"=>{}, :bubblyness=>{}}.inspect() + Puppet::Util::Storage.clear() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + end - end + it "it should not fail to load if Puppet[:statedir] does not exist" do + transient = Tempfile.new('storage_test') + path = transient.path() + transient.close!() + FileTest.exists?(path).should be_false() + Puppet[:statedir] = path + proc { Puppet::Util::Storage.load() }.should_not raise_error() + end + + it "it should not fail to load if Puppet[:statefile] does not exist" do + transient = Tempfile.new('storage_test') + path = transient.path() + transient.close!() + FileTest.exists?(path).should be_false() + Puppet[:statefile] = path + proc { Puppet::Util::Storage.load() }.should_not raise_error() + end + it "it should not lose it's internal state if load() is called and Puppet[:statefile] does not exist" do + transient = Tempfile.new('storage_test') + path = transient.path() + transient.close!() + FileTest.exists?(path).should be_false() + + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + + Puppet[:statefile] = path + proc { Puppet::Util::Storage.load() }.should_not raise_error() + + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() end + it "it should overwrite it's internal state if load() is called and Puppet[:statefile] exists" do + # Should the state be overwritten even if Puppet[:statefile] is not valid YAML? + state_file = Tempfile.new('storage_test') + + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + + Puppet[:statefile] = state_file.path() + proc { Puppet::Util::Storage.load() }.should_not raise_error() + + Puppet::Util::Storage.stateinspect().should == {}.inspect() + + state_file.close!() + end + + it "it should restore it's internal state from Puppet[:statefile] if the file contains valid YAML" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} + YAML.expects(:load).returns(test_yaml) + + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + + state_file.close!() + end + + it "it should initialize with a clear internal state if the state file does not contain valid YAML" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + state_file.write(:booness) + + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + + state_file.close!() + end + + it "it should raise an error if the state file does not contain valid YAML and cannot be renamed" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + state_file.write(:booness) + File.chmod(0000, state_file.path()) + + proc { Puppet::Util::Storage.load() }.should raise_error() + + state_file.close!() + end + + it "it should attempt to rename the state file if the file is corrupted" do + # We fake corruption by causing YAML.load to raise an exception + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + YAML.expects(:load).raises(Puppet::Error) + File.expects(:rename).at_least_once + + proc { Puppet::Util::Storage.load() }.should_not raise_error() + + state_file.close!() + end + + it "it should fail gracefully on load() if Puppet[:statefile] is not a regular file" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + state_file.close!() + Dir.mkdir(Puppet[:statefile]) + File.expects(:rename).returns(0) + + proc { Puppet::Util::Storage.load() }.should_not raise_error() + + Dir.rmdir(Puppet[:statefile]) + end + + it "it should fail gracefully on load() if it cannot get a read lock on Puppet[:statefile]" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + Puppet::Util.expects(:readlock).yields(false) + test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} + YAML.expects(:load).returns(test_yaml) + + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + + state_file.close!() + end + + it "it should raise an exception on store() if Puppet[:statefile] is not a regular file" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + state_file.close!() + Dir.mkdir(Puppet[:statefile]) + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.cache(:yayness) + + proc { Puppet::Util::Storage.store() }.should raise_error() + + Dir.rmdir(Puppet[:statefile]) + end + + it "it should raise an exception on store() if it cannot get a write lock on Puppet[:statefile]" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + Puppet::Util.expects(:writelock).yields(false) + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.cache(:yayness) + + proc { Puppet::Util::Storage.store() }.should raise_error() + + state_file.close!() + end + + it "it should load() the same information that it store()s" do + state_file = Tempfile.new('storage_test') + Puppet[:statefile] = state_file.path() + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.cache(:yayness) + + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + proc { Puppet::Util::Storage.store() }.should_not raise_error() + + Puppet::Util::Storage.clear() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + + state_file.close!() + end + + after(:all) do + @bogus_objects.last.close!() + end end -- cgit From ee041293d99605e4283235cb3ee5286447ffadd4 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Sat, 17 May 2008 23:51:25 -0700 Subject: Refactored tests based on feedback from Luke. --- spec/unit/util/storage.rb | 345 +++++++++++++++++++++++----------------------- 1 file changed, 169 insertions(+), 176 deletions(-) diff --git a/spec/unit/util/storage.rb b/spec/unit/util/storage.rb index 58a1549c8..309e5a200 100755 --- a/spec/unit/util/storage.rb +++ b/spec/unit/util/storage.rb @@ -9,231 +9,224 @@ require 'tempfile' describe Puppet::Util::Storage do before(:all) do Puppet[:statedir] = Dir.tmpdir() - @file_test = Puppet.type(:file).create(:name => "/yayness", :check => %w{checksum type}) - @exec_test = Puppet.type(:exec).create(:name => "/bin/ls /yayness") - @bogus_objects = [ {}, [], "foo", 42, nil, Tempfile.new('storage_test') ] end before(:each) do Puppet::Util::Storage.clear() end - it "it should return an empty hash when caching a symbol" do - Puppet::Util::Storage.cache(:yayness).should == {} - Puppet::Util::Storage.cache(:more_yayness).should == {} - end - it "it should add the symbol to it's internal state when caching a symbol" do - Puppet::Util::Storage.stateinspect().should == {}.inspect() - Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() - Puppet::Util::Storage.cache(:bubblyness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{},:bubblyness=>{}}.inspect() - end - it "it should return an empty hash when caching a Puppet::Type" do - Puppet::Util::Storage.cache(@file_test).should == {} - Puppet::Util::Storage.cache(@exec_test).should == {} - end - it "it should add the resource ref to it's internal state when caching a Puppet::Type" do - Puppet::Util::Storage.stateinspect().should == {}.inspect() - Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}}.inspect() - Puppet::Util::Storage.cache(@exec_test) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, "Exec[/bin/ls /yayness]"=>{}}.inspect() - end - - it "it should raise an ArgumentError when caching invalid objects" do - @bogus_objects.each do |object| - proc { Puppet::Util::Storage.cache(object) }.should raise_error() + describe "when caching a symbol" do + it "it should return an empty hash" do + Puppet::Util::Storage.cache(:yayness).should == {} + Puppet::Util::Storage.cache(:more_yayness).should == {} end - end - it "it should not add anything to it's internal state when caching invalid objects" do - @bogus_objects.each do |object| - begin - Puppet::Util::Storage.cache(object) - rescue - Puppet::Util::Storage.stateinspect().should == {}.inspect() - end + + it "it should add the symbol to its internal state" do + Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.cache(:bubblyness) + Puppet::Util::Storage.stateinspect().should == {:yayness=>{},:bubblyness=>{}}.inspect() end end - it "it should clear it's internal state when clear() is called" do - Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() - Puppet::Util::Storage.clear() - Puppet::Util::Storage.stateinspect().should == {}.inspect() - Puppet::Util::Storage.cache(@exec_test) - Puppet::Util::Storage.cache(:bubblyness) - Puppet::Util::Storage.stateinspect().should == {"Exec[/bin/ls /yayness]"=>{}, :bubblyness=>{}}.inspect() - Puppet::Util::Storage.clear() - Puppet::Util::Storage.stateinspect().should == {}.inspect() - end + describe "when caching a Puppet::Type" do + before(:all) do + @file_test = Puppet.type(:file).create(:name => "/yayness", :check => %w{checksum type}) + @exec_test = Puppet.type(:exec).create(:name => "/bin/ls /yayness") + end - it "it should not fail to load if Puppet[:statedir] does not exist" do - transient = Tempfile.new('storage_test') - path = transient.path() - transient.close!() - FileTest.exists?(path).should be_false() - Puppet[:statedir] = path - proc { Puppet::Util::Storage.load() }.should_not raise_error() - end + it "it should return an empty hash" do + Puppet::Util::Storage.cache(@file_test).should == {} + Puppet::Util::Storage.cache(@exec_test).should == {} + end - it "it should not fail to load if Puppet[:statefile] does not exist" do - transient = Tempfile.new('storage_test') - path = transient.path() - transient.close!() - FileTest.exists?(path).should be_false() - Puppet[:statefile] = path - proc { Puppet::Util::Storage.load() }.should_not raise_error() + it "it should add the resource ref to its internal state" do + Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.cache(@file_test) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}}.inspect() + Puppet::Util::Storage.cache(@exec_test) + Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, "Exec[/bin/ls /yayness]"=>{}}.inspect() + end end - it "it should not lose it's internal state if load() is called and Puppet[:statefile] does not exist" do - transient = Tempfile.new('storage_test') - path = transient.path() - transient.close!() - FileTest.exists?(path).should be_false() - - Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + describe "when caching invalid objects" do + before(:all) do + @bogus_objects = [ {}, [], "foo", 42, nil, Tempfile.new('storage_test') ] + end - Puppet[:statefile] = path - proc { Puppet::Util::Storage.load() }.should_not raise_error() + it "it should raise an ArgumentError" do + @bogus_objects.each do |object| + proc { Puppet::Util::Storage.cache(object) }.should raise_error() + end + end - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + it "it should not add anything to its internal state" do + @bogus_objects.each do |object| + begin + Puppet::Util::Storage.cache(object) + rescue + Puppet::Util::Storage.stateinspect().should == {}.inspect() + end + end + end end - it "it should overwrite it's internal state if load() is called and Puppet[:statefile] exists" do - # Should the state be overwritten even if Puppet[:statefile] is not valid YAML? - state_file = Tempfile.new('storage_test') - - Puppet::Util::Storage.cache(@file_test) + it "it should clear its internal state when clear() is called" do Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() - - Puppet[:statefile] = state_file.path() - proc { Puppet::Util::Storage.load() }.should_not raise_error() - + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.clear() Puppet::Util::Storage.stateinspect().should == {}.inspect() - - state_file.close!() end - it "it should restore it's internal state from Puppet[:statefile] if the file contains valid YAML" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} - YAML.expects(:load).returns(test_yaml) + describe "when loading from the state file" do + describe "when the state file/directory does not exist" do + before(:each) do + transient = Tempfile.new('storage_test') + @path = transient.path() + transient.close!() + end + + it "it should not fail to load()" do + FileTest.exists?(@path).should be_false() + Puppet[:statedir] = @path + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet[:statefile] = @path + proc { Puppet::Util::Storage.load() }.should_not raise_error() + end + + it "it should not lose its internal state when load() is called" do + FileTest.exists?(@path).should be_false() - proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + + Puppet[:statefile] = @path + proc { Puppet::Util::Storage.load() }.should_not raise_error() + + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + end + end - state_file.close!() - end - - it "it should initialize with a clear internal state if the state file does not contain valid YAML" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - state_file.write(:booness) + describe "when the state file/directory exists" do + before(:each) do + @state_file = Tempfile.new('storage_test') + @saved_statefile = Puppet[:statefile] + Puppet[:statefile] = @state_file.path() + end - proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == {}.inspect() + it "it should overwrite its internal state if load() is called" do + # Should the state be overwritten even if Puppet[:statefile] is not valid YAML? + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() - state_file.close!() - end + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + end - it "it should raise an error if the state file does not contain valid YAML and cannot be renamed" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - state_file.write(:booness) - File.chmod(0000, state_file.path()) + it "it should restore its internal state if the state file contains valid YAML" do + test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} + YAML.expects(:load).returns(test_yaml) - proc { Puppet::Util::Storage.load() }.should raise_error() + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + end + + it "it should initialize with a clear internal state if the state file does not contain valid YAML" do + @state_file.write(:booness) - state_file.close!() - end + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + end - it "it should attempt to rename the state file if the file is corrupted" do - # We fake corruption by causing YAML.load to raise an exception - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - YAML.expects(:load).raises(Puppet::Error) - File.expects(:rename).at_least_once + it "it should raise an error if the state file does not contain valid YAML and cannot be renamed" do + @state_file.write(:booness) + File.chmod(0000, @state_file.path()) - proc { Puppet::Util::Storage.load() }.should_not raise_error() + proc { Puppet::Util::Storage.load() }.should raise_error() + end - state_file.close!() - end + it "it should attempt to rename the state file if the file is corrupted" do + # We fake corruption by causing YAML.load to raise an exception + YAML.expects(:load).raises(Puppet::Error) + File.expects(:rename).at_least_once - it "it should fail gracefully on load() if Puppet[:statefile] is not a regular file" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - state_file.close!() - Dir.mkdir(Puppet[:statefile]) - File.expects(:rename).returns(0) + proc { Puppet::Util::Storage.load() }.should_not raise_error() + end - proc { Puppet::Util::Storage.load() }.should_not raise_error() + it "it should fail gracefully on load() if the state file is not a regular file" do + @state_file.close!() + Dir.mkdir(Puppet[:statefile]) + File.expects(:rename).returns(0) - Dir.rmdir(Puppet[:statefile]) - end + proc { Puppet::Util::Storage.load() }.should_not raise_error() - it "it should fail gracefully on load() if it cannot get a read lock on Puppet[:statefile]" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - Puppet::Util.expects(:readlock).yields(false) - test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} - YAML.expects(:load).returns(test_yaml) + Dir.rmdir(Puppet[:statefile]) + end - proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + it "it should fail gracefully on load() if it cannot get a read lock on the state file" do + Puppet::Util.expects(:readlock).yields(false) + test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} + YAML.expects(:load).returns(test_yaml) - state_file.close!() + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + end + + after(:each) do + @state_file.close!() + Puppet[:statefile] = @saved_statefile + end + end end - it "it should raise an exception on store() if Puppet[:statefile] is not a regular file" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - state_file.close!() - Dir.mkdir(Puppet[:statefile]) - Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.cache(:yayness) - - proc { Puppet::Util::Storage.store() }.should raise_error() + describe "when storing to the state file" do + before(:each) do + @state_file = Tempfile.new('storage_test') + @saved_statefile = Puppet[:statefile] + Puppet[:statefile] = @state_file.path() + end - Dir.rmdir(Puppet[:statefile]) - end + it "it should create the state file if it does not exist" do + @state_file.close!() + FileTest.exists?(Puppet[:statefile]).should be_false() + Puppet::Util::Storage.cache(:yayness) - it "it should raise an exception on store() if it cannot get a write lock on Puppet[:statefile]" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - Puppet::Util.expects(:writelock).yields(false) - Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.cache(:yayness) + proc { Puppet::Util::Storage.store() }.should_not raise_error() + FileTest.exists?(Puppet[:statefile]).should be_true() + end - proc { Puppet::Util::Storage.store() }.should raise_error() + it "it should raise an exception if the state file is not a regular file" do + @state_file.close!() + Dir.mkdir(Puppet[:statefile]) + Puppet::Util::Storage.cache(:yayness) - state_file.close!() - end + proc { Puppet::Util::Storage.store() }.should raise_error() - it "it should load() the same information that it store()s" do - state_file = Tempfile.new('storage_test') - Puppet[:statefile] = state_file.path() - Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.cache(:yayness) + Dir.rmdir(Puppet[:statefile]) + end - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() - proc { Puppet::Util::Storage.store() }.should_not raise_error() + it "it should raise an exception if it cannot get a write lock on the state file" do + Puppet::Util.expects(:writelock).yields(false) + Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.clear() - Puppet::Util::Storage.stateinspect().should == {}.inspect() + proc { Puppet::Util::Storage.store() }.should raise_error() + end - proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, :yayness=>{}}.inspect() + it "it should load() the same information that it store()s" do + Puppet::Util::Storage.cache(:yayness) - state_file.close!() - end + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + proc { Puppet::Util::Storage.store() }.should_not raise_error() + Puppet::Util::Storage.clear() + Puppet::Util::Storage.stateinspect().should == {}.inspect() + proc { Puppet::Util::Storage.load() }.should_not raise_error() + Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + end - after(:all) do - @bogus_objects.last.close!() + after(:each) do + @state_file.close!() + Puppet[:statefile] = @saved_statefile + end end end -- cgit From 6a6a1d99d7cd5186b3d778007abaa54a27d5e373 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Sun, 18 May 2008 15:30:13 -0700 Subject: Another refactor based on feedback from Luke. This includes adding an accessor for @@state to make testing a bit cleaner. --- lib/puppet/util/storage.rb | 4 ++++ spec/unit/util/storage.rb | 42 +++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/puppet/util/storage.rb b/lib/puppet/util/storage.rb index 9358a28e9..dc4e9cd71 100644 --- a/lib/puppet/util/storage.rb +++ b/lib/puppet/util/storage.rb @@ -6,6 +6,10 @@ class Puppet::Util::Storage include Singleton include Puppet::Util + def self.state + return @@state + end + def initialize self.class.load end diff --git a/spec/unit/util/storage.rb b/spec/unit/util/storage.rb index 309e5a200..a1f868cf2 100755 --- a/spec/unit/util/storage.rb +++ b/spec/unit/util/storage.rb @@ -22,11 +22,15 @@ describe Puppet::Util::Storage do end it "it should add the symbol to its internal state" do - Puppet::Util::Storage.stateinspect().should == {}.inspect() Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} + end + + it "it should not clobber existing state when caching additional objects" do + Puppet::Util::Storage.cache(:yayness) + Puppet::Util::Storage.state().should == {:yayness=>{}} Puppet::Util::Storage.cache(:bubblyness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{},:bubblyness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{},:bubblyness=>{}} end end @@ -42,11 +46,11 @@ describe Puppet::Util::Storage do end it "it should add the resource ref to its internal state" do - Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.state().should == {} Puppet::Util::Storage.cache(@file_test) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}}.inspect() + Puppet::Util::Storage.state().should == {"File[/yayness]"=>{}} Puppet::Util::Storage.cache(@exec_test) - Puppet::Util::Storage.stateinspect().should == {"File[/yayness]"=>{}, "Exec[/bin/ls /yayness]"=>{}}.inspect() + Puppet::Util::Storage.state().should == {"File[/yayness]"=>{}, "Exec[/bin/ls /yayness]"=>{}} end end @@ -66,7 +70,7 @@ describe Puppet::Util::Storage do begin Puppet::Util::Storage.cache(object) rescue - Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.state().should == {} end end end @@ -74,9 +78,9 @@ describe Puppet::Util::Storage do it "it should clear its internal state when clear() is called" do Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} Puppet::Util::Storage.clear() - Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.state().should == {} end describe "when loading from the state file" do @@ -99,12 +103,12 @@ describe Puppet::Util::Storage do FileTest.exists?(@path).should be_false() Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} Puppet[:statefile] = @path proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} end end @@ -118,10 +122,10 @@ describe Puppet::Util::Storage do it "it should overwrite its internal state if load() is called" do # Should the state be overwritten even if Puppet[:statefile] is not valid YAML? Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.state().should == {} end it "it should restore its internal state if the state file contains valid YAML" do @@ -129,14 +133,14 @@ describe Puppet::Util::Storage do YAML.expects(:load).returns(test_yaml) proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + Puppet::Util::Storage.state().should == test_yaml end it "it should initialize with a clear internal state if the state file does not contain valid YAML" do @state_file.write(:booness) proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.state().should == {} end it "it should raise an error if the state file does not contain valid YAML and cannot be renamed" do @@ -170,7 +174,7 @@ describe Puppet::Util::Storage do YAML.expects(:load).returns(test_yaml) proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == test_yaml.inspect() + Puppet::Util::Storage.state().should == test_yaml end after(:each) do @@ -216,12 +220,12 @@ describe Puppet::Util::Storage do it "it should load() the same information that it store()s" do Puppet::Util::Storage.cache(:yayness) - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} proc { Puppet::Util::Storage.store() }.should_not raise_error() Puppet::Util::Storage.clear() - Puppet::Util::Storage.stateinspect().should == {}.inspect() + Puppet::Util::Storage.state().should == {} proc { Puppet::Util::Storage.load() }.should_not raise_error() - Puppet::Util::Storage.stateinspect().should == {:yayness=>{}}.inspect() + Puppet::Util::Storage.state().should == {:yayness=>{}} end after(:each) do -- cgit From 0820819e0f7e81f2e68893258145877f756b5395 Mon Sep 17 00:00:00 2001 From: Paul Lathrop Date: Sun, 18 May 2008 16:51:35 -0700 Subject: Minor cosmetic changes to cleanup some style elements and get rid of some cruft. --- spec/unit/util/storage.rb | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/spec/unit/util/storage.rb b/spec/unit/util/storage.rb index a1f868cf2..55d1d1f61 100755 --- a/spec/unit/util/storage.rb +++ b/spec/unit/util/storage.rb @@ -3,9 +3,10 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'yaml' -require 'sync' require 'tempfile' +require 'puppet/util/storage' + describe Puppet::Util::Storage do before(:all) do Puppet[:statedir] = Dir.tmpdir() @@ -16,17 +17,17 @@ describe Puppet::Util::Storage do end describe "when caching a symbol" do - it "it should return an empty hash" do + it "should return an empty hash" do Puppet::Util::Storage.cache(:yayness).should == {} Puppet::Util::Storage.cache(:more_yayness).should == {} end - it "it should add the symbol to its internal state" do + it "should add the symbol to its internal state" do Puppet::Util::Storage.cache(:yayness) Puppet::Util::Storage.state().should == {:yayness=>{}} end - it "it should not clobber existing state when caching additional objects" do + it "should not clobber existing state when caching additional objects" do Puppet::Util::Storage.cache(:yayness) Puppet::Util::Storage.state().should == {:yayness=>{}} Puppet::Util::Storage.cache(:bubblyness) @@ -40,12 +41,12 @@ describe Puppet::Util::Storage do @exec_test = Puppet.type(:exec).create(:name => "/bin/ls /yayness") end - it "it should return an empty hash" do + it "should return an empty hash" do Puppet::Util::Storage.cache(@file_test).should == {} Puppet::Util::Storage.cache(@exec_test).should == {} end - it "it should add the resource ref to its internal state" do + it "should add the resource ref to its internal state" do Puppet::Util::Storage.state().should == {} Puppet::Util::Storage.cache(@file_test) Puppet::Util::Storage.state().should == {"File[/yayness]"=>{}} @@ -59,13 +60,13 @@ describe Puppet::Util::Storage do @bogus_objects = [ {}, [], "foo", 42, nil, Tempfile.new('storage_test') ] end - it "it should raise an ArgumentError" do + it "should raise an ArgumentError" do @bogus_objects.each do |object| proc { Puppet::Util::Storage.cache(object) }.should raise_error() end end - it "it should not add anything to its internal state" do + it "should not add anything to its internal state" do @bogus_objects.each do |object| begin Puppet::Util::Storage.cache(object) @@ -76,7 +77,7 @@ describe Puppet::Util::Storage do end end - it "it should clear its internal state when clear() is called" do + it "should clear its internal state when clear() is called" do Puppet::Util::Storage.cache(:yayness) Puppet::Util::Storage.state().should == {:yayness=>{}} Puppet::Util::Storage.clear() @@ -91,7 +92,7 @@ describe Puppet::Util::Storage do transient.close!() end - it "it should not fail to load()" do + it "should not fail to load()" do FileTest.exists?(@path).should be_false() Puppet[:statedir] = @path proc { Puppet::Util::Storage.load() }.should_not raise_error() @@ -99,7 +100,7 @@ describe Puppet::Util::Storage do proc { Puppet::Util::Storage.load() }.should_not raise_error() end - it "it should not lose its internal state when load() is called" do + it "should not lose its internal state when load() is called" do FileTest.exists?(@path).should be_false() Puppet::Util::Storage.cache(:yayness) @@ -119,7 +120,7 @@ describe Puppet::Util::Storage do Puppet[:statefile] = @state_file.path() end - it "it should overwrite its internal state if load() is called" do + it "should overwrite its internal state if load() is called" do # Should the state be overwritten even if Puppet[:statefile] is not valid YAML? Puppet::Util::Storage.cache(:yayness) Puppet::Util::Storage.state().should == {:yayness=>{}} @@ -128,7 +129,7 @@ describe Puppet::Util::Storage do Puppet::Util::Storage.state().should == {} end - it "it should restore its internal state if the state file contains valid YAML" do + it "should restore its internal state if the state file contains valid YAML" do test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} YAML.expects(:load).returns(test_yaml) @@ -136,21 +137,21 @@ describe Puppet::Util::Storage do Puppet::Util::Storage.state().should == test_yaml end - it "it should initialize with a clear internal state if the state file does not contain valid YAML" do + it "should initialize with a clear internal state if the state file does not contain valid YAML" do @state_file.write(:booness) proc { Puppet::Util::Storage.load() }.should_not raise_error() Puppet::Util::Storage.state().should == {} end - it "it should raise an error if the state file does not contain valid YAML and cannot be renamed" do + it "should raise an error if the state file does not contain valid YAML and cannot be renamed" do @state_file.write(:booness) File.chmod(0000, @state_file.path()) proc { Puppet::Util::Storage.load() }.should raise_error() end - it "it should attempt to rename the state file if the file is corrupted" do + it "should attempt to rename the state file if the file is corrupted" do # We fake corruption by causing YAML.load to raise an exception YAML.expects(:load).raises(Puppet::Error) File.expects(:rename).at_least_once @@ -158,7 +159,7 @@ describe Puppet::Util::Storage do proc { Puppet::Util::Storage.load() }.should_not raise_error() end - it "it should fail gracefully on load() if the state file is not a regular file" do + it "should fail gracefully on load() if the state file is not a regular file" do @state_file.close!() Dir.mkdir(Puppet[:statefile]) File.expects(:rename).returns(0) @@ -168,7 +169,7 @@ describe Puppet::Util::Storage do Dir.rmdir(Puppet[:statefile]) end - it "it should fail gracefully on load() if it cannot get a read lock on the state file" do + it "should fail gracefully on load() if it cannot get a read lock on the state file" do Puppet::Util.expects(:readlock).yields(false) test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}} YAML.expects(:load).returns(test_yaml) @@ -191,7 +192,7 @@ describe Puppet::Util::Storage do Puppet[:statefile] = @state_file.path() end - it "it should create the state file if it does not exist" do + it "should create the state file if it does not exist" do @state_file.close!() FileTest.exists?(Puppet[:statefile]).should be_false() Puppet::Util::Storage.cache(:yayness) @@ -200,7 +201,7 @@ describe Puppet::Util::Storage do FileTest.exists?(Puppet[:statefile]).should be_true() end - it "it should raise an exception if the state file is not a regular file" do + it "should raise an exception if the state file is not a regular file" do @state_file.close!() Dir.mkdir(Puppet[:statefile]) Puppet::Util::Storage.cache(:yayness) @@ -210,14 +211,14 @@ describe Puppet::Util::Storage do Dir.rmdir(Puppet[:statefile]) end - it "it should raise an exception if it cannot get a write lock on the state file" do + it "should raise an exception if it cannot get a write lock on the state file" do Puppet::Util.expects(:writelock).yields(false) Puppet::Util::Storage.cache(:yayness) proc { Puppet::Util::Storage.store() }.should raise_error() end - it "it should load() the same information that it store()s" do + it "should load() the same information that it store()s" do Puppet::Util::Storage.cache(:yayness) Puppet::Util::Storage.state().should == {:yayness=>{}} -- cgit