1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#!/usr/bin/env ruby
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppet'
require 'puppet/rails'
require 'puppettest'
require 'puppettest/railstesting'
require 'puppettest/resourcetesting'
# Don't do any tests w/out this class
if Puppet.features.rails?
class TestRailsResource < Test::Unit::TestCase
include PuppetTest::RailsTesting
include PuppetTest::ResourceTesting
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")
# Now build a resource
resource = host.resources.create(
:title => "/tmp/to_resource",
:restype => "file",
:exported => true)
# Now add some params
params.each do |param, value|
pn = Puppet::Rails::ParamName.find_or_create_by_name(param)
pv = resource.param_values.create(:value => value,
:param_name => pn)
end
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
# Find the new resource and include all it's parameters.
resource = Puppet::Rails::Resource.find_by_id(resource.id)
# Now, try to convert our resource to a real resource
res = nil
assert_nothing_raised do
res = resource.to_resource(scope)
end
assert_instance_of(Puppet::Parser::Resource, res)
assert_equal("root", res[:owner])
assert_equal("644", res[:mode])
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.inject({}) { |h, a|
h[a[0]] = a[1][0]
h
}
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"
end
# A separate class for testing rails integration
class TestExportedResources < PuppetTest::TestCase
include PuppetTest
include PuppetTest::ParserTesting
include PuppetTest::ResourceTesting
include PuppetTest::RailsTesting
Parser = Puppet::Parser
AST = Parser::AST
Reference = Puppet::Parser::Resource::Reference
def setup
super
Puppet[:trace] = false
@interp, @scope, @source = mkclassframing
end
confine "Missing rails support" => Puppet.features.rails?
# Compare a parser resource to a rails resource.
def compare_resources(host, res, updating, options = {})
newobj = nil
# If the resource is in the db, then use modify_rails, else use to_rails
if newobj = Puppet::Rails::Resource.find_by_restype_and_title(res.type, res.title)
assert_nothing_raised("Call to modify_rails failed") do
res.modify_rails(newobj)
end
else
assert_nothing_raised("Call to to_rails failed") do
newobj = res.to_rails(host)
end
end
assert_instance_of(Puppet::Rails::Resource, newobj)
newobj.save
if updating
tail = "on update"
else
tail = ""
end
# Make sure we find our object and only our object
count = 0
obj = nil
Puppet::Rails::Resource.find(:all).each do |obj|
assert_equal(newobj.id, obj.id, "A new resource was created instead of modifying an existing resource")
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 was not set correctly in rails %s" % [param, tail])
end
end
assert_equal(1, count, "Got too many resources %s" % tail)
# Now make sure we can find it again
assert_nothing_raised do
obj = Puppet::Rails::Resource.find_by_restype_and_title(
res.type, res.title, :include => :param_names
)
end
assert_instance_of(Puppet::Rails::Resource, obj)
# Make sure we get the parameters back
params = options[: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 %s" % [name, tail])
else
assert(! param, "resource did not delete %s %s" % [name, tail])
end
if param
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 %s" % [param.name, tail])
end
end
end
def test_to_rails
railsteardown
railsinit
ref1 = Reference.new :type => "exec", :title => "one"
ref2 = Reference.new :type => "exec", :title => "two"
res = mkresource :type => "file", :title => "/tmp/testing",
:source => @source, :scope => @scope,
:params => {:owner => "root", :source => ["/tmp/A", "/tmp/B"],
:mode => "755", :require => [ref1, ref2], :subscribe => ref1}
res.line = 50
# We also need a Rails Host to store under
host = Puppet::Rails::Host.new(:name => Facter.hostname)
compare_resources(host, res, false, :params => %w{owner source mode})
# Now make sure our parameters did not change
assert_instance_of(Array, res[:require], "Parameter array changed")
res[:require].each do |ref|
assert_instance_of(Reference, ref, "Resource reference changed")
end
# Now make some changes to our resource. We're removing the mode,
# changing the source, and adding 'check'.
res = mkresource :type => "file", :title => "/tmp/testing",
:source => @source, :scope => @scope,
:params => {:owner => "bin", :source => ["/tmp/A", "/tmp/C"],
:check => "checksum", :require => [ref1, ref2], :subscribe => ref2}
res.line = 75
res.exported = true
compare_resources(host, res, true, :params => %w{owner source mode check})
# Again make sure our parameters did not change
assert_instance_of(Array, res[:require], "Parameter array changed")
res[:require].each do |ref|
assert_instance_of(Reference, ref, "Resource reference changed")
end
end
end
# $Id$
|