diff options
-rw-r--r-- | lib/chef/provider/mount/mount.rb | 64 | ||||
-rw-r--r-- | spec/unit/provider/mount/mount_spec.rb | 22 |
2 files changed, 59 insertions, 27 deletions
diff --git a/lib/chef/provider/mount/mount.rb b/lib/chef/provider/mount/mount.rb index ad3f74ebc8..85045eddfa 100644 --- a/lib/chef/provider/mount/mount.rb +++ b/lib/chef/provider/mount/mount.rb @@ -164,36 +164,19 @@ class Chef if @current_resource.enabled # The current options don't match what we have, so - # disable, then enable. - disable_fs - end - ::File.open("/etc/fstab", "a") do |fstab| - fstab.puts("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") - logger.trace("#{@new_resource} is enabled at #{@new_resource.mount_point}") + # update the last matching entry with current option + # and order will remain the same. + update_or_delete_fs(true) + else + ::File.open("/etc/fstab", "a") do |fstab| + fstab.puts("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") + logger.trace("#{@new_resource} is enabled at #{@new_resource.mount_point}") + end end end def disable_fs - if @current_resource.enabled - contents = [] - - found = false - ::File.readlines("/etc/fstab").reverse_each do |line| - if !found && line =~ /^#{device_fstab_regex}\s+#{Regexp.escape(@new_resource.mount_point)}\s/ - found = true - logger.trace("#{@new_resource} is removed from fstab") - next - else - contents << line - end - end - - ::File.open("/etc/fstab", "w") do |fstab| - contents.reverse_each { |line| fstab.puts line } - end - else - logger.trace("#{@new_resource} is not enabled - nothing to do") - end + update_or_delete_fs end def network_device? @@ -264,6 +247,35 @@ class Chef @current_resource.pass == @new_resource.pass end + # It will update or delete the entry from fstab. + def update_or_delete_fs(update_fs=false) + if @current_resource.enabled + contents = [] + + found = false + ::File.readlines("/etc/fstab").reverse_each do |line| + if !found && line =~ /^#{device_fstab_regex}\s+#{Regexp.escape(@new_resource.mount_point)}\s/ + found = true + if update_fs + logger.trace("#{@new_resource} is updated with new content in fstab") + contents << ("#{device_fstab} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? default_mount_options : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") + else + logger.trace("#{@new_resource} is removed from fstab") + end + next + else + contents << line + end + end + + ::File.open("/etc/fstab", "w") do |fstab| + contents.reverse_each { |line| fstab.puts line } + end + else + logger.trace("#{@new_resource} is not enabled - nothing to do") + end + end + end end end diff --git a/spec/unit/provider/mount/mount_spec.rb b/spec/unit/provider/mount/mount_spec.rb index 2ac9d12f42..9a7d9198b5 100644 --- a/spec/unit/provider/mount/mount_spec.rb +++ b/spec/unit/provider/mount/mount_spec.rb @@ -418,10 +418,30 @@ describe Chef::Provider::Mount::Mount do @fstab = StringIO.new allow(::File).to receive(:readlines).and_return([]) expect(::File).to receive(:open).once.with("/etc/fstab", "w").and_yield(@fstab) - expect(::File).to receive(:open).once.with("/etc/fstab", "a").and_yield(@fstab) @provider.enable_fs end + + it "should update the last matching entry if enabled is true" do + @new_resource.fstype("ext4") + @new_resource.dump(2) + @new_resource.pass(1) + allow(@current_resource).to receive(: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", + "/dev/sdz1 /tmp/foobar ext3 defaults 1 2\n"] + + fstab_write = StringIO.new + allow(::File).to receive(:readlines).with("/etc/fstab").and_return(fstab_read) + allow(::File).to receive(:open).with("/etc/fstab", "w").and_yield(fstab_write) + + @provider.enable_fs + expect(fstab_write.string).to eq("/dev/sdz1 /tmp/foo ext3 defaults 1 2\n" + + "/dev/sdy1 /tmp/foo ext3 defaults 1 2\n" + + "/dev/sdz1 /tmp/foo #{@new_resource.fstype} defaults #{@new_resource.dump} #{@new_resource.pass}\n" + + "/dev/sdz1 /tmp/foobar ext3 defaults 1 2\n") + end end describe "when disabling the fs" do |