summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-10-22 19:28:22 -0500
committerLuke Kanies <luke@madstop.com>2007-10-22 19:28:22 -0500
commit688fcdf11a685dfda297beff50de8d4c751494d9 (patch)
treef2d737a2382de16a43e3d37f09dab2c76ad57d91
parent393a3e8743f503543ad34a874e9296d684b0d49b (diff)
downloadpuppet-688fcdf11a685dfda297beff50de8d4c751494d9.tar.gz
puppet-688fcdf11a685dfda297beff50de8d4c751494d9.tar.xz
puppet-688fcdf11a685dfda297beff50de8d4c751494d9.zip
Adding searchability to the fileserving termini, using the
new Fileset class. The tests aren't the cleanest, in that there is still a good bit of duplication in them, but it's what we got.
-rw-r--r--lib/puppet/file_serving/fileset.rb4
-rw-r--r--lib/puppet/file_serving/terminus_helper.rb15
-rw-r--r--lib/puppet/indirector/file_content/local.rb11
-rw-r--r--lib/puppet/indirector/file_metadata/local.rb8
-rw-r--r--lib/puppet/indirector/file_server.rb23
-rw-r--r--lib/puppet/indirector/module_files.rb43
-rwxr-xr-xspec/unit/file_serving/fileset.rb22
-rwxr-xr-xspec/unit/file_serving/terminus_helper.rb38
-rwxr-xr-xspec/unit/indirector/file_content/local.rb24
-rwxr-xr-xspec/unit/indirector/file_metadata/local.rb38
-rwxr-xr-xspec/unit/indirector/file_server.rb54
-rwxr-xr-xspec/unit/indirector/module_files.rb71
12 files changed, 297 insertions, 54 deletions
diff --git a/lib/puppet/file_serving/fileset.rb b/lib/puppet/file_serving/fileset.rb
index 7f7b9fc2d..fe54350b1 100644
--- a/lib/puppet/file_serving/fileset.rb
+++ b/lib/puppet/file_serving/fileset.rb
@@ -11,11 +11,11 @@ class Puppet::FileServing::Fileset
attr_reader :path, :ignore, :links
attr_accessor :recurse
- # Find our collection of files. This is different from the
+ # Return a list of all files in our fileset. This is different from the
# normal definition of find in that we support specific levels
# of recursion, which means we need to know when we're going another
# level deep, which Find doesn't do.
- def find
+ def files
files = perform_recursion
# Now strip off the leading path, so each file becomes relative, and remove
diff --git a/lib/puppet/file_serving/terminus_helper.rb b/lib/puppet/file_serving/terminus_helper.rb
new file mode 100644
index 000000000..9542cbf84
--- /dev/null
+++ b/lib/puppet/file_serving/terminus_helper.rb
@@ -0,0 +1,15 @@
+#
+# Created by Luke Kanies on 2007-10-22.
+# Copyright (c) 2007. All rights reserved.
+
+require 'puppet/file_serving'
+require 'puppet/file_serving/fileset'
+
+# Define some common methods for FileServing termini.
+module Puppet::FileServing::TerminusHelper
+ # Create model instances for all files in a fileset.
+ def path2instances(path, options = {})
+ args = [:links, :ignore, :recurse].inject({}) { |hash, param| hash[param] = options[param] if options[param]; hash }
+ Puppet::FileServing::Fileset.new(path, args).files.collect { |file| model.new(file) }
+ end
+end
diff --git a/lib/puppet/indirector/file_content/local.rb b/lib/puppet/indirector/file_content/local.rb
index e429c6c25..c2262c82d 100644
--- a/lib/puppet/indirector/file_content/local.rb
+++ b/lib/puppet/indirector/file_content/local.rb
@@ -3,6 +3,7 @@
# Copyright (c) 2007. All rights reserved.
require 'puppet/file_serving/content'
+require 'puppet/file_serving/terminus_helper'
require 'puppet/util/uri_helper'
require 'puppet/indirector/file_content'
require 'puppet/indirector/file'
@@ -11,13 +12,17 @@ class Puppet::Indirector::FileContent::Local < Puppet::Indirector::File
desc "Retrieve file contents from disk."
include Puppet::Util::URIHelper
+ include Puppet::FileServing::TerminusHelper
def find(key, options = {})
uri = key2uri(key)
-
return nil unless FileTest.exists?(uri.path)
- data = model.new(uri.path)
+ model.new(uri.path)
+ end
- return data
+ def search(key, options = {})
+ uri = key2uri(key)
+ return nil unless FileTest.exists?(uri.path)
+ path2instances(uri.path, options)
end
end
diff --git a/lib/puppet/indirector/file_metadata/local.rb b/lib/puppet/indirector/file_metadata/local.rb
index f40d4ce43..86e1172f9 100644
--- a/lib/puppet/indirector/file_metadata/local.rb
+++ b/lib/puppet/indirector/file_metadata/local.rb
@@ -3,6 +3,7 @@
# Copyright (c) 2007. All rights reserved.
require 'puppet/file_serving/metadata'
+require 'puppet/file_serving/terminus_helper'
require 'puppet/indirector/file_metadata'
require 'puppet/util/uri_helper'
require 'puppet/indirector/code'
@@ -11,6 +12,7 @@ class Puppet::Indirector::FileMetadata::Local < Puppet::Indirector::Code
desc "Retrieve file metadata directly from the local filesystem."
include Puppet::Util::URIHelper
+ include Puppet::FileServing::TerminusHelper
def find(key)
uri = key2uri(key)
@@ -21,4 +23,10 @@ class Puppet::Indirector::FileMetadata::Local < Puppet::Indirector::Code
return data
end
+
+ def search(key, options = {})
+ uri = key2uri(key)
+ return nil unless FileTest.exists?(uri.path)
+ path2instances(uri.path, options).each { |instance| instance.get_attributes }
+ end
end
diff --git a/lib/puppet/indirector/file_server.rb b/lib/puppet/indirector/file_server.rb
index 51e53d8c9..46b0c40f2 100644
--- a/lib/puppet/indirector/file_server.rb
+++ b/lib/puppet/indirector/file_server.rb
@@ -4,11 +4,14 @@
require 'puppet/util/uri_helper'
require 'puppet/file_serving/configuration'
+require 'puppet/file_serving/fileset'
+require 'puppet/file_serving/terminus_helper'
require 'puppet/indirector/terminus'
# Look files up using the file server.
class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus
include Puppet::Util::URIHelper
+ include Puppet::FileServing::TerminusHelper
# Is the client authorized to perform this action?
def authorized?(method, key, options = {})
@@ -21,11 +24,16 @@ class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus
# Find our key using the fileserver.
def find(key, options = {})
- uri = key2uri(key)
+ return nil unless path = find_path(key, options)
+ return model.new(path)
+ end
- return nil unless path = configuration.file_path(uri.path, :node => options[:node]) and FileTest.exists?(path)
+ # Search for files. This returns an array rather than a single
+ # file.
+ def search(key, options = {})
+ return nil unless path = find_path(key, options)
- return model.new(path)
+ path2instances(path, options)
end
private
@@ -34,4 +42,13 @@ class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus
def configuration
Puppet::FileServing::Configuration.create
end
+
+ # Find our path; used by :find and :search.
+ def find_path(key, options)
+ uri = key2uri(key)
+
+ return nil unless path = configuration.file_path(uri.path, :node => options[:node])
+
+ return path
+ end
end
diff --git a/lib/puppet/indirector/module_files.rb b/lib/puppet/indirector/module_files.rb
index 739d7b7b5..815da2efe 100644
--- a/lib/puppet/indirector/module_files.rb
+++ b/lib/puppet/indirector/module_files.rb
@@ -5,10 +5,13 @@
require 'puppet/util/uri_helper'
require 'puppet/indirector/terminus'
require 'puppet/file_serving/configuration'
+require 'puppet/file_serving/fileset'
+require 'puppet/file_serving/terminus_helper'
# Look files up in Puppet modules.
class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus
include Puppet::Util::URIHelper
+ include Puppet::FileServing::TerminusHelper
# Is the client allowed access to this key with this method?
def authorized?(method, key, options = {})
@@ -16,7 +19,8 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus
uri = key2uri(key)
- # Make sure our file path starts with /modules
+ # 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
configuration.authorized?(path, :node => options[:node], :ipaddress => options[:ipaddress])
@@ -24,18 +28,7 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus
# Find our key in a module.
def find(key, options = {})
- uri = key2uri(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)
-
- # And use the environment to look up the module.
- return nil unless mod = find_module(module_name, options[:node])
-
- path = File.join(mod.files, relative_path)
-
- return nil unless FileTest.exists?(path)
+ return nil unless path = find_path(key, options)
return model.new(path)
end
@@ -45,6 +38,12 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus
Puppet::Module::find(module_name, environment(node_name))
end
+ # Search for a list of files.
+ def search(key, options = {})
+ return nil unless path = find_path(key, options)
+ path2instances(path, options)
+ end
+
private
# Our fileserver configuration, if needed.
@@ -62,4 +61,22 @@ class Puppet::Indirector::ModuleFiles < Puppet::Indirector::Terminus
nil
end
end
+
+ # The abstracted method for turning a key into a path; used by both :find and :search.
+ def find_path(key, options)
+ uri = key2uri(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)
+
+ # And use the environment to look up the module.
+ return nil unless mod = find_module(module_name, options[:node])
+
+ path = File.join(mod.files, relative_path)
+
+ return nil unless FileTest.exists?(path)
+
+ return path
+ end
end
diff --git a/spec/unit/file_serving/fileset.rb b/spec/unit/file_serving/fileset.rb
index 518d7298d..2cd3e83dd 100755
--- a/spec/unit/file_serving/fileset.rb
+++ b/spec/unit/file_serving/fileset.rb
@@ -83,7 +83,7 @@ describe Puppet::FileServing::Fileset, " when determining whether to recurse" do
@fileset.recurse?(1).should be_true
end
- it "should not recurse if :recurse is set to an integer and the current depth is great than that integer" do
+ it "should not recurse if :recurse is set to an integer and the current depth is greater than that integer" do
@fileset.recurse = 1
@fileset.recurse?(2).should be_false
end
@@ -126,13 +126,13 @@ describe Puppet::FileServing::Fileset, " when recursing" do
it "should recurse through the whole file tree if :recurse is set to 'true'" do
mock_dir_structure(@path)
@fileset.stubs(:recurse?).returns(true)
- @fileset.find.sort.should == @files.sort
+ @fileset.files.sort.should == @files.sort
end
it "should not recurse if :recurse is set to 'false'" do
mock_dir_structure(@path)
@fileset.stubs(:recurse?).returns(false)
- @fileset.find.should == %w{.}
+ @fileset.files.should == %w{.}
end
# It seems like I should stub :recurse? here, or that I shouldn't stub the
@@ -140,41 +140,41 @@ describe Puppet::FileServing::Fileset, " when recursing" do
it "should recurse to the level set if :recurse is set to an integer" do
mock_dir_structure(@path)
@fileset.recurse = 1
- @fileset.find.should == %w{. one two .svn CVS}
+ @fileset.files.should == %w{. one two .svn CVS}
end
it "should ignore the '.' and '..' directories in subdirectories" do
mock_dir_structure(@path)
@fileset.recurse = true
- @fileset.find.sort.should == @files.sort
+ @fileset.files.sort.should == @files.sort
end
it "should ignore files that match a single pattern in the ignore list" do
mock_dir_structure(@path)
@fileset.recurse = true
@fileset.ignore = ".svn"
- @fileset.find.find { |file| file.include?(".svn") }.should be_nil
+ @fileset.files.find { |file| file.include?(".svn") }.should be_nil
end
it "should ignore files that match any of multiple patterns in the ignore list" do
mock_dir_structure(@path)
@fileset.recurse = true
@fileset.ignore = %w{.svn CVS}
- @fileset.find.find { |file| file.include?(".svn") or file.include?("CVS") }.should be_nil
+ @fileset.files.find { |file| file.include?(".svn") or file.include?("CVS") }.should be_nil
end
it "should use File.stat if :links is set to :follow" do
mock_dir_structure(@path, :stat)
@fileset.recurse = true
@fileset.links = :follow
- @fileset.find.sort.should == @files.sort
+ @fileset.files.sort.should == @files.sort
end
it "should use File.lstat if :links is set to :manage" do
mock_dir_structure(@path, :lstat)
@fileset.recurse = true
@fileset.links = :manage
- @fileset.find.sort.should == @files.sort
+ @fileset.files.sort.should == @files.sort
end
end
@@ -194,11 +194,11 @@ describe Puppet::FileServing::Fileset, " when following links that point to miss
end
it "should not fail" do
- proc { @fileset.find }.should_not raise_error
+ proc { @fileset.files }.should_not raise_error
end
it "should still manage the link" do
- @fileset.find.sort.should == %w{. mylink}.sort
+ @fileset.files.sort.should == %w{. mylink}.sort
end
end
diff --git a/spec/unit/file_serving/terminus_helper.rb b/spec/unit/file_serving/terminus_helper.rb
new file mode 100755
index 000000000..3a5274b5a
--- /dev/null
+++ b/spec/unit/file_serving/terminus_helper.rb
@@ -0,0 +1,38 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke Kanies on 2007-10-22.
+# Copyright (c) 2007. All rights reserved.
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/file_serving/terminus_helper'
+
+describe Puppet::FileServing::TerminusHelper do
+ before do
+ @helper = Object.new
+ @helper.extend(Puppet::FileServing::TerminusHelper)
+
+ @model = mock 'model'
+ @helper.stubs(:model).returns(@model)
+ end
+
+ 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")
+ 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)
+ 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]
+ end
+end
diff --git a/spec/unit/indirector/file_content/local.rb b/spec/unit/indirector/file_content/local.rb
index 361628767..ca7202ae1 100755
--- a/spec/unit/indirector/file_content/local.rb
+++ b/spec/unit/indirector/file_content/local.rb
@@ -35,3 +35,27 @@ describe Puppet::Indirector::FileContent::Local, "when finding a single file" do
@content.find(@uri).should be_nil
end
end
+
+describe Puppet::Indirector::FileContent::Local, "when searching for multiple files" do
+ before do
+ @content = Puppet::Indirector::FileContent::Local.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)
+ end
+end
diff --git a/spec/unit/indirector/file_metadata/local.rb b/spec/unit/indirector/file_metadata/local.rb
index 604cdf6af..f613d35c7 100755
--- a/spec/unit/indirector/file_metadata/local.rb
+++ b/spec/unit/indirector/file_metadata/local.rb
@@ -15,7 +15,7 @@ end
describe Puppet::Indirector::FileMetadata::Local, "when finding a single file" do
before do
- @content = Puppet::Indirector::FileMetadata::Local.new
+ @metadata = Puppet::Indirector::FileMetadata::Local.new
@uri = "file:///my/local"
@data = mock 'metadata'
@@ -25,7 +25,7 @@ describe Puppet::Indirector::FileMetadata::Local, "when finding a single file" d
FileTest.expects(:exists?).with("/my/local").returns true
Puppet::FileServing::Metadata.expects(:new).with("/my/local").returns(@data)
- @content.find(@uri).should == @data
+ @metadata.find(@uri).should == @data
end
it "should collect its attributes when a file is found" do
@@ -33,11 +33,41 @@ describe Puppet::Indirector::FileMetadata::Local, "when finding a single file" d
FileTest.expects(:exists?).with("/my/local").returns true
Puppet::FileServing::Metadata.expects(:new).with("/my/local").returns(@data)
- @content.find(@uri).should == @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
- @content.find(@uri).should be_nil
+ @metadata.find(@uri).should be_nil
+ end
+end
+
+describe Puppet::Indirector::FileMetadata::Local, "when searching for multiple files" do
+ before do
+ @metadata = Puppet::Indirector::FileMetadata::Local.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
+ @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", :get_attributes => nil), mock("two", :get_attributes => nil)] )
+ @metadata.search(@uri)
end
end
diff --git a/spec/unit/indirector/file_server.rb b/spec/unit/indirector/file_server.rb
index 7bd55c650..4bc56ce7d 100755
--- a/spec/unit/indirector/file_server.rb
+++ b/spec/unit/indirector/file_server.rb
@@ -26,9 +26,6 @@ module FileServerTerminusTesting
@uri = "puppetmounts://host/my/local/file"
@configuration = mock 'configuration'
Puppet::FileServing::Configuration.stubs(:create).returns(@configuration)
-
- @module_server = mock 'module_server'
- @indirection.stubs(:terminus).with(:modules).returns(@module_server)
end
end
@@ -37,39 +34,26 @@ describe Puppet::Indirector::FileServer, " when finding files" do
it "should use the path portion of the URI as the file name" do
@configuration.expects(:file_path).with("/my/local/file", :node => nil)
- @module_server.stubs(:find).returns(nil)
@file_server.find(@uri)
end
it "should use the FileServing configuration to convert the file name to a fully qualified path" do
@configuration.expects(:file_path).with("/my/local/file", :node => nil)
- @module_server.stubs(:find).returns(nil)
@file_server.find(@uri)
end
it "should pass the node name to the FileServing configuration if one is provided" do
@configuration.expects(:file_path).with("/my/local/file", :node => "testing")
- @module_server.stubs(:find)
@file_server.find(@uri, :node => "testing")
end
it "should return nil if no fully qualified path is found" do
@configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil)
- @module_server.stubs(:find).returns(nil)
@file_server.find(@uri).should be_nil
end
- it "should return nil if the configuration returns a file path that does not exist" 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")
- FileTest.expects(:exists?).with("/some/file").returns(false)
- @module_server.stubs(:find).returns(nil)
- @file_server.find(@uri).should be_nil
- end
-
- it "should return an instance of the model created with the full path if a file is found and it exists" do
- @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file")
- FileTest.expects(:exists?).with("/some/file").returns(true)
- @module_server.stubs(:find).returns(nil)
@model.expects(:new).with("/some/file").returns(:myinstance)
@file_server.find(@uri).should == :myinstance
end
@@ -127,3 +111,39 @@ describe Puppet::Indirector::FileServer, " when checking authorization" do
@file_server.authorized?(:find, "puppetmounts://host/my/file").should be_true
end
end
+
+describe Puppet::Indirector::FileServer, " when searching for files" do
+ include FileServerTerminusTesting
+
+ it "should use the path portion of the URI as the file name" do
+ @configuration.expects(:file_path).with("/my/local/file", :node => nil)
+ @file_server.search(@uri)
+ end
+
+ it "should use the FileServing configuration to convert the file name to a fully qualified path" do
+ @configuration.expects(:file_path).with("/my/local/file", :node => nil)
+ @file_server.search(@uri)
+ end
+
+ it "should pass the node name to the FileServing configuration if one is provided" do
+ @configuration.expects(:file_path).with("/my/local/file", :node => "testing")
+ @file_server.search(@uri, :node => "testing")
+ end
+
+ it "should return nil if no fully qualified path is found" do
+ @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil)
+ @file_server.search(@uri).should be_nil
+ end
+
+ 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.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.search(@uri, :testing => :one, :other => :two)
+ end
+end
diff --git a/spec/unit/indirector/module_files.rb b/spec/unit/indirector/module_files.rb
index a6c27b84b..e65f15697 100755
--- a/spec/unit/indirector/module_files.rb
+++ b/spec/unit/indirector/module_files.rb
@@ -82,7 +82,7 @@ describe Puppet::Indirector::ModuleFiles, " when finding files" do
@module_files.find(@uri)
end
- it "should not us an environment when looking up the module if the node name is not provided and the environment is set to ''" do
+ it "should not use an environment when looking up the module if the node name is not provided and the environment is set to ''" do
Puppet.settings.stubs(:value).with(:environment).returns("")
Puppet::Module.expects(:find).with('my', nil)
@module_files.find(@uri)
@@ -150,3 +150,72 @@ describe Puppet::Indirector::ModuleFiles, " when authorizing" do
@module_files.authorized?(:find, "puppetmounts://host/my/file").should be_true
end
end
+
+describe Puppet::Indirector::ModuleFiles, " when searching for files" do
+ include ModuleFilesTerminusTesting
+
+ it "should strip off the leading '/modules' mount name" do
+ Puppet::Module.expects(:find).with('my', nil).returns @module
+ @module_files.search(@uri)
+ end
+
+ it "should not strip off leading terms that start with '/modules' but are longer words" do
+ Puppet::Module.expects(:find).with('modulestart', nil).returns nil
+ @module_files.search("puppetmounts://host/modulestart/my/local/file")
+ 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', nil).returns @module
+ @module_files.search(@uri)
+ end
+
+ it "should search for a file relative to the module's files directory" do
+ Puppet::Module.expects(:find).with('my', nil).returns @module
+ FileTest.expects(:exists?).with("/module/path/files/local/file")
+ @module_files.search(@uri)
+ end
+
+ it "should return nil if the module does not exist" do
+ Puppet::Module.expects(:find).with('my', nil).returns nil
+ @module_files.search(@uri).should be_nil
+ end
+
+ it "should return nil if the module exists but the file does not" do
+ Puppet::Module.expects(:find).with('my', nil).returns @module
+ FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false)
+ @module_files.search(@uri).should be_nil
+ end
+
+ 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")
+ @module_files.search(@uri, :node => "mynode")
+ end
+
+ it "should use the local environment setting to look up the module if the node name is not provided and the environment is not set to ''" do
+ Puppet.settings.stubs(:value).with(:environment).returns("testing")
+ Puppet::Module.expects(:find).with('my', "testing")
+ @module_files.search(@uri)
+ end
+
+ it "should not use an environment when looking up the module if the node name is not provided and the environment is set to ''" do
+ Puppet.settings.stubs(:value).with(:environment).returns("")
+ Puppet::Module.expects(:find).with('my', nil)
+ @module_files.search(@uri)
+ end
+
+ 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.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.search(@uri, :testing => :one, :other => :two)
+ end
+end