diff options
author | Adam Jacob <adam@hjksolutions.com> | 2009-03-03 17:29:42 -0800 |
---|---|---|
committer | Adam Jacob <adam@hjksolutions.com> | 2009-03-03 17:29:42 -0800 |
commit | bdc84ef0b02dc79037791c5f63e18da524ba6fbb (patch) | |
tree | 81a50aa8c2ccb39ddad274aaaae8f753762ebd9c | |
parent | e010cc7effbe1b2af27c174b954f283d53570b5c (diff) | |
parent | 5dab8417dc17d77357db80f4d7f7179315cec99c (diff) | |
download | chef-bdc84ef0b02dc79037791c5f63e18da524ba6fbb.tar.gz |
Merge commit 'johnhampton/chef-152'
-rw-r--r-- | chef/lib/chef/provider/mount.rb | 42 | ||||
-rw-r--r-- | chef/lib/chef/provider/mount/mount.rb | 117 | ||||
-rw-r--r-- | chef/lib/chef/resource/mount.rb | 15 | ||||
-rw-r--r-- | chef/spec/unit/provider/mount/mount_spec.rb | 242 | ||||
-rw-r--r-- | chef/spec/unit/provider/mount_spec.rb | 100 | ||||
-rw-r--r-- | chef/spec/unit/resource/mount_spec.rb | 20 |
6 files changed, 483 insertions, 53 deletions
diff --git a/chef/lib/chef/provider/mount.rb b/chef/lib/chef/provider/mount.rb index 69a920d9fc..da7734653d 100644 --- a/chef/lib/chef/provider/mount.rb +++ b/chef/lib/chef/provider/mount.rb @@ -35,6 +35,7 @@ class Chef Chef::Log.debug("#{@new_resource}: attempting to mount") status = mount_fs() if status + @new_resource.updated = true Chef::Log.info("#{@new_resource}: mounted succesfully") end else @@ -47,6 +48,7 @@ class Chef Chef::Log.debug("#{@new_resource}: attempting to unmount") status = umount_fs() if status + @new_resource.updated = true Chef::Log.info("#{@new_resource}: unmounted succesfully") end else @@ -62,6 +64,7 @@ class Chef Chef::Log.debug("#{@new_resource}: attempting to remount") status = remount_fs() if status + @new_resource.updated = true Chef::Log.info("#{@new_resource}: remounted succesfully") end else @@ -69,19 +72,50 @@ class Chef end end end + + def action_enable + unless @current_resource.enabled + status = enable_fs + if status + @new_resource.updated = true + Chef::Log.info("#{@new_resource}: enabled successfully") + else + Chef::Log.debug("#{@new_resource}: not enabling, already enabled") + end + end + end + + def action_disable + if @current_resource.enabled + status = disable_fs + if status + @new_resource.updated = true + Chef::Log.info("#{@new_resource}: disabled successfully") + else + Chef::Log.debug("#{@new_resource}: not disabling, already disabled") + end + end + end - def mount_fs(name) + def mount_fs raise Chef::Exception::UnsupportedAction, "#{self.to_s} does not support :mount" end - def umount_fs(name) + def umount_fs raise Chef::Exception::UnsupportedAction, "#{self.to_s} does not support :umount" end - def remount_fs(name) + def remount_fs raise Chef::Exception::UnsupportedAction, "#{self.to_s} does not support :remount" end - + + def enable_fs + raise Chef::Exception::UnsupportedAction, "#{self.to_s} does not support :enable" + end + + def disable_fs + raise Chef::Exception::UnsupportedAction, "#{self.to_s} does not support :disable" + end end end end diff --git a/chef/lib/chef/provider/mount/mount.rb b/chef/lib/chef/provider/mount/mount.rb index c3e3f07506..9c9388f12d 100644 --- a/chef/lib/chef/provider/mount/mount.rb +++ b/chef/lib/chef/provider/mount/mount.rb @@ -6,9 +6,9 @@ # 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. @@ -22,57 +22,67 @@ require 'chef/mixin/command' class Chef class Provider - class Mount + class Mount class Mount < Chef::Provider::Mount - + include Chef::Mixin::Command - + def initialize(node, new_resource) super(node, new_resource) end - + def load_current_resource @current_resource = Chef::Resource::Mount.new(@new_resource.name) @current_resource.mount_point(@new_resource.mount_point) + @current_resource.device(@new_resource.device) Chef::Log.debug("Checking for mount point #{@current_resource.mount_point}") + + # Check to see if the volume is mounted. Last volume entry wins. + mounted = false popen4("mount") do |pid, stdin, stdout, stderr| stdout.each do |line| case line - when /^#{@new_resource.device}\s+on\s+#{@new_resource.mount_point}/ - @current_resource.mounted(true) + when /^#{device_regex}\s+on\s+#{@new_resource.mount_point}/ + mounted = true Chef::Log.debug("Special device #{@new_resource.device} mounted as #{@new_resource.mount_point}") - else - @current_resource.mounted(false) + when /^([\/\w])+\son\s#{@new_resource.mount_point}\s+/ + mounted = false + Chef::Log.debug("Special device #{$~[1]} mounted as #{@new_resource.mount_point}") end end end - # revisit for enable/disable - # File.read("/etc/fstab").each do |line| - # case line - # when /^[#\s]/ - # next - # when /^[\/\w]+#{@new_resource.mount_point}/ - # @mount_exists = true - # Chef::Log.debug("Found mount point #{@new_resource.mount_point} in /etc/fstab") - # end - # end - @current_resource + @current_resource.mounted(mounted) + + # Check to see if there is a entry in /etc/fstab. Last entry for a volume wins. + enabled = false + ::File.read("/etc/fstab").each do |line| + case line + when /^[#\s]/ + next + when /^#{device_regex}\s+#{@new_resource.mount_point}/ + enabled = true + Chef::Log.debug("Found mount #{@new_resource.device} to #{@new_resource.mount_point} in /etc/fstab") + when /^[\/\w]+\s+#{@new_resource.mount_point}/ + enabled = false + Chef::Log.debug("Found conflicting mount point #{@new_resource.mount_point} in /etc/fstab") + end + end + @current_resource.enabled(enabled) end - + def mount_fs unless @current_resource.mounted - if @new_resource.options.include?("defaults") or @new_resource.options == nil - command = "mount -t #{@new_resource.fstype} " - else - command = "mount -t #{@new_resource.fstype} -o #{@new_resource.options} " - end - command << "#{@new_resource.device} " - command << "#{@new_resource.mount_point}" + command = "mount -t #{@new_resource.fstype}" + command << " -o #{@new_resource.options.join(',')}" unless @new_resource.options.nil? || @new_resource.options.empty? + command << " #{@new_resource.device}" + command << " #{@new_resource.mount_point}" run_command(:command => command) Chef::Log.info("Mounted #{@new_resource.mount_point}") + else + Chef::Log.debug("#{@new_resource.mount_point} is already mounted.") end end - + def umount_fs if @current_resource.mounted command = "umount #{@new_resource.mount_point}" @@ -82,13 +92,15 @@ class Chef Chef::Log.debug("#{@new_resource.mount_point} is not mounted.") end end - + def remount_fs if @current_resource.mounted and @new_resource.supports[:remount] command = "mount -o remount #{@new_resource.mount_point}" run_command(:command => command) + + @new_resource.updated = true Chef::Log.info("Remounted #{@new_resource.mount_point}") - elsif @current_resource.mounted + elsif @current_resource.mounted umount_fs sleep 1 mount_fs @@ -96,8 +108,45 @@ class Chef Chef::Log.debug("#{@new_resource.mount_point} is not mounted.") end end - - # def action_enable, action_disable, future feature/improvement. + + def enable_fs + unless @current_resource.enabled + ::File.open("/etc/fstab", "a") do |fstab| + fstab.puts("#{@new_resource.device} #{@new_resource.mount_point} #{@new_resource.fstype} #{@new_resource.options.nil? ? "defaults" : @new_resource.options.join(",")} #{@new_resource.dump} #{@new_resource.pass}") + Chef::Log.info("Enabled #{@new_resource.mount_point}") + end + else + Chef::Log.debug("#{@new_resource.mount_point} is already enabled.") + end + end + + def disable_fs + if @current_resource.enabled + contents = [] + + found = false + ::File.readlines("/etc/fstab").reverse_each do |line| + if !found && line =~ /^#{device_regex}\s+#{@new_resource.mount_point}/ + found = true + Chef::Log.debug("Removing #{@new_resource.mount_point} from fstab") + next + else + contents << line + end + end + + ::File.open("/etc/fstab", "w") do |fstab| + contents.reverse_each { |line| fstab.puts line} + end + else + Chef::Log.debug("#{@new_resource.mount_point} is not enabled") + end + end + + private + def device_regex + ::File.symlink?(@new_resource.device) ? "(?:#{@new_resource.device})|(?:#{::File.readlink(@new_resource.device)})" : @new_resource.device + end end end end diff --git a/chef/lib/chef/resource/mount.rb b/chef/lib/chef/resource/mount.rb index 6db6830c8f..d807a90d95 100644 --- a/chef/lib/chef/resource/mount.rb +++ b/chef/lib/chef/resource/mount.rb @@ -28,13 +28,14 @@ class Chef @mount_point = name @device = nil @fstype = nil - @options = "defaults" + @options = ["defaults"] @dump = 0 @pass = 2 @mounted = false + @enabled = false @action = :mount @supports = { :remount => false } - @allowed_actions.push(:mount, :umount, :remount) + @allowed_actions.push(:mount, :umount, :remount, :enable, :disable) end def mount_point(arg=nil) @@ -98,7 +99,15 @@ class Chef :kind_of => [ TrueClass, FalseClass ] ) end - + + def enabled(arg=nil) + set_or_return( + :enabled, + arg, + :kind_of => [ TrueClass, FalseClass ] + ) + end + def supports(args={}) if args.is_a? Array args.each { |arg| @supports[arg] = true } diff --git a/chef/spec/unit/provider/mount/mount_spec.rb b/chef/spec/unit/provider/mount/mount_spec.rb index a504c7317a..ecfee1532d 100644 --- a/chef/spec/unit/provider/mount/mount_spec.rb +++ b/chef/spec/unit/provider/mount/mount_spec.rb @@ -42,6 +42,7 @@ describe Chef::Provider::Mount::Mount, "load_current_resource" do @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + ::File.stub!(:read).with("/etc/fstab").and_return "\n" @status = mock("Status", :exitstatus => 0) @provider.stub!(:popen4).and_return(@status) @@ -61,6 +62,11 @@ describe Chef::Provider::Mount::Mount, "load_current_resource" do @provider.load_current_resource() end + it "should set the current resources device to the new resources device" do + @current_resource.should_receive(:device).with(@new_resource.device) + @provider.load_current_resource() + end + it "should set mounted true if the mount point is found in the mounts list" do @stdout.stub!(:each).and_yield("#{@new_resource.device} on #{@new_resource.mount_point}") @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(0) @@ -68,13 +74,99 @@ describe Chef::Provider::Mount::Mount, "load_current_resource" do @provider.load_current_resource() end + it "should set mounted true if the symlink target of the device is found in the mounts list" 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) + + @stdout.stub!(:each).and_yield("#{target} on #{@new_resource.mount_point} type ext3 (rw)\n") + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(0) + @current_resource.should_receive(:mounted).with(true) + @provider.load_current_resource() + + 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" + + y = @stdout.stub!(:each) + mount.each {|l| y.and_yield(l)} + + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(0) + @current_resource.should_receive(:mounted).with(true) + @provider.load_current_resource() + 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" + + y = @stdout.stub!(:each) + mount.each {|l| y.and_yield(l)} + + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(0) + @current_resource.should_receive(:mounted).with(false) + @provider.load_current_resource() + end + it "mounted should be false if the mount point is not found in the mounts list" do - @stdout.stub!(:each).and_yield("#{@new_resource.mount_point} on #{@new_resource.mount_point}") + @stdout.stub!(:each).and_yield("/dev/sdy1 on #{@new_resource.mount_point} type ext3 (rw)\n") @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(0) @current_resource.should_receive(:mounted).with(false) @provider.load_current_resource() end + + it "should set enabled to true if the mount point is last in fstab" do + fstab = "/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n" + fstab << "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + + ::File.stub!(:read).with("/etc/fstab").and_return fstab + + @current_resource.should_receive(:enabled).with(true) + @provider.load_current_resource + 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 = "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + + ::File.stub!(:read).with("/etc/fstab").and_return fstab + + @current_resource.should_receive(:enabled).with(true) + @provider.load_current_resource + 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!(:read).with("/etc/fstab").and_return fstab + + @current_resource.should_receive(:enabled).with(false) + @provider.load_current_resource + + end + + it "should ignore commented lines in fstab " do + fstab = "\# #{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + ::File.stub!(:read).with("/etc/fstab").and_return fstab + + @current_resource.should_receive(:enabled).with(false) + @provider.load_current_resource + end + + it "should set enabled to false if the mount point is not last in fstab" do + fstab = "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + fstab << "/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n" + ::File.stub!(:read).with("/etc/fstab").and_return fstab + + @current_resource.should_receive(:enabled).with(false) + @provider.load_current_resource + end end describe Chef::Provider::Mount::Mount, "mount_fs" do @@ -119,10 +211,11 @@ describe Chef::Provider::Mount::Mount, "mount_fs" do end it "should mount the filesystem with options if options were passed" do + options = "rw,noexec,noauto" @stdout.stub!(:each).and_yield("#{@new_resource.mount_point} on #{@new_resource.mount_point}") @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(0) - @new_resource.stub!(:options).and_return("rw,noexec,noauto") - @provider.should_receive(:run_command).with({:command => "mount -t #{@new_resource.fstype} -o #{@new_resource.options} #{@new_resource.device} #{@new_resource.mount_point}"}) + @new_resource.stub!(:options).and_return(options.split(",")) + @provider.should_receive(:run_command).with({:command => "mount -t #{@new_resource.fstype} -o #{options} #{@new_resource.device} #{@new_resource.mount_point}"}) @provider.mount_fs() end @@ -240,3 +333,146 @@ describe Chef::Provider::Mount::Mount, "remount_fs" do @provider.remount_fs() end end + +describe Chef::Provider::Mount::Mount, "enable_fs" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false, + :options => ["defaults"], + :dump => 0, + :pass => 2 + ) + @new_resource.stub!(:supports).and_return({:remount => false}) + + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false + ) + + @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) + Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + @provider.current_resource = @current_resource + + @fstab = mock("File", :null_object => true) + end + + it "should enable if enabled isn't true" do + @current_resource.stub!(:enabled).and_return(false) + + ::File.stub!(:open).with("/etc/fstab", "a").and_yield(@fstab) + @fstab.should_receive(:puts).with(/^#{@new_resource.device}\s+#{@new_resource.mount_point}\s+#{@new_resource.fstype}\s+defaults\s+#{@new_resource.dump}\s+#{@new_resource.pass}\s*$/) + + @provider.enable_fs + end + + it "should not enabled if enabled is true" do + @current_resource.stub!(:enabled).and_return(true) + ::File.should_not_receive(:open).with("/etc/fstab", "a").and_yield(@fstab) + + @provider.enable_fs + end +end + +describe Chef::Provider::Mount::Mount, "disable_fs" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false, + :options => ["defaults"], + :dump => 0, + :pass => 2 + ) + @new_resource.stub!(:supports).and_return({:remount => false}) + + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false + ) + + @provider = Chef::Provider::Mount::Mount.new(@node, @new_resource) + Chef::Resource::Mount.stub!(:new).and_return(@current_resource) + @provider.current_resource = @current_resource + + @fstab = mock("File", :null_object => true) + end + + it "should disable if enabled is true" do + @current_resource.stub!(:enabled).and_return(true) + + fstab = ["/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n"] + fstab << "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + + ::File.stub!(:readlines).with("/etc/fstab").and_return(fstab) + ::File.stub!(:open).with("/etc/fstab", "w").and_yield(@fstab) + + @fstab.should_receive(:puts).with(fstab[0]).once.ordered + @fstab.should_not_receive(:puts).with(fstab[1]) + + @provider.disable_fs + end + + it "should disable if enabled is true and ignore commented lines" do + @current_resource.stub!(:enabled).and_return(true) + + fstab = ["/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n"] + fstab << "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + fstab << "\##{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + + + ::File.stub!(:readlines).with("/etc/fstab").and_return(fstab) + ::File.stub!(:open).with("/etc/fstab", "w").and_yield(@fstab) + + @fstab.should_receive(:puts).with(fstab[0]).once.ordered + @fstab.should_receive(:puts).with(fstab[2]).once.ordered + + @fstab.should_not_receive(:puts).with(fstab[1]) + + @provider.disable_fs + end + + it "should disable only the last entry if enabled is true" do + @current_resource.stub!(:enabled).and_return(true) + fstab = ["#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n"] + fstab << "/dev/sdy1 #{@new_resource.mount_point} ext3 defaults 1 2\n" + fstab << "#{@new_resource.device} #{@new_resource.mount_point} ext3 defaults 1 2\n" + + + ::File.stub!(:readlines).with("/etc/fstab").and_return(fstab) + ::File.stub!(:open).with("/etc/fstab", "w").and_yield(@fstab) + + @fstab.should_receive(:puts).with(fstab[0]).once.ordered + @fstab.should_receive(:puts).with(fstab[1]).once.ordered + + @fstab.should_not_receive(:puts).with(fstab[2]) + + @provider.disable_fs + 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 diff --git a/chef/spec/unit/provider/mount_spec.rb b/chef/spec/unit/provider/mount_spec.rb index e8f3bc0295..7641aafc2d 100644 --- a/chef/spec/unit/provider/mount_spec.rb +++ b/chef/spec/unit/provider/mount_spec.rb @@ -41,7 +41,8 @@ describe Chef::Provider::Mount, "action_mount" do :name => "/tmp/foo", :mount_point => "/tmp/foo", :fstype => "ext3", - :mounted => false + :mounted => false, + :enabled => false ) @current_resource = mock("Chef::Resource::Mount", :null_object => true, @@ -49,7 +50,8 @@ describe Chef::Provider::Mount, "action_mount" do :name => "/tmp/foo", :mount_point => "/tmp/foo", :fstype => "ext3", - :mounted => false + :mounted => false, + :enabled => false ) @provider = Chef::Provider::Mount.new(@node, @new_resource) @provider.current_resource = @current_resource @@ -79,7 +81,8 @@ describe Chef::Provider::Mount, "action_umount" do :name => "/tmp/foo", :mount_point => "/tmp/foo", :fstype => "ext3", - :mounted => false + :mounted => false, + :enabled => false ) @current_resource = mock("Chef::Resource::Mount", :null_object => true, @@ -87,7 +90,8 @@ describe Chef::Provider::Mount, "action_umount" do :name => "/tmp/foo", :mount_point => "/tmp/foo", :fstype => "ext3", - :mounted => false + :mounted => false, + :enabled => false ) @provider = Chef::Provider::Mount.new(@node, @new_resource) @provider.current_resource = @current_resource @@ -116,7 +120,8 @@ describe Chef::Provider::Mount, "action_remount" do :name => "/tmp/foo", :mount_point => "/tmp/foo", :fstype => "ext3", - :mounted => false + :mounted => false, + :enabled => false ) @current_resource = mock("Chef::Resource::Mount", :null_object => true, @@ -124,7 +129,8 @@ describe Chef::Provider::Mount, "action_remount" do :name => "/tmp/foo", :mount_point => "/tmp/foo", :fstype => "ext3", - :mounted => false + :mounted => false, + :enabled => false ) @provider = Chef::Provider::Mount.new(@node, @new_resource) @provider.current_resource = @current_resource @@ -145,7 +151,85 @@ describe Chef::Provider::Mount, "action_remount" do end end -%w{mount umount remount}.each do |act| +describe Chef::Provider::Mount, "action_enable" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false, + :enabled => false + ) + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false, + :enabled => false + ) + @provider = Chef::Provider::Mount.new(@node, @new_resource) + @provider.current_resource = @current_resource + @provider.stub!(:enable_fs).and_return(true) + end + + it "should enable the mount if it isn't enable" do + @current_resource.stub!(:enabled).and_return(false) + @provider.should_receive(:enable_fs).with.and_return(true) + @provider.action_enable + end + + it "should not enable the mount if it is enabled" do + @current_resource.stub!(:enabled).and_return(true) + @provider.should_not_receive(:enable_fs).with.and_return(true) + @provider.action_enable + end +end + +describe Chef::Provider::Mount, "action_disable" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false, + :enabled => false + ) + @current_resource = mock("Chef::Resource::Mount", + :null_object => true, + :device => "/dev/sdz1", + :name => "/tmp/foo", + :mount_point => "/tmp/foo", + :fstype => "ext3", + :mounted => false, + :enabled => false + ) + @provider = Chef::Provider::Mount.new(@node, @new_resource) + @provider.current_resource = @current_resource + @provider.stub!(:disable_fs).and_return(true) + end + + it "should disable the mount if it is enabled" do + @current_resource.stub!(:enabled).and_return(true) + @provider.should_receive(:disable_fs).with.and_return(true) + @provider.action_disable + end + + it "should not disable the mount if it isn't enabled" do + @current_resource.stub!(:enabled).and_return(false) + @provider.should_not_receive(:disable_fs).with.and_return(true) + @provider.action_disable + end +end + +%w{mount umount remount enable disable}.each do |act| act_string = "#{act}_fs" describe Chef::Provider::Service, act_string do @@ -173,7 +257,7 @@ end end it "should raise Chef::Exception::UnsupportedAction on an unsupported action" do - lambda { @provider.send(act_string, @new_resource.name) }.should raise_error(Chef::Exception::UnsupportedAction) + lambda { @provider.send(act_string) }.should raise_error(Chef::Exception::UnsupportedAction) end end end
\ No newline at end of file diff --git a/chef/spec/unit/resource/mount_spec.rb b/chef/spec/unit/resource/mount_spec.rb index beebe2e81b..9886fb47ae 100644 --- a/chef/spec/unit/resource/mount_spec.rb +++ b/chef/spec/unit/resource/mount_spec.rb @@ -68,7 +68,7 @@ describe Chef::Resource::Mount do end it "should set the options attribute to defaults" do - @resource.options.should eql("defaults") + @resource.options.should eql(["defaults"]) end it "should allow options to be sent as a string, and convert to array" do @@ -99,6 +99,24 @@ describe Chef::Resource::Mount do lambda { @resource.mounted("poop") }.should raise_error(ArgumentError) end + it "should accept true for enabled" do + @resource.enabled(true) + @resource.enabled.should eql(true) + end + + it "should accept false for enabled" do + @resource.enabled(false) + @resource.enabled.should eql(false) + end + + it "should set enabled to false by default" do + @resource.enabled.should eql(false) + end + + it "should not accept a string for enabled" do + lambda { @resource.enabled("poop") }.should raise_error(ArgumentError) + end + it "should default all feature support to false" do support_hash = { :remount => false } @resource.supports.should == support_hash |