summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuke Kanies <luke@reductivelabs.com>2010-03-19 22:02:14 -0700
committertest branch <puppet-dev@googlegroups.com>2010-02-17 06:50:53 -0800
commit47c3ca18272e38f16d0e5690c2c9a0e0dbac3285 (patch)
treeb0419940aa95b1e4ce159f3e968364e57a426b33 /spec
parent44cba9cfb85a43f758c457bf3a5e661706f1e8f3 (diff)
downloadpuppet-47c3ca18272e38f16d0e5690c2c9a0e0dbac3285.tar.gz
puppet-47c3ca18272e38f16d0e5690c2c9a0e0dbac3285.tar.xz
puppet-47c3ca18272e38f16d0e5690c2c9a0e0dbac3285.zip
Converted File[checksum] to a parameter not property
At the same time I removed all of the code in checksum that managed tracking changes to the checksum over time. I'll add it back in as I fix the fact that changes aren't being tracked like the should at the moment. Signed-off-by: Luke Kanies <luke@reductivelabs.com>
Diffstat (limited to 'spec')
-rwxr-xr-xspec/integration/type/file.rb14
-rwxr-xr-xspec/unit/type/file.rb4
-rw-r--r--spec/unit/type/file/checksum.rb57
-rwxr-xr-xspec/unit/type/file/content.rb75
4 files changed, 90 insertions, 60 deletions
diff --git a/spec/integration/type/file.rb b/spec/integration/type/file.rb
index d1e46a206..259c975ff 100755
--- a/spec/integration/type/file.rb
+++ b/spec/integration/type/file.rb
@@ -130,6 +130,19 @@ describe Puppet::Type.type(:file) do
bucket.bucket.getfile(foomd5).should == "fooyay"
bucket.bucket.getfile(barmd5).should == "baryay"
end
+
+ it "should propagate failures encountered when renaming the temporary file" do
+ file = Puppet::Type.type(:file).new :path => tmpfile("fail_rename"), :content => "foo"
+ catalog = Puppet::Resource::Catalog.new
+ catalog.add_resource file
+
+ File.open(file[:path], "w") { |f| f.print "bar" }
+
+ File.expects(:rename).raises ArgumentError
+
+ lambda { file.write("something", :content) }.should raise_error(Puppet::Error)
+ File.read(file[:path]).should == "bar"
+ end
end
describe "when recursing" do
@@ -372,6 +385,7 @@ describe Puppet::Type.type(:file) do
# Now change the file
File.open(source, "w") { |f| f.print "bar" }
+ file.expire
catalog.apply
# And make sure it's changed
diff --git a/spec/unit/type/file.rb b/spec/unit/type/file.rb
index cedb1701d..64ac135f8 100755
--- a/spec/unit/type/file.rb
+++ b/spec/unit/type/file.rb
@@ -139,13 +139,13 @@ describe Puppet::Type.type(:file) do
end
describe "when validating attributes" do
- %w{path backup recurse recurselimit source replace force ignore links purge sourceselect}.each do |attr|
+ %w{path checksum backup recurse recurselimit source replace force ignore links purge sourceselect}.each do |attr|
it "should have a '#{attr}' parameter" do
Puppet::Type.type(:file).attrtype(attr.intern).should == :param
end
end
- %w{checksum content target ensure owner group mode type}.each do |attr|
+ %w{content target ensure owner group mode type}.each do |attr|
it "should have a '#{attr}' property" do
Puppet::Type.type(:file).attrtype(attr.intern).should == :property
end
diff --git a/spec/unit/type/file/checksum.rb b/spec/unit/type/file/checksum.rb
index 5d715d15c..8f0f84290 100644
--- a/spec/unit/type/file/checksum.rb
+++ b/spec/unit/type/file/checksum.rb
@@ -5,24 +5,55 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f
checksum = Puppet::Type.type(:file).attrclass(:checksum)
describe checksum do
before do
- # Wow that's a messy interface to the resource.
- @resource = stub 'resource', :[] => nil, :[]= => nil, :property => nil, :newattr => nil, :parameter => nil
+ @resource = Puppet::Type.type(:file).new :path => "/foo/bar"
+ @checksum = @resource.parameter(:checksum)
end
- it "should be a subclass of Property" do
- checksum.superclass.must == Puppet::Property
+ it "should be a parameter" do
+ checksum.superclass.must == Puppet::Parameter
end
- it "should have default checksum of :md5" do
- @checksum = checksum.new(:resource => @resource)
- @checksum.checktype.should == :md5
+ it "should use its current value when asked to sum content" do
+ @checksum.value = :md5lite
+ @checksum.expects(:md5lite).with("foobar").returns "yay"
+ @checksum.sum("foobar")
end
- [:none, nil, ""].each do |ck|
- it "should use a none checksum for #{ck.inspect}" do
- @checksum = checksum.new(:resource => @resource)
- @checksum.should = "none"
- @checksum.checktype.should == :none
- end
+ it "should use :md5 to sum when no value is set" do
+ @checksum.expects(:md5).with("foobar").returns "yay"
+ @checksum.sum("foobar")
+ end
+
+ it "should return the summed contents with a checksum label" do
+ sum = Digest::MD5.hexdigest("foobar")
+ @resource[:checksum] = :md5
+ @checksum.sum("foobar").should == "{md5}#{sum}"
+ end
+
+ it "should use :md5 as its default type" do
+ @checksum.default.should == :md5
+ end
+
+ it "should use its current value when asked to sum a file's content" do
+ @checksum.value = :md5lite
+ @checksum.expects(:md5lite_file).with("/foo/bar").returns "yay"
+ @checksum.sum_file("/foo/bar")
+ end
+
+ it "should use :md5 to sum a file when no value is set" do
+ @checksum.expects(:md5_file).with("/foo/bar").returns "yay"
+ @checksum.sum_file("/foo/bar")
+ end
+
+ it "should convert all sums to strings when summing files" do
+ @checksum.value = :mtime
+ @checksum.expects(:mtime_file).with("/foo/bar").returns Time.now
+ lambda { @checksum.sum_file("/foo/bar") }.should_not raise_error
+ end
+
+ it "should return the summed contents of a file with a checksum label" do
+ @resource[:checksum] = :md5
+ @checksum.expects(:md5_file).returns "mysum"
+ @checksum.sum_file("/foo/bar").should == "{md5}mysum"
end
end
diff --git a/spec/unit/type/file/content.rb b/spec/unit/type/file/content.rb
index 442de1309..1f2100981 100755
--- a/spec/unit/type/file/content.rb
+++ b/spec/unit/type/file/content.rb
@@ -5,8 +5,7 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f
content = Puppet::Type.type(:file).attrclass(:content)
describe content do
before do
- # Wow that's a messy interface to the resource.
- @resource = stub 'resource', :[] => nil, :[]= => nil, :property => nil, :newattr => nil, :parameter => nil
+ @resource = Puppet::Type.type(:file).new :path => "/foo/bar"
end
it "should be a subclass of Property" do
@@ -14,45 +13,31 @@ describe content do
end
describe "when determining the checksum type" do
- it "should use the type specified in the source checksum if a source is set" do
- source = mock 'source'
- source.expects(:checksum).returns "{litemd5}eh"
-
- @resource.expects(:parameter).with(:source).returns source
-
- @content = content.new(:resource => @resource)
- @content.checksum_type.should == :litemd5
+ before do
+ @resource = Puppet::Type.type(:file).new :path => "/foo/bar"
end
- it "should use the type specified by the checksum parameter if no source is set" do
- checksum = mock 'checksum'
- checksum.expects(:checktype).returns :litemd5
-
- @resource.expects(:parameter).with(:source).returns nil
- @resource.expects(:parameter).with(:checksum).returns checksum
+ it "should use the type specified in the source checksum if a source is set" do
+ @resource[:source] = "/foo"
+ @resource.parameter(:source).expects(:checksum).returns "{md5lite}eh"
@content = content.new(:resource => @resource)
- @content.checksum_type.should == :litemd5
+ @content.checksum_type.should == :md5lite
end
- it "should only return the checksum type from the checksum parameter if the parameter returns a whole checksum" do
- checksum = mock 'checksum'
- checksum.expects(:checktype).returns "{md5}something"
-
- @resource.expects(:parameter).with(:source).returns nil
- @resource.expects(:parameter).with(:checksum).returns checksum
-
- @content = content.new(:resource => @resource)
- @content.checksum_type.should == :md5
- end
+ it "should use the type specified by the checksum parameter if no source is set" do
+ @resource[:checksum] = :md5lite
- it "should use md5 if neither a source nor a checksum parameter is available" do
@content = content.new(:resource => @resource)
- @content.checksum_type.should == :md5
+ @content.checksum_type.should == :md5lite
end
end
describe "when determining the actual content to write" do
+ before do
+ @resource = Puppet::Type.type(:file).new :path => "/foo/bar"
+ end
+
it "should use the set content if available" do
@content = content.new(:resource => @resource)
@content.should = "ehness"
@@ -69,9 +54,9 @@ describe content do
@content.actual_content.should == "scont"
end
- it "should return nil if no source is available and no content is set" do
+ it "should fail if no source is available and no content is set" do
@content = content.new(:resource => @resource)
- @content.actual_content.should be_nil
+ lambda { @content.actual_content }.should raise_error(Puppet::Error)
end
end
@@ -100,6 +85,16 @@ describe content do
@content.should.must == :absent
end
+
+ it "should accept a checksum as the desired content" do
+ @content = content.new(:resource => @resource)
+ digest = Digest::MD5.hexdigest("this is some content")
+
+ string = "{md5}#{digest}"
+ @content.should = string
+
+ @content.should.must == string
+ end
end
describe "when retrieving the current content" do
@@ -130,29 +125,22 @@ describe content do
it "should always return the checksum as a string" do
@content = content.new(:resource => @resource)
- @content.stubs(:checksum_type).returns "mtime"
+ @resource[:checksum] = :mtime
stat = mock 'stat', :ftype => "file"
@resource.expects(:stat).returns stat
- @resource.expects(:[]).with(:path).returns "/my/file"
-
time = Time.now
- @content.expects(:mtime_file).with("/my/file").returns time
+ @resource.parameter(:checksum).expects(:mtime_file).with(@resource[:path]).returns time
@content.retrieve.should == "{mtime}%s" % time
end
it "should return the checksum of the file if it exists and is a normal file" do
@content = content.new(:resource => @resource)
- @content.stubs(:checksum_type).returns "md5"
-
stat = mock 'stat', :ftype => "file"
@resource.expects(:stat).returns stat
-
- @resource.expects(:[]).with(:path).returns "/my/file"
-
- @content.expects(:md5_file).with("/my/file").returns "mysum"
+ @resource.parameter(:checksum).expects(:md5_file).with(@resource[:path]).returns "mysum"
@content.retrieve.should == "{md5}mysum"
end
@@ -160,11 +148,8 @@ describe content do
describe "when testing whether the content is in sync" do
before do
- @resource.stubs(:[]).with(:ensure).returns :file
- @resource.stubs(:replace?).returns true
- @resource.stubs(:should_be_file?).returns true
+ @resource[:ensure] = :file
@content = content.new(:resource => @resource)
- @content.stubs(:checksum_type).returns "md5"
end
it "should return true if the resource shouldn't be a regular file" do