diff options
author | Luke Kanies <luke@madstop.com> | 2008-08-24 14:11:48 -0500 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2008-08-26 22:40:40 -0700 |
commit | 550e3d6ad5aadfe99fc1e10efa77cc193d3a9df3 (patch) | |
tree | 5945db48003ab63c68db4f5d229345fb3ecef1a9 | |
parent | 90e70227b0bb7cfd104ae34de8f7c2b7250edb09 (diff) | |
download | puppet-550e3d6ad5aadfe99fc1e10efa77cc193d3a9df3.tar.gz puppet-550e3d6ad5aadfe99fc1e10efa77cc193d3a9df3.tar.xz puppet-550e3d6ad5aadfe99fc1e10efa77cc193d3a9df3.zip |
Finishing the rename of FileBase => Base.
Git did something really strange, in that it apparently didn't
add the new base.rb files even though I used 'git mv'.
Also fixing some other failing tests I hadn't previously tracked
down because of the magical tuple of autotest's suckiness and
my laziness.
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r-- | lib/puppet/file_serving/base.rb | 76 | ||||
-rw-r--r-- | lib/puppet/indirector/module_files.rb | 7 | ||||
-rwxr-xr-x | spec/unit/file_serving/base.rb | 124 | ||||
-rwxr-xr-x | spec/unit/file_serving/content.rb | 4 | ||||
-rwxr-xr-x | spec/unit/indirector/file_metadata/modules.rb | 2 | ||||
-rwxr-xr-x | spec/unit/indirector/module_files.rb | 64 |
6 files changed, 237 insertions, 40 deletions
diff --git a/lib/puppet/file_serving/base.rb b/lib/puppet/file_serving/base.rb new file mode 100644 index 000000000..1cfd0bc4c --- /dev/null +++ b/lib/puppet/file_serving/base.rb @@ -0,0 +1,76 @@ +# +# Created by Luke Kanies on 2007-10-22. +# Copyright (c) 2007. All rights reserved. + +require 'puppet/file_serving' + +# The base class for Content and Metadata; provides common +# functionality like the behaviour around links. +class Puppet::FileServing::Base + attr_accessor :key + + # Does our file exist? + def exist? + begin + stat + return true + rescue => detail + return false + end + end + + # Return the full path to our file. Fails if there's no path set. + def full_path + raise(ArgumentError, "You must set a path to get a file's path") unless self.path + + if relative_path.nil? or relative_path == "" + path + else + File.join(path, relative_path) + end + end + + def initialize(key, options = {}) + @key = key + @links = :manage + + options.each do |param, value| + begin + send param.to_s + "=", value + rescue NoMethodError + raise ArgumentError, "Invalid option %s for %s" % [param, self.class] + end + end + end + + # Determine how we deal with links. + attr_reader :links + def links=(value) + value = :manage if value == :ignore + raise(ArgumentError, ":links can only be set to :manage or :follow") unless [:manage, :follow].include?(value) + @links = value + end + + # Set our base path. + attr_reader :path + def path=(path) + raise ArgumentError.new("Paths must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/ + @path = path + end + + # Set a relative path; this is used for recursion, and sets + # the file's path relative to the initial recursion point. + attr_reader :relative_path + def relative_path=(path) + raise ArgumentError.new("Relative paths must not be fully qualified") if path =~ /^#{::File::SEPARATOR}/ + @relative_path = path + end + + # Stat our file, using the appropriate link-sensitive method. + def stat + unless defined?(@stat_method) + @stat_method = self.links == :manage ? :lstat : :stat + end + File.send(@stat_method, full_path()) + end +end diff --git a/lib/puppet/indirector/module_files.rb b/lib/puppet/indirector/module_files.rb index cf5c29cab..40dd06941 100644 --- a/lib/puppet/indirector/module_files.rb +++ b/lib/puppet/indirector/module_files.rb @@ -21,7 +21,7 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus # Make sure our file path starts with /modules, so that we authorize # against the 'modules' mount. - path = uri.path =~ /^\/modules/ ? uri.path : "/modules" + uri.path + path = uri.path =~ /^modules\// ? uri.path : "modules/" + uri.path configuration.authorized?(path, :node => request.node, :ipaddress => request.ip) end @@ -66,9 +66,8 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus def find_path(request) uri = key2uri(request.key) - # Strip off /modules if it's there -- that's how requests get routed to this terminus. - # Also, strip off the leading slash if present. - module_name, relative_path = uri.path.sub(/^\/modules\b/, '').sub(%r{^/}, '').split(File::Separator, 2) + # Strip off modules/ if it's there -- that's how requests get routed to this terminus. + module_name, relative_path = uri.path.sub(/^modules\//, '').sub(%r{^/}, '').split(File::Separator, 2) # And use the environment to look up the module. return nil unless mod = find_module(module_name, request.node) diff --git a/spec/unit/file_serving/base.rb b/spec/unit/file_serving/base.rb new file mode 100755 index 000000000..9d900e30a --- /dev/null +++ b/spec/unit/file_serving/base.rb @@ -0,0 +1,124 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/file_serving/base' + +describe Puppet::FileServing::Base do + it "should accept a key in the form of a URI" do + Puppet::FileServing::Base.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::Base.new("puppet://host/module/dir/file", :links => :manage).links.should == :manage + end + + it "should consider :ignore links equivalent to :manage links" do + Puppet::FileServing::Base.new("puppet://host/module/dir/file", :links => :ignore).links.should == :manage + end + + it "should fail if :links is set to anything other than :manage, :follow, or :ignore" do + proc { Puppet::FileServing::Base.new("puppet://host/module/dir/file", :links => :else) }.should raise_error(ArgumentError) + end + + it "should default to :manage for :links" do + Puppet::FileServing::Base.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::Base.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::Base.new("puppet://host/module/dir/file", :relative_path => "my/file").relative_path.should == "my/file" + end + + it "should have a means of determining if the file exists" do + Puppet::FileServing::Base.new("blah").should respond_to(:exist?) + end + + it "should correctly indicate if the file is present" do + File.expects(:lstat).with("/my/file").returns(mock("stat")) + Puppet::FileServing::Base.new("blah", :path => "/my/file").exist?.should be_true + end + + it "should correctly indicate if the file is asbsent" do + File.expects(:lstat).with("/my/file").raises RuntimeError + Puppet::FileServing::Base.new("blah", :path => "/my/file").exist?.should be_false + end + + describe "when setting the base path" do + before do + @file = Puppet::FileServing::Base.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 "when setting the relative path" do + it "should require that the relative path be unqualified" do + @file = Puppet::FileServing::Base.new("puppet://host/module/dir/file") + FileTest.stubs(:exists?).returns(true) + proc { @file.relative_path = "/qualified/file" }.should raise_error(ArgumentError) + end + end + + describe "when determining the full file path" do + before do + @file = Puppet::FileServing::Base.new("mykey", :path => "/this/file") + end + + it "should return the path if there is no relative path" do + @file.full_path.should == "/this/file" + end + + it "should return the path if the relative_path is set to ''" do + @file.relative_path = "" + @file.full_path.should == "/this/file" + end + + it "should return the path joined with the relative path if there is a relative path and it is not set to '/' or ''" do + @file.relative_path = "not/qualified" + @file.full_path.should == "/this/file/not/qualified" + end + + it "should should fail if there is no path set" do + @file = Puppet::FileServing::Base.new("not/qualified") + proc { @file.full_path }.should raise_error(ArgumentError) + end + end + + describe "when stat'ing files" do + before do + @file = Puppet::FileServing::Base.new("mykey", :path => "/this/file") + end + + 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 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").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").returns stub("stat", :ftype => "file") + @file.links = :follow + @file.stat + end + end +end diff --git a/spec/unit/file_serving/content.rb b/spec/unit/file_serving/content.rb index b747ced78..82353d23d 100755 --- a/spec/unit/file_serving/content.rb +++ b/spec/unit/file_serving/content.rb @@ -5,8 +5,8 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/content' describe Puppet::FileServing::Content do - it "should should be a subclass of FileBase" do - Puppet::FileServing::Content.superclass.should equal(Puppet::FileServing::FileBase) + it "should should be a subclass of Base" do + Puppet::FileServing::Content.superclass.should equal(Puppet::FileServing::Base) end it "should indirect file_content" do diff --git a/spec/unit/indirector/file_metadata/modules.rb b/spec/unit/indirector/file_metadata/modules.rb index 3905a49ad..838ac3b5f 100755 --- a/spec/unit/indirector/file_metadata/modules.rb +++ b/spec/unit/indirector/file_metadata/modules.rb @@ -24,7 +24,7 @@ describe Puppet::Indirector::FileMetadata::Modules, " when finding metadata" do @module = Puppet::Module.new("mymod", "/path/to") @finder.stubs(:find_module).returns(@module) - @request = Puppet::Indirector::Request.new(:metadata, :find, "puppetmounts://hostname/modules/mymod/my/file") + @request = Puppet::Indirector::Request.new(:metadata, :find, "puppet://hostname/modules/mymod/my/file") end it "should return nil if the file is not found" do diff --git a/spec/unit/indirector/module_files.rb b/spec/unit/indirector/module_files.rb index 32dedd4f1..20c9f499a 100755 --- a/spec/unit/indirector/module_files.rb +++ b/spec/unit/indirector/module_files.rb @@ -25,49 +25,47 @@ describe Puppet::Indirector::ModuleFiles do @module_files = @module_files_class.new - @uri = "puppetmounts://host/modules/my/local/file" @module = Puppet::Module.new("mymod", "/module/path") - @request = stub 'request', :key => @uri, :options => {}, :node => nil, :ip => nil, :method => :find + @request = Puppet::Indirector::Request.new(:mytype, :find, "puppet://host/modules/mymod/local/file") end describe Puppet::Indirector::ModuleFiles, " when finding files" do + before do + Puppet::Module.stubs(:find).returns @module + end - it "should strip off the leading '/modules' mount name" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module + it "should strip off the leading 'modules/' mount name" do + Puppet::Module.expects(:find).with { |key, env| key == 'mymod' }.returns @module @module_files.find(@request) end - it "should not strip off leading terms that start with '/modules' but are longer words" do - @request.stubs(:key).returns "puppetmounts://host/modulestart/my/local/file" - Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil + it "should not strip off leading terms that start with 'modules' but are longer words" do + @request.stubs(:key).returns "modulestart/mymod/local/file" + Puppet::Module.expects(:find).with { |key, env| key == 'modulestart'}.returns nil @module_files.find(@request) end it "should search for a module whose name is the first term in the remaining file path" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module @module_files.find(@request) end it "should search for a file relative to the module's files directory" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file") @module_files.find(@request) end it "should return nil if the module does not exist" do - Puppet::Module.expects(:find).with('my', "myenv").returns nil + Puppet::Module.expects(:find).returns nil @module_files.find(@request).should be_nil end it "should return nil if the module exists but the file does not" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) @module_files.find(@request).should be_nil end it "should return an instance of the model if a module is found and the file exists" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) @model.expects(:new).returns(:myinstance) @module_files.find(@request).should == :myinstance @@ -76,7 +74,7 @@ describe Puppet::Indirector::ModuleFiles do it "should use the node's environment to look up the module if the node name is provided" do node = stub "node", :environment => "testing" Puppet::Node.expects(:find).with("mynode").returns(node) - Puppet::Module.expects(:find).with('my', "testing") + Puppet::Module.expects(:find).with('mymod', "testing") @request.stubs(:node).returns "mynode" @module_files.find(@request) @@ -85,7 +83,7 @@ describe Puppet::Indirector::ModuleFiles do it "should use the default environment setting to look up the module if the node name is not provided" do env = stub "environment", :name => "testing" Puppet::Node::Environment.stubs(:new).returns(env) - Puppet::Module.expects(:find).with('my', "testing") + Puppet::Module.expects(:find).with('mymod', "testing") @module_files.find(@request) end end @@ -93,13 +91,13 @@ describe Puppet::Indirector::ModuleFiles do describe Puppet::Indirector::ModuleFiles, " when returning instances" do before do - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with('mymod', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) @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 } + @model.expects(:new).with { |key, *options| key == @request.key } @module_files.find(@request) end @@ -149,19 +147,19 @@ describe Puppet::Indirector::ModuleFiles do end it "should use the path directly from the URI if it already includes /modules" do - @request.expects(:key).returns "puppetmounts://host/modules/my/file" - @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" } + @request.expects(:key).returns "modules/my/file" + @configuration.expects(:authorized?).with { |uri, *args| uri == "modules/my/file" } @module_files.authorized?(@request) end - it "should add /modules to the file path if it's not included in the URI" do - @request.expects(:key).returns "puppetmounts://host/my/file" - @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" } + it "should add modules/ to the file path if it's not included in the URI" do + @request.expects(:key).returns "my/file" + @configuration.expects(:authorized?).with { |uri, *args| uri == "modules/my/file" } @module_files.authorized?(@request) end it "should pass the node name to the file server configuration" do - @request.expects(:key).returns "puppetmounts://host/my/file" + @request.expects(:key).returns "my/file" @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } @request.stubs(:node).returns "mynode" @module_files.authorized?(@request) @@ -186,41 +184,41 @@ describe Puppet::Indirector::ModuleFiles do describe Puppet::Indirector::ModuleFiles, " when searching for files" do - it "should strip off the leading '/modules' mount name" do + it "should strip off the leading 'modules/' mount name" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with { |key, env| key == 'mymod'}.returns @module @module_files.search(@request) end it "should not strip off leading terms that start with '/modules' but are longer words" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil - @request.stubs(:key).returns "puppetmounts://host/modulestart/my/local/file" + @request.stubs(:key).returns "modulestart/my/local/file" @module_files.search @request end it "should search for a module whose name is the first term in the remaining file path" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with('mymod', "myenv").returns @module @module_files.search(@request) end it "should search for a file relative to the module's files directory" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with('mymod', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file") @module_files.search(@request) end it "should return nil if the module does not exist" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns nil + Puppet::Module.expects(:find).with('mymod', "myenv").returns nil @module_files.search(@request).should be_nil end it "should return nil if the module exists but the file does not" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with('mymod', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) @module_files.search(@request).should be_nil end @@ -228,7 +226,7 @@ describe Puppet::Indirector::ModuleFiles do it "should use the node's environment to look up the module if the node name is provided" do node = stub "node", :environment => "testing" Puppet::Node.expects(:find).with("mynode").returns(node) - Puppet::Module.expects(:find).with('my', "testing") + Puppet::Module.expects(:find).with('mymod', "testing") @request.stubs(:node).returns "mynode" @module_files.search(@request) end @@ -236,13 +234,13 @@ describe Puppet::Indirector::ModuleFiles do it "should use the default environment setting to look up the module if the node name is not provided and the environment is not set to ''" do env = stub 'env', :name => "testing" Puppet::Node::Environment.stubs(:new).returns(env) - Puppet::Module.expects(:find).with('my', "testing") + Puppet::Module.expects(:find).with('mymod', "testing") @module_files.search(@request) end it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with('mymod', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) @module_files.expects(:path2instances).with(@request, "/module/path/files/local/file") @module_files.search(@request) @@ -250,7 +248,7 @@ describe Puppet::Indirector::ModuleFiles do it "should pass the request directly to :path2instances" do Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module + Puppet::Module.expects(:find).with('mymod', "myenv").returns @module FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) @module_files.expects(:path2instances).with(@request, "/module/path/files/local/file") @module_files.search(@request) |