summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-17 21:38:30 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-17 21:38:30 +0000
commit95f2fe70bf63791fb691d539281f5cfbfd1fb664 (patch)
treebde480ce5ada8ea2abbc5fecf8f5c9aa3ca2aabf /test
parent86dae84dad4cd5688029c398109b15b6074cf3c4 (diff)
downloadpuppet-95f2fe70bf63791fb691d539281f5cfbfd1fb664.tar.gz
puppet-95f2fe70bf63791fb691d539281f5cfbfd1fb664.tar.xz
puppet-95f2fe70bf63791fb691d539281f5cfbfd1fb664.zip
Ported mount over to using providers
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1801 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
-rw-r--r--test/lib/puppettest/fakes.rb30
-rw-r--r--test/lib/puppettest/support/utils.rb13
-rwxr-xr-xtest/providers/parsedmount.rb213
-rwxr-xr-xtest/types/mount.rb274
4 files changed, 321 insertions, 209 deletions
diff --git a/test/lib/puppettest/fakes.rb b/test/lib/puppettest/fakes.rb
index f2d424295..40fe96a10 100644
--- a/test/lib/puppettest/fakes.rb
+++ b/test/lib/puppettest/fakes.rb
@@ -3,6 +3,7 @@ require 'puppettest'
module PuppetTest
# A baseclass for the faketypes.
class FakeModel
+ include Puppet::Util
class << self
attr_accessor :name
@name = :fakemodel
@@ -29,7 +30,8 @@ module PuppetTest
end
def []=(param, value)
- unless @realmodel.attrtype(param)
+ param = symbolize(param)
+ unless @realmodel.validattr?(param)
raise Puppet::DevError, "Invalid attribute %s for %s" %
[param, @realmodel.name]
end
@@ -46,7 +48,7 @@ module PuppetTest
@is = {}
@should = {}
@params = {}
- self[:name] = name
+ self[@realmodel.namevar] = name
end
def inspect
@@ -121,6 +123,29 @@ module PuppetTest
end
end
+ class FakeParsedProvider < FakeProvider
+ def hash
+ ret = {}
+ instance_variables.each do |v|
+ v = v.sub("@", '')
+ if val = self.send(v)
+ ret[v.intern] = val
+ end
+ end
+
+ return ret
+ end
+
+ def store(hash)
+ hash.each do |n, v|
+ method = n.to_s + "="
+ if respond_to? method
+ send(method, v)
+ end
+ end
+ end
+ end
+
@@fakemodels = {}
@@fakeproviders = {}
@@ -132,7 +157,6 @@ module PuppetTest
end
obj = @@fakemodels[type].new(name)
- obj[:name] = name
options.each do |name, val|
obj[name] = val
end
diff --git a/test/lib/puppettest/support/utils.rb b/test/lib/puppettest/support/utils.rb
index ea2d5483c..c7d54d5e6 100644
--- a/test/lib/puppettest/support/utils.rb
+++ b/test/lib/puppettest/support/utils.rb
@@ -142,6 +142,19 @@ module PuppetTest
return comp
end
+
+ def setme
+ # retrieve the user name
+ id = %x{id}.chomp
+ if id =~ /uid=\d+\(([^\)]+)\)/
+ @me = $1
+ else
+ puts id
+ end
+ unless defined? @me
+ raise "Could not retrieve user name; 'id' did not work"
+ end
+ end
end
# $Id$
diff --git a/test/providers/parsedmount.rb b/test/providers/parsedmount.rb
new file mode 100755
index 000000000..a4bcb7534
--- /dev/null
+++ b/test/providers/parsedmount.rb
@@ -0,0 +1,213 @@
+#!/usr/bin/env ruby
+
+$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
+
+require 'puppettest'
+require 'puppet'
+require 'facter'
+
+class TestParsedMounts < Test::Unit::TestCase
+ include PuppetTest
+
+ def setup
+ super
+ @provider = Puppet.type(:mount).provider(:parsed)
+
+ @oldfiletype = @provider.filetype
+ end
+
+ def teardown
+ Puppet::FileType.filetype(:ram).clear
+ @provider.filetype = @oldfiletype
+ super
+ end
+
+ def mkmountargs
+ mount = nil
+
+ if defined? @pcount
+ @pcount += 1
+ else
+ @pcount = 1
+ end
+ args = {
+ :path => "/fspuppet%s" % @pcount,
+ :device => "/dev/dsk%s" % @pcount,
+ }
+
+ @provider.fields.each do |field|
+ unless args.include? field
+ args[field] = "fake%s" % @pcount
+ end
+ end
+
+ return args
+ end
+
+ def mkmount
+ hash = mkmountargs()
+ #hash[:provider] = @provider.name
+
+ fakemodel = fakemodel(:mount, hash[:path])
+
+ mount = @provider.new(fakemodel)
+ #mount = Puppet.type(:mount).create(hash)
+
+ hash.each do |name, val|
+ fakemodel[name] = val
+ end
+ assert(mount, "Could not create provider mount")
+
+ return mount
+ end
+
+ # Here we just create a fake host type that answers to all of the methods
+ # but does not modify our actual system.
+ def mkfaketype
+ @provider.filetype = Puppet::FileType.filetype(:ram)
+ end
+
+ def test_simplemount
+ mkfaketype
+ assert_nothing_raised {
+ assert_equal([], @provider.retrieve)
+ }
+
+ # Now create a provider
+ mount = nil
+ assert_nothing_raised {
+ mount = mkmount
+ }
+
+ # Make sure we're still empty
+ assert_nothing_raised {
+ assert_equal([], @provider.retrieve)
+ }
+
+ hash = mount.model.to_hash
+
+ # Try storing it
+ assert_nothing_raised do
+ mount.store(hash)
+ end
+
+ # Make sure we get the mount back
+ assert_nothing_raised {
+ assert_equal([hash], @provider.retrieve)
+ }
+
+ # Now remove the whole object
+ assert_nothing_raised {
+ mount.store({})
+ assert_equal([], @provider.retrieve)
+ }
+ end
+
+ unless Facter["operatingsystem"].value == "Darwin"
+ def test_mountsparse
+ fakedataparse(fake_fstab) do
+ # Now just make we've got some mounts we know will be there
+ hashes = @provider.retrieve.find_all { |i| i.is_a? Hash }
+ assert(hashes.length > 0, "Did not create any hashes")
+ root = hashes.find { |i| i[:path] == "/" }
+ assert(root, "Could not retrieve root mount")
+ end
+ end
+
+ def test_rootfs
+ fs = nil
+ @provider.path = fake_fstab()
+ fakemodel = fakemodel(:mount, "/")
+ mount = @provider.new(fakemodel)
+ mount.model[:path] = "/"
+ assert(mount.hash, "Could not retrieve root fs")
+
+ assert_nothing_raised {
+ assert(mount.mounted?, "Root is considered not mounted")
+ }
+ end
+ end
+
+ if Puppet::SUIDManager.uid == 0
+ def test_mountfs
+ fs = nil
+ case Facter["hostname"].value
+ when "culain": fs = "/ubuntu"
+ when "atalanta": fs = "/mnt"
+ when "figurehead": fs = "/cg4/net/depts"
+ else
+ $stderr.puts "No mount for mount testing; skipping"
+ return
+ end
+
+ oldtext = @provider.fileobj.read
+
+ ftype = @provider.filetype
+
+ # Make sure the original gets reinstalled.
+ if ftype == Puppet::FileType.filetype(:netinfo)
+ cleanup do
+ IO.popen("niload -r /mounts .", "w") do |file|
+ file.puts oldtext
+ end
+ end
+ else
+ cleanup do
+ @provider.fileobj.write(oldtext)
+ end
+ end
+
+ fakemodel = fakemodel(:mount, "/")
+ obj = @provider.new(fakemodel)
+ obj.model[:path] = fs
+
+ current = nil
+
+ assert_nothing_raised {
+ current = obj.mounted?
+ }
+
+ if current
+ # Make sure the original gets reinstalled.
+ cleanup do
+ unless obj.mounted?
+ obj.mount
+ end
+ end
+ end
+
+ unless current
+ assert_nothing_raised {
+ obj.mount
+ }
+ end
+
+ assert_nothing_raised {
+ obj.unmount
+ }
+ assert(! obj.mounted?, "FS still mounted")
+ assert_nothing_raised {
+ obj.mount
+ }
+ assert(obj.mounted?, "FS not mounted")
+
+ end
+ end
+
+ def fake_fstab
+ os = Facter['operatingsystem']
+ if os == "Solaris"
+ name = "solaris.fstab"
+ elsif os == "FreeBSD"
+ name = "freebsd.fstab"
+ else
+ # Catchall for other fstabs
+ name = "linux.fstab"
+ end
+ oldpath = @provider.path
+ cleanup do @provider.path = oldpath end
+ return fakefile(File::join("data/types/mount", name))
+ end
+end
+
+# $Id$
diff --git a/test/types/mount.rb b/test/types/mount.rb
index 302e2376a..aab80506d 100755
--- a/test/types/mount.rb
+++ b/test/types/mount.rb
@@ -2,43 +2,64 @@
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
-# Test host job creation, modification, and destruction
-
require 'puppettest'
require 'puppet'
-require 'puppet/type/parsedtype/mount'
-require 'facter'
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 create
+ @ensure = :present
+ end
+
+ def delete
+ @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
- @mounttype = Puppet.type(:mount)
- @oldfiletype = @mounttype.filetype
+ @realprovider = Puppet::Type.type(:mount).defaultprovider
+ Puppet::Type.type(:mount).defaultprovider = FakeMountProvider
end
def teardown
- @mounttype.filetype = @oldfiletype
- Puppet.type(:file).clear
+ Puppet.type(:mount).clear
+ Puppet::Type.type(:mount).defaultprovider = nil
super
end
- # Here we just create a fake host type that answers to all of the methods
- # but does not modify our actual system.
- def mkfaketype
- pfile = tempfile()
- old = @mounttype.filetype
- @mounttype.filetype = Puppet::FileType.filetype(:ram)
-
- cleanup do
- @mounttype.filetype = old
- @mounttype.fileobj = nil
- end
-
- # Reset this, just in case
- @mounttype.fileobj = nil
- end
-
def mkmount
mount = nil
@@ -52,7 +73,7 @@ class TestMounts < Test::Unit::TestCase
:device => "/dev/dsk%s" % @pcount,
}
- Puppet.type(:mount).fields.each do |field|
+ @realprovider.fields.each do |field|
unless args.include? field
args[field] = "fake%s" % @pcount
end
@@ -66,178 +87,47 @@ class TestMounts < Test::Unit::TestCase
end
def test_simplemount
- mkfaketype
- host = nil
- assert_nothing_raised {
- assert_nil(Puppet.type(:mount).retrieve)
- }
-
- mount = mkmount
-
+ mount = nil
+ oldprv = Puppet.type(:mount).defaultprovider
+ Puppet.type(:mount).defaultprovider = nil
assert_nothing_raised {
- Puppet.type(:mount).store
- }
+ Puppet.type(:mount).defaultprovider.retrieve
- assert_nothing_raised {
- assert(
- Puppet.type(:mount).to_file.include?(
- Puppet.type(:mount).fileobj.read
- ),
- "File does not include all of our objects"
- )
- }
- end
-
- unless Facter["operatingsystem"].value == "Darwin"
- def test_mountsparse
- use_fake_fstab
- assert_nothing_raised {
- @mounttype.retrieve
- }
-
- # Now just make we've got some mounts we know will be there
- root = @mounttype["/"]
- assert(root, "Could not retrieve root mount")
- end
-
- def test_rootfs
- fs = nil
- use_fake_fstab
- assert_nothing_raised {
- Puppet.type(:mount).retrieve
- }
-
- assert_nothing_raised {
- fs = Puppet.type(:mount)["/"]
- }
- assert(fs, "Could not retrieve root fs")
-
- assert_nothing_raised {
- assert(fs.mounted?, "Root is considered not mounted")
- }
- end
- end
+ count = 0
+ Puppet.type(:mount).each do |h|
+ count += 1
+ end
- # Make sure it reads and writes correctly.
- def test_readwrite
- use_fake_fstab
- assert_nothing_raised {
- Puppet::Type.type(:mount).retrieve
+ assert_equal(0, count, "Found mounts in empty file somehow")
}
+ Puppet.type(:mount).defaultprovider = oldprv
- oldtype = Puppet::Type.type(:mount).filetype
-
- # Now switch to storing in ram
- mkfaketype
-
- fs = mkmount
-
- assert(Puppet::Type.type(:mount).filetype != oldtype)
-
- assert_events([:mount_created], fs)
-
- text = Puppet::Type.type(:mount).fileobj.read
-
- assert(text =~ /#{fs[:path]}/, "Text did not include new fs")
-
- fs[:ensure] = :absent
-
- assert_events([:mount_removed], fs)
- text = Puppet::Type.type(:mount).fileobj.read
-
- assert(text !~ /#{fs[:path]}/, "Text still includes new fs")
-
- fs[:ensure] = :present
-
- assert_events([:mount_created], fs)
-
- text = Puppet::Type.type(:mount).fileobj.read
+ mount = mkmount
- assert(text =~ /#{fs[:path]}/, "Text did not include new fs")
+ assert_apply(mount)
- fs[:options] = "rw,noauto"
+ assert_nothing_raised { mount.retrieve }
- assert_events([:mount_changed], fs)
+ assert_equal(:mounted, mount.is(:ensure))
end
- if Puppet::SUIDManager.uid == 0
+ # 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
- fs = nil
- case Facter["hostname"].value
- when "culain": fs = "/ubuntu"
- when "atalanta": fs = "/mnt"
- when "figurehead": fs = "/cg4/net/depts"
- else
- $stderr.puts "No mount for mount testing; skipping"
- return
- end
-
- assert_nothing_raised {
- Puppet.type(:mount).retrieve
- }
-
- oldtext = Puppet::Type.type(:mount).fileobj.read
-
- ftype = Puppet::Type.type(:mount).filetype
-
- # Make sure the original gets reinstalled.
- if ftype == Puppet::FileType.filetype(:netinfo)
- cleanup do
- IO.popen("niload -r /mounts .", "w") do |file|
- file.puts oldtext
- end
- end
- else
- cleanup do
- Puppet::Type.type(:mount).fileobj.write(oldtext)
- end
- end
-
- obj = Puppet.type(:mount)[fs]
+ obj = mkmount
- assert(obj, "Could not retrieve %s object" % fs)
-
- current = nil
-
- assert_nothing_raised {
- current = obj.mounted?
- }
-
- if current
- # Make sure the original gets reinstalled.
- cleanup do
- unless obj.mounted?
- obj.mount
- end
- end
- end
-
- unless current
- assert_nothing_raised {
- obj.mount
- }
- end
-
- # Now copy all of the states' "is" values to the "should" values
- obj.each do |state|
- state.should = state.is
- end
+ assert_apply(obj)
# Verify we can remove the mount
assert_nothing_raised {
obj[:ensure] = :absent
}
- assert_events([:mount_removed], obj)
+ assert_events([:mount_deleted], obj)
assert_events([], obj)
# And verify it's gone
- assert(!obj.mounted?, "Object is mounted after being removed")
-
- text = Puppet.type(:mount).fileobj.read
-
- assert(text !~ /#{fs}/,
- "Fstab still contains %s" % fs)
+ assert(!obj.provider.mounted?, "Object is mounted after being removed")
assert_nothing_raised {
obj[:ensure] = :present
@@ -246,10 +136,7 @@ class TestMounts < Test::Unit::TestCase
assert_events([:mount_created], obj)
assert_events([], obj)
- text = Puppet::Type.type(:mount).fileobj.read
- assert(text =~ /#{fs}/, "Fstab does not contain %s" % fs)
-
- assert(! obj.mounted?, "Object is mounted incorrectly")
+ assert(! obj.provider.mounted?, "Object is mounted incorrectly")
assert_nothing_raised {
obj[:ensure] = :mounted
@@ -258,33 +145,8 @@ class TestMounts < Test::Unit::TestCase
assert_events([:mount_mounted], obj)
assert_events([], obj)
- text = Puppet::Type.type(:mount).fileobj.read
- assert(text =~ /#{fs}/,
- "Fstab does not contain %s" % fs)
-
obj.retrieve
- assert(obj.mounted?, "Object is not mounted")
-
- unless current
- assert_nothing_raised {
- obj.unmount
- }
- end
- end
- end
-
- def use_fake_fstab
- os = Facter['operatingsystem']
- if os == "Solaris"
- name = "solaris.fstab"
- elsif os == "FreeBSD"
- name = "freebsd.fstab"
- else
- # Catchall for other fstabs
- name = "linux.fstab"
- end
- fstab = fakefile(File::join("data/types/mount", name))
- Puppet::Type.type(:mount).path = fstab
+ assert(obj.provider.mounted?, "Object is not mounted")
end
end