diff options
author | Luke Kanies <luke@madstop.com> | 2009-02-18 16:33:47 -0600 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2009-02-19 17:50:28 -0600 |
commit | d3bc1e8279b6e1d372ab3624982788cde026461d (patch) | |
tree | 549d4365b52e7aea1cddf7365368d34cdb980b93 /spec/unit | |
parent | 00726bac02211be3c269c23a564bdcc8fdd28c2b (diff) | |
download | puppet-d3bc1e8279b6e1d372ab3624982788cde026461d.tar.gz puppet-d3bc1e8279b6e1d372ab3624982788cde026461d.tar.xz puppet-d3bc1e8279b6e1d372ab3624982788cde026461d.zip |
Adding pluginsyncing support to the Indirector
This switches away from the use of terminii for
each type of fileserving - it goes back to the traditional
fileserving method, and is much cleaner and simpler
as a result.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'spec/unit')
-rwxr-xr-x | spec/unit/file_serving/configuration.rb | 171 | ||||
-rwxr-xr-x | spec/unit/file_serving/configuration/parser.rb | 86 | ||||
-rwxr-xr-x | spec/unit/file_serving/indirection_hooks.rb | 63 | ||||
-rwxr-xr-x | spec/unit/file_serving/mount.rb | 141 | ||||
-rwxr-xr-x | spec/unit/file_serving/mount/file.rb | 184 | ||||
-rwxr-xr-x | spec/unit/file_serving/mount/modules.rb | 72 | ||||
-rwxr-xr-x | spec/unit/file_serving/mount/plugins.rb | 70 | ||||
-rwxr-xr-x | spec/unit/indirector/file_content/modules.rb | 18 | ||||
-rwxr-xr-x | spec/unit/indirector/file_metadata/modules.rb | 42 | ||||
-rwxr-xr-x | spec/unit/indirector/file_server.rb | 264 | ||||
-rwxr-xr-x | spec/unit/indirector/module_files.rb | 261 | ||||
-rwxr-xr-x | spec/unit/module.rb | 6 |
12 files changed, 666 insertions, 712 deletions
diff --git a/spec/unit/file_serving/configuration.rb b/spec/unit/file_serving/configuration.rb index 21fdfe3b4..c2d2e5014 100755 --- a/spec/unit/file_serving/configuration.rb +++ b/spec/unit/file_serving/configuration.rb @@ -28,6 +28,7 @@ describe Puppet::FileServing::Configuration do before :each do @path = "/path/to/configuration/file.conf" + Puppet.settings.stubs(:value).with(:trace).returns(false) Puppet.settings.stubs(:value).with(:fileserverconfig).returns(@path) end @@ -35,7 +36,7 @@ describe Puppet::FileServing::Configuration do Puppet::Util::Cacher.expire end - describe Puppet::FileServing::Configuration, "when initializing" do + describe "when initializing" do it "should work without a configuration file" do FileTest.stubs(:exists?).with(@path).returns(false) @@ -55,7 +56,7 @@ describe Puppet::FileServing::Configuration do end end - describe Puppet::FileServing::Configuration, "when parsing the configuration file" do + describe "when parsing the configuration file" do before do FileTest.stubs(:exists?).with(@path).returns(true) @@ -95,135 +96,113 @@ describe Puppet::FileServing::Configuration do end end - describe Puppet::FileServing::Configuration, "when finding files" do - - before do - @parser = mock 'parser' - @parser.stubs(:changed?).returns true - FileTest.stubs(:exists?).with(@path).returns(true) - Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) + describe "when finding the specified mount" do + it "should choose the named mount if one exists" do + config = Puppet::FileServing::Configuration.create + config.expects(:mounts).returns("one" => "foo") + config.find_mount("one", "mynode").should == "foo" + end - @mount1 = stub 'mount', :name => "one" - @mounts = {"one" => @mount1} + it "should modules mount's environment to find a matching module if the named module cannot be found" do + config = Puppet::FileServing::Configuration.create - Facter.stubs(:value).with("hostname").returns("whatever") + mod = mock 'module' + env = mock 'environment' + env.expects(:module).with("foo").returns mod + mount = mock 'mount' + mount.expects(:environment).with("mynode").returns env - @config = Puppet::FileServing::Configuration.create + config.stubs(:mounts).returns("modules" => mount) + config.find_mount("foo", "mynode").should equal(mount) end - it "should fail if the uri has a leading slash" do - @parser.expects(:parse).returns(@mounts) - proc { @config.file_path("/something") }.should raise_error(ArgumentError) - end + it "should return nil if there is no such named mount and no module with the same name exists" do + config = Puppet::FileServing::Configuration.create - it "should fail if the uri does not start with a valid mount name" do - @parser.expects(:parse).returns(@mounts) - proc { @config.file_path("some.thing") }.should raise_error(ArgumentError) - end + env = mock 'environment' + env.expects(:module).with("foo").returns nil + mount = mock 'mount' + mount.expects(:environment).with("mynode").returns env - it "should use the first term after the first slash for the mount name" do - @parser.expects(:parse).returns(@mounts) - FileTest.stubs(:exists?).returns(true) - @mount1.expects(:file) - @config.file_path("one") + config.stubs(:mounts).returns("modules" => mount) + config.find_mount("foo", "mynode").should be_nil end + end - it "should use the remainder of the URI after the mount name as the file name" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with("something/else", {}) - FileTest.stubs(:exists?).returns(true) - @config.file_path("one/something/else") - end + describe "when finding the mount name and relative path in a request key" do + before do + @config = Puppet::FileServing::Configuration.create + @config.stubs(:find_mount) - it "should treat a bare name as a mount and no relative file" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with(nil, {}) - FileTest.stubs(:exists?).returns(true) - @config.file_path("one") + @request = stub 'request', :key => "puppet:///foo/bar/baz", :options => {} end - it "should treat a name with a trailing slash equivalently to a name with no trailing slash" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with(nil, {}) - FileTest.stubs(:exists?).returns(true) - @config.file_path("one/") - end + it "should reread the configuration" do + @config.expects(:readconfig) - it "should return nil if the mount cannot be found" do - @parser.expects(:changed?).returns(true) - @parser.expects(:parse).returns({}) - @config.file_path("one/something").should be_nil + @config.split_path(@request) end - it "should return nil if the mount does not contain the file" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with("something/else", {}).returns(nil) - @config.file_path("one/something/else").should be_nil - end + it "should treat the first field of the URI path as the mount name" do + @config.expects(:find_mount).with { |name, node| name == "foo" } - it "should return the fully qualified path if the mount exists" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with("something/else", {}).returns("/full/path") - @config.file_path("one/something/else").should == "/full/path" + @config.split_path(@request) end - it "should reparse the configuration file when it has changed" do - @mount1.stubs(:file).returns("whatever") - @parser.expects(:changed?).returns(true) - @parser.expects(:parse).returns(@mounts) - FileTest.stubs(:exists?).returns(true) - @config.file_path("one/something") + it "should fail if the mount name is not alpha-numeric" do + @request.expects(:key).returns "puppet:///foo&bar/asdf" - @parser.expects(:changed?).returns(true) - @parser.expects(:parse).returns({}) - @config.file_path("one/something").should be_nil + lambda { @config.split_path(@request) }.should raise_error(ArgumentError) end - end - - describe Puppet::FileServing::Configuration, "when checking authorization" do - before do - @parser = mock 'parser' - @parser.stubs(:changed?).returns true - FileTest.stubs(:exists?).with(@path).returns(true) - Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) + it "should support dashes in the mount name" do + @request.expects(:key).returns "puppet:///foo-bar/asdf" - @mount1 = stub 'mount', :name => "one" - @mounts = {"one" => @mount1} - @parser.stubs(:parse).returns(@mounts) + lambda { @config.split_path(@request) }.should_not raise_error(ArgumentError) + end - Facter.stubs(:value).with("hostname").returns("whatever") + it "should use the mount name and node to find the mount" do + @config.expects(:find_mount).with { |name, node| name == "foo" and node == "mynode" } + @request.options[:node] = "mynode" - @config = Puppet::FileServing::Configuration.create + @config.split_path(@request) end - it "should return false if the mount cannot be found" do - @config.authorized?("nope/my/file").should be_false - end + it "should return nil if the mount cannot be found" do + @config.expects(:find_mount).returns nil - it "should use the mount to determine authorization" do - @mount1.expects(:allowed?) - @config.authorized?("one/my/file") + @config.split_path(@request).should be_nil end - it "should pass the client's name to the mount if provided" do - @mount1.expects(:allowed?).with("myhost", nil) - @config.authorized?("one/my/file", :node => "myhost") + it "should return the mount and the relative path if the mount is found" do + mount = stub 'mount', :name => "foo" + @config.expects(:find_mount).returns mount + + @config.split_path(@request).should == [mount, "bar/baz"] end - it "should pass the client's IP to the mount if provided" do - @mount1.expects(:allowed?).with("myhost", "myip") - @config.authorized?("one/my/file", :node => "myhost", :ipaddress => "myip") + it "should remove any double slashes" do + @request.stubs(:key).returns "puppet:///foo/bar//baz" + mount = stub 'mount', :name => "foo" + @config.expects(:find_mount).returns mount + + @config.split_path(@request).should == [mount, "bar/baz"] end - it "should return true if the mount allows the client" do - @mount1.expects(:allowed?).returns(true) - @config.authorized?("one/my/file").should be_true + it "should return the relative path as nil if it is an empty string" do + @request.expects(:key).returns "puppet:///foo" + mount = stub 'mount', :name => "foo" + @config.expects(:find_mount).returns mount + + @config.split_path(@request).should == [mount, nil] end - it "should return false if the mount denies the client" do - @mount1.expects(:allowed?).returns(false) - @config.authorized?("one/my/file").should be_false + it "should add 'modules/' to the relative path if the modules mount is used but not specified, for backward compatibility" do + @request.expects(:key).returns "puppet:///foo/bar" + mount = stub 'mount', :name => "modules" + @config.expects(:find_mount).returns mount + + @config.split_path(@request).should == [mount, "foo/bar"] end end end diff --git a/spec/unit/file_serving/configuration/parser.rb b/spec/unit/file_serving/configuration/parser.rb index 93d30ca1c..389e58389 100755 --- a/spec/unit/file_serving/configuration/parser.rb +++ b/spec/unit/file_serving/configuration/parser.rb @@ -47,23 +47,33 @@ describe Puppet::FileServing::Configuration::Parser do end it "should create a new mount for each section in the configuration" do - mount1 = mock 'one' - mount2 = mock 'two' - Puppet::FileServing::Mount.expects(:new).with("one").returns(mount1) - Puppet::FileServing::Mount.expects(:new).with("two").returns(mount2) + mount1 = mock 'one', :validate => true + mount2 = mock 'two', :validate => true + Puppet::FileServing::Mount::File.expects(:new).with("one").returns(mount1) + Puppet::FileServing::Mount::File.expects(:new).with("two").returns(mount2) mock_file_content "[one]\n[two]\n" @parser.parse end # This test is almost the exact same as the previous one. it "should return a hash of the created mounts" do - mount1 = mock 'one' - mount2 = mock 'two' - Puppet::FileServing::Mount.expects(:new).with("one").returns(mount1) - Puppet::FileServing::Mount.expects(:new).with("two").returns(mount2) + mount1 = mock 'one', :validate => true + mount2 = mock 'two', :validate => true + Puppet::FileServing::Mount::File.expects(:new).with("one").returns(mount1) + Puppet::FileServing::Mount::File.expects(:new).with("two").returns(mount2) mock_file_content "[one]\n[two]\n" - @parser.parse.should == {"one" => mount1, "two" => mount2} + result = @parser.parse + result["one"].should equal(mount1) + result["two"].should equal(mount2) + end + + it "should add plugins and modules mounts if they do not exist" do + mock_file_content "[one]\npath /foo" + + result = @parser.parse + result["plugins"].should_not be_nil + result["modules"].should_not be_nil end it "should only allow mount names that are alphanumeric plus dashes" do @@ -75,14 +85,34 @@ describe Puppet::FileServing::Configuration::Parser do mock_file_content "[one]\npath = /testing" proc { @parser.parse }.should raise_error(ArgumentError) end + + it "should validate each created mount" do + mount1 = mock 'one' + Puppet::FileServing::Mount::File.expects(:new).with("one").returns(mount1) + mock_file_content "[one]\n" + + mount1.expects(:validate) + + @parser.parse + end + + it "should fail if any mount does not pass validation" do + mount1 = mock 'one' + Puppet::FileServing::Mount::File.expects(:new).with("one").returns(mount1) + mock_file_content "[one]\n" + + mount1.expects(:validate).raises RuntimeError + + lambda { @parser.parse }.should raise_error(RuntimeError) + end end describe Puppet::FileServing::Configuration::Parser, " when parsing mount attributes" do include FSConfigurationParserTesting before do - @mount = stub 'mount', :name => "one" - Puppet::FileServing::Mount.expects(:new).with("one").returns(@mount) + @mount = stub 'testmount', :name => "one", :validate => true + Puppet::FileServing::Mount::File.expects(:new).with("one").returns(@mount) @parser.stubs(:add_modules_mount) end @@ -120,14 +150,42 @@ describe Puppet::FileServing::Configuration::Parser do include FSConfigurationParserTesting before do - @mount = stub 'mount', :name => "modules" - Puppet::FileServing::Mount.expects(:new).with("modules").returns(@mount) + @mount = stub 'modulesmount', :name => "modules", :validate => true + end + + it "should create an instance of the Modules Mount class" do + mock_file_content "[modules]\n" + + Puppet::FileServing::Mount::Modules.expects(:new).with("modules").returns @mount + @parser.parse end it "should warn if a path is set" do mock_file_content "[modules]\npath /some/path\n" + Puppet::FileServing::Mount::Modules.expects(:new).with("modules").returns(@mount) + + Puppet.expects(:warning) + @parser.parse + end + end + + describe Puppet::FileServing::Configuration::Parser, " when parsing the plugins mount" do + include FSConfigurationParserTesting + + before do + @mount = stub 'pluginsmount', :name => "plugins", :validate => true + end + + it "should create an instance of the Plugins Mount class" do + mock_file_content "[plugins]\n" + + Puppet::FileServing::Mount::Plugins.expects(:new).with("plugins").returns @mount + @parser.parse + end + + it "should warn if a path is set" do + mock_file_content "[plugins]\npath /some/path\n" - @modules.expects(:path=).never Puppet.expects(:warning) @parser.parse end diff --git a/spec/unit/file_serving/indirection_hooks.rb b/spec/unit/file_serving/indirection_hooks.rb index 83ff5848f..fee504bb6 100755 --- a/spec/unit/file_serving/indirection_hooks.rb +++ b/spec/unit/file_serving/indirection_hooks.rb @@ -50,75 +50,14 @@ describe Puppet::FileServing::IndirectionHooks do @object.select_terminus(@request).should == :rest end - it "should not choose :rest when the settings name is 'puppet' and no server is specified" do + it "should choose :file_server when the settings name is 'puppet' and no server is specified" do modules = mock 'modules' - @object.stubs(:terminus).with(:modules).returns(modules) - modules.stubs(:find_module).with("mymod", @request.options[:node]).returns nil - @request.expects(:protocol).returns "puppet" @request.expects(:server).returns nil Puppet.settings.expects(:value).with(:name).returns "puppet" - @object.select_terminus(@request).should_not == :rest - end - end - - describe "and the terminus is not :rest or :file" do - before do - @request.stubs(:protocol).returns nil - end - - it "should choose :modules if the mount name is 'modules'" do - @request.stubs(:key).returns "modules/mymod/file" - @object.select_terminus(@request).should == :modules - end - - it "should choose :modules and provide a deprecation notice if a module exists with the mount name" do - modules = mock 'modules' - - @object.expects(:terminus).with(:modules).returns(modules) - modules.expects(:find_module).with("mymod", @request.options[:node]).returns(:thing) - - Puppet.expects(:warning) - - @request.stubs(:key).returns "mymod/file" - @object.select_terminus(@request).should == :modules - end - - it "should choose :file_server if the mount name is not 'modules' nor matches a module name" do - modules = mock 'modules' - @object.stubs(:terminus).with(:modules).returns(modules) - modules.stubs(:find_module).returns(nil) - - @request.stubs(:key).returns "notmodules/file" - @object.select_terminus(@request).should == :file_server end end - - describe "when looking for a module whose name matches the mount name" do - before do - @modules = mock 'modules' - @object.stubs(:terminus).with(:modules).returns(@modules) - - @request.stubs(:key).returns "mymod/file" - end - - it "should use the modules terminus to look up the module" do - @modules.expects(:find_module).with("mymod", @request.options[:node]) - @object.select_terminus @request - end - - it "should pass the node name to the modules terminus" do - @modules.expects(:find_module).with("mymod", @request.options[:node]) - @object.select_terminus @request - end - - it "should log a deprecation warning if a module is found" do - @modules.expects(:find_module).with("mymod", @request.options[:node]).returns(:something) - Puppet.expects(:warning) - @object.select_terminus @request - end - end end end diff --git a/spec/unit/file_serving/mount.rb b/spec/unit/file_serving/mount.rb index 42f5accb9..e9e1f0860 100755 --- a/spec/unit/file_serving/mount.rb +++ b/spec/unit/file_serving/mount.rb @@ -3,141 +3,48 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/mount' -module FileServingMountTesting - def stub_facter(hostname) - Facter.stubs(:value).with("hostname").returns(hostname.sub(/\..+/, '')) - Facter.stubs(:value).with("domain").returns(hostname.sub(/^[^.]+\./, '')) - end -end - describe Puppet::FileServing::Mount do - it "should provide a method for clearing its cached host information" do - old = Puppet::FileServing::Mount.localmap - Puppet::Util::Cacher.expire - Puppet::FileServing::Mount.localmap.should_not equal(old) - end -end - -describe Puppet::FileServing::Mount, " when initializing" do - it "should fail on non-alphanumeric name" do - proc { Puppet::FileServing::Mount.new("non alpha") }.should raise_error(ArgumentError) - end - - it "should allow dashes in its name" do - Puppet::FileServing::Mount.new("non-alpha").name.should == "non-alpha" - end - - it "should allow an optional path" do - Puppet::FileServing::Mount.new("name", "/").path.should == "/" - end -end - -describe Puppet::FileServing::Mount, " when setting the path" do - before do - @mount = Puppet::FileServing::Mount.new("test") - @dir = "/this/path/does/not/exist" - end - - it "should fail if the path is not a directory" do - FileTest.expects(:directory?).returns(false) - proc { @mount.path = @dir }.should raise_error(ArgumentError) - end - - it "should fail if the path is not readable" do - FileTest.expects(:directory?).returns(true) - FileTest.expects(:readable?).returns(false) - proc { @mount.path = @dir }.should raise_error(ArgumentError) - end -end - -describe Puppet::FileServing::Mount, " when finding files" do - include FileServingMountTesting - before do - FileTest.stubs(:directory?).returns(true) - FileTest.stubs(:readable?).returns(true) - @mount = Puppet::FileServing::Mount.new("test") - @host = "host.domain.com" - end - - it "should fail if the mount path has not been set" do - proc { @mount.file_path("/blah") }.should raise_error(ArgumentError) + @mount = Puppet::FileServing::Mount.new("foo") end - it "should replace incidences of %h in the path with the client's short name" do - @mount.path = "/dir/%h/yay" - @mount.path(@host).should == "/dir/host/yay" - end - - it "should replace incidences of %H in the path with the client's fully qualified name" do - @mount.path = "/dir/%H/yay" - @mount.path(@host).should == "/dir/host.domain.com/yay" - end - - it "should replace incidences of %d in the path with the client's domain name" do - @mount.path = "/dir/%d/yay" - @mount.path(@host).should == "/dir/domain.com/yay" - end - - it "should perform all necessary replacements" do - @mount.path = "/%h/%d/%H" - @mount.path(@host).should == "/host/domain.com/host.domain.com" - end - - it "should perform replacements on the base path" do - @mount.path = "/blah/%h" - @mount.file_path("/my/stuff", @host).should == "/blah/host/my/stuff" - end + it "should be able to look up a node's environment" do + Puppet::Node.expects(:find).with("mynode").returns mock('node', :environment => "myenv") + Puppet::Node::Environment.expects(:new).with("myenv").returns "eh" - it "should not perform replacements on the per-file path" do - @mount.path = "/blah" - @mount.file_path("/%h/stuff", @host).should == "/blah/%h/stuff" + @mount.environment("mynode").should == "eh" end - it "should look for files relative to its base directory" do - @mount.path = "/blah" - @mount.file_path("/my/stuff", @host).should == "/blah/my/stuff" - end + it "should use the default environment if no node information is provided" do + Puppet::Node.expects(:find).with("mynode").returns nil + Puppet::Node::Environment.expects(:new).with(nil).returns "eh" - it "should use local host information if no client data is provided" do - stub_facter("myhost.mydomain.com") - @mount.path = "/%h/%d/%H" - @mount.path().should == "/myhost/mydomain.com/myhost.mydomain.com" + @mount.environment("mynode").should == "eh" end - after do - Puppet::Util::Cacher.expire + it "should use 'mount[$name]' as its string form" do + @mount.to_s.should == "mount[foo]" end end -describe Puppet::FileServing::Mount, " when providing file paths" do - include FileServingMountTesting - - before do - FileTest.stubs(:exists?).returns(true) - FileTest.stubs(:directory?).returns(true) - FileTest.stubs(:readable?).returns(true) - @mount = Puppet::FileServing::Mount.new("test", "/mount/%h") - stub_facter("myhost.mydomain.com") - @host = "host.domain.com" - end - - it "should return nil if the file is absent" do - FileTest.stubs(:exists?).returns(false) - @mount.file("/my/path").should be_nil +describe Puppet::FileServing::Mount, " when initializing" do + it "should fail on non-alphanumeric name" do + proc { Puppet::FileServing::Mount.new("non alpha") }.should raise_error(ArgumentError) end - it "should return the file path if the file is absent" do - FileTest.stubs(:exists?).with("/my/path").returns(true) - @mount.file("/my/path").should == "/mount/myhost/my/path" + it "should allow dashes in its name" do + Puppet::FileServing::Mount.new("non-alpha").name.should == "non-alpha" end +end - it "should treat a nil file name as the path to the mount itself" do - FileTest.stubs(:exists?).returns(true) - @mount.file(nil).should == "/mount/myhost" +describe Puppet::FileServing::Mount, " when finding files" do + it "should fail" do + lambda { Puppet::FileServing::Mount.new("test").find("foo", :one => "two") }.should raise_error(NotImplementedError) end +end - it "should use the client host name if provided in the options" do - @mount.file("/my/path", :node => @host).should == "/mount/host/my/path" +describe Puppet::FileServing::Mount, " when searching for files" do + it "should fail" do + lambda { Puppet::FileServing::Mount.new("test").search("foo", :one => "two") }.should raise_error(NotImplementedError) end end diff --git a/spec/unit/file_serving/mount/file.rb b/spec/unit/file_serving/mount/file.rb new file mode 100755 index 000000000..499a0357d --- /dev/null +++ b/spec/unit/file_serving/mount/file.rb @@ -0,0 +1,184 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' +require 'puppet/file_serving/mount/file' + +module FileServingMountTesting + def stub_facter(hostname) + Facter.stubs(:value).with("hostname").returns(hostname.sub(/\..+/, '')) + Facter.stubs(:value).with("domain").returns(hostname.sub(/^[^.]+\./, '')) + end +end + +describe Puppet::FileServing::Mount::File do + it "should provide a method for clearing its cached host information" do + old = Puppet::FileServing::Mount::File.localmap + Puppet::Util::Cacher.expire + Puppet::FileServing::Mount::File.localmap.should_not equal(old) + end + + it "should be invalid if it does not have a path" do + lambda { Puppet::FileServing::Mount::File.new("foo").validate }.should raise_error(ArgumentError) + end + + it "should be valid if it has a path" do + FileTest.stubs(:directory?).returns true + FileTest.stubs(:readable?).returns true + mount = Puppet::FileServing::Mount::File.new("foo") + mount.path = "/foo" + lambda { mount.validate }.should_not raise_error(ArgumentError) + end +end + +describe Puppet::FileServing::Mount::File, " when setting the path" do + before do + @mount = Puppet::FileServing::Mount::File.new("test") + @dir = "/this/path/does/not/exist" + end + + it "should fail if the path is not a directory" do + FileTest.expects(:directory?).returns(false) + proc { @mount.path = @dir }.should raise_error(ArgumentError) + end + + it "should fail if the path is not readable" do + FileTest.expects(:directory?).returns(true) + FileTest.expects(:readable?).returns(false) + proc { @mount.path = @dir }.should raise_error(ArgumentError) + end +end + +describe Puppet::FileServing::Mount::File, " when substituting hostnames and ip addresses into file paths" do + include FileServingMountTesting + + before do + FileTest.stubs(:directory?).returns(true) + FileTest.stubs(:readable?).returns(true) + @mount = Puppet::FileServing::Mount::File.new("test") + @host = "host.domain.com" + end + + it "should replace incidences of %h in the path with the client's short name" do + @mount.path = "/dir/%h/yay" + @mount.path(@host).should == "/dir/host/yay" + end + + it "should replace incidences of %H in the path with the client's fully qualified name" do + @mount.path = "/dir/%H/yay" + @mount.path(@host).should == "/dir/host.domain.com/yay" + end + + it "should replace incidences of %d in the path with the client's domain name" do + @mount.path = "/dir/%d/yay" + @mount.path(@host).should == "/dir/domain.com/yay" + end + + it "should perform all necessary replacements" do + @mount.path = "/%h/%d/%H" + @mount.path(@host).should == "/host/domain.com/host.domain.com" + end + + it "should use local host information if no client data is provided" do + stub_facter("myhost.mydomain.com") + @mount.path = "/%h/%d/%H" + @mount.path().should == "/myhost/mydomain.com/myhost.mydomain.com" + end + + after do + Puppet::Util::Cacher.expire + end +end + +describe Puppet::FileServing::Mount::File, "when determining the complete file path" do + include FileServingMountTesting + + before do + FileTest.stubs(:exist?).returns(true) + FileTest.stubs(:directory?).returns(true) + FileTest.stubs(:readable?).returns(true) + @mount = Puppet::FileServing::Mount::File.new("test") + @mount.path = "/mount" + stub_facter("myhost.mydomain.com") + @host = "host.domain.com" + end + + it "should return nil if the file is absent" do + FileTest.stubs(:exist?).returns(false) + @mount.complete_path("/my/path").should be_nil + end + + it "should return the file path if the file is present" do + FileTest.stubs(:exist?).with("/my/path").returns(true) + @mount.complete_path("/my/path").should == "/mount/my/path" + end + + it "should treat a nil file name as the path to the mount itself" do + FileTest.stubs(:exist?).returns(true) + @mount.complete_path(nil).should == "/mount" + end + + it "should use the client host name if provided in the options" do + @mount.path = "/mount/%h" + @mount.complete_path("/my/path", @host).should == "/mount/host/my/path" + end + + it "should perform replacements on the base path" do + @mount.path = "/blah/%h" + @mount.complete_path("/my/stuff", @host).should == "/blah/host/my/stuff" + end + + it "should not perform replacements on the per-file path" do + @mount.path = "/blah" + @mount.complete_path("/%h/stuff", @host).should == "/blah/%h/stuff" + end + + it "should look for files relative to its base directory" do + @mount.complete_path("/my/stuff", @host).should == "/mount/my/stuff" + end +end + +describe Puppet::FileServing::Mount::File, "when finding files" do + include FileServingMountTesting + + before do + FileTest.stubs(:exist?).returns(true) + FileTest.stubs(:directory?).returns(true) + FileTest.stubs(:readable?).returns(true) + @mount = Puppet::FileServing::Mount::File.new("test") + @mount.path = "/mount" + stub_facter("myhost.mydomain.com") + @host = "host.domain.com" + end + + it "should return the results of the complete file path" do + FileTest.stubs(:exist?).returns(false) + @mount.expects(:complete_path).with("/my/path", "foo").returns "eh" + @mount.find("/my/path", :node => "foo").should == "eh" + end +end + +describe Puppet::FileServing::Mount::File, "when searching for files" do + include FileServingMountTesting + + before do + FileTest.stubs(:exist?).returns(true) + FileTest.stubs(:directory?).returns(true) + FileTest.stubs(:readable?).returns(true) + @mount = Puppet::FileServing::Mount::File.new("test") + @mount.path = "/mount" + stub_facter("myhost.mydomain.com") + @host = "host.domain.com" + end + + it "should return the results of the complete file path as an array" do + FileTest.stubs(:exist?).returns(false) + @mount.expects(:complete_path).with("/my/path", "foo").returns "eh" + @mount.search("/my/path", :node => "foo").should == ["eh"] + end + + it "should return nil if the complete path is nil" do + FileTest.stubs(:exist?).returns(false) + @mount.expects(:complete_path).with("/my/path", "foo").returns nil + @mount.search("/my/path", :node => "foo").should be_nil + end +end diff --git a/spec/unit/file_serving/mount/modules.rb b/spec/unit/file_serving/mount/modules.rb new file mode 100755 index 000000000..6861e94a5 --- /dev/null +++ b/spec/unit/file_serving/mount/modules.rb @@ -0,0 +1,72 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' +require 'puppet/file_serving/mount/modules' + +describe Puppet::FileServing::Mount::Modules, "when finding files" do + before do + @mount = Puppet::FileServing::Mount::Modules.new("modules") + + @environment = stub 'environment', :module => nil + @mount.stubs(:environment).returns @environment + end + + it "should use the node's environment to find the module" do + env = mock 'env' + @mount.expects(:environment).with("mynode").returns env + env.expects(:module) + + @mount.find("foo", :node => "mynode") + end + + it "should treat the first field of the relative path as the module name" do + @environment.expects(:module).with("foo") + @mount.find("foo/bar/baz") + end + + it "should return nil if the specified module does not exist" do + @environment.expects(:module).with("foo").returns nil + @mount.find("foo/bar/baz") + end + + it "should return the file path from the module" do + mod = mock 'module' + mod.expects(:file).with("bar/baz").returns "eh" + @environment.expects(:module).with("foo").returns mod + @mount.find("foo/bar/baz").should == "eh" + end +end + +describe Puppet::FileServing::Mount::Modules, "when searching for files" do + before do + @mount = Puppet::FileServing::Mount::Modules.new("modules") + + @environment = stub 'environment', :module => nil + @mount.stubs(:environment).returns @environment + end + + it "should use the node's environment to search the module" do + env = mock 'env' + @mount.expects(:environment).with("mynode").returns env + env.expects(:module) + + @mount.search("foo", :node => "mynode") + end + + it "should treat the first field of the relative path as the module name" do + @environment.expects(:module).with("foo") + @mount.search("foo/bar/baz") + end + + it "should return nil if the specified module does not exist" do + @environment.expects(:module).with("foo").returns nil + @mount.search("foo/bar/baz") + end + + it "should return the file path as an array from the module" do + mod = mock 'module' + mod.expects(:file).with("bar/baz").returns "eh" + @environment.expects(:module).with("foo").returns mod + @mount.search("foo/bar/baz").should == ["eh"] + end +end diff --git a/spec/unit/file_serving/mount/plugins.rb b/spec/unit/file_serving/mount/plugins.rb new file mode 100755 index 000000000..c658b8cd6 --- /dev/null +++ b/spec/unit/file_serving/mount/plugins.rb @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' +require 'puppet/file_serving/mount/plugins' + +describe Puppet::FileServing::Mount::Plugins, "when finding files" do + before do + @mount = Puppet::FileServing::Mount::Plugins.new("modules") + + @environment = stub 'environment', :module => nil + @mount.stubs(:environment).returns @environment + end + + it "should use the node's environment to find the modules" do + env = mock 'env' + @mount.expects(:environment).with("mynode").returns env + env.expects(:modules).returns [] + + @mount.find("foo", :node => "mynode") + end + + it "should return nil if no module can be found with a matching plugin" do + mod = mock 'module' + mod.stubs(:plugin).with("foo/bar").returns nil + + @environment.expects(:modules).returns [mod] + @mount.find("foo/bar").should be_nil + end + + it "should return the file path from the module" do + mod = mock 'module' + mod.stubs(:plugin).with("foo/bar").returns "eh" + + @environment.expects(:modules).returns [mod] + @mount.find("foo/bar").should == "eh" + end +end + +describe Puppet::FileServing::Mount::Plugins, "when searching for files" do + before do + @mount = Puppet::FileServing::Mount::Plugins.new("modules") + + @environment = stub 'environment', :module => nil + @mount.stubs(:environment).returns @environment + end + + it "should use the node's environment to find the modules" do + env = mock 'env' + @mount.expects(:environment).with("mynode").returns env + env.expects(:modules).returns [] + + @mount.search("foo", :node => "mynode") + end + + it "should return nil if no modules can be found that have plugins" do + mod = mock 'module' + mod.stubs(:plugins?).returns false + + @environment.expects(:modules).returns [mod] + @mount.search("foo/bar").should be_nil + end + + it "should return the plugin paths for each module that has plugins" do + one = stub 'module', :plugins? => true, :plugins => "/one" + two = stub 'module', :plugins? => true, :plugins => "/two" + + @environment.expects(:modules).returns [one, two] + @mount.search("foo/bar").should == %w{/one /two} + end +end diff --git a/spec/unit/indirector/file_content/modules.rb b/spec/unit/indirector/file_content/modules.rb deleted file mode 100755 index 00f02bb0b..000000000 --- a/spec/unit/indirector/file_content/modules.rb +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env ruby -# -# Created by Luke Kanies on 2007-10-18. -# Copyright (c) 2007. All rights reserved. - -require File.dirname(__FILE__) + '/../../../spec_helper' - -require 'puppet/indirector/file_content/modules' - -describe Puppet::Indirector::FileContent::Modules do - it "should be registered with the file_content indirection" do - Puppet::Indirector::Terminus.terminus_class(:file_content, :modules).should equal(Puppet::Indirector::FileContent::Modules) - end - - it "should be a subclass of the ModuleFiles terminus" do - Puppet::Indirector::FileContent::Modules.superclass.should equal(Puppet::Indirector::ModuleFiles) - end -end diff --git a/spec/unit/indirector/file_metadata/modules.rb b/spec/unit/indirector/file_metadata/modules.rb deleted file mode 100755 index 7e5113df5..000000000 --- a/spec/unit/indirector/file_metadata/modules.rb +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env ruby -# -# Created by Luke Kanies on 2007-10-18. -# Copyright (c) 2007. All rights reserved. - -require File.dirname(__FILE__) + '/../../../spec_helper' - -require 'puppet/indirector/file_metadata/modules' - -describe Puppet::Indirector::FileMetadata::Modules do - it "should be registered with the file_metadata indirection" do - Puppet::Indirector::Terminus.terminus_class(:file_metadata, :modules).should equal(Puppet::Indirector::FileMetadata::Modules) - end - - it "should be a subclass of the ModuleFiles terminus" do - Puppet::Indirector::FileMetadata::Modules.superclass.should equal(Puppet::Indirector::ModuleFiles) - end -end - -describe Puppet::Indirector::FileMetadata::Modules, " when finding metadata" do - before do - @finder = Puppet::Indirector::FileMetadata::Modules.new - @finder.stubs(:environment).returns(nil) - @module = Puppet::Module.new("mymod", "/path/to") - @finder.stubs(:find_module).returns(@module) - - @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 - FileTest.expects(:exists?).with("/path/to/files/my/file").returns false - @finder.find(@request).should be_nil - end - - it "should retrieve the instance's attributes if the file is found" do - FileTest.expects(:exists?).with("/path/to/files/my/file").returns true - instance = mock 'metadta' - Puppet::FileServing::Metadata.expects(:new).returns instance - instance.expects :collect - @finder.find(@request) - end -end diff --git a/spec/unit/indirector/file_server.rb b/spec/unit/indirector/file_server.rb index e17deecf9..a80f5aed3 100755 --- a/spec/unit/indirector/file_server.rb +++ b/spec/unit/indirector/file_server.rb @@ -31,161 +31,221 @@ describe Puppet::Indirector::FileServer do @request = Puppet::Indirector::Request.new(:myind, :mymethod, @uri) end - describe Puppet::Indirector::FileServer, " when finding files" do + describe "when finding files" do + before do + @mount = stub 'mount', :find => nil + @instance = stub('instance', :links= => nil, :collect => nil) + end + + it "should use the configuration to find the mount and relative path" do + @configuration.expects(:split_path).with(@request) - 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.find(@request) 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.find(@request) + it "should return nil if it cannot find the mount" do + @configuration.expects(:split_path).with(@request).returns(nil, nil) + + @file_server.find(@request).should be_nil 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") - @request.node = "testing" + it "should use the mount to find the full path" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:find).with { |key, options| key == "rel/path" } + @file_server.find(@request) 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) + it "should return nil if it cannot find a full path" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:find).with { |key, options| key == "rel/path" }.returns nil + @file_server.find(@request).should be_nil end - 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") - instance = stub("instance", :collect => nil) - @model.expects(:new).returns instance - @file_server.find(@request).should equal(instance) + it "should create an instance with the found path" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:find).with { |key, options| key == "rel/path" }.returns "/my/file" + + @model.expects(:new).with("/my/file").returns @instance + + @file_server.find(@request).should equal(@instance) end - end - describe Puppet::Indirector::FileServer, " when returning instances" do - before :each do - @configuration.expects(:file_path).with("my/local/file", :node => nil).returns("/some/file") - @instance = stub 'instance', :collect => nil + it "should set 'links' on the instance if it is set in the request options" do + @request.options[:links] = true + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:find).with { |key, options| key == "rel/path" }.returns "/my/file" + + @model.expects(:new).with("/my/file").returns @instance + + @instance.expects(:links=).with(true) + + @file_server.find(@request).should equal(@instance) end - it "should create the instance with the path at which the instance was found" do - @model.expects(:new).with { |key, options| key == "/some/file" }.returns @instance - @file_server.find(@request) + it "should collect the instance" do + @request.options[:links] = true + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:find).with { |key, options| key == "rel/path" }.returns "/my/file" + + @model.expects(:new).with("/my/file").returns @instance + + @instance.expects(:collect) + + @file_server.find(@request).should equal(@instance) end + 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) - @request.options[:links] = :mytest - @file_server.find(@request) + describe "when searching for instances" do + before do + @mount = stub 'mount', :search => nil + @instance = stub('instance', :links= => nil, :collect => nil) end - it "should not set a :links value if no :links parameter is provided" do - @model.expects(:new).returns(@instance) - @file_server.find(@request) + it "should use the configuration to search the mount and relative path" do + @configuration.expects(:split_path).with(@request) + + @file_server.search(@request) end - it "should collect each instance's attributes before returning" do - @instance.expects(:collect) - @model.expects(:new).returns @instance - @file_server.find(@request) + it "should return nil if it cannot search the mount" do + @configuration.expects(:split_path).with(@request).returns(nil, nil) + + @file_server.search(@request).should be_nil end - end - describe Puppet::Indirector::FileServer, " when checking authorization" do + it "should use the mount to search for the full paths" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - it "should have an authorization hook" do - @file_server.should respond_to(:authorized?) + @mount.expects(:search).with { |key, options| key == "rel/path" } + + @file_server.search(@request) end - it "should deny the :destroy method" do - @request.method = :destroy - @file_server.authorized?(@request).should be_false + it "should return nil if searching does not find any full paths" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:search).with { |key, options| key == "rel/path" }.returns nil + + @file_server.search(@request).should be_nil end - it "should deny the :save method" do - @request.method = :save - @file_server.authorized?(@request).should be_false + it "should create a fileset with each returned path and merge them" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:search).with { |key, options| key == "rel/path" }.returns %w{/one /two} + + FileTest.stubs(:exist?).returns true + + one = mock 'fileset_one' + Puppet::FileServing::Fileset.expects(:new).with("/one", @request).returns(one) + two = mock 'fileset_two' + Puppet::FileServing::Fileset.expects(:new).with("/two", @request).returns(two) + + Puppet::FileServing::Fileset.expects(:merge).with(one, two).returns [] + + @file_server.search(@request) end - - describe "and finding file information" do - before do - @request.method = :find - end - it "should use the file server configuration to determine authorization" do - @configuration.expects(:authorized?) - @file_server.authorized?(@request) - end + it "should create an instance with each path resulting from the merger of the filesets" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) - it "should pass the file path from the URI to the file server configuration" do - @configuration.expects(:authorized?).with { |uri, *args| uri == "my/local/file" } - @file_server.authorized?(@request) - end + @mount.expects(:search).with { |key, options| key == "rel/path" }.returns [] - it "should pass the node name to the file server configuration" do - @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } - @request.node = "mynode" - @file_server.authorized?(@request) - end + FileTest.stubs(:exist?).returns true - it "should pass the IP address to the file server configuration" do - @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" } - @request.ip = "myip" - @file_server.authorized?(@request) - end + Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one", "two" => "/two") - it "should return false if the file server configuration denies authorization" do - @configuration.expects(:authorized?).returns(false) - @file_server.authorized?(@request) - end + one = stub 'one', :collect => nil + @model.expects(:new).with("/one", :relative_path => "one").returns one - it "should return true if the file server configuration approves authorization" do - @configuration.expects(:authorized?).returns(true) - @file_server.authorized?(@request) - end + two = stub 'two', :collect => nil + @model.expects(:new).with("/two", :relative_path => "two").returns two + + # order can't be guaranteed + result = @file_server.search(@request) + result.should be_include(one) + result.should be_include(two) + result.length.should == 2 end - end - describe Puppet::Indirector::FileServer, " when searching for files" do + it "should set 'links' on the instances if it is set in the request options" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:search).with { |key, options| key == "rel/path" }.returns [] + + FileTest.stubs(:exist?).returns true + + Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one") + + one = stub 'one', :collect => nil + @model.expects(:new).with("/one", :relative_path => "one").returns one + one.expects(:links=).with true + + @request.options[:links] = true - 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(@request) 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) + it "should collect the instances" do + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @mount.expects(:search).with { |key, options| key == "rel/path" }.returns [] + + FileTest.stubs(:exist?).returns true + + Puppet::FileServing::Fileset.expects(:merge).returns("one" => "/one") + + one = mock 'one' + @model.expects(:new).with("/one", :relative_path => "one").returns one + one.expects(:collect) + @file_server.search(@request) end + 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") - @request.node = "testing" - @file_server.search(@request) + describe "when checking authorization" do + before do + @request.method = :find 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(@request).should be_nil + it "should return false when destroying" do + @request.method = :destroy + @file_server.should_not be_authorized(@request) 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) - @file_server.search(@request) + it "should return false when saving" do + @request.method = :save + @file_server.should_not be_authorized(@request) end - it "should pass the request on to :path2instances" do - @configuration.expects(:file_path).with("my/local/file", :node => nil).returns("my/file") - @file_server.expects(:path2instances).with(@request, "my/file").returns [] - @file_server.search(@request) + it "should use the configuration to find the mount and relative path" do + @configuration.expects(:split_path).with(@request) + + @file_server.authorized?(@request) end - it "should return the result of :path2instances" do - @configuration.expects(:file_path).with("my/local/file", :node => nil).returns("my/file") - @file_server.expects(:path2instances).with(@request, "my/file").returns :footest - @file_server.search(@request).should == :footest + it "should return false if it cannot find the mount" do + @configuration.expects(:split_path).with(@request).returns(nil, nil) + + @file_server.should_not be_authorized(@request) + end + + it "should return the results of asking the mount whether the node and IP are authorized" do + @mount = stub 'mount' + @configuration.expects(:split_path).with(@request).returns([@mount, "rel/path"]) + + @request.options[:node] = "mynode" + @request.options[:ipaddress] = "myip" + @mount.expects(:allowed?).with("mynode", "myip").returns "something" + + @file_server.authorized?(@request).should == "something" end end end diff --git a/spec/unit/indirector/module_files.rb b/spec/unit/indirector/module_files.rb deleted file mode 100755 index 14ca8913c..000000000 --- a/spec/unit/indirector/module_files.rb +++ /dev/null @@ -1,261 +0,0 @@ -#!/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/module_files' - - -describe Puppet::Indirector::ModuleFiles do - - before :each do - @environment = stub('env', :name => 'myenv') - Puppet::Node::Environment.stubs(:new).returns(@environment) - - 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) - - @module_files_class = Class.new(Puppet::Indirector::ModuleFiles) do - def self.to_s - "Testing::Mytype" - end - end - - @module_files = @module_files_class.new - - @module = Puppet::Module.new("mymod", "/module/path") - - @request = Puppet::Indirector::Request.new(:mytype, :find, "puppet://host/modules/mymod/local/file") - end - - describe Puppet::Indirector::ModuleFiles, " when finding files" do - before do - @environment.stubs(:module).returns @module - end - - it "should strip off the leading 'modules/' mount name" do - @environment.expects(:module).with("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 "modulestart/mymod/local/file" - @environment.expects(:module).with("modulestart").returns @module - @module_files.find(@request) - end - - it "should search for a module whose name is the first term in the remaining file path" do - @module_files.find(@request) - end - - it "should search for a file relative to the module's files directory" do - 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 - @environment.expects(:module).returns nil - @module_files.find(@request).should be_nil - end - - it "should return nil if the module exists but the file does not" do - 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 - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @model.expects(:new).returns(:myinstance) - @module_files.find(@request).should == :myinstance - 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) - - newenv = stub 'newenv', :name => "newenv" - - Puppet::Node::Environment.expects(:new).with("testing").returns newenv - newenv.expects(:module).returns nil - - @request.stubs(:node).returns "mynode" - @module_files.find(@request) - end - - it "should use the default environment setting to look up the module if the node name is not provided" do - newenv = stub 'newenv', :name => "newenv" - - Puppet::Node::Environment.expects(:new).with(nil).returns newenv - newenv.expects(:module) - - @request.stubs(:node).returns nil - @module_files.find(@request) - end - end - - describe Puppet::Indirector::ModuleFiles, " when returning instances" do - - before do - @environment.expects(:module).with("mymod").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @instance = mock 'instance' - end - - it "should create the instance with the path at which the instance was found" do - @model.expects(:new).with { |key, options| key == "/module/path/files/local/file" } - @module_files.find(@request) - 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) - - @request.options[:links] = :mytest - @module_files.find(@request) - end - - it "should not set a :links value if no :links parameter is provided" do - @model.expects(:new).returns(@instance) - @module_files.find(@request) - end - end - - describe Puppet::Indirector::ModuleFiles, " when authorizing" do - - before do - @configuration = mock 'configuration' - Puppet::FileServing::Configuration.stubs(:create).returns(@configuration) - end - - it "should have an authorization hook" do - @module_files.should respond_to(:authorized?) - end - - it "should deny the :destroy method" do - @request.expects(:method).returns :destroy - @module_files.authorized?(@request).should be_false - end - - it "should deny the :save method" do - @request.expects(:method).returns :save - @module_files.authorized?(@request).should be_false - end - - it "should use the file server configuration to determine authorization" do - @configuration.expects(:authorized?) - @module_files.authorized?(@request) - end - - it "should use the path directly from the URI if it already includes /modules" do - @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 "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 "my/file" - @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } - @request.stubs(:node).returns "mynode" - @module_files.authorized?(@request) - end - - it "should pass the IP address to the file server configuration" do - @request.expects(:ip).returns "myip" - @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" } - @module_files.authorized?(@request) - end - - it "should return false if the file server configuration denies authorization" do - @configuration.expects(:authorized?).returns(false) - @module_files.authorized?(@request).should be_false - end - - it "should return true if the file server configuration approves authorization" do - @configuration.expects(:authorized?).returns(true) - @module_files.authorized?(@request).should be_true - end - end - - describe Puppet::Indirector::ModuleFiles, " when searching for files" do - - it "should strip off the leading 'modules/' mount name" do - @environment.expects(:module).with("mymod").returns @module - @module_files.search(@request) - end - - it "should not strip off leading terms that start with '/modules' but are longer words" do - @environment.expects(:module).with("modulestart").returns @module - @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 - @environment.expects(:module).with("mymod").returns @module - @module_files.search(@request) - end - - it "should search for a file relative to the module's files directory" do - @environment.expects(:module).with("mymod").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 - @environment.expects(:module).with("mymod").returns @module - @module_files.search(@request).should be_nil - end - - it "should return nil if the module exists but the file does not" do - @environment.expects(:module).with("mymod").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) - @module_files.search(@request).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) - - newenv = stub 'newenv', :name => "newenv" - - Puppet::Node::Environment.expects(:new).with("testing").returns newenv - newenv.expects(:module).returns nil - - @request.stubs(:node).returns "mynode" - @module_files.search(@request) - end - - 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 - newenv = stub 'newenv', :name => "newenv" - - Puppet::Node::Environment.expects(:new).with(nil).returns newenv - newenv.expects(:module) - - @request.stubs(:node).returns nil - @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 - @environment.expects(:module).with("mymod").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) - end - - it "should pass the request directly to :path2instances" do - @environment.expects(:module).with("mymod").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) - end - end -end diff --git a/spec/unit/module.rb b/spec/unit/module.rb index d7fb14c66..313de6761 100755 --- a/spec/unit/module.rb +++ b/spec/unit/module.rb @@ -32,6 +32,12 @@ describe Puppet::Module do FileTest.expects(:exist?).with(File.join("/foo/bar", filetype.to_s, "my/file")).returns false Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), "my/file").should be_nil end + + it "should return the base directory if asked for a nil path" do + path = File.join("/foo/bar", filetype.to_s) + FileTest.expects(:exist?).with(path).returns true + Puppet::Module.new("foo", "/foo/bar").send(filetype.to_s.sub(/s$/, ''), nil).should == path + end end end |