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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../lib/puppettest'
require 'puppettest'
require 'puppettest/support/utils'
require 'fileutils'
require 'mocha'
class TestFile < Test::Unit::TestCase
include PuppetTest::Support::Utils
include PuppetTest::FileTesting
def mkfile(hash)
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(hash)
}
return file
end
def mktestfile
tmpfile = tempfile()
File.open(tmpfile, "w") { |f| f.puts rand(100) }
@@tmpfiles.push tmpfile
mkfile(:name => tmpfile)
end
def setup
super
@file = Puppet::Type.type(:file)
$method = @method_name
Puppet[:filetimeout] = -1
Facter.stubs(:to_hash).returns({})
end
def teardown
system("rm -rf %s" % Puppet[:statefile])
super
end
def initstorage
Puppet::Util::Storage.init
Puppet::Util::Storage.load
end
def clearstorage
Puppet::Util::Storage.store
Puppet::Util::Storage.clear
end
def test_owner
file = mktestfile()
users = {}
count = 0
# collect five users
Etc.passwd { |passwd|
if count > 5
break
else
count += 1
end
users[passwd.uid] = passwd.name
}
fake = {}
# find a fake user
while true
a = rand(1000)
begin
Etc.getpwuid(a)
rescue
fake[a] = "fakeuser"
break
end
end
uid, name = users.shift
us = {}
us[uid] = name
users.each { |uid, name|
assert_apply(file)
assert_nothing_raised() {
file[:owner] = name
}
assert_nothing_raised() {
file.retrieve
}
assert_apply(file)
}
end
def test_group
file = mktestfile()
[%x{groups}.chomp.split(/ /), Process.groups].flatten.each { |group|
assert_nothing_raised() {
file[:group] = group
}
assert(file.property(:group))
assert(file.property(:group).should)
}
end
def test_groups_fails_when_invalid
assert_raise(Puppet::Error, "did not fail when the group was empty") do
Puppet::Type.type(:file).new :path => "/some/file", :group => ""
end
end
if Puppet.features.root?
def test_createasuser
dir = tmpdir()
user = nonrootuser()
path = File.join(tmpdir, "createusertesting")
@@tmpfiles << path
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:path => path,
:owner => user.name,
:ensure => "file",
:mode => "755"
)
}
comp = mk_catalog("createusertest", file)
assert_events([:file_created], comp)
end
def test_nofollowlinks
basedir = tempfile()
Dir.mkdir(basedir)
file = File.join(basedir, "file")
link = File.join(basedir, "link")
File.open(file, "w", 0644) { |f| f.puts "yayness"; f.flush }
File.symlink(file, link)
# First test 'user'
user = nonrootuser()
inituser = File.lstat(link).uid
File.lchown(inituser, nil, link)
obj = nil
assert_nothing_raised {
obj = Puppet::Type.type(:file).new(
:title => link,
:owner => user.name
)
}
obj.retrieve
# Make sure it defaults to managing the link
assert_events([:file_changed], obj)
assert_equal(user.uid, File.lstat(link).uid)
assert_equal(inituser, File.stat(file).uid)
File.chown(inituser, nil, file)
File.lchown(inituser, nil, link)
# Try following
obj[:links] = :follow
assert_events([:file_changed], obj)
assert_equal(user.uid, File.stat(file).uid)
assert_equal(inituser, File.lstat(link).uid)
# And then explicitly managing
File.chown(inituser, nil, file)
File.lchown(inituser, nil, link)
obj[:links] = :manage
assert_events([:file_changed], obj)
assert_equal(user.uid, File.lstat(link).uid)
assert_equal(inituser, File.stat(file).uid)
obj.delete(:owner)
obj[:links] = :follow
# And then test 'group'
group = nonrootgroup
initgroup = File.stat(file).gid
obj[:group] = group.name
obj[:links] = :follow
assert_events([:file_changed], obj)
assert_equal(group.gid, File.stat(file).gid)
File.chown(nil, initgroup, file)
File.lchown(nil, initgroup, link)
obj[:links] = :manage
assert_events([:file_changed], obj)
assert_equal(group.gid, File.lstat(link).gid)
assert_equal(initgroup, File.stat(file).gid)
end
def test_ownerasroot
file = mktestfile()
users = {}
count = 0
# collect five users
Etc.passwd { |passwd|
if count > 5
break
else
count += 1
end
next if passwd.uid < 0
users[passwd.uid] = passwd.name
}
fake = {}
# find a fake user
while true
a = rand(1000)
begin
Etc.getpwuid(a)
rescue
fake[a] = "fakeuser"
break
end
end
users.each { |uid, name|
assert_nothing_raised() {
file[:owner] = name
}
assert_apply(file)
currentvalue = file.retrieve
assert(file.insync?(currentvalue))
assert_nothing_raised() {
file[:owner] = uid
}
assert_apply(file)
currentvalue = file.retrieve
# make sure changing to number doesn't cause a sync
assert(file.insync?(currentvalue))
}
# We no longer raise an error here, because we check at run time
#fake.each { |uid, name|
# assert_raise(Puppet::Error) {
# file[:owner] = name
# }
# assert_raise(Puppet::Error) {
# file[:owner] = uid
# }
#}
end
def test_groupasroot
file = mktestfile()
[%x{groups}.chomp.split(/ /), Process.groups].flatten.each { |group|
next unless Puppet::Util.gid(group) # grr.
assert_nothing_raised() {
file[:group] = group
}
assert(file.property(:group))
assert(file.property(:group).should)
assert_apply(file)
currentvalue = file.retrieve
assert(file.insync?(currentvalue))
assert_nothing_raised() {
file.delete(:group)
}
}
end
if Facter.value(:operatingsystem) == "Darwin"
def test_sillyowner
file = tempfile()
File.open(file, "w") { |f| f.puts "" }
File.chown(-2, nil, file)
assert(File.stat(file).uid > 120000, "eh?")
user = nonrootuser
obj = Puppet::Type.newfile(
:path => file,
:owner => user.name
)
assert_apply(obj)
assert_equal(user.uid, File.stat(file).uid)
end
end
else
$stderr.puts "Run as root for complete owner and group testing"
end
def test_create
%w{a b c d}.collect { |name| tempfile() + name.to_s }.each { |path|
file =nil
assert_nothing_raised() {
file = Puppet::Type.type(:file).new(
:name => path,
:ensure => "file"
)
}
assert_events([:file_created], file)
assert_events([], file)
assert(FileTest.file?(path), "File does not exist")
@@tmpfiles.push path
}
end
def test_create_dir
basedir = tempfile()
Dir.mkdir(basedir)
%w{a b c d}.collect { |name| "#{basedir}/%s" % name }.each { |path|
file = nil
assert_nothing_raised() {
file = Puppet::Type.type(:file).new(
:name => path,
:ensure => "directory"
)
}
assert(! FileTest.directory?(path), "Directory %s already exists" %
[path])
assert_events([:directory_created], file)
assert_events([], file)
assert(FileTest.directory?(path))
@@tmpfiles.push path
}
end
def test_modes
file = mktestfile
# Set it to something else initially
File.chmod(0775, file.title)
[0644,0755,0777,0641].each { |mode|
assert_nothing_raised() {
file[:mode] = mode
}
assert_events([:mode_changed], file)
assert_events([], file)
assert_nothing_raised() {
file.delete(:mode)
}
}
end
def cyclefile(path)
# i had problems with using :name instead of :path
[:name,:path].each { |param|
file = nil
changes = nil
comp = nil
trans = nil
initstorage
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
param => path,
:recurse => true,
:checksum => "md5"
)
}
comp = Puppet::Type.type(:component).new(
:name => "component"
)
comp.push file
assert_nothing_raised {
trans = comp.evaluate
}
assert_nothing_raised {
trans.evaluate
}
clearstorage
Puppet::Type.allclear
}
end
def test_filetype_retrieval
file = nil
# Verify it retrieves files of type directory
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:name => tmpdir(),
:check => :type
)
}
assert_equal("directory", file.property(:type).retrieve)
# And then check files
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:name => tempfile(),
:ensure => "file"
)
}
assert_apply(file)
file[:check] = "type"
assert_apply(file)
assert_equal("file", file.property(:type).retrieve)
end
def test_path
dir = tempfile()
path = File.join(dir, "subdir")
assert_nothing_raised("Could not make file") {
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") { |f| f.puts "yayness" }
}
file = nil
dirobj = nil
assert_nothing_raised("Could not make file object") {
dirobj = Puppet::Type.type(:file).new(
:path => dir,
:recurse => true,
:check => %w{mode owner group}
)
}
catalog = mk_catalog dirobj
transaction = Puppet::Transaction.new(catalog)
transaction.eval_generate(dirobj)
#assert_nothing_raised {
# dirobj.eval_generate
#}
file = catalog.resource(:file, path)
assert(file, "Could not retrieve file object")
assert_equal("/%s" % file.ref, file.path)
end
def test_autorequire
basedir = tempfile()
subfile = File.join(basedir, "subfile")
baseobj = Puppet::Type.type(:file).new(
:name => basedir,
:ensure => "directory"
)
subobj = Puppet::Type.type(:file).new(
:name => subfile,
:ensure => "file"
)
catalog = mk_catalog(baseobj, subobj)
edge = nil
assert_nothing_raised do
edge = subobj.autorequire.shift
end
assert_equal(baseobj, edge.source, "file did not require its parent dir")
assert_equal(subobj, edge.target, "file did not require its parent dir")
end
# Unfortunately, I know this fails
def disabled_test_recursivemkdir
path = tempfile()
subpath = File.join(path, "this", "is", "a", "dir")
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:name => subpath,
:ensure => "directory",
:recurse => true
)
}
comp = mk_catalog("yay", file)
comp.finalize
assert_apply(comp)
#assert_events([:directory_created], comp)
assert(FileTest.directory?(subpath), "Did not create directory")
end
# Make sure that content updates the checksum on the same run
def test_checksumchange_for_content
dest = tempfile()
File.open(dest, "w") { |f| f.puts "yayness" }
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:name => dest,
:checksum => "md5",
:content => "This is some content",
:backup => false
)
}
file.retrieve
assert_events([:content_changed], file)
file.retrieve
assert_events([], file)
end
# Make sure that content updates the checksum on the same run
def test_checksumchange_for_ensure
dest = tempfile()
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:name => dest,
:checksum => "md5",
:ensure => "file"
)
}
file.retrieve
assert_events([:file_created], file)
file.retrieve
assert_events([], file)
end
def test_nameandpath
path = tempfile()
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:title => "fileness",
:path => path,
:content => "this is some content"
)
}
assert_apply(file)
assert(FileTest.exists?(path))
end
# Make sure that a missing group isn't fatal at object instantiation time.
def test_missinggroup
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:path => tempfile(),
:group => "fakegroup"
)
}
assert(file.property(:group), "Group property failed")
end
def test_modecreation
path = tempfile()
file = Puppet::Type.type(:file).new(
:path => path,
:ensure => "file",
:mode => "0777"
)
assert_equal(0777, file.should(:mode),
"Mode did not get set correctly")
assert_apply(file)
assert_equal(0777, File.stat(path).mode & 007777,
"file mode is incorrect")
File.unlink(path)
file[:ensure] = "directory"
assert_apply(file)
assert_equal(0777, File.stat(path).mode & 007777,
"directory mode is incorrect")
end
# If both 'ensure' and 'content' are used, make sure that all of the other
# properties are handled correctly.
def test_contentwithmode
path = tempfile()
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:path => path,
:ensure => "file",
:content => "some text\n",
:mode => 0755
)
}
assert_apply(file)
assert_equal("%o" % 0755, "%o" % (File.stat(path).mode & 007777))
end
def test_replacefilewithlink
path = tempfile()
link = tempfile()
File.open(path, "w") { |f| f.puts "yay" }
File.open(link, "w") { |f| f.puts "a file" }
file = nil
assert_nothing_raised {
file = Puppet::Type.type(:file).new(
:ensure => path,
:path => link,
:backup => false
)
}
assert_events([:link_created], file)
assert(FileTest.symlink?(link), "Link was not created")
assert_equal(path, File.readlink(link), "Link was created incorrectly")
end
def test_file_with_spaces
dir = tempfile()
Dir.mkdir(dir)
source = File.join(dir, "file spaces")
dest = File.join(dir, "another space")
File.open(source, "w") { |f| f.puts :yay }
obj = Puppet::Type.type(:file).new(
:path => dest,
:source => source
)
assert(obj, "Did not create file")
assert_apply(obj)
assert(FileTest.exists?(dest), "File did not get created")
end
# Testing #274. Make sure target can be used without 'ensure'.
def test_target_without_ensure
source = tempfile()
dest = tempfile()
File.open(source, "w") { |f| f.puts "funtest" }
obj = nil
assert_nothing_raised {
obj = Puppet::Type.newfile(:path => dest, :target => source)
}
assert_apply(obj)
end
def test_autorequire_owner_and_group
file = tempfile()
comp = nil
user = nil
group =nil
home = nil
ogroup = nil
assert_nothing_raised {
user = Puppet::Type.type(:user).new(
:name => "pptestu",
:home => file,
:gid => "pptestg"
)
home = Puppet::Type.type(:file).new(
:path => file,
:owner => "pptestu",
:group => "pptestg",
:ensure => "directory"
)
group = Puppet::Type.type(:group).new(
:name => "pptestg"
)
comp = mk_catalog(user, group, home)
}
# Now make sure we get a relationship for each of these
rels = nil
assert_nothing_raised { rels = home.autorequire }
assert(rels.detect { |e| e.source == user }, "owner was not autorequired")
assert(rels.detect { |e| e.source == group }, "group was not autorequired")
end
# Testing #309 -- //my/file => /my/file
def test_slash_deduplication
["/my/////file/for//testing", "/my/file/for/testing///",
"/my/file/for/testing"].each do |path|
file = nil
assert_nothing_raised do
file = Puppet::Type.newfile(:path => path)
end
assert_equal("/my/file/for/testing", file[:path])
end
end
if Process.uid == 0
# Testing #364.
def test_writing_in_directories_with_no_write_access
# Make a directory that our user does not have access to
dir = tempfile()
Dir.mkdir(dir)
# Get a fake user
user = nonrootuser
# and group
group = nonrootgroup
# First try putting a file in there
path = File.join(dir, "file")
file = Puppet::Type.newfile :path => path, :owner => user.name, :group => group.name, :content => "testing"
# Make sure we can create it
assert_apply(file)
assert(FileTest.exists?(path), "File did not get created")
# And that it's owned correctly
assert_equal(user.uid, File.stat(path).uid, "File has the wrong owner")
assert_equal(group.gid, File.stat(path).gid, "File has the wrong group")
assert_equal("testing", File.read(path), "file has the wrong content")
# Now make a dir
subpath = File.join(dir, "subdir")
subdir = Puppet::Type.newfile :path => subpath, :owner => user.name, :group => group.name, :ensure => :directory
# Make sure we can create it
assert_apply(subdir)
assert(FileTest.directory?(subpath), "File did not get created")
# And that it's owned correctly
assert_equal(user.uid, File.stat(subpath).uid, "File has the wrong owner")
assert_equal(group.gid, File.stat(subpath).gid, "File has the wrong group")
assert_equal("testing", File.read(path), "file has the wrong content")
end
end
# #366
def test_replace_aliases
file = Puppet::Type.newfile :path => tempfile()
file[:replace] = :yes
assert_equal(:true, file[:replace], ":replace did not alias :true to :yes")
file[:replace] = :no
assert_equal(:false, file[:replace], ":replace did not alias :false to :no")
end
def test_pathbuilder
dir = tempfile()
Dir.mkdir(dir)
file = File.join(dir, "file")
File.open(file, "w") { |f| f.puts "" }
obj = Puppet::Type.newfile :path => dir, :recurse => true, :mode => 0755
catalog = mk_catalog obj
transaction = Puppet::Transaction.new(catalog)
assert_equal("/%s" % obj.ref, obj.path)
list = transaction.eval_generate(obj)
fileobj = catalog.resource(:file, file)
assert(fileobj, "did not generate file object")
assert_equal("/%s" % fileobj.ref, fileobj.path, "did not generate correct subfile path")
end
# Testing #403
def test_removal_with_content_set
path = tempfile()
File.open(path, "w") { |f| f.puts "yay" }
file = Puppet::Type.newfile(:name => path, :ensure => :absent, :content => "foo", :backup => false)
assert_apply(file)
assert(! FileTest.exists?(path), "File was not removed")
end
# Testing #438
def test_creating_properties_conflict
file = tempfile()
first = tempfile()
second = tempfile()
params = [:content, :source, :target]
params.each do |param|
assert_nothing_raised("%s conflicted with ensure" % [param]) do
Puppet::Type.newfile(:path => file, param => first, :ensure => :file)
end
params.each do |other|
next if other == param
assert_raise(Puppet::Error, "%s and %s did not conflict" % [param, other]) do
Puppet::Type.newfile(:path => file, other => first, param => second)
end
end
end
end
# Testing #508
if Process.uid == 0
def test_files_replace_with_right_attrs
source = tempfile()
File.open(source, "w") { |f|
f.puts "some text"
}
File.chmod(0755, source)
user = nonrootuser
group = nonrootgroup
path = tempfile()
good = {:uid => user.uid, :gid => group.gid, :mode => 0640}
run = Proc.new do |obj, msg|
assert_apply(obj)
stat = File.stat(obj[:path])
good.each do |should, sval|
if should == :mode
current = filemode(obj[:path])
else
current = stat.send(should)
end
assert_equal(sval, current,
"Attr %s was not correct %s" % [should, msg])
end
end
file = Puppet::Type.newfile(:path => path, :owner => user.name,
:group => group.name, :mode => 0640, :backup => false)
{:source => source, :content => "some content"}.each do |attr, value|
file[attr] = value
# First create the file
run.call(file, "upon creation with %s" % attr)
# Now change something so that we replace the file
case attr
when :source
File.open(source, "w") { |f| f.puts "some different text" }
when :content; file[:content] = "something completely different"
else
raise "invalid attr %s" % attr
end
# Run it again
run.call(file, "after modification with %s" % attr)
# Now remove the file and the attr
file.delete(attr)
File.unlink(path)
end
end
end
def test_root_dir_is_named_correctly
obj = Puppet::Type.newfile(:path => '/', :mode => 0755)
assert_equal("/", obj.title, "/ directory was changed to empty string")
end
end
|