diff options
author | Luke Kanies <luke@madstop.com> | 2008-02-21 23:18:40 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-02-21 23:18:40 -0500 |
commit | b06767ee2d7c22c27d746d3e8d1b6effa37deaa6 (patch) | |
tree | 1b31ca784215113e5962310299826616c8768cd7 /spec/unit | |
parent | 5e18b8dc91b2313a96dd3a9ff9cb0a88bfe0d6a0 (diff) | |
download | puppet-b06767ee2d7c22c27d746d3e8d1b6effa37deaa6.tar.gz puppet-b06767ee2d7c22c27d746d3e8d1b6effa37deaa6.tar.xz puppet-b06767ee2d7c22c27d746d3e8d1b6effa37deaa6.zip |
Quashed commit of my fixes for #1010.
Diffstat (limited to 'spec/unit')
-rwxr-xr-x | spec/unit/ral/types/file/checksum.rb | 119 | ||||
-rwxr-xr-x | spec/unit/util/checksums.rb | 8 |
2 files changed, 123 insertions, 4 deletions
diff --git a/spec/unit/ral/types/file/checksum.rb b/spec/unit/ral/types/file/checksum.rb new file mode 100755 index 000000000..3ce95362c --- /dev/null +++ b/spec/unit/ral/types/file/checksum.rb @@ -0,0 +1,119 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +require 'puppet/type/file' + +describe Puppet::Type::File, " when used with replace=>false and content" do + before do + @path = Tempfile.new("puppetspec") + @path.close!() + @path = @path.path + @file = Puppet::Type::File.create( { :name => @path, :content => "foo", :replace => :false } ) + end + + %w{md5 md5lite timestamp time}.each do |type| + end + + def test_checksums + types = %w{md5 md5lite timestamp time} + exists = "/tmp/sumtest-exists" + nonexists = "/tmp/sumtest-nonexists" + + @@tmpfiles << exists + @@tmpfiles << nonexists + + # try it both with files that exist and ones that don't + files = [exists, nonexists] + initstorage + File.open(exists,File::CREAT|File::TRUNC|File::WRONLY) { |of| + of.puts "initial text" + } + types.each { |type| + files.each { |path| + if Puppet[:debug] + Puppet.warning "Testing %s on %s" % [type,path] + end + file = nil + events = nil + # okay, we now know that we have a file... + assert_nothing_raised() { + file = Puppet.type(:file).create( + :name => path, + :ensure => "file", + :checksum => type + ) + } + trans = nil + + currentvalues = file.retrieve + + if file.title !~ /nonexists/ + sum = file.property(:checksum) + assert(sum.insync?(currentvalues[sum]), "file is not in sync") + end + + events = assert_apply(file) + + assert(events) + + assert(! events.include?(:file_changed), "File incorrectly changed") + assert_events([], file) + + # We have to sleep because the time resolution of the time-based + # mechanisms is greater than one second + sleep 1 if type =~ /time/ + + assert_nothing_raised() { + File.open(path,File::CREAT|File::TRUNC|File::WRONLY) { |of| + of.puts "some more text, yo" + } + } + Puppet.type(:file).clear + + # now recreate the file + assert_nothing_raised() { + file = Puppet.type(:file).create( + :name => path, + :checksum => type + ) + } + trans = nil + + assert_events([:file_changed], file) + + # Run it a few times to make sure we aren't getting + # spurious changes. + sum = nil + assert_nothing_raised do + sum = file.property(:checksum).retrieve + end + assert(file.property(:checksum).insync?(sum), + "checksum is not in sync") + + sleep 1.1 if type =~ /time/ + assert_nothing_raised() { + File.unlink(path) + File.open(path,File::CREAT|File::TRUNC|File::WRONLY) { |of| + # We have to put a certain amount of text in here or + # the md5-lite test fails + 2.times { + of.puts rand(100) + } + of.flush + } + } + assert_events([:file_changed], file) + + # verify that we're actually getting notified when a file changes + assert_nothing_raised() { + Puppet.type(:file).clear + } + + if path =~ /nonexists/ + File.unlink(path) + end + } + } + end +end diff --git a/spec/unit/util/checksums.rb b/spec/unit/util/checksums.rb index 31cf24f5b..0e0d06c0d 100755 --- a/spec/unit/util/checksums.rb +++ b/spec/unit/util/checksums.rb @@ -14,7 +14,7 @@ describe Puppet::Util::Checksums do end content_sums = [:md5, :md5lite, :sha1, :sha1lite] - file_only = [:timestamp, :mtime] + file_only = [:ctime, :mtime] content_sums.each do |sumtype| it "should be able to calculate %s sums from strings" % sumtype do @@ -84,11 +84,11 @@ describe Puppet::Util::Checksums do end end - {:timestamp => :ctime, :mtime => :mtime}.each do |sum, method| + [:ctime, :mtime].each do |sum| describe("when using %s" % sum) do - it "should use the '#{method}' on the file to determine the timestamp" do + it "should use the '#{sum}' on the file to determine the ctime" do file = "/my/file" - stat = mock 'stat', method => "mysum" + stat = mock 'stat', sum => "mysum" File.expects(:stat).with(file).returns(stat) |