diff options
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/application/filebucket.rb | 14 | ||||
-rwxr-xr-x | spec/unit/application/main.rb | 2 | ||||
-rw-r--r-- | spec/unit/file_bucket/file.rb | 239 | ||||
-rwxr-xr-x | spec/unit/indirector/file_bucket_file/file.rb | 288 | ||||
-rwxr-xr-x | spec/unit/indirector/file_bucket_file/rest.rb | 11 | ||||
-rwxr-xr-x | spec/unit/indirector/indirection.rb | 4 | ||||
-rwxr-xr-x | spec/unit/indirector/request.rb | 4 | ||||
-rwxr-xr-x | spec/unit/network/client/dipper.rb | 102 | ||||
-rwxr-xr-x | spec/unit/network/http/handler.rb | 5 | ||||
-rwxr-xr-x | spec/unit/other/checksum.rb | 92 | ||||
-rwxr-xr-x | spec/unit/type/file.rb | 2 | ||||
-rw-r--r-- | spec/unit/type/filebucket.rb | 6 |
12 files changed, 653 insertions, 116 deletions
diff --git a/spec/unit/application/filebucket.rb b/spec/unit/application/filebucket.rb index e87bab402..f78c0b7be 100644 --- a/spec/unit/application/filebucket.rb +++ b/spec/unit/application/filebucket.rb @@ -43,7 +43,7 @@ describe "Filebucket" do Puppet.stubs(:settraps) Puppet::Log.stubs(:level=) Puppet.stubs(:parse_config) - Puppet::Network::Client.dipper.stubs(:new) + Puppet::FileBucket::Dipper.stubs(:new) @filebucket.options.stubs(:[]).with(any_parameters) end @@ -106,15 +106,15 @@ describe "Filebucket" do it "should create a client with the default bucket if none passed" do Puppet.stubs(:[]).with(:bucketdir).returns("path") - Puppet::Network::Client::Dipper.expects(:new).with { |h| h[:Path] == "path" } + Puppet::FileBucket::Dipper.expects(:new).with { |h| h[:Path] == "path" } @filebucket.run_setup end - it "should create a local Client dipper with the given bucket" do + it "should create a local Dipper with the given bucket" do @filebucket.options.stubs(:[]).with(:bucket).returns("path") - Puppet::Network::Client::Dipper.expects(:new).with { |h| h[:Path] == "path" } + Puppet::FileBucket::Dipper.expects(:new).with { |h| h[:Path] == "path" } @filebucket.run_setup end @@ -126,7 +126,7 @@ describe "Filebucket" do it "should create a remote Client to the configured server" do Puppet.stubs(:[]).with(:server).returns("puppet.reductivelabs.com") - Puppet::Network::Client::Dipper.expects(:new).with { |h| h[:Server] == "puppet.reductivelabs.com" } + Puppet::FileBucket::Dipper.expects(:new).with { |h| h[:Server] == "puppet.reductivelabs.com" } @filebucket.run_setup end @@ -142,11 +142,11 @@ describe "Filebucket" do Puppet.stubs(:settraps) Puppet::Log.stubs(:level=) Puppet.stubs(:parse_config) - Puppet::Network::Client.dipper.stubs(:new) + Puppet::FileBucket::Dipper.stubs(:new) @filebucket.options.stubs(:[]).with(any_parameters) @client = stub 'client' - Puppet::Network::Client::Dipper.stubs(:new).returns(@client) + Puppet::FileBucket::Dipper.stubs(:new).returns(@client) @filebucket.run_setup end diff --git a/spec/unit/application/main.rb b/spec/unit/application/main.rb index 74c40c14a..ea8c43fe6 100755 --- a/spec/unit/application/main.rb +++ b/spec/unit/application/main.rb @@ -53,7 +53,7 @@ describe "Puppet" do Puppet.stubs(:trap) Puppet::Log.stubs(:level=) Puppet.stubs(:parse_config) - Puppet::Network::Client.dipper.stubs(:new) + Puppet::FileBucket::Dipper.stubs(:new) STDIN.stubs(:read) @main.options.stubs(:[]).with(any_parameters) diff --git a/spec/unit/file_bucket/file.rb b/spec/unit/file_bucket/file.rb new file mode 100644 index 000000000..76f8e2599 --- /dev/null +++ b/spec/unit/file_bucket/file.rb @@ -0,0 +1,239 @@ +#!/usr/bin/env ruby + +require ::File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/file_bucket/file' +require 'digest/md5' +require 'digest/sha1' + +describe Puppet::FileBucket::File do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" + + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "md5:4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + + @contents = "file contents" + end + + it "should save a file" do + ::File.expects(:exist?).with("#{@dir}/contents").returns false + ::File.expects(:directory?).with(@dir).returns false + ::FileUtils.expects(:mkdir_p).with(@dir) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440) + + bucketfile = Puppet::FileBucket::File.new(@contents) + bucketfile.save + + end + + describe "using the indirector's find method" do + it "should return nil if a file doesn't exist" do + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + bucketfile = Puppet::FileBucket::File.find("md5:#{@digest}") + bucketfile.should == nil + end + + it "should find a filebucket if the file exists" do + ::File.expects(:exist?).with("#{@dir}/contents").returns true + ::File.expects(:exist?).with("#{@dir}/paths").returns false + ::File.expects(:read).with("#{@dir}/contents").returns @contents + + bucketfile = Puppet::FileBucket::File.find("md5:#{@digest}") + bucketfile.should_not == nil + end + + describe "using RESTish digest notation" do + it "should return nil if a file doesn't exist" do + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + bucketfile = Puppet::FileBucket::File.find("md5/#{@digest}") + bucketfile.should == nil + end + + it "should find a filebucket if the file exists" do + ::File.expects(:exist?).with("#{@dir}/contents").returns true + ::File.expects(:exist?).with("#{@dir}/paths").returns false + ::File.expects(:read).with("#{@dir}/contents").returns @contents + + bucketfile = Puppet::FileBucket::File.find("md5/#{@digest}") + bucketfile.should_not == nil + end + + end + end + + it "should have a to_s method to return the contents" do + Puppet::FileBucket::File.new(@contents).to_s.should == @contents + end + + it "should have a method that returns the digest algorithm" do + Puppet::FileBucket::File.new(@contents, :checksum => @checksum).checksum_type.should == :md5 + end + + it "should allow contents to be specified in a block" do + bucket = Puppet::FileBucket::File.new(nil) do |fb| + fb.contents = "content" + end + bucket.contents.should == "content" + end + + it "should raise an error if changing content" do + x = Puppet::FileBucket::File.new("first") + proc { x.contents = "new" }.should raise_error + end + + it "should require contents to be a string" do + proc { Puppet::FileBucket::File.new(5) }.should raise_error(ArgumentError) + end + + it "should raise an error if setting contents to a non-string" do + proc do + Puppet::FileBucket::File.new(nil) do |x| + x.contents = 5 + end + end.should raise_error(ArgumentError) + end + + it "should set the contents appropriately" do + Puppet::FileBucket::File.new(@contents).contents.should == @contents + end + + it "should calculate the checksum" do + Digest::MD5.expects(:hexdigest).with(@contents).returns('mychecksum') + Puppet::FileBucket::File.new(@contents).checksum.should == 'md5:mychecksum' + end + + it "should remove the old checksum value if the algorithm is changed" do + Digest::MD5.expects(:hexdigest).with(@contents).returns('oldsum') + sum = Puppet::FileBucket::File.new(@contents) + oldsum = sum.checksum + + sum.checksum_type = :sha1 + Digest::SHA1.expects(:hexdigest).with(@contents).returns('newsum') + sum.checksum.should == 'sha1:newsum' + end + + it "should default to 'md5' as the checksum algorithm if the algorithm is not in the name" do + Puppet::FileBucket::File.new(@contents).checksum_type.should == :md5 + end + + it "should support specifying the checksum_type during initialization" do + sum = Puppet::FileBucket::File.new(@contents, :checksum_type => :sha1) + sum.checksum_type.should == :sha1 + end + + it "should fail when an unsupported checksum_type is used" do + proc { Puppet::FileBucket::File.new(@contents, :checksum_type => :nope) }.should raise_error(ArgumentError) + end + + it "should fail if given an invalid checksum at initialization" do + proc { Puppet::FileBucket::File.new(@contents, :checksum => "md5:00000000000000000000000000000000") }.should raise_error(RuntimeError) + end + + it "should fail if assigned an invalid checksum " do + bucket = Puppet::FileBucket::File.new(@contents) + proc { bucket.checksum = "md5:00000000000000000000000000000000" }.should raise_error(RuntimeError) + end + + it "should accept checksum_data without a prefix" do + bucket = Puppet::FileBucket::File.new(@contents) + bucket.checksum_data = @digest + end + + + describe "when using back-ends" do + it "should redirect using Puppet::Indirector" do + Puppet::Indirector::Indirection.instance(:file_bucket_file).model.should equal(Puppet::FileBucket::File) + end + + it "should have a :save instance method" do + Puppet::FileBucket::File.new("mysum").should respond_to(:save) + end + + it "should respond to :find" do + Puppet::FileBucket::File.should respond_to(:find) + end + + it "should respond to :destroy" do + Puppet::FileBucket::File.should respond_to(:destroy) + end + end + + describe "when saving files" do + it "should save the contents to the calculated path" do + ::File.stubs(:directory?).with(@dir).returns(true) + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + mockfile = mock "file" + mockfile.expects(:print).with(@contents) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440).yields(mockfile) + + Puppet::FileBucket::File.new(@contents).save + end + + it "should make any directories necessary for storage" do + FileUtils.expects(:mkdir_p).with do |arg| + ::File.umask == 0007 and arg == @dir + end + ::File.expects(:directory?).with(@dir).returns(false) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440) + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + Puppet::FileBucket::File.new(@contents).save + end + end + + it "should accept a path" do + remote_path = '/path/on/the/remote/box' + Puppet::FileBucket::File.new(@contents, :path => remote_path).path.should == remote_path + end + + it "should append the path to the paths file" do + remote_path = '/path/on/the/remote/box' + + ::File.expects(:directory?).with(@dir).returns(true) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440) + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + mockfile = mock "file" + mockfile.expects(:puts).with('/path/on/the/remote/box') + ::File.expects(:exist?).with("#{@dir}/paths").returns false + ::File.expects(:open).with("#{@dir}/paths", ::File::WRONLY|::File::CREAT|::File::APPEND).yields mockfile + Puppet::FileBucket::File.new(@contents, :path => remote_path).save + + end + + it "should return a url-ish name" do + Puppet::FileBucket::File.new(@contents).name.should == "md5/4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + end + + it "should reject a url-ish name with an invalid checksum" do + bucket = Puppet::FileBucket::File.new(@contents) + lambda { bucket.name = "sha1/4a8ec4fa5f01b4ab1a0ab8cbccb709f0/new/path" }.should raise_error + end + + it "should accept a url-ish name" do + bucket = Puppet::FileBucket::File.new(@contents) + lambda { bucket.name = "sha1/034fa2ed8e211e4d20f20e792d777f4a30af1a93/new/path" }.should_not raise_error + bucket.checksum_type.should == :sha1 + bucket.checksum_data.should == '034fa2ed8e211e4d20f20e792d777f4a30af1a93' + bucket.path.should == "new/path" + end + + it "should return a url-ish name with a path" do + Puppet::FileBucket::File.new(@contents, :path => 'my/path').name.should == "md5/4a8ec4fa5f01b4ab1a0ab8cbccb709f0/my/path" + end + + it "should convert the contents to PSON" do + Puppet::FileBucket::File.new(@contents).to_pson.should == '{"contents":"file contents"}' + end + + it "should load from PSON" do + Puppet::FileBucket::File.from_pson({"contents"=>"file contents"}).contents.should == "file contents" + end + +end diff --git a/spec/unit/indirector/file_bucket_file/file.rb b/spec/unit/indirector/file_bucket_file/file.rb new file mode 100755 index 000000000..0df530d74 --- /dev/null +++ b/spec/unit/indirector/file_bucket_file/file.rb @@ -0,0 +1,288 @@ +#!/usr/bin/env ruby + +require ::File.dirname(__FILE__) + '/../../../spec_helper' + +require 'puppet/indirector/file_bucket_file/file' + +describe Puppet::FileBucketFile::File do + it "should be a subclass of the Code terminus class" do + Puppet::FileBucketFile::File.superclass.should equal(Puppet::Indirector::Code) + end + + it "should have documentation" do + Puppet::FileBucketFile::File.doc.should be_instance_of(String) + end + + describe "when initializing" do + it "should use the filebucket settings section" do + Puppet.settings.expects(:use).with(:filebucket) + Puppet::FileBucketFile::File.new + end + end + + + describe "the find_by_checksum method" do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" + + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "md5:4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + + @contents = "file contents" + end + + it "should return nil if a file doesn't exist" do + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + bucketfile = Puppet::FileBucketFile::File.new.send(:find_by_checksum, "md5:#{@digest}") + bucketfile.should == nil + end + + it "should find a filebucket if the file exists" do + ::File.expects(:exist?).with("#{@dir}/contents").returns true + ::File.expects(:exist?).with("#{@dir}/paths").returns false + ::File.expects(:read).with("#{@dir}/contents").returns @contents + + bucketfile = Puppet::FileBucketFile::File.new.send(:find_by_checksum, "md5:#{@digest}") + bucketfile.should_not == nil + end + + it "should load the paths" do + paths = ["path1", "path2"] + ::File.expects(:exist?).with("#{@dir}/contents").returns true + ::File.expects(:exist?).with("#{@dir}/paths").returns true + ::File.expects(:read).with("#{@dir}/contents").returns @contents + + mockfile = mock "file" + mockfile.expects(:readlines).returns( paths ) + ::File.expects(:open).with("#{@dir}/paths").yields mockfile + + Puppet::FileBucketFile::File.new.send(:find_by_checksum, "md5:#{@digest}").paths.should == paths + end + + end + + describe "when retrieving files" do + before :each do + Puppet.settings.stubs(:use) + @store = Puppet::FileBucketFile::File.new + + @digest = "70924d6fa4b2d745185fa4660703a5c0" + @sum = stub 'sum', :name => @digest + + @dir = "/what/ever" + + Puppet.stubs(:[]).with(:bucketdir).returns(@dir) + + @contents_path = '/what/ever/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/contents' + @paths_path = '/what/ever/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/paths' + + @request = stub 'request', :key => "md5/#{@digest}/remote/path" + end + + it "should call find_by_checksum" do + @store.expects(:find_by_checksum).with("md5:#{@digest}").returns(false) + @store.find(@request) + end + + it "should look for the calculated path" do + ::File.expects(:exist?).with(@contents_path).returns(false) + @store.find(@request) + end + + it "should return an instance of Puppet::FileBucket::File created with the content if the file exists" do + content = "my content" + bucketfile = stub 'bucketfile' + bucketfile.stubs(:bucket_path) + bucketfile.stubs(:checksum_data).returns(@digest) + bucketfile.stubs(:checksum).returns(@checksum) + + bucketfile.expects(:contents=).with(content) + Puppet::FileBucket::File.expects(:new).with(nil, {:checksum => "md5:#{@digest}"}).yields(bucketfile).returns(bucketfile) + + ::File.expects(:exist?).with(@contents_path).returns(true) + ::File.expects(:exist?).with(@paths_path).returns(false) + ::File.expects(:read).with(@contents_path).returns(content) + + @store.find(@request).should equal(bucketfile) + end + + it "should return nil if no file is found" do + ::File.expects(:exist?).with(@contents_path).returns(false) + @store.find(@request).should be_nil + end + + it "should fail intelligently if a found file cannot be read" do + ::File.expects(:exist?).with(@contents_path).returns(true) + ::File.expects(:read).with(@contents_path).raises(RuntimeError) + proc { @store.find(@request) }.should raise_error(Puppet::Error) + end + + end + + describe "when determining file paths" do + before do + Puppet[:bucketdir] = '/dev/null/bucketdir' + @digest = 'DEADBEEFC0FFEE' + @bucket = stub_everything "bucket" + @bucket.expects(:checksum_data).returns(@digest) + end + + it "should use the value of the :bucketdir setting as the root directory" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + path.should =~ %r{^/dev/null/bucketdir} + end + + it "should choose a path 8 directories deep with each directory name being the respective character in the filebucket" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + dirs = @digest[0..7].split("").join(File::SEPARATOR) + path.should be_include(dirs) + end + + it "should use the full filebucket as the final directory name" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + ::File.basename(::File.dirname(path)).should == @digest + end + + it "should use 'contents' as the actual file name" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + ::File.basename(path).should == "contents" + end + + it "should use the bucketdir, the 8 sum character directories, the full filebucket, and 'contents' as the full file name" do + path = Puppet::FileBucketFile::File.new.send(:contents_path_for, @bucket) + path.should == ['/dev/null/bucketdir', @digest[0..7].split(""), @digest, "contents"].flatten.join(::File::SEPARATOR) + end + end + + describe "when saving files" do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" + + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "md5:4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + + @contents = "file contents" + + @bucket = stub "bucket file" + @bucket.stubs(:bucket_path) + @bucket.stubs(:checksum_data).returns(@digest) + @bucket.stubs(:path).returns(nil) + @bucket.stubs(:contents).returns("file contents") + end + + it "should save the contents to the calculated path" do + ::File.stubs(:directory?).with(@dir).returns(true) + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + mockfile = mock "file" + mockfile.expects(:print).with(@contents) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440).yields(mockfile) + + Puppet::FileBucketFile::File.new.send(:save_to_disk, @bucket) + end + + it "should make any directories necessary for storage" do + FileUtils.expects(:mkdir_p).with do |arg| + ::File.umask == 0007 and arg == @dir + end + ::File.expects(:directory?).with(@dir).returns(false) + ::File.expects(:open).with("#{@dir}/contents", ::File::WRONLY|::File::CREAT, 0440) + ::File.expects(:exist?).with("#{@dir}/contents").returns false + + Puppet::FileBucketFile::File.new.send(:save_to_disk, @bucket) + end + end + + + describe "when verifying identical files" do + before do + # this is the default from spec_helper, but it keeps getting reset at odd times + Puppet[:bucketdir] = "/dev/null/bucket" + + @digest = "4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @checksum = "md5:4a8ec4fa5f01b4ab1a0ab8cbccb709f0" + @dir = '/dev/null/bucket/4/a/8/e/c/4/f/a/4a8ec4fa5f01b4ab1a0ab8cbccb709f0' + + @contents = "file contents" + + @bucket = stub "bucket file" + @bucket.stubs(:bucket_path) + @bucket.stubs(:checksum).returns(@checksum) + @bucket.stubs(:checksum_data).returns(@digest) + @bucket.stubs(:path).returns(nil) + @bucket.stubs(:contents).returns("file contents") + end + + it "should raise an error if the files don't match" do + File.expects(:read).with("#{@dir}/contents").returns("corrupt contents") + lambda{ Puppet::FileBucketFile::File.new.send(:verify_identical_file!, @bucket) }.should raise_error(Puppet::FileBucket::BucketError) + end + + it "should do nothing if the files match" do + File.expects(:read).with("#{@dir}/contents").returns("file contents") + Puppet::FileBucketFile::File.new.send(:verify_identical_file!, @bucket) + end + + end + + + describe "when writing to the paths file" do + before do + Puppet[:bucketdir] = '/dev/null/bucketdir' + @digest = '70924d6fa4b2d745185fa4660703a5c0' + @bucket = stub_everything "bucket" + + @paths_path = '/dev/null/bucketdir/7/0/9/2/4/d/6/f/70924d6fa4b2d745185fa4660703a5c0/paths' + + @paths = [] + @bucket.stubs(:paths).returns(@paths) + @bucket.stubs(:checksum_data).returns(@digest) + end + + it "should create a file if it doesn't exist" do + @bucket.expects(:path).returns('path/to/save').at_least_once + File.expects(:exist?).with(@paths_path).returns(false) + file = stub "file" + file.expects(:puts).with('path/to/save') + File.expects(:open).with(@paths_path, ::File::WRONLY|::File::CREAT|::File::APPEND).yields(file) + + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end + + it "should append to a file if it exists" do + @bucket.expects(:path).returns('path/to/save').at_least_once + File.expects(:exist?).with(@paths_path).returns(true) + old_file = stub "file" + old_file.stubs(:readlines).returns [] + File.expects(:open).with(@paths_path).yields(old_file) + + file = stub "file" + file.expects(:puts).with('path/to/save') + File.expects(:open).with(@paths_path, ::File::WRONLY|::File::CREAT|::File::APPEND).yields(file) + + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end + + it "should not alter a file if it already contains the path" do + @bucket.expects(:path).returns('path/to/save').at_least_once + File.expects(:exist?).with(@paths_path).returns(true) + old_file = stub "file" + old_file.stubs(:readlines).returns ["path/to/save\n"] + File.expects(:open).with(@paths_path).yields(old_file) + + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end + + it "should do nothing if there is no path" do + @bucket.expects(:path).returns(nil).at_least_once + + Puppet::FileBucketFile::File.new.send(:save_path_to_paths_file, @bucket) + end + end + +end diff --git a/spec/unit/indirector/file_bucket_file/rest.rb b/spec/unit/indirector/file_bucket_file/rest.rb new file mode 100755 index 000000000..3aacd3ca4 --- /dev/null +++ b/spec/unit/indirector/file_bucket_file/rest.rb @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby + +Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } + +require 'puppet/indirector/file_bucket_file/rest' + +describe Puppet::FileBucketFile::Rest do + it "should be a sublcass of Puppet::Indirector::REST" do + Puppet::FileBucketFile::Rest.superclass.should equal(Puppet::Indirector::REST) + end +end diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb index ca2a412e3..02d04a3f7 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -198,8 +198,8 @@ describe Puppet::Indirector::Indirection do @indirection.request(:funtest, "yayness", :one => :two) end - it "should default to the arguments being nil" do - Puppet::Indirector::Request.expects(:new).with { |name, method, key, args| args.nil? } + it "should default to the arguments being empty" do + Puppet::Indirector::Request.expects(:new).with { |name, method, key, args| args == {} } @indirection.request(:funtest, "yayness") end diff --git a/spec/unit/indirector/request.rb b/spec/unit/indirector/request.rb index 848a608a4..b885779ed 100755 --- a/spec/unit/indirector/request.rb +++ b/spec/unit/indirector/request.rb @@ -132,8 +132,8 @@ describe Puppet::Indirector::Request do Puppet::Indirector::Request.new(:ind, :method, "http://host/stuff").port.should == 80 end - it "should set the request key to the unescaped unqualified path from the URI" do - Puppet::Indirector::Request.new(:ind, :method, "http:///stuff with spaces").key.should == "stuff with spaces" + it "should set the request key to the unescaped key part path from the URI" do + Puppet::Indirector::Request.new(:ind, :method, "http://host/environment/terminus/stuff with spaces").key.should == "stuff with spaces" end it "should set the :uri attribute to the full URI" do diff --git a/spec/unit/network/client/dipper.rb b/spec/unit/network/client/dipper.rb index d1631fbb5..7d8b3da08 100755 --- a/spec/unit/network/client/dipper.rb +++ b/spec/unit/network/client/dipper.rb @@ -2,15 +2,109 @@ require File.dirname(__FILE__) + '/../../../spec_helper' -describe Puppet::Network::Client.dipper do +require 'puppet/file_bucket/dipper' +describe Puppet::FileBucket::Dipper do it "should fail in an informative way when there are failures backing up to the server" do - FileTest.stubs(:exists?).returns true + File.stubs(:exists?).returns true File.stubs(:read).returns "content" - @dipper = Puppet::Network::Client::Dipper.new(:Path => "/my/bucket") + @dipper = Puppet::FileBucket::Dipper.new(:Path => "/my/bucket") - @dipper.driver.expects(:addfile).raises ArgumentError + filemock = stub "bucketfile" + Puppet::FileBucket::File.stubs(:new).returns(filemock) + filemock.expects(:name).returns "name" + filemock.expects(:save).raises ArgumentError lambda { @dipper.backup("/my/file") }.should raise_error(Puppet::Error) end + + it "should backup files to a local bucket" do + @dipper = Puppet::FileBucket::Dipper.new( + :Path => "/my/bucket" + ) + + File.stubs(:exists?).returns true + File.stubs(:read).with("/my/file").returns "my contents" + + req = stub "req" + bucketfile = stub "bucketfile" + bucketfile.stubs(:name).returns('md5/DIGEST123') + bucketfile.stubs(:checksum_data).returns("DIGEST123") + bucketfile.expects(:save).with(req) + + Puppet::FileBucket::File.stubs(:new).with( + "my contents", + :bucket_path => '/my/bucket', + :path => '/my/file' + ).returns(bucketfile) + + Puppet::Indirector::Request.stubs(:new).with(:file_bucket_file, :save, 'md5/DIGEST123').returns(req) + + @dipper.backup("/my/file").should == "DIGEST123" + end + + it "should retrieve files from a local bucket" do + @dipper = Puppet::FileBucket::Dipper.new( + :Path => "/my/bucket" + ) + + File.stubs(:exists?).returns true + File.stubs(:read).with("/my/file").returns "my contents" + + bucketfile = stub "bucketfile" + bucketfile.stubs(:to_s).returns "Content" + + Puppet::FileBucket::File.expects(:find).with( + 'md5/DIGEST123' + ).returns(bucketfile) + + @dipper.getfile("DIGEST123").should == "Content" + end + + it "should backup files to a remote server" do + @dipper = Puppet::FileBucket::Dipper.new( + :Server => "puppetmaster", + :Port => "31337" + ) + + File.stubs(:exists?).returns true + File.stubs(:read).with("/my/file").returns "my contents" + + req = stub "req" + bucketfile = stub "bucketfile" + bucketfile.stubs(:name).returns('md5/DIGEST123') + bucketfile.stubs(:checksum_data).returns("DIGEST123") + bucketfile.expects(:save).with(req) + + Puppet::FileBucket::File.stubs(:new).with( + "my contents", + :bucket_path => nil, + :path => '/my/file' + ).returns(bucketfile) + + Puppet::Indirector::Request.stubs(:new).with(:file_bucket_file, :save, 'https://puppetmaster:31337/production/file_bucket_file/md5/DIGEST123').returns(req) + + @dipper.backup("/my/file").should == "DIGEST123" + end + + it "should retrieve files from a remote server" do + @dipper = Puppet::FileBucket::Dipper.new( + :Server => "puppetmaster", + :Port => "31337" + ) + + File.stubs(:exists?).returns true + File.stubs(:read).with("/my/file").returns "my contents" + + bucketfile = stub "bucketfile" + bucketfile.stubs(:to_s).returns "Content" + + Puppet::FileBucket::File.expects(:find).with( + 'https://puppetmaster:31337/production/file_bucket_file/md5/DIGEST123' + ).returns(bucketfile) + + @dipper.getfile("DIGEST123").should == "Content" + end + + end diff --git a/spec/unit/network/http/handler.rb b/spec/unit/network/http/handler.rb index 559812159..2218a0a78 100755 --- a/spec/unit/network/http/handler.rb +++ b/spec/unit/network/http/handler.rb @@ -402,10 +402,7 @@ describe Puppet::Network::HTTP::Handler do end it "should use a common method for determining the request parameters" do - @irequest.stubs(:to_hash).returns(:foo => :baz, :bar => :xyzzy) - @model_instance.expects(:save).with do |args| - args[:foo] == :baz and args[:bar] == :xyzzy - end + @model_instance.expects(:save).with(@irequest) @handler.do_save(@irequest, @request, @response) end diff --git a/spec/unit/other/checksum.rb b/spec/unit/other/checksum.rb deleted file mode 100755 index 6a63e833d..000000000 --- a/spec/unit/other/checksum.rb +++ /dev/null @@ -1,92 +0,0 @@ -#!/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/checksum' - -describe Puppet::Checksum do - it "should have 'Checksum' and the checksum algorithm when converted to a string" do - inst = Puppet::Checksum.new("whatever", "md5") - inst.to_s.should == "Checksum<{md5}#{inst.checksum}>" - end - - it "should convert algorithm names to symbols when they are set after checksum creation" do - sum = Puppet::Checksum.new("whatever") - sum.algorithm = "md5" - sum.algorithm.should == :md5 - end - - it "should return the checksum as the name" do - sum = Puppet::Checksum.new("whatever") - sum.checksum.should == sum.name - end -end - -describe Puppet::Checksum, " when initializing" do - before do - @content = "this is some content" - @sum = Puppet::Checksum.new(@content) - end - - it "should require content" do - proc { Puppet::Checksum.new(nil) }.should raise_error(ArgumentError) - end - - it "should set the content appropriately" do - @sum.content.should == @content - end - - it "should calculate the checksum" do - require 'digest/md5' - Digest::MD5.expects(:hexdigest).with(@content).returns(:mychecksum) - @sum.checksum.should == :mychecksum - end - - it "should not calculate the checksum until it is asked for" do - require 'digest/md5' - Digest::MD5.expects(:hexdigest).never - sum = Puppet::Checksum.new(@content, :md5) - end - - it "should remove the old checksum value if the algorithm is changed" do - Digest::MD5.expects(:hexdigest).with(@content).returns(:oldsum) - oldsum = @sum.checksum - @sum.algorithm = :sha1 - Digest::SHA1.expects(:hexdigest).with(@content).returns(:newsum) - @sum.checksum.should == :newsum - end - - it "should default to 'md5' as the checksum algorithm if the algorithm is not in the name" do - @sum.algorithm.should == :md5 - end - - it "should support specifying the algorithm during initialization" do - sum = Puppet::Checksum.new(@content, :sha1) - sum.algorithm.should == :sha1 - end - - it "should fail when an unsupported algorithm is used" do - proc { Puppet::Checksum.new(@content, :nope) }.should raise_error(ArgumentError) - end -end - -describe Puppet::Checksum, " when using back-ends" do - it "should redirect using Puppet::Indirector" do - Puppet::Indirector::Indirection.instance(:checksum).model.should equal(Puppet::Checksum) - end - - it "should have a :save instance method" do - Puppet::Checksum.new("mysum").should respond_to(:save) - end - - it "should respond to :find" do - Puppet::Checksum.should respond_to(:find) - end - - it "should respond to :destroy" do - Puppet::Checksum.should respond_to(:destroy) - end -end diff --git a/spec/unit/type/file.rb b/spec/unit/type/file.rb index afb050a1f..cedb1701d 100755 --- a/spec/unit/type/file.rb +++ b/spec/unit/type/file.rb @@ -811,7 +811,7 @@ describe Puppet::Type.type(:file) do it "should be able to use the default filebucket without a catalog" do file = Puppet::Type::File.new(:name => "/my/file", :backup => "puppet") - file.bucket.should be_instance_of(Puppet::Network::Client::Dipper) + file.bucket.should be_instance_of(Puppet::FileBucket::Dipper) end it "should look up the filebucket during finish()" do diff --git a/spec/unit/type/filebucket.rb b/spec/unit/type/filebucket.rb index 78bfa3655..a0ff45ab6 100644 --- a/spec/unit/type/filebucket.rb +++ b/spec/unit/type/filebucket.rb @@ -55,19 +55,19 @@ describe Puppet::Type.type(:filebucket) do it "should use any provided path" do bucket = Puppet::Type.type(:filebucket).new :name => "main", :path => "/foo/bar" - Puppet::Network::Client.client(:Dipper).expects(:new).with(:Path => "/foo/bar").returns @bucket + Puppet::FileBucket::Dipper.expects(:new).with(:Path => "/foo/bar").returns @bucket bucket.bucket end it "should use any provided server and port" do bucket = Puppet::Type.type(:filebucket).new :name => "main", :server => "myserv", :port => "myport", :path => false - Puppet::Network::Client.client(:Dipper).expects(:new).with(:Server => "myserv", :Port => "myport").returns @bucket + Puppet::FileBucket::Dipper.expects(:new).with(:Server => "myserv", :Port => "myport").returns @bucket bucket.bucket end it "should use the default server if the path is unset and no server is provided" do Puppet.settings[:server] = "myserv" bucket = Puppet::Type.type(:filebucket).new :name => "main", :path => false - Puppet::Network::Client.client(:Dipper).expects(:new).with { |args| args[:Server] == "myserv" }.returns @bucket + Puppet::FileBucket::Dipper.expects(:new).with { |args| args[:Server] == "myserv" }.returns @bucket bucket.bucket end end |