summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-12 21:33:54 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-12 21:33:54 +0000
commit8829aa7d489fffaba31c0ff8ac4ff0562357551c (patch)
tree99fde22b54d529f04a8fa66520601da9e07a77f4
parent4abbdc13f4a1762eb5d848763dde1780f6408de8 (diff)
downloadpuppet-8829aa7d489fffaba31c0ff8ac4ff0562357551c.tar.gz
puppet-8829aa7d489fffaba31c0ff8ac4ff0562357551c.tar.xz
puppet-8829aa7d489fffaba31c0ff8ac4ff0562357551c.zip
Adding a metatype to manage resources of a specified type. For now, this metatype just supports purging unmanaged resources. Also, fixed a few tests here and there
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1912 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--lib/puppet/pgraph.rb4
-rw-r--r--lib/puppet/type/resources.rb68
-rwxr-xr-xtest/other/dsl.rb1
-rw-r--r--test/other/pgraph.rb4
-rwxr-xr-xtest/other/transactions.rb3
-rwxr-xr-xtest/types/resources.rb129
-rwxr-xr-xtest/util/graph.rb4
7 files changed, 203 insertions, 10 deletions
diff --git a/lib/puppet/pgraph.rb b/lib/puppet/pgraph.rb
index d988ff36a..a8ff952ed 100644
--- a/lib/puppet/pgraph.rb
+++ b/lib/puppet/pgraph.rb
@@ -111,9 +111,9 @@ class Puppet::PGraph < GRATR::Digraph
# For some reason, unconnected vertices do not show up in
# this graph.
- def to_jpg(name)
+ def to_jpg(path, name)
gv = vertices()
- Dir.chdir("/Users/luke/Desktop/pics") do
+ Dir.chdir(path) do
induced_subgraph(gv).write_to_graphic_file('jpg', name)
end
end
diff --git a/lib/puppet/type/resources.rb b/lib/puppet/type/resources.rb
new file mode 100644
index 000000000..7e81a08b3
--- /dev/null
+++ b/lib/puppet/type/resources.rb
@@ -0,0 +1,68 @@
+# Created by Luke Kanies on 2006-12-12.
+# Copyright (c) 2006. All rights reserved.
+
+require 'puppet'
+
+Puppet::Type.newtype(:resources) do
+ @doc = "This is a metatype that can manage other resource types. Any
+ metaparams specified here will be passed on to any generated resources,
+ so you can purge umanaged resources but set ``noop`` to true so the
+ purging is only logged and does not actually happen."
+
+
+ newparam(:name) do
+ desc "The name of the type to be managed."
+
+ validate do |name|
+ unless Puppet::Type.type(name)
+ raise ArgumentError, "Could not find resource type '%s'" % name
+ end
+ end
+ end
+
+ newparam(:purge, :boolean => true) do
+ desc "Purge unmanaged resources. This will delete any resource
+ that is not specified in your configuration
+ and is not required by any specified resources."
+
+ newvalues(:true, :false)
+
+ validate do |value|
+ if [:true, true, "true"].include?(value)
+ unless @parent.resource_type.respond_to?(:list)
+ raise ArgumentError, "Purging resources of type %s is not supported" % @parent[:name]
+ end
+ unless @parent.resource_type.validstate?(:ensure)
+ raise ArgumentError, "Purging is only supported on types that accept 'ensure'"
+ end
+ end
+ end
+ end
+
+ # Generate any new resources we need to manage.
+ def generate
+ resource_type.list.find_all do |resource|
+ ! resource.managed?
+ end.each do |resource|
+ begin
+ resource[:ensure] = :absent
+ rescue ArgumentError, Parse::Error => detail
+ err "The 'ensure' attribute on %s resources does not accept 'absent' as a value" %
+ [self[:name]]
+ return []
+ end
+ end
+ end
+
+ def resource_type
+ unless defined? @resource_type
+ unless type = Puppet::Type.type(self[:name])
+ raise Puppet::DevError, "Could not find resource type"
+ end
+ @resource_type = type
+ end
+ @resource_type
+ end
+end
+
+# $Id$ \ No newline at end of file
diff --git a/test/other/dsl.rb b/test/other/dsl.rb
index 50eac4781..b36cca745 100755
--- a/test/other/dsl.rb
+++ b/test/other/dsl.rb
@@ -200,6 +200,7 @@ class TestDSL < Test::Unit::TestCase
a = aspect :testing
Puppet::Type.eachtype do |type|
+ next if type.name.to_s =~ /test/
assert(a.respond_to?(type.name),
"Aspects do not have a %s method" % type.name)
end
diff --git a/test/other/pgraph.rb b/test/other/pgraph.rb
index 88f753131..1ef853cc4 100644
--- a/test/other/pgraph.rb
+++ b/test/other/pgraph.rb
@@ -88,8 +88,6 @@ class TestPGraph < Test::Unit::TestCase
deps.add_edge!(source, target, :callback => :refresh)
end
- deps.to_jpg("deps-before")
-
deps.splice!(contgraph, Container)
assert(! deps.cyclic?, "Created a cyclic graph")
@@ -125,4 +123,4 @@ class TestPGraph < Test::Unit::TestCase
end
end
-# $Id$ \ No newline at end of file
+# $Id$
diff --git a/test/other/transactions.rb b/test/other/transactions.rb
index 67a2daea9..4190ada3a 100755
--- a/test/other/transactions.rb
+++ b/test/other/transactions.rb
@@ -559,7 +559,6 @@ class TestTransactions < Test::Unit::TestCase
file = f(letter)
assert(reverse.vertex?(file), "%s did not make it into reversal" % letter)
end
- graph.to_jpg("normal_relations")
end
# Test pre-evaluation generation
@@ -769,4 +768,4 @@ class TestTransactions < Test::Unit::TestCase
end
end
-# $Id$ \ No newline at end of file
+# $Id$
diff --git a/test/types/resources.rb b/test/types/resources.rb
new file mode 100755
index 000000000..f8a2f6640
--- /dev/null
+++ b/test/types/resources.rb
@@ -0,0 +1,129 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke Kanies on 2006-12-12.
+# Copyright (c) 2006. All rights reserved.
+
+$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
+
+require 'puppettest'
+
+class TestResources < Test::Unit::TestCase
+ include PuppetTest
+
+ def add_purge_lister
+ # Now define the list method
+ class << @purgetype
+ def list
+ $purgemembers.values
+ end
+ end
+ end
+
+ def mk_purger(managed = false)
+ @purgenum ||= 0
+ @purgenum += 1
+ obj = @purgetype.create :name => "purger%s" % @purgenum
+ $purgemembers[obj[:name]] = obj
+ if managed
+ obj[:fake] = "testing"
+ end
+ obj
+ end
+
+ def mkpurgertype
+ # Create a purgeable type
+ $purgemembers = {}
+ @purgetype = Puppet::Type.newtype(:purgetest) do
+ newparam(:name, :namevar => true) {}
+ newstate(:ensure) do
+ newvalue(:absent) do
+ $purgemembers[@parent[:name]] = @parent
+ end
+ newvalue(:present) do
+ $purgemembers.delete(@parent[:name])
+ end
+ end
+ newstate(:fake) do
+ def sync
+ :faked
+ end
+ end
+ end
+ cleanup do
+ Puppet::Type.rmtype(:purgetest)
+ end
+ end
+
+ def setup
+ super
+ @type = Puppet::Type.type(:resources)
+ end
+
+ def test_initialize
+ assert(@type, "Could not retrieve resources type")
+ # Make sure we can't create them for types that don't exist
+ assert_raise(ArgumentError) do
+ @type.create :name => "thereisnotypewiththisname"
+ end
+
+ # Now make sure it works for a normal type
+ usertype = nil
+ assert_nothing_raised do
+ usertype = @type.create :name => "user"
+ end
+ assert(usertype, "did not create user resource type")
+ assert_equal(Puppet::Type.type(:user), usertype.resource_type,
+ "resource_type was not set correctly")
+ end
+
+ def test_purge
+ # Create a purgeable type
+ mkpurgertype
+
+ purger = nil
+ assert_nothing_raised do
+ purger = @type.create :name => "purgetest"
+ end
+ assert(purger, "did not get purger manager")
+
+ # Make sure we throw an error, because the purger type does
+ # not support listing.
+
+ # It should work when we set it to false
+ assert_nothing_raised do
+ purger[:purge] = false
+ end
+ # but not true
+ assert_raise(ArgumentError) do
+ purger[:purge] = true
+ end
+ add_purge_lister()
+
+ assert_equal($purgemembers.values.sort, @purgetype.list.sort)
+
+ # and it should now succeed
+ assert_nothing_raised do
+ purger[:purge] = true
+ end
+ assert(purger.purge?, "purge boolean was not enabled")
+
+ # Okay, now let's try doing some purging, yo
+ managed = []
+ unmanned = []
+ 3.times { managed << mk_purger(true) } # 3 managed
+ 3.times { unmanned << mk_purger(false) } # 3 unmanaged
+
+ managed.each do |m|
+ assert(m.managed?, "managed resource was not considered managed")
+ end
+ unmanned.each do |u|
+ assert(! u.managed?, "unmanaged resource was considered managed")
+ end
+
+ # Now make sure the generate method only finds the unmanaged resources
+ assert_equal(unmanned.collect { |r| r.title }.sort, purger.generate.collect { |r| r.title },
+ "Did not return correct purge list")
+ end
+end
+
+# $Id$ \ No newline at end of file
diff --git a/test/util/graph.rb b/test/util/graph.rb
index 1df294c77..7d43ed6f9 100755
--- a/test/util/graph.rb
+++ b/test/util/graph.rb
@@ -51,8 +51,6 @@ class TestUtilGraph < Test::Unit::TestCase
end
end
- graph.to_jpg("graph")
-
# Now make sure we correctly retrieve the leaves from each container
{top => %w{a b c d e f g h},
one => %w{a b},
@@ -106,4 +104,4 @@ class TestUtilGraph < Test::Unit::TestCase
end
end
-# $Id$ \ No newline at end of file
+# $Id$