summaryrefslogtreecommitdiffstats
path: root/spec/unit/file_serving/mount/file_spec.rb
blob: 70c804abda4978c0778b6978e5d21c5b06214885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env rspec
require '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", nil).should be_nil
  end

  it "should write a log message if the file is absent" do
    FileTest.stubs(:exist?).returns(false)

    Puppet.expects(:info).with("File does not exist or is not accessible: /mount/my/path")

    @mount.complete_path("/my/path", 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", nil).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, 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"

    @request = stub 'request', :node => "foo"
  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", @request).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"

    @request = stub 'request', :node => "foo"
  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", @request).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", @request).should be_nil
  end
end