~ubuntu-branches/ubuntu/trusty/puppet/trusty

« back to all changes in this revision

Viewing changes to spec/unit/file_serving/mount/file_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2011-10-22 14:08:22 UTC
  • mfrom: (1.1.25) (3.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20111022140822-odxde5lohc45yhuz
Tags: 2.7.6-1
* New upstream release (CVE-2011-3872)
* Remove cherry-picked "groupadd_aix_warning" patch
* Install all new manpages

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
end
11
11
 
12
12
describe Puppet::FileServing::Mount::File do
13
 
  it "should provide a method for clearing its cached host information" do
14
 
    old = Puppet::FileServing::Mount::File.localmap
15
 
    Puppet::Util::Cacher.expire
16
 
    Puppet::FileServing::Mount::File.localmap.should_not equal(old)
17
 
  end
18
 
 
19
13
  it "should be invalid if it does not have a path" do
20
14
    lambda { Puppet::FileServing::Mount::File.new("foo").validate }.should raise_error(ArgumentError)
21
15
  end
27
21
    mount.path = "/foo"
28
22
    lambda { mount.validate }.should_not raise_error(ArgumentError)
29
23
  end
30
 
end
31
 
 
32
 
describe Puppet::FileServing::Mount::File, " when setting the path" do
33
 
  before do
34
 
    @mount = Puppet::FileServing::Mount::File.new("test")
35
 
    @dir = "/this/path/does/not/exist"
36
 
  end
37
 
 
38
 
  it "should fail if the path is not a directory" do
39
 
    FileTest.expects(:directory?).returns(false)
40
 
    proc { @mount.path = @dir }.should raise_error(ArgumentError)
41
 
  end
42
 
 
43
 
  it "should fail if the path is not readable" do
44
 
    FileTest.expects(:directory?).returns(true)
45
 
    FileTest.expects(:readable?).returns(false)
46
 
    proc { @mount.path = @dir }.should raise_error(ArgumentError)
47
 
  end
48
 
end
49
 
 
50
 
describe Puppet::FileServing::Mount::File, " when substituting hostnames and ip addresses into file paths" do
51
 
  include FileServingMountTesting
52
 
 
53
 
  before do
54
 
    FileTest.stubs(:directory?).returns(true)
55
 
    FileTest.stubs(:readable?).returns(true)
56
 
    @mount = Puppet::FileServing::Mount::File.new("test")
57
 
    @host = "host.domain.com"
58
 
  end
59
 
 
60
 
  it "should replace incidences of %h in the path with the client's short name" do
61
 
    @mount.path = "/dir/%h/yay"
62
 
    @mount.path(@host).should == "/dir/host/yay"
63
 
  end
64
 
 
65
 
  it "should replace incidences of %H in the path with the client's fully qualified name" do
66
 
    @mount.path = "/dir/%H/yay"
67
 
    @mount.path(@host).should == "/dir/host.domain.com/yay"
68
 
  end
69
 
 
70
 
  it "should replace incidences of %d in the path with the client's domain name" do
71
 
    @mount.path = "/dir/%d/yay"
72
 
    @mount.path(@host).should == "/dir/domain.com/yay"
73
 
  end
74
 
 
75
 
  it "should perform all necessary replacements" do
76
 
    @mount.path = "/%h/%d/%H"
77
 
    @mount.path(@host).should == "/host/domain.com/host.domain.com"
78
 
  end
79
 
 
80
 
  it "should use local host information if no client data is provided" do
81
 
    stub_facter("myhost.mydomain.com")
82
 
    @mount.path = "/%h/%d/%H"
83
 
    @mount.path.should == "/myhost/mydomain.com/myhost.mydomain.com"
84
 
  end
85
 
 
86
 
  after do
87
 
    Puppet::Util::Cacher.expire
88
 
  end
89
 
end
90
 
 
91
 
describe Puppet::FileServing::Mount::File, "when determining the complete file path" do
92
 
  include FileServingMountTesting
93
 
 
94
 
  before do
95
 
    FileTest.stubs(:exist?).returns(true)
96
 
    FileTest.stubs(:directory?).returns(true)
97
 
    FileTest.stubs(:readable?).returns(true)
98
 
    @mount = Puppet::FileServing::Mount::File.new("test")
99
 
    @mount.path = "/mount"
100
 
    stub_facter("myhost.mydomain.com")
101
 
    @host = "host.domain.com"
102
 
  end
103
 
 
104
 
  it "should return nil if the file is absent" do
105
 
    FileTest.stubs(:exist?).returns(false)
106
 
    @mount.complete_path("/my/path", nil).should be_nil
107
 
  end
108
 
 
109
 
  it "should write a log message if the file is absent" do
110
 
    FileTest.stubs(:exist?).returns(false)
111
 
 
112
 
    Puppet.expects(:info).with("File does not exist or is not accessible: /mount/my/path")
113
 
 
114
 
    @mount.complete_path("/my/path", nil)
115
 
  end
116
 
 
117
 
  it "should return the file path if the file is present" do
118
 
    FileTest.stubs(:exist?).with("/my/path").returns(true)
119
 
    @mount.complete_path("/my/path", nil).should == "/mount/my/path"
120
 
  end
121
 
 
122
 
  it "should treat a nil file name as the path to the mount itself" do
123
 
    FileTest.stubs(:exist?).returns(true)
124
 
    @mount.complete_path(nil, nil).should == "/mount"
125
 
  end
126
 
 
127
 
  it "should use the client host name if provided in the options" do
128
 
    @mount.path = "/mount/%h"
129
 
    @mount.complete_path("/my/path", @host).should == "/mount/host/my/path"
130
 
  end
131
 
 
132
 
  it "should perform replacements on the base path" do
133
 
    @mount.path = "/blah/%h"
134
 
    @mount.complete_path("/my/stuff", @host).should == "/blah/host/my/stuff"
135
 
  end
136
 
 
137
 
  it "should not perform replacements on the per-file path" do
138
 
    @mount.path = "/blah"
139
 
    @mount.complete_path("/%h/stuff", @host).should == "/blah/%h/stuff"
140
 
  end
141
 
 
142
 
  it "should look for files relative to its base directory" do
143
 
    @mount.complete_path("/my/stuff", @host).should == "/mount/my/stuff"
144
 
  end
145
 
end
146
 
 
147
 
describe Puppet::FileServing::Mount::File, "when finding files" do
148
 
  include FileServingMountTesting
149
 
 
150
 
  before do
151
 
    FileTest.stubs(:exist?).returns(true)
152
 
    FileTest.stubs(:directory?).returns(true)
153
 
    FileTest.stubs(:readable?).returns(true)
154
 
    @mount = Puppet::FileServing::Mount::File.new("test")
155
 
    @mount.path = "/mount"
156
 
    stub_facter("myhost.mydomain.com")
157
 
    @host = "host.domain.com"
158
 
 
159
 
    @request = stub 'request', :node => "foo"
160
 
  end
161
 
 
162
 
  it "should return the results of the complete file path" do
163
 
    FileTest.stubs(:exist?).returns(false)
164
 
    @mount.expects(:complete_path).with("/my/path", "foo").returns "eh"
165
 
    @mount.find("/my/path", @request).should == "eh"
166
 
  end
167
 
end
168
 
 
169
 
describe Puppet::FileServing::Mount::File, "when searching for files" do
170
 
  include FileServingMountTesting
171
 
 
172
 
  before do
173
 
    FileTest.stubs(:exist?).returns(true)
174
 
    FileTest.stubs(:directory?).returns(true)
175
 
    FileTest.stubs(:readable?).returns(true)
176
 
    @mount = Puppet::FileServing::Mount::File.new("test")
177
 
    @mount.path = "/mount"
178
 
    stub_facter("myhost.mydomain.com")
179
 
    @host = "host.domain.com"
180
 
 
181
 
    @request = stub 'request', :node => "foo"
182
 
  end
183
 
 
184
 
  it "should return the results of the complete file path as an array" do
185
 
    FileTest.stubs(:exist?).returns(false)
186
 
    @mount.expects(:complete_path).with("/my/path", "foo").returns "eh"
187
 
    @mount.search("/my/path", @request).should == ["eh"]
188
 
  end
189
 
 
190
 
  it "should return nil if the complete path is nil" do
191
 
    FileTest.stubs(:exist?).returns(false)
192
 
    @mount.expects(:complete_path).with("/my/path", "foo").returns nil
193
 
    @mount.search("/my/path", @request).should be_nil
 
24
 
 
25
  describe "when setting the path" do
 
26
    before do
 
27
      @mount = Puppet::FileServing::Mount::File.new("test")
 
28
      @dir = "/this/path/does/not/exist"
 
29
    end
 
30
 
 
31
    it "should fail if the path is not a directory" do
 
32
      FileTest.expects(:directory?).returns(false)
 
33
      proc { @mount.path = @dir }.should raise_error(ArgumentError)
 
34
    end
 
35
 
 
36
    it "should fail if the path is not readable" do
 
37
      FileTest.expects(:directory?).returns(true)
 
38
      FileTest.expects(:readable?).returns(false)
 
39
      proc { @mount.path = @dir }.should raise_error(ArgumentError)
 
40
    end
 
41
  end
 
42
 
 
43
  describe "when substituting hostnames and ip addresses into file paths" do
 
44
    include FileServingMountTesting
 
45
 
 
46
    before do
 
47
      FileTest.stubs(:directory?).returns(true)
 
48
      FileTest.stubs(:readable?).returns(true)
 
49
      @mount = Puppet::FileServing::Mount::File.new("test")
 
50
      @host = "host.domain.com"
 
51
    end
 
52
 
 
53
    after :each do
 
54
      Puppet::FileServing::Mount::File.instance_variable_set(:@localmap, nil)
 
55
    end
 
56
 
 
57
    it "should replace incidences of %h in the path with the client's short name" do
 
58
      @mount.path = "/dir/%h/yay"
 
59
      @mount.path(@host).should == "/dir/host/yay"
 
60
    end
 
61
 
 
62
    it "should replace incidences of %H in the path with the client's fully qualified name" do
 
63
      @mount.path = "/dir/%H/yay"
 
64
      @mount.path(@host).should == "/dir/host.domain.com/yay"
 
65
    end
 
66
 
 
67
    it "should replace incidences of %d in the path with the client's domain name" do
 
68
      @mount.path = "/dir/%d/yay"
 
69
      @mount.path(@host).should == "/dir/domain.com/yay"
 
70
    end
 
71
 
 
72
    it "should perform all necessary replacements" do
 
73
      @mount.path = "/%h/%d/%H"
 
74
      @mount.path(@host).should == "/host/domain.com/host.domain.com"
 
75
    end
 
76
 
 
77
    it "should use local host information if no client data is provided" do
 
78
      stub_facter("myhost.mydomain.com")
 
79
      @mount.path = "/%h/%d/%H"
 
80
      @mount.path.should == "/myhost/mydomain.com/myhost.mydomain.com"
 
81
    end
 
82
  end
 
83
 
 
84
  describe "when determining the complete file path" do
 
85
    include FileServingMountTesting
 
86
 
 
87
    before do
 
88
      FileTest.stubs(:exist?).returns(true)
 
89
      FileTest.stubs(:directory?).returns(true)
 
90
      FileTest.stubs(:readable?).returns(true)
 
91
      @mount = Puppet::FileServing::Mount::File.new("test")
 
92
      @mount.path = "/mount"
 
93
      stub_facter("myhost.mydomain.com")
 
94
      @host = "host.domain.com"
 
95
    end
 
96
 
 
97
    it "should return nil if the file is absent" do
 
98
      FileTest.stubs(:exist?).returns(false)
 
99
      @mount.complete_path("/my/path", nil).should be_nil
 
100
    end
 
101
 
 
102
    it "should write a log message if the file is absent" do
 
103
      FileTest.stubs(:exist?).returns(false)
 
104
 
 
105
      Puppet.expects(:info).with("File does not exist or is not accessible: /mount/my/path")
 
106
 
 
107
      @mount.complete_path("/my/path", nil)
 
108
    end
 
109
 
 
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"
 
113
    end
 
114
 
 
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"
 
118
    end
 
119
 
 
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"
 
123
    end
 
124
 
 
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"
 
128
    end
 
129
 
 
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"
 
133
    end
 
134
 
 
135
    it "should look for files relative to its base directory" do
 
136
      @mount.complete_path("/my/stuff", @host).should == "/mount/my/stuff"
 
137
    end
 
138
  end
 
139
 
 
140
  describe "when finding files" do
 
141
    include FileServingMountTesting
 
142
 
 
143
    before do
 
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"
 
151
 
 
152
      @request = stub 'request', :node => "foo"
 
153
    end
 
154
 
 
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"
 
159
    end
 
160
  end
 
161
 
 
162
  describe "when searching for files" do
 
163
    include FileServingMountTesting
 
164
 
 
165
    before do
 
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"
 
173
 
 
174
      @request = stub 'request', :node => "foo"
 
175
    end
 
176
 
 
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"]
 
181
    end
 
182
 
 
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
 
187
    end
194
188
  end
195
189
end