diff --git a/lib/puppet/provider/mount.rb b/lib/puppet/provider/mount.rb index d3597ef15..d1ba79c7c 100644 --- a/lib/puppet/provider/mount.rb +++ b/lib/puppet/provider/mount.rb @@ -1,39 +1,39 @@ # Created by Luke Kanies on 2006-11-12. # Copyright (c) 2006. All rights reserved. require 'puppet' # A module just to store the mount/unmount methods. Individual providers # still need to add the mount commands manually. module Puppet::Provider::Mount # This only works when the mount point is synced to the fstab. def mount mountcmd @model[:name] end # This only works when the mount point is synced to the fstab. def unmount umount @model[:name] end # Is the mount currently mounted? def mounted? platform = Facter["operatingsystem"].value df = command(:df) case Facter["operatingsystem"].value # Solaris's df prints in a very weird format when "Solaris": df = "#{command(:df)} -k" end %x{#{df}}.split("\n").find do |line| fs = line.split(/\s+/)[-1] if platform == "Darwin" fs == "/private/var/automount" + @model[:name] or fs == @model[:name] else fs == @model[:name] end end end end -# $Id$ \ No newline at end of file +# $Id$ diff --git a/lib/puppet/type/mount.rb b/lib/puppet/type/mount.rb index 4ec6d3516..884b86c00 100755 --- a/lib/puppet/type/mount.rb +++ b/lib/puppet/type/mount.rb @@ -1,157 +1,157 @@ require 'puppet/type/parsedtype' module Puppet newtype(:mount) do # Use the normal parent class, because we actually want to # call code when sync() is called. newstate(:ensure) do desc "Control what to do with this mount. If the value is ``present``, the mount is entered into the mount table, but not mounted, if it is ``absent``, the entry is removed from the mount table and the filesystem is unmounted if currently mounted, if it is ``mounted``, the filesystem is entered into the mount table and mounted." newvalue(:present) do if provider.mounted? provider.unmount return :mount_unmounted else provider.create return :mount_created end end aliasvalue :unmounted, :present newvalue(:absent, :event => :mount_deleted) do if provider.mounted? provider.unmount end provider.destroy end newvalue(:mounted, :event => :mount_mounted) do # Create the mount point if it does not already exist. if self.is == :absent or self.is.nil? provider.create end # We have to flush any changes to disk. @parent.flush provider.mount end defaultto do if @parent.managed? :mounted else nil end end def retrieve if provider.mounted? @is = :mounted else @is = super() end end end newstate(:device) do desc "The device providing the mount. This can be whatever device is supporting by the mount, including network devices or devices specified by UUID rather than device path, depending on the operating system." end # Solaris specifies two devices, not just one. newstate(:blockdevice) do desc "The the device to fsck. This is state is only valid on Solaris, and in most cases will default to the correct value." # Default to the device but with "dsk" replaced with "rdsk". defaultto do if Facter["operatingsystem"].value == "Solaris" device = @parent.value(:device) if device =~ %r{/dsk/} device.sub(%r{/dsk/}, "/rdsk/") else nil end else nil end end end newstate(:fstype) do desc "The mount type. Valid values depend on the operating system." end newstate(:options) do desc "Mount options for the mounts, as they would appear in the fstab." end newstate(:pass) do desc "The pass in which the mount is checked." end newstate(:atboot) do desc "Whether to mount the mount at boot. Not all platforms support this." end newstate(:dump) do desc "Whether to dump the mount. Not all platforms support this." end newstate(:target) do desc "The file in which to store the mount table. Only used by those providers that write to disk (i.e., not NetInfo)." defaultto { if @parent.class.defaultprovider.ancestors.include?(Puppet::Provider::ParsedFile) @parent.class.defaultprovider.default_target else nil end } end newparam(:name) do desc "The mount path for the mount." isnamevar end newparam(:path) do desc "The deprecated name for the mount point. Please use ``name`` now." - def should=(value) + def value=(value) warning "'path' is deprecated for mounts. Please use 'name'." @parent[:name] = value super end end @doc = "Manages mounted mounts, including putting mount information into the mount table. The actual behavior depends on the value of the 'ensure' parameter." def value(name) name = symbolize(name) ret = nil if state = @states[name] return state.value end end end end # $Id$ diff --git a/test/types/mount.rb b/test/types/mount.rb index 8b8cf9ab5..1134b664a 100755 --- a/test/types/mount.rb +++ b/test/types/mount.rb @@ -1,228 +1,247 @@ #!/usr/bin/env ruby $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppettest' require 'puppet' class TestMounts < Test::Unit::TestCase include PuppetTest p = Puppet::Type.type(:mount).provide :fake, :parent => PuppetTest::FakeParsedProvider do @name = :fake apimethods :ensure attr_accessor :mounted def self.default_target :yayness end def create @ensure = :present @model.class.validstates.each do |state| if value = @model.should(state) self.send(state.to_s + "=", value) end end end def destroy @ensure = :absent @mounted = false end def exists? if defined? @ensure and @ensure == :present true else false end end def mounted? self.mounted end def mount self.mounted = true end def unmount self.mounted = false end end FakeMountProvider = p @@fakeproviders[:mount] = p def setup super @mount = Puppet::Type.type(:mount) @realprovider = @mount.defaultprovider @mount.defaultprovider = FakeMountProvider end def teardown Puppet.type(:mount).clear if @realprovider.respond_to?(:clear) @realprovider.clear end Puppet::Type.type(:mount).defaultprovider = nil super end def mkmount mount = nil + assert_nothing_raised { + mount = Puppet.type(:mount).create(mkmount_args) + } + + return mount + end + + def mkmount_args if defined? @pcount @pcount += 1 else @pcount = 1 end args = { :path => "/fspuppet%s" % @pcount, :device => "/dev/dsk%s" % @pcount, } [@mount.validstates, @mount.parameters].flatten.each do |field| next if field == :provider next if field == :target next if field == :ensure unless args.include? field args[field] = "fake%s" % @pcount end end - assert_nothing_raised { - mount = Puppet.type(:mount).create(args) - } - - return mount + return args end def test_simplemount mount = mkmount assert_apply(mount) mount.send(:states).each do |state| assert_equal(state.should, mount.provider.send(state.name), "%s was not set to %s" % [state.name, state.should]) end assert_events([], mount) assert_nothing_raised { mount.retrieve } assert_equal(:mounted, mount.is(:ensure)) end # Make sure fs mounting behaves appropriately. This is more a test of # whether things get mounted and unmounted based on the value of 'ensure'. def test_mountfs obj = mkmount assert_apply(obj) # Verify we can remove the mount assert_nothing_raised { obj[:ensure] = :absent } assert_events([:mount_deleted], obj) assert_events([], obj) # And verify it's gone assert(!obj.provider.mounted?, "Object is mounted after being removed") assert_nothing_raised { obj[:ensure] = :present } assert_events([:mount_created], obj) assert_events([], obj) assert(! obj.provider.mounted?, "Object is mounted incorrectly") assert_nothing_raised { obj[:ensure] = :mounted } assert_events([:mount_mounted], obj) assert_events([], obj) obj.retrieve assert_equal(:mounted, obj.is(:ensure)) obj.retrieve assert(obj.provider.mounted?, "Object is not mounted") end # Darwin doesn't put its mount table into netinfo unless Facter.value(:operatingsystem) == "Darwin" def test_list list = nil assert(@mount.respond_to?(:list), "No list method defined for mount") assert_nothing_raised do list = Puppet::Type.type(:mount).list end assert(list.length > 0, "Did not return any mounts") root = list.find { |o| o[:name] == "/" } assert(root, "Could not find root root filesystem in list results") assert(root.is(:device), "Device was not set") assert(root.state(:device).value, "Device was not returned by value method") assert_nothing_raised do root.retrieve end assert(root.is(:device), "Device was not set") assert(root.state(:device).value, "Device was not returned by value method") end end # Make sure we actually remove the object from the file and such. # Darwin will actually write to netinfo here. if Facter.value(:operatingsystem) != "Darwin" or Process.uid == 0 def test_removal # Reset the provider so that we're using the real thing @mount.defaultprovider = nil provider = @mount.defaultprovider assert(provider, "Could not retrieve default provider") if provider.respond_to?(:default_target) file = provider.default_target assert(FileTest.exists?(file), "FSTab %s does not exist" % file) # Now switch to ram, so we're just doing this there, not really on disk. provider.filetype = :ram #provider.target_object(file).write text end mount = mkmount mount[:ensure] = :present assert_events([:mount_created], mount) assert_events([], mount) mount[:ensure] = :absent assert_events([:mount_deleted], mount) assert_events([], mount) # Now try listing and making sure the object is actually gone. list = mount.provider.class.list assert(! list.find { |r| r[:name] == mount[:name] }, "Mount was not actually removed") end end + + # Make sure that the name gets correctly set if they set the path, + # which used to be the namevar. + def test_name_and_path + mount = nil + args = mkmount_args + args[:name] = "mount_name" + args[:path] = "mount_path" + + assert_nothing_raised do + mount = @mount.create(args) + end + + assert_equal("mount_path", mount[:name], "Name did not get copied over") + end end # $Id$