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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
#!/usr/bin/env ruby
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppettest'
require 'puppettest/fileparsing'
require 'puppet'
require 'puppet/provider/parsedfile'
require 'facter'
class TestParsedFile < Test::Unit::TestCase
include PuppetTest
include PuppetTest::FileParsing
Puppet::Type.newtype(:parsedfiletype) do
newstate(:one) do
newvalue(:a) {}
newvalue(:b) {}
end
newstate(:two) do
newvalue(:c) {}
newvalue(:d) {}
end
newparam(:name) do
end
newparam(:target) do
defaultto { @parent.class.defaultprovider.default_target }
end
end
# A simple block to skip the complexity of a full transaction.
def apply(model)
[:one, :two].each do |st|
model.provider.send(st.to_s + "=", model.should(st))
end
end
def mkmodel(name, options = {})
options[:one] ||= "a"
options[:two] ||= "c"
options[:name] ||= name
model = @type.create(options)
end
def mkprovider(name = :parsed)
@provider = @type.provide(name, :parent => Puppet::Provider::ParsedFile) do
record_line name, :fields => %w{name one two}
end
end
def setup
super
@type = Puppet::Type.type(:parsedfiletype)
end
def teardown
if defined? @provider
@type.unprovide(@provider.name)
@provider = nil
end
super
end
def test_create_provider
assert_nothing_raised do
mkprovider
end
end
def test_model_attributes
prov = nil
assert_nothing_raised do
prov = mkprovider
end
[:one, :two, :name].each do |attr|
assert(prov.method_defined?(attr), "Did not define %s" % attr)
end
# Now make sure they stay around
fakemodel = fakemodel(:parsedfiletype, "yay")
file = prov.new(fakemodel)
assert_nothing_raised do
file.name = :yayness
end
# The provider converts to strings
assert_equal("yayness", file.name)
end
def test_filetype
prov = mkprovider
flat = Puppet::FileType.filetype(:flat)
ram = Puppet::FileType.filetype(:ram)
assert_nothing_raised do
prov.filetype = :flat
end
assert_equal(flat, prov.filetype)
assert_nothing_raised do
prov.filetype = ram
end
assert_equal(ram, prov.filetype)
end
# Make sure we correctly create a new filetype object, but only when
# necessary.
def test_fileobject
prov = mkprovider
path = tempfile()
obj = nil
assert_nothing_raised do
obj = prov.target_object(path)
end
# The default filetype is 'flat'
assert_instance_of(Puppet::FileType.filetype(:flat), obj)
newobj = nil
assert_nothing_raised do
newobj = prov.target_object(path)
end
assert_equal(obj, newobj, "did not reuse file object")
# now make sure clear does the right thing
assert_nothing_raised do
prov.clear
end
assert_nothing_raised do
newobj = prov.target_object(path)
end
assert(obj != newobj, "did not reuse file object")
end
def test_retrieve
prov = mkprovider
prov.filetype = :ram
# Override the parse method with our own
prov.meta_def(:parse) do |text|
return [text]
end
path = :yayness
file = prov.target_object(path)
text = "a test"
file.write(text)
ret = nil
assert_nothing_raised do
ret = prov.retrieve(path)
end
assert_equal([text], ret)
# Now set the text to nil and make sure we get an empty array
file.write(nil)
assert_nothing_raised do
ret = prov.retrieve(path)
end
assert_equal([], ret)
# And the empty string should return an empty array
file.write("")
assert_nothing_raised do
ret = prov.retrieve(path)
end
assert_equal([], ret)
end
# Verify that prefetch will parse the file, create any necessary instances,
# and set the 'is' values appropriately.
def test_prefetch
prov = mkprovider
prov.filetype = :ram
prov.default_target = :default
# Create a couple of demo files
prov.target_object(:file1).write "bill b c"
prov.target_object(:file2).write "jill b d"
prov.target_object(:default).write "will b d"
# Create some models for some of those demo files
model = mkmodel "bill", :target => :file1
default = mkmodel "will", :target => :default
assert_nothing_raised do
prov.prefetch
end
# Make sure we prefetched our models.
assert_equal("b", model.provider.one)
assert_equal("b", default.provider.one)
assert_equal("d", default.provider.two)
end
# Make sure we can correctly prefetch on a target.
def test_prefetch_target
prov = mkprovider
prov.filetype = :ram
target = :yayness
prov.target_object(target).write "yay b d"
model = mkmodel "yay", :target => :yayness
assert_nothing_raised do
prov.prefetch_target(:yayness)
end
# Now make sure we correctly got the hash
mprov = model.provider
assert_equal("b", mprov.one)
assert_equal("d", mprov.two)
end
def test_prefetch_match
prov = mkprovider
prov.meta_def(:match) do |record|
# Look for matches on :one
self.model.find do |m|
m.should(:one).to_s == record[:one].to_s
end
end
prov.filetype = :ram
target = :yayness
prov.target_object(target).write "foo b d"
model = mkmodel "yay", :target => :yayness, :one => "b"
assert_nothing_raised do
prov.prefetch_target(:yayness)
end
# Now make sure we correctly got the hash
mprov = model.provider
assert_equal("yay", model[:name])
assert_equal("b", mprov.one)
assert_equal("d", mprov.two)
end
# We need to test that we're retrieving files from all three locations:
# from any existing target_objects, from the default file location, and
# from any existing model instances.
def test_targets
prov = mkprovider
files = {}
# Set the default target
default = tempfile()
files[:default] = default
prov.default_target = default
# Create a file object
inmem = tempfile()
files[:inmemory] = inmem
prov.target_object(inmem).write("inmem yay ness")
# Lastly, create a model
mtarget = tempfile()
files[:models] = mtarget
model = mkmodel "yay", :target => mtarget
assert(model[:target], "Did not get a value for target")
list = nil
assert_nothing_raised do
list = prov.targets
end
files.each do |name, file|
assert(list.include?(file), "Provider did not find %s file" % name)
end
end
# Make sure that flushing behaves correctly. This is what actually writes
# the data out to disk.
def test_flush
prov = mkprovider
prov.filetype = :ram
prov.default_target = :yayness
# Create some models.
one = mkmodel "one", :one => "a", :two => "c", :target => :yayness
two = mkmodel "two", :one => "b", :two => "d", :target => :yayness
# Write out a file with different data.
prov.target_object(:yayness).write "one b d\ntwo a c"
prov.prefetch
# Apply and flush the first model.
assert_nothing_raised do
apply(one)
end
assert_nothing_raised { one.flush }
# Make sure it changed our file
assert_equal("a", one.provider.one)
assert_equal("c", one.provider.two)
# And make sure it's right on disk
assert(prov.target_object(:yayness).read.include?("one a c"),
"Did not write out correct data")
# Make sure the second model has not been modified
assert_equal("a", two.provider.one, "Two was flushed early")
assert_equal("c", two.provider.two, "Two was flushed early")
# And on disk
assert(prov.target_object(:yayness).read.include?("two a c"),
"Wrote out other model")
# Now fetch the data again and make sure we're still right
assert_nothing_raised { prov.prefetch }
assert_equal("a", one.provider.one)
assert_equal("a", two.provider.one)
# Now flush the second model and make sure it goes well
assert_nothing_raised { apply(two) }
assert_nothing_raised { two.flush }
assert_equal("b", two.provider.one)
end
def test_creating_file
prov = mkprovider
prov.filetype = :ram
prov.default_target = :basic
model = mkmodel "yay", :target => :basic, :one => "a", :two => "c"
apply(model)
assert_nothing_raised do
model.flush
end
assert(prov.target_object(:basic).read.include?("yay a c"),
"Did not create file")
# Make a change
model.provider.one = "b"
# Flush it
assert_nothing_raised do
model.flush
end
# And make sure our model doesn't appear twice in the file.
assert_equal("yay b c\n",
prov.target_object(:basic).read)
end
# Make sure a record can switch targets.
def test_switching_targets
prov = mkprovider
prov.filetype = :ram
prov.default_target = :first
# Make three models, one for each target and one to switch
first = mkmodel "first", :target => :first
second = mkmodel "second", :target => :second
mover = mkmodel "mover", :target => :first
# Apply them all
[first, second, mover].each do |m|
assert_nothing_raised("Could not apply %s" % m[:name]) do
apply(m)
end
end
# Flush
assert_nothing_raised do
[first, second, mover].each do |m| m.flush end
end
check = proc do |target, name|
assert(prov.target_object(target).read.include?("%s a c" % name),
"Did not sync %s" % name)
end
# Make sure the data is there
check.call(:first, :first)
check.call(:second, :second)
check.call(:first, :mover)
# Now change the target for the mover
mover[:target] = :second
# Apply it
assert_nothing_raised do
apply(mover)
end
# Flush
assert_nothing_raised do
mover.flush
end
# Make sure the data is there
check.call(:first, :first)
check.call(:second, :second)
check.call(:second, :mover)
# And make sure the mover is no longer in the first file
assert(prov.target_object(:first) !~ /mover/,
"Mover was not removed from first file")
end
end
# $Id$
|