3
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
7
require 'puppettest/parsertesting'
8
require 'puppettest/resourcetesting'
9
require 'puppettest/railstesting'
11
class TestCollector < Test::Unit::TestCase
13
include PuppetTest::ParserTesting
14
include PuppetTest::ResourceTesting
15
include PuppetTest::RailsTesting
16
Parser = Puppet::Parser
21
Puppet[:trace] = false
22
@interp, @scope, @source = mkclassframing
25
# Test just collecting a specific resource. This is used by the 'realize'
26
# function, and it's much faster than iterating over all of the resources.
27
def test_collect_resource
28
# Make a couple of virtual resources
29
one = mkresource(:type => "file", :title => "/tmp/virtual1",
30
:virtual => true, :params => {:owner => "root"})
31
two = mkresource(:type => "file", :title => "/tmp/virtual2",
32
:virtual => true, :params => {:owner => "root"})
33
@scope.setresource one
34
@scope.setresource two
36
# Now make a collector
38
assert_nothing_raised do
39
coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :virtual)
42
# Now set the resource in the collector
43
assert_nothing_raised do
44
coll.resources = one.ref
47
# Now run the collector
48
assert_nothing_raised do
52
# And make sure the resource is no longer virtual
53
assert(! one.virtual?,
54
"Resource is still virtual")
56
# But the other still is
58
"Resource got realized")
62
# Make a virtual resource
63
virtual = mkresource(:type => "file", :title => "/tmp/virtual",
64
:virtual => true, :params => {:owner => "root"})
65
@scope.setresource virtual
68
real = mkresource(:type => "file", :title => "/tmp/real",
69
:params => {:owner => "root"})
70
@scope.setresource real
72
# Now make a collector
79
assert_nothing_raised do
80
coll = Puppet::Parser::Collector.new(@scope, "file", nil, code, :virtual)
84
@scope.newcollection(coll)
86
# Make sure it's in the collections
87
assert_equal([coll], @scope.collections)
89
# And try to collect the virtual resources.
91
assert_nothing_raised do
92
ret = coll.collect_virtual
95
assert_equal([virtual], ret)
97
# Now make sure evaluate does the right thing.
98
assert_nothing_raised do
102
# Make sure it got deleted from the collection list
103
assert_equal([], @scope.collections)
105
# And make sure our virtual object is no longer virtual
106
assert(! virtual.virtual?, "Virtual object did not get realized")
108
# Now make a new collector of a different type and make sure it
110
assert_nothing_raised do
111
coll = Puppet::Parser::Collector.new(@scope, "exec", nil, nil, :virtual)
114
# Remark this as virtual
115
virtual.virtual = true
117
assert_nothing_raised do
121
assert_equal([], ret)
124
if defined? ActiveRecord::Base
125
def test_collect_exported
127
# make an exported resource
128
exported = mkresource(:type => "file", :title => "/tmp/exported",
129
:exported => true, :params => {:owner => "root"})
130
@scope.setresource exported
132
assert(exported.exported?, "Object was not marked exported")
133
assert(exported.virtual?, "Object was not marked virtual")
136
real = mkresource(:type => "file", :title => "/tmp/real",
137
:params => {:owner => "root"})
138
@scope.setresource real
140
# Now make a collector
142
assert_nothing_raised do
143
coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :exported)
146
# Set it in our scope
147
@scope.newcollection(coll)
149
# Make sure it's in the collections
150
assert_equal([coll], @scope.collections)
152
# And try to collect the virtual resources.
154
assert_nothing_raised do
155
ret = coll.collect_exported
158
assert_equal([exported], ret)
160
# Now make sure evaluate does the right thing.
161
assert_nothing_raised do
165
# Make sure it got deleted from the collection list
166
assert_equal([], @scope.collections)
168
# And make sure our exported object is no longer exported
169
assert(! exported.virtual?, "Virtual object did not get realized")
171
# But it should still be marked exported.
172
assert(exported.exported?, "Resource got un-exported")
174
# Now make a new collector of a different type and make sure it
176
assert_nothing_raised do
177
coll = Puppet::Parser::Collector.new(@scope, "exec", nil, nil, :exported)
180
# Remark this as virtual
181
exported.virtual = true
183
assert_nothing_raised do
187
assert_equal([], ret)
190
def test_collection_conflicts
193
# First make a railshost we can conflict with
194
host = Puppet::Rails::Host.new(:name => "myhost")
196
host.rails_resources.build(:title => "/tmp/conflicttest", :restype => "file",
201
# Now make a normal resource
202
normal = mkresource(:type => "file", :title => "/tmp/conflicttest",
203
:params => {:owner => "root"})
204
@scope.setresource normal
206
# Now make a collector
208
assert_nothing_raised do
209
coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :exported)
212
# And try to collect the virtual resources.
213
assert_raise(Puppet::ParseError) do
214
ret = coll.collect_exported
218
# Make sure we do not collect resources from the host we're on
219
def test_no_resources_from_me
222
# Make our configuration
223
host = Puppet::Rails::Host.new(:name => "myhost")
225
host.rails_resources.build(:title => "/tmp/hosttest", :restype => "file",
230
@scope.host = "myhost"
232
# Now make a collector
234
assert_nothing_raised do
235
coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :exported)
238
# And make sure we get nada back
240
assert_nothing_raised do
241
ret = coll.collect_exported
244
assert(ret.empty?, "Found exports from our own host")
249
# $Id: collector.rb 1810 2006-10-18 06:01:18Z luke $