summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/checksum
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-10-15 19:36:32 -0500
committerLuke Kanies <luke@madstop.com>2007-10-15 19:36:32 -0500
commita815f7888b021a46332c23450795f057533d0093 (patch)
treea2f8081e47750c7782db9c3751ec4091f3f7424f /spec/unit/indirector/checksum
parent694f98b4d9e7172cec58d407bc5aeae7861e1a06 (diff)
downloadpuppet-a815f7888b021a46332c23450795f057533d0093.tar.gz
puppet-a815f7888b021a46332c23450795f057533d0093.tar.xz
puppet-a815f7888b021a46332c23450795f057533d0093.zip
Reorganizing the file structure for indirection terminus types.
Previously, for example, the configuration terminus that was a subclass of 'code' would have been stored at lib/puppet/indirector/code/configuration and would have had to have been named 'configuration'. Now, the subclass can be named however the author prefers, and it must be stored at lib/puppet/indirector/configuration/<name>.rb, where <name> is the name you've chosen for the terminus type. The name only matters insomuch as it is used to load the file from disk and find the appropriate class when asked. The additional restriction is that the class constant for the terminus type must have its name as the last word, and the indirection must be the second to last word. Thus, in our example, we can choose any class constant that ends with Configuration::Code; given that there's only one Configuration class at this point, it makes the most sense to define the class as Puppet::Node::Configuration::Code. This is somewhat awkward, because of the class's location on disk, but the only other real option is to autogenerate a Puppet::Indirector::Configuration class constant, which is, I think, uglier.
Diffstat (limited to 'spec/unit/indirector/checksum')
-rwxr-xr-xspec/unit/indirector/checksum/file.rb142
1 files changed, 142 insertions, 0 deletions
diff --git a/spec/unit/indirector/checksum/file.rb b/spec/unit/indirector/checksum/file.rb
new file mode 100755
index 000000000..82319fa40
--- /dev/null
+++ b/spec/unit/indirector/checksum/file.rb
@@ -0,0 +1,142 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke Kanies on 2007-9-22.
+# Copyright (c) 2007. All rights reserved.
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/indirector/checksum/file'
+
+module FileChecksumTesting
+ def setup
+ Puppet.settings.stubs(:use)
+ @store = Puppet::Checksum::File.new
+
+ @value = "70924d6fa4b2d745185fa4660703a5c0"
+ @sum = stub 'sum', :name => @value
+
+ @dir = "/what/ever"
+
+ Puppet.stubs(:[]).with(:bucketdir).returns(@dir)
+
+ @path = @store.path(@value)
+ end
+end
+
+describe Puppet::Checksum::File do
+ it "should be a subclass of the File terminus class" do
+ Puppet::Checksum::File.superclass.should equal(Puppet::Indirector::File)
+ end
+
+ it "should have documentation" do
+ Puppet::Checksum::File.doc.should be_instance_of(String)
+ end
+end
+
+describe Puppet::Checksum::File, " when initializing" do
+ it "should use the filebucket settings section" do
+ Puppet.settings.expects(:use).with(:filebucket)
+ Puppet::Checksum::File.new
+ end
+end
+
+describe Puppet::Checksum::File, " when determining file paths" do
+ include FileChecksumTesting
+
+ # I was previously passing the object in.
+ it "should use the value passed in to path() as the checksum" do
+ @value.expects(:name).never
+ @store.path(@value)
+ end
+
+ it "should use the value of the :bucketdir setting as the root directory" do
+ @path.should =~ %r{^#{@dir}}
+ end
+
+ it "should choose a path 8 directories deep with each directory name being the respective character in the checksum" do
+ dirs = @value[0..7].split("").join(File::SEPARATOR)
+ @path.should be_include(dirs)
+ end
+
+ it "should use the full checksum as the final directory name" do
+ File.basename(File.dirname(@path)).should == @value
+ end
+
+ it "should use 'contents' as the actual file name" do
+ File.basename(@path).should == "contents"
+ end
+
+ it "should use the bucketdir, the 8 sum character directories, the full checksum, and 'contents' as the full file name" do
+ @path.should == [@dir, @value[0..7].split(""), @value, "contents"].flatten.join(File::SEPARATOR)
+ end
+end
+
+describe Puppet::Checksum::File, " when retrieving files" do
+ include FileChecksumTesting
+
+ # The smallest test that will use the calculated path
+ it "should look for the calculated path" do
+ File.expects(:exist?).with(@path).returns(false)
+ @store.find(@value)
+ end
+
+ it "should return an instance of Puppet::Checksum created with the content if the file exists" do
+ content = "my content"
+ sum = stub 'file'
+ Puppet::Checksum.expects(:new).with(content).returns(sum)
+
+ File.expects(:exist?).with(@path).returns(true)
+ File.expects(:read).with(@path).returns(content)
+
+ @store.find(@value).should equal(sum)
+ end
+
+ it "should return nil if no file is found" do
+ File.expects(:exist?).with(@path).returns(false)
+ @store.find(@value).should be_nil
+ end
+
+ it "should fail intelligently if a found file cannot be read" do
+ File.expects(:exist?).with(@path).returns(true)
+ File.expects(:read).with(@path).raises(RuntimeError)
+ proc { @store.find(@value) }.should raise_error(Puppet::Error)
+ end
+end
+
+describe Puppet::Checksum::File, " when saving files" do
+ include FileChecksumTesting
+
+ # LAK:FIXME I don't know how to include in the spec the fact that we're
+ # using the superclass's save() method and thus are acquiring all of
+ # it's behaviours.
+ it "should save the content to the calculated path" do
+ File.stubs(:directory?).with(File.dirname(@path)).returns(true)
+ File.expects(:open).with(@path, "w")
+
+ file = stub 'file', :name => @value
+ @store.save(file)
+ end
+
+ it "should make any directories necessary for storage" do
+ FileUtils.expects(:mkdir_p).with do |arg|
+ File.umask == 0007 and arg == File.dirname(@path)
+ end
+ File.expects(:directory?).with(File.dirname(@path)).returns(true)
+ File.expects(:open).with(@path, "w")
+
+ file = stub 'file', :name => @value
+ @store.save(file)
+ end
+end
+
+describe Puppet::Checksum::File, " when deleting files" do
+ include FileChecksumTesting
+
+ it "should remove the file at the calculated path" do
+ File.expects(:exist?).with(@path).returns(true)
+ File.expects(:unlink).with(@path)
+
+ file = stub 'file', :name => @value
+ @store.destroy(file)
+ end
+end