3
require File.dirname(__FILE__) + '/../../../spec_helper'
4
require 'puppet/file_serving/mount/file'
6
module FileServingMountTesting
7
def stub_facter(hostname)
8
Facter.stubs(:value).with("hostname").returns(hostname.sub(/\..+/, ''))
9
Facter.stubs(:value).with("domain").returns(hostname.sub(/^[^.]+\./, ''))
13
describe Puppet::FileServing::Mount::File do
14
it "should provide a method for clearing its cached host information" do
15
old = Puppet::FileServing::Mount::File.localmap
16
Puppet::Util::Cacher.expire
17
Puppet::FileServing::Mount::File.localmap.should_not equal(old)
20
it "should be invalid if it does not have a path" do
21
lambda { Puppet::FileServing::Mount::File.new("foo").validate }.should raise_error(ArgumentError)
24
it "should be valid if it has a path" do
25
FileTest.stubs(:directory?).returns true
26
FileTest.stubs(:readable?).returns true
27
mount = Puppet::FileServing::Mount::File.new("foo")
29
lambda { mount.validate }.should_not raise_error(ArgumentError)
33
describe Puppet::FileServing::Mount::File, " when setting the path" do
35
@mount = Puppet::FileServing::Mount::File.new("test")
36
@dir = "/this/path/does/not/exist"
39
it "should fail if the path is not a directory" do
40
FileTest.expects(:directory?).returns(false)
41
proc { @mount.path = @dir }.should raise_error(ArgumentError)
44
it "should fail if the path is not readable" do
45
FileTest.expects(:directory?).returns(true)
46
FileTest.expects(:readable?).returns(false)
47
proc { @mount.path = @dir }.should raise_error(ArgumentError)
51
describe Puppet::FileServing::Mount::File, " when substituting hostnames and ip addresses into file paths" do
52
include FileServingMountTesting
55
FileTest.stubs(:directory?).returns(true)
56
FileTest.stubs(:readable?).returns(true)
57
@mount = Puppet::FileServing::Mount::File.new("test")
58
@host = "host.domain.com"
61
it "should replace incidences of %h in the path with the client's short name" do
62
@mount.path = "/dir/%h/yay"
63
@mount.path(@host).should == "/dir/host/yay"
66
it "should replace incidences of %H in the path with the client's fully qualified name" do
67
@mount.path = "/dir/%H/yay"
68
@mount.path(@host).should == "/dir/host.domain.com/yay"
71
it "should replace incidences of %d in the path with the client's domain name" do
72
@mount.path = "/dir/%d/yay"
73
@mount.path(@host).should == "/dir/domain.com/yay"
76
it "should perform all necessary replacements" do
77
@mount.path = "/%h/%d/%H"
78
@mount.path(@host).should == "/host/domain.com/host.domain.com"
81
it "should use local host information if no client data is provided" do
82
stub_facter("myhost.mydomain.com")
83
@mount.path = "/%h/%d/%H"
84
@mount.path().should == "/myhost/mydomain.com/myhost.mydomain.com"
88
Puppet::Util::Cacher.expire
92
describe Puppet::FileServing::Mount::File, "when determining the complete file path" do
93
include FileServingMountTesting
96
FileTest.stubs(:exist?).returns(true)
97
FileTest.stubs(:directory?).returns(true)
98
FileTest.stubs(:readable?).returns(true)
99
@mount = Puppet::FileServing::Mount::File.new("test")
100
@mount.path = "/mount"
101
stub_facter("myhost.mydomain.com")
102
@host = "host.domain.com"
105
it "should return nil if the file is absent" do
106
FileTest.stubs(:exist?).returns(false)
107
@mount.complete_path("/my/path", nil).should be_nil
110
it "should return the file path if the file is present" do
111
FileTest.stubs(:exist?).with("/my/path").returns(true)
112
@mount.complete_path("/my/path", nil).should == "/mount/my/path"
115
it "should treat a nil file name as the path to the mount itself" do
116
FileTest.stubs(:exist?).returns(true)
117
@mount.complete_path(nil, nil).should == "/mount"
120
it "should use the client host name if provided in the options" do
121
@mount.path = "/mount/%h"
122
@mount.complete_path("/my/path", @host).should == "/mount/host/my/path"
125
it "should perform replacements on the base path" do
126
@mount.path = "/blah/%h"
127
@mount.complete_path("/my/stuff", @host).should == "/blah/host/my/stuff"
130
it "should not perform replacements on the per-file path" do
131
@mount.path = "/blah"
132
@mount.complete_path("/%h/stuff", @host).should == "/blah/%h/stuff"
135
it "should look for files relative to its base directory" do
136
@mount.complete_path("/my/stuff", @host).should == "/mount/my/stuff"
140
describe Puppet::FileServing::Mount::File, "when finding files" do
141
include FileServingMountTesting
144
FileTest.stubs(:exist?).returns(true)
145
FileTest.stubs(:directory?).returns(true)
146
FileTest.stubs(:readable?).returns(true)
147
@mount = Puppet::FileServing::Mount::File.new("test")
148
@mount.path = "/mount"
149
stub_facter("myhost.mydomain.com")
150
@host = "host.domain.com"
152
@request = stub 'request', :node => "foo"
155
it "should return the results of the complete file path" do
156
FileTest.stubs(:exist?).returns(false)
157
@mount.expects(:complete_path).with("/my/path", "foo").returns "eh"
158
@mount.find("/my/path", @request).should == "eh"
162
describe Puppet::FileServing::Mount::File, "when searching for files" do
163
include FileServingMountTesting
166
FileTest.stubs(:exist?).returns(true)
167
FileTest.stubs(:directory?).returns(true)
168
FileTest.stubs(:readable?).returns(true)
169
@mount = Puppet::FileServing::Mount::File.new("test")
170
@mount.path = "/mount"
171
stub_facter("myhost.mydomain.com")
172
@host = "host.domain.com"
174
@request = stub 'request', :node => "foo"
177
it "should return the results of the complete file path as an array" do
178
FileTest.stubs(:exist?).returns(false)
179
@mount.expects(:complete_path).with("/my/path", "foo").returns "eh"
180
@mount.search("/my/path", @request).should == ["eh"]
183
it "should return nil if the complete path is nil" do
184
FileTest.stubs(:exist?).returns(false)
185
@mount.expects(:complete_path).with("/my/path", "foo").returns nil
186
@mount.search("/my/path", @request).should be_nil