summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-19 02:08:11 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-12-19 02:08:11 +0000
commitdc5f4dc0d01dc2ccb4679afbf3802a7ab0f3c126 (patch)
tree6082433e05ad445aa4323a05de7d5820c5c023d6 /test
parent5a52855c1da2cb4716587bf0223c6d20eddaf00a (diff)
Fixing most of the rails stuff. I think everything basically works now, and now I am just going through and making sure things get deleted when they are supposed (i.e., you remove a resource and it gets deleted from the host's config).
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1950 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
-rwxr-xr-xtest/language/collector.rb38
-rwxr-xr-xtest/language/resource.rb77
-rw-r--r--test/lib/puppettest/railstesting.rb3
-rwxr-xr-xtest/rails/rails.rb46
-rwxr-xr-xtest/rails/railsresource.rb56
5 files changed, 162 insertions, 58 deletions
diff --git a/test/language/collector.rb b/test/language/collector.rb
index 0292fac3e..41ce12f66 100755
--- a/test/language/collector.rb
+++ b/test/language/collector.rb
@@ -121,9 +121,13 @@ class TestCollector < Test::Unit::TestCase
assert_equal([], ret)
end
- if defined? ActiveRecord::Base
+ if Puppet.features.rails?
def test_collect_exported
railsinit
+
+ # Set a hostname
+ @scope.host = Facter.value(:hostname)
+
# make an exported resource
exported = mkresource(:type => "file", :title => "/tmp/exported",
:exported => true, :params => {:owner => "root"})
@@ -189,26 +193,27 @@ class TestCollector < Test::Unit::TestCase
# Now create a whole new scope and make sure we can actually retrieve
# the resource from the database, not just from the scope.
# First create a host object and store our resource in it.
- # Now collect our facts
- facts = {}
- Facter.each do |fact, value| facts[fact] = value end
-
-
- # Now try storing our crap
- resources = []
- resources << exported
- host = Puppet::Rails::Host.store(
- :resources => resources,
- :facts => facts,
- :name => facts["hostname"]
- )
+
+ # Now collect our facts
+ facts = {}
+ Facter.each do |fact, value| facts[fact] = value end
+
+ # Now try storing our crap
+ # Remark this as exported
+ exported.exported = true
+ host = Puppet::Rails::Host.store(
+ :resources => [exported],
+ :facts => facts,
+ :name => facts["hostname"]
+ )
assert(host, "did not get rails host")
host.save
# And make sure it's in there
- newres = host.resources.find_by_title("/tmp/exported")
+ newres = host.resources.find_by_restype_and_title_and_exported("file", "/tmp/exported", true)
assert(newres, "Did not find resource in db")
interp, scope, source = mkclassframing
+ scope.host = "two"
# Now make a collector
coll = nil
@@ -246,7 +251,7 @@ class TestCollector < Test::Unit::TestCase
# First make a railshost we can conflict with
host = Puppet::Rails::Host.new(:name => "myhost")
- host.resources.build(:title => "/tmp/conflicttest", :type => "PuppetFile",
+ host.resources.build(:title => "/tmp/conflicttest", :restype => "file",
:exported => true)
host.save
@@ -255,6 +260,7 @@ class TestCollector < Test::Unit::TestCase
normal = mkresource(:type => "file", :title => "/tmp/conflicttest",
:params => {:owner => "root"})
@scope.setresource normal
+ @scope.host = "otherhost"
# Now make a collector
coll = nil
diff --git a/test/language/resource.rb b/test/language/resource.rb
index eb8d2e9aa..872d7b39f 100755
--- a/test/language/resource.rb
+++ b/test/language/resource.rb
@@ -360,19 +360,13 @@ class TestResource < Test::Unit::TestCase
assert_nil(ref.builtintype, "Component was considered builtin")
end
- if defined? ActiveRecord::Base
- def test_store
- railsinit
- res = mkresource :type => "file", :title => "/tmp/testing",
- :source => @source, :scope => @scope,
- :params => {:owner => "root", :mode => "755"}
-
- # We also need a Rails Host to store under
- host = Puppet::Rails::Host.new(:name => Facter.hostname)
+ if Puppet.features.rails?
+ # Compare a parser resource to a rails resource.
+ def compare_resources(host, res)
obj = nil
assert_nothing_raised do
- obj = res.store(host)
+ obj = res.to_rails(host)
end
assert_instance_of(Puppet::Rails::Resource, obj)
@@ -383,20 +377,73 @@ class TestResource < Test::Unit::TestCase
end
end
+ # Make sure we find our object and only our object
+ count = 0
+ Puppet::Rails::Resource.find(:all).each do |obj|
+ count += 1
+ [:title, :restype, :line, :exported].each do |param|
+ if param == :restype
+ method = :type
+ else
+ method = param
+ end
+ assert_equal(res.send(method), obj[param],
+ "attribute %s is incorrect" % param)
+ end
+ end
+ assert_equal(1, count, "Got too many resources")
# Now make sure we can find it again
assert_nothing_raised do
- obj = Puppet::Rails::Resource.find_by_host_id_and_title(
- host.id, res.title
+ obj = Puppet::Rails::Resource.find_by_restype_and_title(
+ res.type, res.title
)
end
assert_instance_of(Puppet::Rails::Resource, obj)
# Make sure we get the parameters back
- obj.parameters.each do |param|
- assert_equal(res[param[:name]], param[:value],
- "%s was different" % param[:name])
+ params = [obj.param_names.collect { |p| p.name },
+ res.to_hash.keys].flatten.collect { |n| n.to_s }.uniq
+
+ params.each do |name|
+ param = obj.param_names.find_by_name(name)
+ if res[name]
+ assert(param, "resource did not keep %s" % name)
+ else
+ assert(! param, "resource did not delete %s" % name)
+ end
+ values = param.param_values.collect { |pv| pv.value }
+ should = res[param.name]
+ should = [should] unless should.is_a?(Array)
+ assert_equal(should, values,
+ "%s was different" % param.name)
end
end
+
+ def test_to_rails
+ railsinit
+ res = mkresource :type => "file", :title => "/tmp/testing",
+ :source => @source, :scope => @scope,
+ :params => {:owner => "root", :source => ["/tmp/A", "/tmp/B"],
+ :mode => "755"}
+
+ res.line = 50
+
+ # We also need a Rails Host to store under
+ host = Puppet::Rails::Host.new(:name => Facter.hostname)
+
+ compare_resources(host, res)
+
+ # Now make some changes to our resource.
+ res = mkresource :type => "file", :title => "/tmp/testing",
+ :source => @source, :scope => @scope,
+ :params => {:owner => "bin", :source => ["/tmp/A", "/tmp/C"],
+ :check => "checksum"}
+
+ res.line = 75
+ res.exported = true
+
+ compare_resources(host, res)
+ end
end
end
diff --git a/test/lib/puppettest/railstesting.rb b/test/lib/puppettest/railstesting.rb
index 1d2d94863..e2ce7ef3f 100644
--- a/test/lib/puppettest/railstesting.rb
+++ b/test/lib/puppettest/railstesting.rb
@@ -5,6 +5,9 @@ module PuppetTest::RailsTesting
def railsinit
Puppet::Rails.init
+ cleanup do
+ ActiveRecord::Base.clear_active_connections!
+ end
end
def railsteardown
diff --git a/test/rails/rails.rb b/test/rails/rails.rb
index 7ffeaccac..69e1fd7b8 100755
--- a/test/rails/rails.rb
+++ b/test/rails/rails.rb
@@ -17,26 +17,25 @@ class TestRails < Test::Unit::TestCase
include PuppetTest::ResourceTesting
include PuppetTest::RailsTesting
- def test_includerails
- assert_nothing_raised {
- require 'puppet/rails'
- }
- end
-
- # Don't do any tests w/out this class
- if Puppet.features.rails?
def setup
super
railsinit
end
def teardown
- super
railsteardown
+ super
end
+ def test_includerails
+ assert_nothing_raised {
+ require 'puppet/rails'
+ }
+ end
+
+ # Don't do any tests w/out this class
+ if Puppet.features.rails?
def test_hostcache
- railsinit
@interp, @scope, @source = mkclassframing
# First make some objects
resources = []
@@ -49,11 +48,11 @@ class TestRails < Test::Unit::TestCase
# And an exec, so we're checking multiple types
resources << mkresource(:type => "exec",
:title => "/bin/echo file#{i.to_s}",
- :params => {})
+ :params => {:user => "user#{i}"})
}
# Now collect our facts
- facts = Facter.to_hash
+ facts = {"hostname" => Facter.value(:hostname), "test1" => "funtest"}
# Now try storing our crap
host = nil
@@ -89,13 +88,30 @@ class TestRails < Test::Unit::TestCase
else
raise "Got weird resource %s" % resource.inspect
end
- assert(resource[:type] != "", "Did not get a type from the resource")
- if resource[:type] != "PuppetExec"
- assert_equal("user#{i}", resource.parameters["owner"])
+ assert(resource[:restype] != "", "Did not get a type from the resource")
+ case resource["restype"]
+ when "file":
+ assert_equal("user#{i}", resource.parameter("owner"),
+ "got no owner for %s" % resource.ref)
+ when "exec":
+ assert_equal("user#{i}", resource.parameter("user"),
+ "got no user for %s" % resource.ref)
+ else
+ raise "Unknown type %s" % resource[:restype].inspect
end
end
assert_equal(20, count, "Did not get enough resources")
+
+ host = nil
+ assert_nothing_raised {
+ host = Puppet::Rails::Host.store(
+ :resources => resources,
+ :facts => facts,
+ :name => facts["hostname"],
+ :classes => ["one", "two::three", "four"]
+ )
+ }
end
else
$stderr.puts "Install Rails for Rails and Caching tests"
diff --git a/test/rails/railsresource.rb b/test/rails/railsresource.rb
index 36df881ca..302dd99fb 100755
--- a/test/rails/railsresource.rb
+++ b/test/rails/railsresource.rb
@@ -9,15 +9,22 @@ require 'puppettest/railstesting'
require 'puppettest/resourcetesting'
# Don't do any tests w/out this class
-if defined? ActiveRecord::Base
+if Puppet.features.rails?
class TestRailsResource < Test::Unit::TestCase
include PuppetTest::RailsTesting
include PuppetTest::ResourceTesting
-
- # Create a resource param from a rails parameter
- def test_to_resource
+
+ def setup
+ super
railsinit
-
+ end
+
+ def teardown
+ railsteardown
+ super
+ end
+
+ def mktest_resource
# We need a host for resources
host = Puppet::Rails::Host.new(:name => "myhost")
@@ -26,21 +33,26 @@ class TestRailsResource < Test::Unit::TestCase
:title => "/tmp/to_resource",
:restype => "file",
:exported => true)
-
- # For some reason the child class doesn't exist until after the resource is created.
- # Probably an issue with the dynamic class generation.
- resource.save
-
+
# Now add some params
- {"owner" => "root", "mode" => "644"}.each do |param, value|
+ params.each do |param, value|
pn = resource.param_names.find_or_create_by_name(param)
pv = pn.param_values.find_or_create_by_value(value)
resource.param_names << pn
end
- # Now save the whole thing
host.save
+ return resource
+ end
+
+ def params
+ {"owner" => "root", "mode" => "644"}
+ end
+
+ # Create a resource param from a rails parameter
+ def test_to_resource
+ resource = mktest_resource
# We need a scope
interp, scope, source = mkclassframing
@@ -59,6 +71,26 @@ class TestRailsResource < Test::Unit::TestCase
assert_equal("/tmp/to_resource", res.title)
assert_equal(source, res.source)
end
+
+ def test_parameters
+ resource = mktest_resource
+
+ setparams = nil
+ assert_nothing_raised do
+ setparams = resource.parameters
+ end
+ assert_equal(params, setparams,
+ "Did not get the right answer from #parameters")
+ end
+
+ # Make sure we can retrieve individual parameters by name.
+ def test_parameter
+ resource = mktest_resource
+
+ params.each do |p,v|
+ assert_equal(v, resource.parameter(p), "%s is not correct" % p)
+ end
+ end
end
else
$stderr.puts "Install Rails for Rails and Caching tests"