diff options
author | Rick Bradley <rick@rickbradley.com> | 2007-10-26 12:50:19 -0500 |
---|---|---|
committer | Rick Bradley <rick@rickbradley.com> | 2007-10-26 12:50:19 -0500 |
commit | 6dd0d71b13f6b98b3fd4152421d0124e9786f726 (patch) | |
tree | 3f852c0f0a4b5d1317143de08cefc8177166b005 /spec | |
parent | 54fc80d5de7b881adca06c85206fb700f4278a73 (diff) | |
parent | d03f68eaed6d05483128b495ad1faaf89208d66a (diff) | |
download | puppet-6dd0d71b13f6b98b3fd4152421d0124e9786f726.tar.gz puppet-6dd0d71b13f6b98b3fd4152421d0124e9786f726.tar.xz puppet-6dd0d71b13f6b98b3fd4152421d0124e9786f726.zip |
Merge branch 'master' of git://reductivelabs.com/puppet into routing
Diffstat (limited to 'spec')
-rwxr-xr-x | spec/integration/indirector/direct_file_server.rb | 78 | ||||
-rwxr-xr-x | spec/integration/indirector/module_files.rb | 35 | ||||
-rw-r--r-- | spec/lib/shared_behaviours/file_server_terminus.rb | 3 | ||||
-rw-r--r-- | spec/lib/shared_behaviours/file_serving.rb | 2 | ||||
-rwxr-xr-x | spec/unit/file_serving/content.rb | 66 | ||||
-rwxr-xr-x | spec/unit/file_serving/file_base.rb | 88 | ||||
-rwxr-xr-x | spec/unit/file_serving/indirection_hooks.rb (renamed from spec/unit/file_serving/terminus_selector.rb) | 10 | ||||
-rwxr-xr-x | spec/unit/file_serving/metadata.rb | 62 | ||||
-rwxr-xr-x | spec/unit/file_serving/terminus_helper.rb | 56 | ||||
-rwxr-xr-x | spec/unit/indirector/direct_file_server.rb | 95 | ||||
-rwxr-xr-x | spec/unit/indirector/file_content/file.rb | 56 | ||||
-rwxr-xr-x | spec/unit/indirector/file_metadata/file.rb | 48 | ||||
-rwxr-xr-x | spec/unit/indirector/file_metadata/modules.rb | 2 | ||||
-rwxr-xr-x | spec/unit/indirector/file_server.rb | 30 | ||||
-rwxr-xr-x | spec/unit/indirector/indirection.rb | 94 | ||||
-rwxr-xr-x | spec/unit/indirector/module_files.rb | 32 |
16 files changed, 491 insertions, 266 deletions
diff --git a/spec/integration/indirector/direct_file_server.rb b/spec/integration/indirector/direct_file_server.rb new file mode 100755 index 000000000..383486986 --- /dev/null +++ b/spec/integration/indirector/direct_file_server.rb @@ -0,0 +1,78 @@ +#!/usr/bin/env ruby +# +# Created by Luke Kanies on 2007-10-19. +# Copyright (c) 2007. All rights reserved. + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/indirector/file_content/file' +require 'puppet/indirector/module_files' + +describe Puppet::Indirector::DirectFileServer, " when interacting with the filesystem and the model" do + before do + # We just test a subclass, since it's close enough. + @terminus = Puppet::Indirector::FileContent::File.new + + @filepath = "/path/to/my/file" + end + + it "should return an instance of the model" do + FileTest.expects(:exists?).with(@filepath).returns(true) + + @terminus.find("file://host#{@filepath}").should be_instance_of(Puppet::FileServing::Content) + end + + it "should return an instance capable of returning its content" do + FileTest.expects(:exists?).with(@filepath).returns(true) + File.stubs(:lstat).with(@filepath).returns(stub("stat", :ftype => "file")) + File.expects(:read).with(@filepath).returns("my content") + + instance = @terminus.find("file://host#{@filepath}") + + instance.content.should == "my content" + end +end + +describe Puppet::Indirector::DirectFileServer, " when interacting with FileServing::Fileset and the model" do + before do + @terminus = Puppet::Indirector::FileContent::File.new + + @filepath = "/my/file" + FileTest.stubs(:exists?).with(@filepath).returns(true) + + stat = stub 'stat', :directory? => true + File.stubs(:lstat).with(@filepath).returns(stat) + + @subfiles = %w{one two} + @subfiles.each do |f| + path = File.join(@filepath, f) + FileTest.stubs(:exists?).with(@path).returns(true) + end + + Dir.expects(:entries).with(@filepath).returns @subfiles + end + + it "should return an instance for every file in the fileset" do + result = @terminus.search("file:///my/file", :recurse => true) + result.should be_instance_of(Array) + result.length.should == 3 + result.each { |r| r.should be_instance_of(Puppet::FileServing::Content) } + end + + it "should return instances capable of returning their content" do + @subfiles.each do |name| + File.stubs(:lstat).with(File.join(@filepath, name)).returns stub("#{name} stat", :ftype => "file", :directory? => false) + File.expects(:read).with(File.join(@filepath, name)).returns("#{name} content") + end + + @terminus.search("file:///my/file", :recurse => true).each do |instance| + case instance.key + when /one/: instance.content.should == "one content" + when /two/: instance.content.should == "two content" + when /\.$/: + else + raise "No valid key for %s" % instance.key.inspect + end + end + end +end diff --git a/spec/integration/indirector/module_files.rb b/spec/integration/indirector/module_files.rb index 3725a1286..3f49ec7fa 100755 --- a/spec/integration/indirector/module_files.rb +++ b/spec/integration/indirector/module_files.rb @@ -5,13 +5,13 @@ require File.dirname(__FILE__) + '/../../spec_helper' -require 'puppet/indirector/file_metadata/modules' +require 'puppet/indirector/file_content/modules' require 'puppet/indirector/module_files' -describe Puppet::Indirector::ModuleFiles, " when interacting with Puppet::Module" do +describe Puppet::Indirector::ModuleFiles, " when interacting with Puppet::Module and FileServing::Content" do it "should look for files in the module's 'files' directory" do # We just test a subclass, since it's close enough. - @terminus = Puppet::Indirector::FileMetadata::Modules.new + @terminus = Puppet::Indirector::FileContent::Modules.new @module = Puppet::Module.new("mymod", "/some/path/mymod") Puppet::Module.expects(:find).with("mymod", nil).returns(@module) @@ -19,8 +19,33 @@ describe Puppet::Indirector::ModuleFiles, " when interacting with Puppet::Module FileTest.expects(:exists?).with(filepath).returns(true) - @terminus.model.expects(:new).with(filepath, :links => nil) + @terminus.find("puppetmounts://host/modules/mymod/myfile").should be_instance_of(Puppet::FileServing::Content) + end +end + +describe Puppet::Indirector::ModuleFiles, " when interacting with FileServing::Fileset and FileServing::Content" do + it "should return an instance for every file in the fileset" do + @terminus = Puppet::Indirector::FileContent::Modules.new + @module = Puppet::Module.new("mymod", "/some/path/mymod") + Puppet::Module.expects(:find).with("mymod", nil).returns(@module) + + filepath = "/some/path/mymod/files/myfile" + FileTest.stubs(:exists?).with(filepath).returns(true) + + stat = stub 'stat', :directory? => true + File.stubs(:lstat).with(filepath).returns(stat) + + subfiles = %w{one two} + subfiles.each do |f| + path = File.join(filepath, f) + FileTest.stubs(:exists?).with(path).returns(true) + end + + Dir.expects(:entries).with(filepath).returns(%w{one two}) - @terminus.find("puppetmounts://host/modules/mymod/myfile") + result = @terminus.search("puppetmounts://host/modules/mymod/myfile", :recurse => true) + result.should be_instance_of(Array) + result.length.should == 3 + result.each { |r| r.should be_instance_of(Puppet::FileServing::Content) } end end diff --git a/spec/lib/shared_behaviours/file_server_terminus.rb b/spec/lib/shared_behaviours/file_server_terminus.rb index e1ec35251..de08f29fc 100644 --- a/spec/lib/shared_behaviours/file_server_terminus.rb +++ b/spec/lib/shared_behaviours/file_server_terminus.rb @@ -32,10 +32,9 @@ describe "Puppet::Indirector::FileServerTerminus", :shared => true do path = "/my/mount/path/my/file" FileTest.stubs(:exists?).with(path).returns(true) - @test_class.expects(:new).with(path, :links => nil).returns(:myinstance) FileTest.stubs(:exists?).with("/my/mount/path").returns(true) @mount1.expects(:file).with("my/file", :node => nil).returns(path) - @terminus.find("puppetmounts://myhost/one/my/file").should == :myinstance + @terminus.find("puppetmounts://myhost/one/my/file").should be_instance_of(@test_class) end end diff --git a/spec/lib/shared_behaviours/file_serving.rb b/spec/lib/shared_behaviours/file_serving.rb index 6eaec6850..a5e80f89b 100644 --- a/spec/lib/shared_behaviours/file_serving.rb +++ b/spec/lib/shared_behaviours/file_serving.rb @@ -27,12 +27,14 @@ describe "Puppet::FileServing::Files", :shared => true do Puppet.settings.stubs(:value).with(:fileserverconfig).returns("/whatever") Puppet.settings.stubs(:value).with(:environment).returns("") @indirection.terminus(:file_server).expects(:find).with(uri) + @indirection.terminus(:file_server).stubs(:authorized?).returns(true) @test_class.find(uri) end it "should use the file_server terminus when the 'puppetmounts' URI scheme is used" do uri = "puppetmounts:///mymod/my/file" @indirection.terminus(:file_server).expects(:find).with(uri) + @indirection.terminus(:file_server).stubs(:authorized?).returns(true) @test_class.find(uri) end diff --git a/spec/unit/file_serving/content.rb b/spec/unit/file_serving/content.rb index e15aa8be6..89d786295 100755 --- a/spec/unit/file_serving/content.rb +++ b/spec/unit/file_serving/content.rb @@ -13,84 +13,48 @@ describe Puppet::FileServing::Content do Puppet::FileServing::Content.indirection.name.should == :file_content end - it "should should include the TerminusSelector module in its indirection" do - Puppet::FileServing::Content.indirection.metaclass.included_modules.should include(Puppet::FileServing::TerminusSelector) - end -end - -describe Puppet::FileServing::Content, " when initializing" do - it "should accept a file path" do - Puppet::FileServing::Content.new("not/qualified").path.should == "not/qualified" - end - - it "should not allow a fully qualified file path" do - proc { Puppet::FileServing::Content.new("/fully/qualified") }.should raise_error(ArgumentError) - end - - it "should allow specification of whether links should be managed" do - Puppet::FileServing::Content.new("not/qualified", :links => :manage) - end - - it "should fail if :links is set to anything other than :manage or :follow" do - Puppet::FileServing::Content.new("not/qualified", :links => :manage) - end - - it "should default to :manage for :links" do - Puppet::FileServing::Content.new("not/qualified", :links => :manage) + it "should should include the IndirectionHooks module in its indirection" do + Puppet::FileServing::Content.indirection.metaclass.included_modules.should include(Puppet::FileServing::IndirectionHooks) end end describe Puppet::FileServing::Content, " when returning the contents" do before do - @content = Puppet::FileServing::Content.new("sub/path", :links => :follow) - @base = "/my/base" - @full = "/my/base/sub/path" + @path = "/my/base" + @content = Puppet::FileServing::Content.new("sub/path", :links => :follow, :path => @path) end it "should fail if the file is a symlink and links are set to :manage" do @content.links = :manage - File.expects(:lstat).with(@full).returns stub("stat", :ftype => "symlink") - proc { @content.content(@base) }.should raise_error(ArgumentError) - end - - it "should accept a base path path to which the file should be relative" do - File.expects(:stat).with(@full).returns stub("stat", :ftype => "file") - File.expects(:read).with(@full).returns(:mycontent) - @content.content(@base).should == :mycontent + File.expects(:lstat).with(@path).returns stub("stat", :ftype => "symlink") + proc { @content.content }.should raise_error(ArgumentError) end - it "should use the set base path if one is not provided" do - @content.base_path = @base - File.expects(:stat).with(@full).returns stub("stat", :ftype => "file") - File.expects(:read).with(@full).returns(:mycontent) - @content.content() - end - - it "should fail if a base path is neither set nor provided" do - proc { @content.content() }.should raise_error(ArgumentError) + it "should fail if a path is not set" do + proc { @content.content() }.should raise_error(Errno::ENOENT) end it "should raise Errno::ENOENT if the file is absent" do - @content.base_path = "/there/is/absolutely/no/chance/that/this/path/exists" + @content.path = "/there/is/absolutely/no/chance/that/this/path/exists" proc { @content.content() }.should raise_error(Errno::ENOENT) end it "should return the contents of the path if the file exists" do - File.expects(:stat).with(@full).returns stub("stat", :ftype => "file") - File.expects(:read).with(@full).returns(:mycontent) - @content.content(@base).should == :mycontent + File.expects(:stat).with(@path).returns stub("stat", :ftype => "file") + File.expects(:read).with(@path).returns(:mycontent) + @content.content.should == :mycontent end end describe Puppet::FileServing::Content, " when converting to yaml" do - it "should fail if no base path has been set" do - @content = Puppet::FileServing::Content.new("some/path") + it "should fail if no path has been set" do + @content = Puppet::FileServing::Content.new("some/key") proc { @content.to_yaml }.should raise_error(ArgumentError) end it "should return the file contents" do @content = Puppet::FileServing::Content.new("some/path") - @content.base_path = "/base/path" + @content.path = "/base/path" @content.expects(:content).returns(:content) @content.to_yaml.should == :content end diff --git a/spec/unit/file_serving/file_base.rb b/spec/unit/file_serving/file_base.rb index 14be6d003..4c7724f7c 100755 --- a/spec/unit/file_serving/file_base.rb +++ b/spec/unit/file_serving/file_base.rb @@ -5,48 +5,67 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/file_base' describe Puppet::FileServing::FileBase, " when initializing" do - it "should accept a file path" do - Puppet::FileServing::FileBase.new("not/qualified").path.should == "not/qualified" - end - - it "should not allow a fully qualified file path" do - proc { Puppet::FileServing::FileBase.new("/fully/qualified") }.should raise_error(ArgumentError) + it "should accept a key in the form of a URI" do + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file").key.should == "puppet://host/module/dir/file" end it "should allow specification of whether links should be managed" do - Puppet::FileServing::FileBase.new("not/qualified", :links => :manage).links.should == :manage + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :manage).links.should == :manage end it "should fail if :links is set to anything other than :manage or :follow" do - proc { Puppet::FileServing::FileBase.new("not/qualified", :links => :else) }.should raise_error(ArgumentError) + proc { Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :links => :else) }.should raise_error(ArgumentError) end it "should default to :manage for :links" do - Puppet::FileServing::FileBase.new("not/qualified").links.should == :manage + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file").links.should == :manage + end + + it "should allow specification of a path" do + FileTest.stubs(:exists?).returns(true) + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :path => "/my/file").path.should == "/my/file" + end + + it "should allow specification of a relative path" do + FileTest.stubs(:exists?).returns(true) + Puppet::FileServing::FileBase.new("puppet://host/module/dir/file", :relative_path => "my/file").relative_path.should == "my/file" end end -describe Puppet::FileServing::FileBase do - it "should provide a method for setting the base path" do - @file = Puppet::FileServing::FileBase.new("not/qualified") - @file.base_path = "/something" - @file.base_path.should == "/something" +describe Puppet::FileServing::FileBase, " when setting the base path" do + before do + @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") + end + + it "should require that the base path be fully qualified" do + FileTest.stubs(:exists?).returns(true) + proc { @file.path = "unqualified/file" }.should raise_error(ArgumentError) + end +end + +describe Puppet::FileServing::FileBase, " when setting the relative path" do + it "should require that the relative path be unqualified" do + @file = Puppet::FileServing::FileBase.new("puppet://host/module/dir/file") + FileTest.stubs(:exists?).returns(true) + proc { @file.relative_path = "/qualified/file" }.should raise_error(ArgumentError) end end describe Puppet::FileServing::FileBase, " when determining the full file path" do - it "should return the provided path joined with the qualified path if a path is provided" do - @file = Puppet::FileServing::FileBase.new("not/qualified") - @file.full_path("/this/file").should == "/this/file/not/qualified" + before do + @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") end - it "should return the set base path joined with the qualified path if a base path is set" do - @file = Puppet::FileServing::FileBase.new("not/qualified") - @file.base_path = "/this/file" + it "should return the path if there is no relative path" do + @file.full_path.should == "/this/file" + end + + it "should return the path joined with the relative path if there is a relative path" do + @file.relative_path = "not/qualified" @file.full_path.should == "/this/file/not/qualified" end - it "should should fail if a base path is neither provided nor set" do + it "should should fail if there is no path set" do @file = Puppet::FileServing::FileBase.new("not/qualified") proc { @file.full_path }.should raise_error(ArgumentError) end @@ -54,32 +73,29 @@ end describe Puppet::FileServing::FileBase, " when stat'ing files" do before do - @file = Puppet::FileServing::FileBase.new("not/qualified") - end - - it "should join the provided path with the qualified path is a path is provided" do - File.expects(:lstat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") - @file.stat("/this/file") + @file = Puppet::FileServing::FileBase.new("mykey", :path => "/this/file") end - it "should use the set base path if no base is provided" do - @file.base_path = "/this/file" - File.expects(:lstat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") + it "should stat the file's full path" do + @file.stubs(:full_path).returns("/this/file") + File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") @file.stat end - it "should fail if a base path is neither set nor provided" do - proc { @file.stat }.should raise_error(ArgumentError) + it "should fail if the file does not exist" do + @file.stubs(:full_path).returns("/this/file") + File.expects(:lstat).with("/this/file").raises(Errno::ENOENT) + proc { @file.stat }.should raise_error(Errno::ENOENT) end it "should use :lstat if :links is set to :manage" do - File.expects(:lstat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") - @file.stat("/this/file") + File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file") + @file.stat end it "should use :stat if :links is set to :follow" do - File.expects(:stat).with("/this/file/not/qualified").returns stub("stat", :ftype => "file") + File.expects(:stat).with("/this/file").returns stub("stat", :ftype => "file") @file.links = :follow - @file.stat("/this/file") + @file.stat end end diff --git a/spec/unit/file_serving/terminus_selector.rb b/spec/unit/file_serving/indirection_hooks.rb index 9c2c01bfd..34614b7b8 100755 --- a/spec/unit/file_serving/terminus_selector.rb +++ b/spec/unit/file_serving/indirection_hooks.rb @@ -5,12 +5,12 @@ require File.dirname(__FILE__) + '/../../spec_helper' -require 'puppet/file_serving/terminus_selector' +require 'puppet/file_serving/indirection_hooks' -describe Puppet::FileServing::TerminusSelector, " when being used to select termini" do +describe Puppet::FileServing::IndirectionHooks, " when being used to select termini" do before do @object = Object.new - @object.extend(Puppet::FileServing::TerminusSelector) + @object.extend(Puppet::FileServing::IndirectionHooks) end it "should escape the key before parsing" do @@ -83,10 +83,10 @@ describe Puppet::FileServing::TerminusSelector, " when being used to select term end end -describe Puppet::FileServing::TerminusSelector, " when looking for a module whose name matches the mount name" do +describe Puppet::FileServing::IndirectionHooks, " when looking for a module whose name matches the mount name" do before do @object = Object.new - @object.extend(Puppet::FileServing::TerminusSelector) + @object.extend(Puppet::FileServing::IndirectionHooks) @modules = mock 'modules' @object.stubs(:terminus).with(:modules).returns(@modules) diff --git a/spec/unit/file_serving/metadata.rb b/spec/unit/file_serving/metadata.rb index 27ebe2471..f7ab0c8d6 100755 --- a/spec/unit/file_serving/metadata.rb +++ b/spec/unit/file_serving/metadata.rb @@ -5,38 +5,16 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/metadata' describe Puppet::FileServing::Metadata do - it "should indirect file_metadata" do - Puppet::FileServing::Metadata.indirection.name.should == :file_metadata - end - - it "should should include the TerminusSelector module in its indirection" do - Puppet::FileServing::Metadata.indirection.metaclass.included_modules.should include(Puppet::FileServing::TerminusSelector) - end -end - -describe Puppet::FileServing::Metadata, " when initializing" do - it "should not allow initialization without a path" do - proc { Puppet::FileServing::Metadata.new() }.should raise_error(ArgumentError) - end - - it "should not allow the path to be fully qualified if it is provided" do - proc { Puppet::FileServing::Metadata.new("/fully/qualified") }.should raise_error(ArgumentError) + it "should should be a subclass of FileBase" do + Puppet::FileServing::Metadata.superclass.should equal(Puppet::FileServing::FileBase) end - it "should allow initialization with a relative path" do - Puppet::FileServing::Metadata.new("not/fully/qualified") - end - - it "should allow specification of whether links should be managed" do - Puppet::FileServing::Metadata.new("not/qualified", :links => :manage) - end - - it "should fail if :links is set to anything other than :manage or :follow" do - Puppet::FileServing::Metadata.new("not/qualified", :links => :manage) + it "should indirect file_metadata" do + Puppet::FileServing::Metadata.indirection.name.should == :file_metadata end - it "should default to :manage for :links" do - Puppet::FileServing::Metadata.new("not/qualified", :links => :manage) + it "should should include the IndirectionHooks module in its indirection" do + Puppet::FileServing::Metadata.indirection.metaclass.included_modules.should include(Puppet::FileServing::IndirectionHooks) end end @@ -44,9 +22,10 @@ describe Puppet::FileServing::Metadata, " when finding the file to use for setti before do @metadata = Puppet::FileServing::Metadata.new("my/path") - @base = "/base/path" @full = "/base/path/my/path" + @metadata.path = @full + # Use a symlink because it's easier to test -- no checksumming @stat = stub "stat", :uid => 10, :gid => 20, :mode => 0755, :ftype => "symlink" end @@ -54,23 +33,22 @@ describe Puppet::FileServing::Metadata, " when finding the file to use for setti it "should accept a base path path to which the file should be relative" do File.expects(:lstat).with(@full).returns @stat File.expects(:readlink).with(@full).returns "/what/ever" - @metadata.collect_attributes(@base) + @metadata.collect_attributes end it "should use the set base path if one is not provided" do - @metadata.base_path = @base File.expects(:lstat).with(@full).returns @stat File.expects(:readlink).with(@full).returns "/what/ever" @metadata.collect_attributes() end it "should fail if a base path is neither set nor provided" do - proc { @metadata.collect_attributes() }.should raise_error(ArgumentError) + proc { @metadata.collect_attributes() }.should raise_error(Errno::ENOENT) end it "should raise an exception if the file does not exist" do - File.expects(:lstat).with("/base/dir/my/path").raises(Errno::ENOENT) - proc { @metadata.collect_attributes("/base/dir")}.should raise_error(Errno::ENOENT) + File.expects(:lstat).with(@full).raises(Errno::ENOENT) + proc { @metadata.collect_attributes()}.should raise_error(Errno::ENOENT) end end @@ -83,12 +61,8 @@ describe Puppet::FileServing::Metadata, " when collecting attributes" do @filehandle.expects(:each_line).yields("some content\n") File.stubs(:open).with(@path, 'r').yields(@filehandle) @checksum = Digest::MD5.hexdigest("some content\n") - @metadata = Puppet::FileServing::Metadata.new("file") - @metadata.collect_attributes("/my") - end - - it "should accept a file path" do - @metadata.path.should == "file" + @metadata = Puppet::FileServing::Metadata.new("file", :path => "/my/file") + @metadata.collect_attributes end # LAK:FIXME This should actually change at some point @@ -128,22 +102,22 @@ end describe Puppet::FileServing::Metadata, " when pointing to a symlink" do it "should store the destination of the symlink in :destination if links are :manage" do - file = Puppet::FileServing::Metadata.new("my/file", :links => :manage) + file = Puppet::FileServing::Metadata.new("mykey", :links => :manage, :path => "/base/path/my/file") File.expects(:lstat).with("/base/path/my/file").returns stub("stat", :uid => 1, :gid => 2, :ftype => "symlink", :mode => 0755) File.expects(:readlink).with("/base/path/my/file").returns "/some/other/path" - file.collect_attributes("/base/path") + file.collect_attributes file.destination.should == "/some/other/path" end it "should not collect the checksum" do - file = Puppet::FileServing::Metadata.new("my/file", :links => :manage) + file = Puppet::FileServing::Metadata.new("my/file", :links => :manage, :path => "/base/path/my/file") File.expects(:lstat).with("/base/path/my/file").returns stub("stat", :uid => 1, :gid => 2, :ftype => "symlink", :mode => 0755) File.expects(:readlink).with("/base/path/my/file").returns "/some/other/path" - file.collect_attributes("/base/path") + file.collect_attributes file.checksum.should be_nil end end diff --git a/spec/unit/file_serving/terminus_helper.rb b/spec/unit/file_serving/terminus_helper.rb index 3a5274b5a..b919469a2 100755 --- a/spec/unit/file_serving/terminus_helper.rb +++ b/spec/unit/file_serving/terminus_helper.rb @@ -19,20 +19,60 @@ describe Puppet::FileServing::TerminusHelper do it "should use a fileset to find paths" do fileset = mock 'fileset', :files => [] Puppet::FileServing::Fileset.expects(:new).with("/my/file", {}).returns(fileset) - @helper.path2instances("/my/file") + @helper.path2instances("url", "/my/file") end it "should pass :recurse, :ignore, and :links settings on to the fileset if present" do fileset = mock 'fileset', :files => [] Puppet::FileServing::Fileset.expects(:new).with("/my/file", :links => :a, :ignore => :b, :recurse => :c).returns(fileset) - @helper.path2instances("/my/file", :links => :a, :ignore => :b, :recurse => :c) + @helper.path2instances("url", "/my/file", :links => :a, :ignore => :b, :recurse => :c) end +end - it "should return an instance of the model for each path returned by the fileset" do - fileset = mock 'fileset', :files => %w{one two} - Puppet::FileServing::Fileset.expects(:new).with("/my/file", {}).returns(fileset) - @model.expects(:new).with("one").returns(:one) - @model.expects(:new).with("two").returns(:two) - @helper.path2instances("/my/file").should == [:one, :two] + +describe Puppet::FileServing::TerminusHelper, " when creating instances" do + before do + @helper = Object.new + @helper.extend(Puppet::FileServing::TerminusHelper) + + @model = mock 'model' + @helper.stubs(:model).returns(@model) + + @key = "puppet://host/mount/dir" + + @fileset = mock 'fileset', :files => %w{one two} + Puppet::FileServing::Fileset.expects(:new).returns(@fileset) + end + + it "should create an instance of the model for each path returned by the fileset" do + @model.expects(:new).returns(:one) + @model.expects(:new).returns(:two) + @helper.path2instances(@key, "/my/file").length.should == 2 + end + + it "should set each instance's key to be the original key plus the file-specific path" do + @model.expects(:new).with { |key, options| key == @key + "/one" }.returns(:one) + @model.expects(:new).with { |key, options| key == @key + "/two" }.returns(:two) + @helper.path2instances(@key, "/my/file") + end + + it "should set each returned instance's path to the original path" do + @model.expects(:new).with { |key, options| options[:path] == "/my/file" }.returns(:one) + @model.expects(:new).with { |key, options| options[:path] == "/my/file" }.returns(:two) + @helper.path2instances(@key, "/my/file") + end + + it "should set each returned instance's relative path to the file-specific path" do + @model.expects(:new).with { |key, options| options[:relative_path] == "one" }.returns(:one) + @model.expects(:new).with { |key, options| options[:relative_path] == "two" }.returns(:two) + @helper.path2instances(@key, "/my/file") + end + + it "should set the links value on each instance if one is provided" do + one = mock 'one', :links= => :manage + two = mock 'two', :links= => :manage + @model.expects(:new).returns(one) + @model.expects(:new).returns(two) + @helper.path2instances(@key, "/my/file", :links => :manage) end end diff --git a/spec/unit/indirector/direct_file_server.rb b/spec/unit/indirector/direct_file_server.rb new file mode 100755 index 000000000..2a8ec1a49 --- /dev/null +++ b/spec/unit/indirector/direct_file_server.rb @@ -0,0 +1,95 @@ +#!/usr/bin/env ruby +# +# Created by Luke Kanies on 2007-10-24. +# Copyright (c) 2007. All rights reserved. + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/indirector/direct_file_server' + +module DirectFileServerTerminusTesting + def setup + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) + + @direct_file_class = Class.new(Puppet::Indirector::DirectFileServer) do + def self.to_s + "Testing::Mytype" + end + end + + @server = @direct_file_class.new + + @uri = "file:///my/local" + end +end + +describe Puppet::Indirector::DirectFileServer, "when finding a single file" do + include DirectFileServerTerminusTesting + + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@uri).should be_nil + end + + it "should return a Content instance created with the full path to the file if the file exists" do + FileTest.expects(:exists?).with("/my/local").returns true + @model.expects(:new).returns(:mycontent) + @server.find(@uri).should == :mycontent + end +end + +describe Puppet::Indirector::DirectFileServer, "when creating the instance for a single found file" do + include DirectFileServerTerminusTesting + + before do + @data = mock 'content' + @data.stubs(:collect_attributes) + FileTest.expects(:exists?).with("/my/local").returns true + end + + it "should create the Content instance with the original key as the key" do + @model.expects(:new).with { |key, options| key == @uri }.returns(@data) + @server.find(@uri) + end + + it "should pass the full path to the instance" do + @model.expects(:new).with { |key, options| options[:path] == "/my/local" }.returns(@data) + @server.find(@uri) + end + + it "should pass the :links setting on to the created Content instance if the file exists and there is a value for :links" do + @model.expects(:new).returns(@data) + @data.expects(:links=).with(:manage) + @server.find(@uri, :links => :manage) + end +end + +describe Puppet::Indirector::DirectFileServer, "when searching for multiple files" do + include DirectFileServerTerminusTesting + + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@uri).should be_nil + end + + it "should pass the original key to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with { |uri, path, options| uri == @uri } + @server.search(@uri) + end + + it "should use :path2instances from the terminus_helper to return instances if the file exists" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances) + @server.search(@uri) + end + + it "should pass any options on to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with { |uri, path, options| options == {:testing => :one, :other => :two}} + @server.search(@uri, :testing => :one, :other => :two) + end +end diff --git a/spec/unit/indirector/file_content/file.rb b/spec/unit/indirector/file_content/file.rb index da2c90770..04656e0ed 100755 --- a/spec/unit/indirector/file_content/file.rb +++ b/spec/unit/indirector/file_content/file.rb @@ -12,59 +12,7 @@ describe Puppet::Indirector::FileContent::File do Puppet::Indirector::Terminus.terminus_class(:file_content, :file).should equal(Puppet::Indirector::FileContent::File) end - it "should be a subclass of the File terminus" do - Puppet::Indirector::FileContent::File.superclass.should equal(Puppet::Indirector::File) - end -end - -describe Puppet::Indirector::FileContent::File, "when finding a single file" do - it "should return a Content instance created with the full path to the file if the file exists" do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Content.expects(:new).with("/my/local", :links => nil).returns(:mycontent) - @content.find(@uri).should == :mycontent - end - - it "should pass the :links setting on to the created Content instance if the file exists" do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Content.expects(:new).with("/my/local", :links => :manage).returns(:mycontent) - @content.find(@uri, :links => :manage) - end - - it "should return nil if the file does not exist" do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - - FileTest.expects(:exists?).with("/my/local").returns false - @content.find(@uri).should be_nil - end -end - -describe Puppet::Indirector::FileContent::File, "when searching for multiple files" do - before do - @content = Puppet::Indirector::FileContent::File.new - @uri = "file:///my/local" - end - - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @content.find(@uri).should be_nil - end - - it "should use :path2instances from the terminus_helper to return instances if the file exists" do - FileTest.expects(:exists?).with("/my/local").returns true - @content.expects(:path2instances).with("/my/local", {}) - @content.search(@uri) - end - - it "should pass any options on to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @content.expects(:path2instances).with("/my/local", :testing => :one, :other => :two) - @content.search(@uri, :testing => :one, :other => :two) + it "should be a subclass of the DirectFileServer terminus" do + Puppet::Indirector::FileContent::File.superclass.should equal(Puppet::Indirector::DirectFileServer) end end diff --git a/spec/unit/indirector/file_metadata/file.rb b/spec/unit/indirector/file_metadata/file.rb index c88d559a7..0a37a6895 100755 --- a/spec/unit/indirector/file_metadata/file.rb +++ b/spec/unit/indirector/file_metadata/file.rb @@ -11,44 +11,27 @@ describe Puppet::Indirector::FileMetadata::File do it "should be registered with the file_metadata indirection" do Puppet::Indirector::Terminus.terminus_class(:file_metadata, :file).should equal(Puppet::Indirector::FileMetadata::File) end + + it "should be a subclass of the DirectFileServer terminus" do + Puppet::Indirector::FileMetadata::File.superclass.should equal(Puppet::Indirector::DirectFileServer) + end end -describe Puppet::Indirector::FileMetadata::File, "when finding a single file" do +describe Puppet::Indirector::FileMetadata::File, "when creating the instance for a single found file" do before do @metadata = Puppet::Indirector::FileMetadata::File.new @uri = "file:///my/local" - @data = mock 'metadata' - end - - it "should return a Metadata instance created with the full path to the file if the file exists" do - @data.stubs(:collect_attributes) - - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Metadata.expects(:new).with("/my/local", :links => nil).returns(@data) - @metadata.find(@uri).should == @data - end - - it "should pass the :links setting on to the created Content instance if the file exists" do @data.stubs(:collect_attributes) - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Metadata.expects(:new).with("/my/local", :links => :manage).returns(@data) - @metadata.find(@uri, :links => :manage) end it "should collect its attributes when a file is found" do @data.expects(:collect_attributes) - FileTest.expects(:exists?).with("/my/local").returns true - Puppet::FileServing::Metadata.expects(:new).with("/my/local", :links => nil).returns(@data) + Puppet::FileServing::Metadata.expects(:new).returns(@data) @metadata.find(@uri).should == @data end - - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @metadata.find(@uri).should be_nil - end end describe Puppet::Indirector::FileMetadata::File, "when searching for multiple files" do @@ -57,26 +40,9 @@ describe Puppet::Indirector::FileMetadata::File, "when searching for multiple fi @uri = "file:///my/local" end - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @metadata.find(@uri).should be_nil - end - - it "should use :path2instances from the terminus_helper to return instances if the file exists" do - FileTest.expects(:exists?).with("/my/local").returns true - @metadata.expects(:path2instances).with("/my/local", {}).returns([]) - @metadata.search(@uri) - end - - it "should pass any options on to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @metadata.expects(:path2instances).with("/my/local", :testing => :one, :other => :two).returns([]) - @metadata.search(@uri, :testing => :one, :other => :two) - end - it "should collect the attributes of the instances returned" do FileTest.expects(:exists?).with("/my/local").returns true - @metadata.expects(:path2instances).with("/my/local", {}).returns( [mock("one", :collect_attributes => nil), mock("two", :collect_attributes => nil)] ) + @metadata.expects(:path2instances).returns( [mock("one", :collect_attributes => nil), mock("two", :collect_attributes => nil)] ) @metadata.search(@uri) end end diff --git a/spec/unit/indirector/file_metadata/modules.rb b/spec/unit/indirector/file_metadata/modules.rb index 94e1bf0dc..62f01832c 100755 --- a/spec/unit/indirector/file_metadata/modules.rb +++ b/spec/unit/indirector/file_metadata/modules.rb @@ -34,7 +34,7 @@ describe Puppet::Indirector::FileMetadata::Modules, " when finding metadata" do FileTest.expects(:exists?).with("/path/to/files/my/file").returns true instance = mock 'metadta' Puppet::FileServing::Metadata.expects(:new).returns instance - instance.expects :get_attributes + instance.expects :collect_attributes @finder.find("puppetmounts://hostname/modules/mymod/my/file") end end diff --git a/spec/unit/indirector/file_server.rb b/spec/unit/indirector/file_server.rb index ed36e180e..fda60f1ec 100755 --- a/spec/unit/indirector/file_server.rb +++ b/spec/unit/indirector/file_server.rb @@ -54,7 +54,7 @@ describe Puppet::Indirector::FileServer, " when finding files" do it "should return an instance of the model created with the full path if a file is found" do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") - @model.expects(:new).with("/some/file", :links => nil).returns(:myinstance) + @model.expects(:new).returns(:myinstance) @file_server.find(@uri).should == :myinstance end end @@ -63,11 +63,31 @@ end describe Puppet::Indirector::FileServer, " when returning instances" do include FileServerTerminusTesting - it "should pass the provided :links setting on to the instance if one is provided" do + before do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") - @model.expects(:new).with("/some/file", :links => :mytest) + @instance = mock 'instance' + end + + it "should create the instance with the key used to find the instance" do + @model.expects(:new).with { |key, *options| key == @uri } + @file_server.find(@uri) + end + + it "should create the instance with the path at which the instance was found" do + @model.expects(:new).with { |key, options| options[:path] == "/some/file" } + @file_server.find(@uri) + end + + it "should set the provided :links setting on to the instance if one is provided" do + @model.expects(:new).returns(@instance) + @instance.expects(:links=).with(:mytest) @file_server.find(@uri, :links => :mytest) end + + it "should not set a :links value if no :links parameter is provided" do + @model.expects(:new).returns(@instance) + @file_server.find(@uri) + end end describe Puppet::Indirector::FileServer, " when checking authorization" do @@ -141,13 +161,13 @@ describe Puppet::Indirector::FileServer, " when searching for files" do it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") - @file_server.expects(:path2instances).with("/my/file", {}) + @file_server.expects(:path2instances).with(@uri, "/my/file", {}) @file_server.search(@uri) end it "should pass any options on to :path2instances" do @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") - @file_server.expects(:path2instances).with("/my/file", :testing => :one, :other => :two) + @file_server.expects(:path2instances).with(@uri, "/my/file", :testing => :one, :other => :two) @file_server.search(@uri, :testing => :one, :other => :two) end end diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb index 00153ff35..5d8453905 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -67,7 +67,15 @@ describe Puppet::Indirector::Indirection, " when looking for a model instance" d proc { @indirection.find(@name) }.should_not raise_error end - it "should call the terminus's authorization hook if there is one" + it "should pass the instance to the :post_find hook if there is one" do + class << @terminus + def post_find + end + end + @terminus.expects(:post_find).with(@instance) + @terminus.expects(:find).with(@name).returns(@instance) + @indirection.find(@name) + end end describe Puppet::Indirector::Indirection, " when removing a model instance" do @@ -77,8 +85,6 @@ describe Puppet::Indirector::Indirection, " when removing a model instance" do @terminus.expects(:destroy).with(@name).returns(@instance) @indirection.destroy(@name).should == @instance end - - it "should call the terminus's authorization hook if there is one" end describe Puppet::Indirector::Indirection, " when searching for multiple model instances" do @@ -89,7 +95,15 @@ describe Puppet::Indirector::Indirection, " when searching for multiple model in @indirection.search(@name).should == @instance end - it "should call the terminus's authorization hook if there is one" + it "should pass the instances to the :post_search hook if there is one" do + class << @terminus + def post_search + end + end + @terminus.expects(:post_search).with(@instance) + @terminus.expects(:search).with(@name).returns(@instance) + @indirection.search(@name) + end end describe Puppet::Indirector::Indirection, " when storing a model instance" do @@ -99,8 +113,6 @@ describe Puppet::Indirector::Indirection, " when storing a model instance" do @terminus.expects(:save).with(@instance).returns(@instance) @indirection.save(@instance).should == @instance end - - it "should call the terminus's authorization hook if there is one" end describe Puppet::Indirector::Indirection, " when handling instance versions" do @@ -111,8 +123,6 @@ describe Puppet::Indirector::Indirection, " when handling instance versions" do @indirection.version(@name).should == 5 end - it "should call the terminus's authorization hook if there is one" - it "should add versions to found instances that do not already have them" do @terminus.expects(:find).with(@name).returns(@instance) time = mock 'time' @@ -470,3 +480,71 @@ describe Puppet::Indirector::Indirection, " when finding and using a cache" do @indirection.find(name).should equal(real) end end + +describe Puppet::Indirector::Indirection, " when an authorization hook is present" do + include IndirectionTesting + + before do + # So the :respond_to? turns out right. + class << @terminus + def authorized? + end + end + end + + it "should not check authorization if a node name is not provided" do + @terminus.expects(:authorized?).never + @terminus.stubs(:find) + @indirection.find("/my/key") + end + + it "should fail while finding instances if authorization returns false" do + @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:find) + proc { @indirection.find("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue finding instances if authorization returns true" do + @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(true) + @terminus.stubs(:find) + @indirection.find("/my/key", :node => "mynode") + end + + it "should fail while saving instances if authorization returns false" do + @terminus.expects(:authorized?).with(:save, :myinstance, :node => "mynode").returns(false) + @terminus.stubs(:save) + proc { @indirection.save(:myinstance, :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue saving instances if authorization returns true" do + instance = stub 'instance', :version => 1.0, :name => "eh" + @terminus.expects(:authorized?).with(:save, instance, :node => "mynode").returns(true) + @terminus.stubs(:save) + @indirection.save(instance, :node => "mynode") + end + + it "should fail while destroying instances if authorization returns false" do + @terminus.expects(:authorized?).with(:destroy, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:destroy) + proc { @indirection.destroy("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue destroying instances if authorization returns true" do + instance = stub 'instance', :version => 1.0, :name => "eh" + @terminus.expects(:authorized?).with(:destroy, instance, :node => "mynode").returns(true) + @terminus.stubs(:destroy) + @indirection.destroy(instance, :node => "mynode") + end + + it "should fail while searching for instances if authorization returns false" do + @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:search) + proc { @indirection.search("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue searching for instances if authorization returns true" do + @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(true) + @terminus.stubs(:search) + @indirection.search("/my/key", :node => "mynode") + end +end diff --git a/spec/unit/indirector/module_files.rb b/spec/unit/indirector/module_files.rb index f2e43f771..9cb683c3c 100755 --- a/spec/unit/indirector/module_files.rb +++ b/spec/unit/indirector/module_files.rb @@ -62,10 +62,10 @@ describe Puppet::Indirector::ModuleFiles, " when finding files" do @module_files.find(@uri).should be_nil end - it "should return an instance of the model created with the full path if a module is found and the file exists" do + it "should return an instance of the model if a module is found and the file exists" do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @model.expects(:new).with("/module/path/files/local/file", :links => nil).returns(:myinstance) + @model.expects(:new).returns(:myinstance) @module_files.find(@uri).should == :myinstance end @@ -92,12 +92,32 @@ end describe Puppet::Indirector::ModuleFiles, " when returning instances" do include ModuleFilesTerminusTesting - it "should pass the provided :links setting on to the instance if one is provided" do + before do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @model.expects(:new).with("/module/path/files/local/file", :links => :mytest) + @instance = mock 'instance' + end + + it "should create the instance with the key used to find the instance" do + @model.expects(:new).with { |key, *options| key == @uri } + @module_files.find(@uri) + end + + it "should create the instance with the path at which the instance was found" do + @model.expects(:new).with { |key, options| options[:path] == "/module/path/files/local/file" } + @module_files.find(@uri) + end + + it "should set the provided :links setting on to the instance if one is provided" do + @model.expects(:new).returns(@instance) + @instance.expects(:links=).with(:mytest) @module_files.find(@uri, :links => :mytest) end + + it "should not set a :links value if no :links parameter is provided" do + @model.expects(:new).returns(@instance) + @module_files.find(@uri) + end end describe Puppet::Indirector::ModuleFiles, " when authorizing" do @@ -213,14 +233,14 @@ describe Puppet::Indirector::ModuleFiles, " when searching for files" do it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @module_files.expects(:path2instances).with("/module/path/files/local/file", {}) + @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", {}) @module_files.search(@uri) end it "should pass any options on to :path2instances" do Puppet::Module.expects(:find).with('my', nil).returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @module_files.expects(:path2instances).with("/module/path/files/local/file", :testing => :one, :other => :two) + @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", :testing => :one, :other => :two) @module_files.search(@uri, :testing => :one, :other => :two) end end |