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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
#
# Author:: Joshua Timberman (<joshua@opscode.com>)
# Copyright:: Copyright (c) 2008 OpsCode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'spec_helper'
require 'ostruct'
describe Chef::Provider::Mount::Mount do
before(:each) do
@node = Chef::Node.new
@events = Chef::EventDispatch::Dispatcher.new
@run_context = Chef::RunContext.new(@node, {}, @events)
@new_resource = Chef::Resource::Mount.new("/tmp/foo")
@new_resource.device "/dev/sdz1"
@new_resource.device_type :device
@new_resource.fstype "ext3"
@new_resource.supports :remount => false
@provider = Chef::Provider::Mount::Mount.new(@new_resource, @run_context)
::File.stub(:exists?).with("/dev/sdz1").and_return true
::File.stub(:exists?).with("/tmp/foo").and_return true
::File.stub(:realpath).with("/dev/sdz1").and_return "/dev/sdz1"
::File.stub(:realpath).with("/tmp/foo").and_return "/tmp/foo"
end
describe "when discovering the current fs state" do
before do
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => ''))
::File.stub(:foreach).with("/etc/fstab")
end
it "should create a current resource with the same mount point and device" do
@provider.load_current_resource
@provider.current_resource.name.should == '/tmp/foo'
@provider.current_resource.mount_point.should == '/tmp/foo'
@provider.current_resource.device.should == '/dev/sdz1'
end
it "should accecpt device_type :uuid", :not_supported_on_solaris do
@new_resource.device_type :uuid
@new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
@stdout_findfs = double("STDOUT", :first => "/dev/sdz1")
@provider.should_receive(:popen4).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_yield(@pid,@stdin,@stdout_findfs,@stderr).and_return(@status)
@provider.load_current_resource()
@provider.mountable?
end
describe "when dealing with network mounts" do
{ "nfs" => "nfsserver:/vol/path",
"cifs" => "//cifsserver/share" }.each do |type, fs_spec|
it "should detect network fs_spec (#{type})" do
@new_resource.device fs_spec
@provider.network_device?.should be_true
end
it "should ignore trailing slash and set mounted to true for network mount (#{type})" do
@new_resource.device fs_spec
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => "#{fs_spec}/ on /tmp/foo type #{type} (rw)\n"))
@provider.load_current_resource
@provider.current_resource.mounted.should be_true
end
end
end
it "should raise an error if the mount device does not exist" do
::File.stub(:exists?).with("/dev/sdz1").and_return false
lambda { @provider.load_current_resource();@provider.mountable? }.should raise_error(Chef::Exceptions::Mount)
end
it "should not call mountable? with load_current_resource - CHEF-1565" do
::File.stub(:exists?).with("/dev/sdz1").and_return false
@provider.should_receive(:mounted?).and_return(true)
@provider.should_receive(:enabled?).and_return(true)
@provider.should_not_receive(:mountable?)
@provider.load_current_resource
end
it "should raise an error if the mount device (uuid) does not exist", :not_supported_on_solaris do
@new_resource.device_type :uuid
@new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
status_findfs = double("Status", :exitstatus => 1)
stdout_findfs = double("STDOUT", :first => nil)
@provider.should_receive(:popen4).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_yield(@pid,@stdin,stdout_findfs,@stderr).and_return(status_findfs)
::File.should_receive(:exists?).with("").and_return(false)
lambda { @provider.load_current_resource();@provider.mountable? }.should raise_error(Chef::Exceptions::Mount)
end
it "should raise an error if the mount point does not exist" do
::File.stub(:exists?).with("/tmp/foo").and_return false
lambda { @provider.load_current_resource();@provider.mountable? }.should raise_error(Chef::Exceptions::Mount)
end
it "does not expect the device to exist for tmpfs" do
@new_resource.fstype("tmpfs")
@new_resource.device("whatever")
lambda { @provider.load_current_resource();@provider.mountable? }.should_not raise_error
end
it "does not expect the device to exist for Fuse filesystems" do
@new_resource.fstype("fuse")
@new_resource.device("nilfs#xxx")
lambda { @provider.load_current_resource();@provider.mountable? }.should_not raise_error
end
it "does not expect the device to exist if it's none" do
@new_resource.device("none")
lambda { @provider.load_current_resource();@provider.mountable? }.should_not raise_error
end
it "should set mounted true if the mount point is found in the mounts list" do
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => '/dev/sdz1 on /tmp/foo'))
@provider.load_current_resource()
@provider.current_resource.mounted.should be_true
end
it "should set mounted true if the symlink target of the device is found in the mounts list" do
# expand the target path to correct specs on Windows
target = ::File.expand_path('/dev/mapper/target')
::File.stub(:symlink?).with("#{@new_resource.device}").and_return(true)
::File.stub(:readlink).with("#{@new_resource.device}").and_return(target)
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => "#{target} on /tmp/foo type ext3 (rw)\n"))
@provider.load_current_resource()
@provider.current_resource.mounted.should be_true
end
it "should set mounted true if the symlink target of the device is relative and is found in the mounts list - CHEF-4957" do
target = "xsdz1"
# expand the target path to correct specs on Windows
absolute_target = ::File.expand_path("/dev/xsdz1")
::File.stub(:symlink?).with("#{@new_resource.device}").and_return(true)
::File.stub(:readlink).with("#{@new_resource.device}").and_return(target)
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => "#{absolute_target} on /tmp/foo type ext3 (rw)\n"))
@provider.load_current_resource()
@provider.current_resource.mounted.should be_true
end
it "should set mounted true if the mount point is found last in the mounts list" do
mount = "/dev/sdy1 on #{@new_resource.mount_point} type ext3 (rw)\n"
mount << "#{@new_resource.device} on #{@new_resource.mount_point} type ext3 (rw)\n"
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => mount))
@provider.load_current_resource()
@provider.current_resource.mounted.should be_true
end
it "should set mounted false if the mount point is not last in the mounts list" do
mount = "#{@new_resource.device} on #{@new_resource.mount_point} type ext3 (rw)\n"
mount << "/dev/sdy1 on #{@new_resource.mount_point} type ext3 (rw)\n"
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => mount))
@provider.load_current_resource()
@provider.current_resource.mounted.should be_false
end
it "mounted should be false if the mount point is not found in the mounts list" do
@provider.stub(:shell_out!).and_return(OpenStruct.new(:stdout => "/dev/sdy1 on /tmp/foo type ext3 (rw)\n"))
@provider.load_current_resource()
@provider.current_resource.mounted.should be_false
end
it "should set enabled to true if the mount point is last in fstab" do
fstab1 = "/dev/sdy1 /tmp/foo ext3 defaults 1 2\n"
fstab2 = "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield(fstab1).and_yield(fstab2)
@provider.load_current_resource
@provider.current_resource.enabled.should be_true
end
it "should set enabled to true if the mount point is not last in fstab and mount_point is a substring of another mount" do
fstab1 = "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n"
fstab2 = "/dev/sdy1 /tmp/foo/bar ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield(fstab1).and_yield(fstab2)
@provider.load_current_resource
@provider.current_resource.enabled.should be_true
end
it "should set enabled to true if the symlink target is in fstab" do
target = "/dev/mapper/target"
::File.stub(:symlink?).with("#{@new_resource.device}").and_return(true)
::File.stub(:readlink).with("#{@new_resource.device}").and_return(target)
fstab = "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield fstab
@provider.load_current_resource
@provider.current_resource.enabled.should be_true
end
it "should set enabled to true if the symlink target is relative and is in fstab - CHEF-4957" do
target = "xsdz1"
::File.stub(:symlink?).with("#{@new_resource.device}").and_return(true)
::File.stub(:readlink).with("#{@new_resource.device}").and_return(target)
fstab = "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield fstab
@provider.load_current_resource
@provider.current_resource.enabled.should be_true
end
it "should set enabled to false if the mount point is not in fstab" do
fstab = "/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield fstab
@provider.load_current_resource
@provider.current_resource.enabled.should be_false
end
it "should ignore commented lines in fstab " do
fstab = "\# #{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield fstab
@provider.load_current_resource
@provider.current_resource.enabled.should be_false
end
it "should set enabled to false if the mount point is not last in fstab" do
line_1 = "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n"
line_2 = "/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield(line_1).and_yield(line_2)
@provider.load_current_resource
@provider.current_resource.enabled.should be_false
end
it "should not mangle the mount options if the device in fstab is a symlink" do
# expand the target path to correct specs on Windows
target = "/dev/mapper/target"
options = "rw,noexec,noauto"
::File.stub(:symlink?).with(@new_resource.device).and_return(true)
::File.stub(:readlink).with(@new_resource.device).and_return(target)
fstab = "#{@new_resource.device} #{@new_resource.mount_point} #{@new_resource.fstype} #{options} 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield fstab
@provider.load_current_resource
@provider.current_resource.options.should eq(options.split(','))
end
it "should not mangle the mount options if the symlink target is in fstab" do
target = ::File.expand_path("/dev/mapper/target")
options = "rw,noexec,noauto"
::File.stub(:symlink?).with(@new_resource.device).and_return(true)
::File.stub(:readlink).with(@new_resource.device).and_return(target)
fstab = "#{target} #{@new_resource.mount_point} #{@new_resource.fstype} #{options} 1 2\n"
::File.stub(:foreach).with("/etc/fstab").and_yield fstab
@provider.load_current_resource
@provider.current_resource.options.should eq(options.split(','))
end
end
context "after the mount's state has been discovered" do
before do
@current_resource = Chef::Resource::Mount.new("/tmp/foo")
@current_resource.device "/dev/sdz1"
@current_resource.device_type :device
@current_resource.fstype "ext3"
@provider.current_resource = @current_resource
end
describe "mount_fs" do
it "should mount the filesystem if it is not mounted" do
@provider.should_receive(:shell_out!).with("mount -t ext3 -o defaults /dev/sdz1 /tmp/foo")
@provider.mount_fs()
end
it "should mount the filesystem with options if options were passed" do
options = "rw,noexec,noauto"
@new_resource.options(%w{rw noexec noauto})
@provider.should_receive(:shell_out!).with("mount -t ext3 -o rw,noexec,noauto /dev/sdz1 /tmp/foo")
@provider.mount_fs()
end
it "should mount the filesystem specified by uuid", :not_supported_on_solaris do
@new_resource.device "d21afe51-a0fe-4dc6-9152-ac733763ae0a"
@new_resource.device_type :uuid
@stdout_findfs = double("STDOUT", :first => "/dev/sdz1")
@provider.stub(:popen4).with("/sbin/findfs UUID=d21afe51-a0fe-4dc6-9152-ac733763ae0a").and_yield(@pid,@stdin,@stdout_findfs,@stderr).and_return(@status)
@stdout_mock = double('stdout mock')
@stdout_mock.stub(:each).and_yield("#{@new_resource.device} on #{@new_resource.mount_point}")
@provider.should_receive(:shell_out!).with("mount -t #{@new_resource.fstype} -o defaults -U #{@new_resource.device} #{@new_resource.mount_point}").and_return(@stdout_mock)
@provider.mount_fs()
end
it "should not mount the filesystem if it is mounted" do
@current_resource.stub(:mounted).and_return(true)
@provider.should_not_receive(:shell_out!)
@provider.mount_fs()
end
end
describe "umount_fs" do
it "should umount the filesystem if it is mounted" do
@current_resource.mounted(true)
@provider.should_receive(:shell_out!).with("umount /tmp/foo")
@provider.umount_fs()
end
it "should not umount the filesystem if it is not mounted" do
@current_resource.mounted(false)
@provider.should_not_receive(:shell_out!)
@provider.umount_fs()
end
end
describe "remount_fs" do
it "should use mount -o remount if remount is supported" do
@new_resource.supports({:remount => true})
@current_resource.mounted(true)
@provider.should_receive(:shell_out!).with("mount -o remount #{@new_resource.mount_point}")
@provider.remount_fs
end
it "should umount and mount if remount is not supported" do
@new_resource.supports({:remount => false})
@current_resource.mounted(true)
@provider.should_receive(:umount_fs)
@provider.should_receive(:sleep).with(1)
@provider.should_receive(:mount_fs)
@provider.remount_fs()
end
it "should not try to remount at all if mounted is false" do
@current_resource.mounted(false)
@provider.should_not_receive(:shell_out!)
@provider.should_not_receive(:umount_fs)
@provider.should_not_receive(:mount_fs)
@provider.remount_fs()
end
end
describe "when enabling the fs" do
it "should enable if enabled isn't true" do
@current_resource.enabled(false)
@fstab = StringIO.new
::File.stub(:open).with("/etc/fstab", "a").and_yield(@fstab)
@provider.enable_fs
@fstab.string.should match(%r{^/dev/sdz1\s+/tmp/foo\s+ext3\s+defaults\s+0\s+2\s*$})
end
it "should not enable if enabled is true and resources match" do
@current_resource.enabled(true)
@current_resource.fstype("ext3")
@current_resource.options(["defaults"])
@current_resource.dump(0)
@current_resource.pass(2)
::File.should_not_receive(:open).with("/etc/fstab", "a")
@provider.enable_fs
end
it "should enable if enabled is true and resources do not match" do
@current_resource.enabled(true)
@current_resource.fstype("auto")
@current_resource.options(["defaults"])
@current_resource.dump(0)
@current_resource.pass(2)
@fstab = StringIO.new
::File.stub(:readlines).and_return([])
::File.should_receive(:open).once.with("/etc/fstab", "w").and_yield(@fstab)
::File.should_receive(:open).once.with("/etc/fstab", "a").and_yield(@fstab)
@provider.enable_fs
end
end
describe "when disabling the fs" do
it "should disable if enabled is true" do
@current_resource.enabled(true)
other_mount = "/dev/sdy1 /tmp/foo ext3 defaults 1 2\n"
this_mount = "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n"
@fstab_read = [this_mount, other_mount]
::File.stub(:readlines).with("/etc/fstab").and_return(@fstab_read)
@fstab_write = StringIO.new
::File.stub(:open).with("/etc/fstab", "w").and_yield(@fstab_write)
@provider.disable_fs
@fstab_write.string.should match(Regexp.escape(other_mount))
@fstab_write.string.should_not match(Regexp.escape(this_mount))
end
it "should disable if enabled is true and ignore commented lines" do
@current_resource.enabled(true)
fstab_read = [%q{/dev/sdy1 /tmp/foo ext3 defaults 1 2},
%q{/dev/sdz1 /tmp/foo ext3 defaults 1 2},
%q{#/dev/sdz1 /tmp/foo ext3 defaults 1 2}]
fstab_write = StringIO.new
::File.stub(:readlines).with("/etc/fstab").and_return(fstab_read)
::File.stub(:open).with("/etc/fstab", "w").and_yield(fstab_write)
@provider.disable_fs
fstab_write.string.should match(%r{^/dev/sdy1 /tmp/foo ext3 defaults 1 2$})
fstab_write.string.should match(%r{^#/dev/sdz1 /tmp/foo ext3 defaults 1 2$})
fstab_write.string.should_not match(%r{^/dev/sdz1 /tmp/foo ext3 defaults 1 2$})
end
it "should disable only the last entry if enabled is true" do
@current_resource.stub(:enabled).and_return(true)
fstab_read = ["/dev/sdz1 /tmp/foo ext3 defaults 1 2\n",
"/dev/sdy1 /tmp/foo ext3 defaults 1 2\n",
"/dev/sdz1 /tmp/foo ext3 defaults 1 2\n"]
fstab_write = StringIO.new
::File.stub(:readlines).with("/etc/fstab").and_return(fstab_read)
::File.stub(:open).with("/etc/fstab", "w").and_yield(fstab_write)
@provider.disable_fs
fstab_write.string.should == "/dev/sdz1 /tmp/foo ext3 defaults 1 2\n/dev/sdy1 /tmp/foo ext3 defaults 1 2\n"
end
it "should not disable if enabled is false" do
@current_resource.stub(:enabled).and_return(false)
::File.stub(:readlines).with("/etc/fstab").and_return([])
::File.should_not_receive(:open).and_yield(@fstab)
@provider.disable_fs
end
end
end
end
|