summaryrefslogtreecommitdiffstats
path: root/test/ral/type/filesources.rb
blob: e35b66fbda535037d3a887eea0cf43b2277e1a12 (plain)
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../lib/puppettest'

require 'puppettest'
require 'puppettest/support/utils'
require 'cgi'
require 'fileutils'
require 'mocha'

class TestFileSources < Test::Unit::TestCase
    include PuppetTest::Support::Utils
    include PuppetTest::FileTesting
    def setup
        super
        if defined? @port
            @port += 1
        else
            @port = 12345
        end
        @file = Puppet::Type.type(:file)
        Puppet[:filetimeout] = -1
        Puppet::Util::SUIDManager.stubs(:asuser).yields 
        Facter.stubs(:to_hash).returns({})
    end

    def teardown
        super
        Puppet::Network::HttpPool.clear_http_instances
    end
    
    def use_storage
        begin
            initstorage
        rescue
            system("rm -rf %s" % Puppet[:statefile])
        end
    end

    def initstorage
        Puppet::Util::Storage.init
        Puppet::Util::Storage.load
    end
    
    # Make a simple recursive tree.
    def mk_sourcetree
        source = tempfile()
        sourcefile = File.join(source, "file")
        Dir.mkdir source
        File.open(sourcefile, "w") { |f| f.puts "yay" }
        
        dest = tempfile()
        destfile = File.join(dest, "file")
        return source, dest, sourcefile, destfile
    end

    def test_newchild
        path = tempfile()
        @@tmpfiles.push path

        FileUtils.mkdir_p path
        File.open(File.join(path,"childtest"), "w") { |of|
            of.puts "yayness"
        }
        file = nil
        comp = nil
        trans = nil
        assert_nothing_raised {
            file = Puppet.type(:file).create(
                :name => path
            )
        }
        catalog = mk_catalog(file)
        child = nil
        assert_nothing_raised {
            child = file.newchild("childtest", true)
        }
        assert(child)
        assert_raise(Puppet::DevError) {
            file.newchild(File.join(path,"childtest"), true)
        }
    end
    
    def test_describe
        source = tempfile()
        dest = tempfile()
        
        file = Puppet::Type.newfile :path => dest, :source => source,
            :title => "copier"
        
        property = file.property(:source)
        
        # First try describing with a normal source
        result = nil
        assert_nothing_raised do
            result = property.describe(source)
        end
        assert_nil(result, "Got a result back when source is missing")
        
        # Now make a remote directory
        Dir.mkdir(source)
        assert_nothing_raised do
            result = property.describe(source)
        end
        assert_equal("directory", result[:type])
        
        # And as a file
        Dir.rmdir(source)
        File.open(source, "w") { |f| f.puts "yay" }
        assert_nothing_raised do
            result = property.describe(source)
        end
        assert_equal("file", result[:type])
        assert(result[:checksum], "did not get value for checksum")
        if Puppet::Util::SUIDManager.uid == 0
            assert(result.has_key?(:owner), "Lost owner in describe")
        else
            assert(! result.has_key?(:owner),
                "Kept owner in describe even tho not root")
        end
        
        # Now let's do the various link things
        File.unlink(source)
        target = tempfile()
        File.open(target, "w") { |f| f.puts "yay" }
        File.symlink(target, source)
        
        # And then make sure links get followed, otherwise
        file[:links] = :follow
        assert_equal("file", property.describe(source)[:type])
    end
    
    def test_source_retrieve
        source = tempfile()
        dest = tempfile()
        
        file = Puppet::Type.newfile :path => dest, :source => source,
            :title => "copier"
        
        assert(file.property(:checksum), "source property did not create checksum property")
        property = file.property(:source)
        assert(property, "did not get source property")
        
        # Make sure the munge didn't actually change the source
        assert_equal([source], property.should, "munging changed the source")
        
        currentvalue = nil
        # Now make the dest a directory, and make sure the object sets :ensure
        # up to create a directory
        Dir.mkdir(source)
        assert_nothing_raised do
            currentvalue = property.retrieve
        end
        assert_equal(:directory, file.should(:ensure),
            "Did not set to create directory")
        
        # And make sure the source property won't try to do anything with a
        # remote dir
        assert(property.insync?(currentvalue), "Source was out of sync even tho remote is dir")
        
        # Now remove the source, and make sure :ensure was not modified
        Dir.rmdir(source)
        assert_equal(:directory, file.should(:ensure),
            "Did not keep :ensure setting")
        
        # Now have a remote file and make sure things work correctly
        File.open(source, "w") { |f| f.puts "yay" }
        File.chmod(0755, source)
        
        assert_nothing_raised do
            property.retrieve
        end
        assert_equal(:file, file.should(:ensure),
            "Did not make correct :ensure setting")
        assert_equal(0755, file.should(:mode),
            "Mode was not copied over")
        
        # Now let's make sure that we get the first found source
        fake = tempfile()
        property.should = [fake, source]
        assert_nothing_raised do
            property.retrieve
        end
        assert_equal(Digest::MD5.hexdigest(File.read(source)), property.checksum.sub(/^\{\w+\}/, ''), 
            "Did not catch later source")
    end
    
    def test_insync
        source = tempfile()
        dest = tempfile()
        
        file = Puppet::Type.newfile :path => dest, :source => source,
            :title => "copier"
        
        property = file.property(:source)
        assert(property, "did not get source property")
        
        # with a directory
        Dir.mkdir(source)
        currentvalues = file.retrieve
        assert(property.insync?(currentvalues[property]), "source property not in sync with directory as source")
        Dir.rmdir(source)
        
        # with a file
        File.open(source, "w") { |f| f.puts "yay" }
        currentvalues = file.retrieve
        assert(!property.insync?(currentvalues[property]), "source property was in sync when file was missing")
        
        # With a different file
        File.open(dest, "w") { |f| f.puts "foo" }
        currentvalues = file.retrieve
        assert(!property.insync?(currentvalues[property]), "source property was in sync with different file")
        
        # with matching files
        File.open(dest, "w") { |f| f.puts "yay" }
        currentvalues = file.retrieve
        assert(property.insync?(currentvalues[property]), "source property was not in sync with matching file")
    end
    
    def test_source_sync
        source = tempfile()
        dest = tempfile()

        file = Puppet::Type.newfile :path => dest, :source => source,
            :title => "copier"
        property = file.property(:source)
        
        File.open(source, "w") { |f| f.puts "yay" }
        
        currentvalues = file.retrieve
        assert(! property.insync?(currentvalues[property]), "source thinks it's in sync")
        
        event = nil
        assert_nothing_raised do
            event = property.sync
        end
        assert_equal(:file_created, event)
        assert_equal(File.read(source), File.read(dest),
            "File was not copied correctly")
        
        # Now write something different
        File.open(source, "w") { |f| f.puts "rah" }
        currentvalues = file.retrieve
        assert(! property.insync?(currentvalues[property]), "source should be out of sync")
        assert_nothing_raised do
            event = property.sync
        end
        assert_equal(:file_changed, event)
        assert_equal(File.read(source), File.read(dest),
            "File was not copied correctly")
    end
    
    # XXX This test doesn't cover everything.  Specifically,
    # it doesn't handle 'ignore' and 'links'.
    def test_sourcerecurse
        source, dest, sourcefile, destfile = mk_sourcetree
        
        # The sourcerecurse method will only ever get called when we're
        # recursing, so we go ahead and set it.
        obj = Puppet::Type.newfile :source => source, :path => dest, :recurse => true
        catalog = mk_catalog(obj)

        result = nil
        sourced = nil
        assert_nothing_raised do
            result, sourced = obj.sourcerecurse(true)
        end

        assert_equal([destfile], sourced, "Did not get correct list of sourced objects")
        dfileobj = catalog.resource(:file, destfile)
        assert(dfileobj, "Did not create destfile object")
        assert_equal([dfileobj], result)
        
        # Clean this up so it can be recreated
        catalog.remove_resource(dfileobj)
        
        # Make sure we correctly iterate over the sources
        nosource = tempfile()
        obj[:source] = [nosource, source]

        result = nil
        assert_nothing_raised do
            result, sourced = obj.sourcerecurse(true)
        end
        assert_equal([destfile], sourced, "Did not get correct list of sourced objects")
        dfileobj = catalog.resource(:file, destfile)
        assert(dfileobj, "Did not create destfile object with a missing source")
        assert_equal([dfileobj], result)
        dfileobj.remove
        
        # Lastly, make sure we return an empty array when no sources are there
        obj[:source] = [nosource, tempfile()]
        
        assert_nothing_raised do
            result, sourced = obj.sourcerecurse(true)
        end
        assert_equal([], sourced, "Did not get correct list of sourced objects")
        assert_equal([], result, "Sourcerecurse failed when all sources are missing")
    end

    def test_simplelocalsource
        path = tempfile()
        FileUtils.mkdir_p path
        frompath = File.join(path,"source")
        topath = File.join(path,"dest")
        fromfile = nil
        tofile = nil
        trans = nil

        File.open(frompath, File::WRONLY|File::CREAT|File::APPEND) { |of|
            of.puts "yayness"
        }
        assert_nothing_raised {
            tofile = Puppet.type(:file).create(
                :name => topath,
                :source => frompath
            )
        }

        assert_apply(tofile)

        assert(FileTest.exists?(topath), "File #{topath} is missing")
        from = File.open(frompath) { |o| o.read }
        to = File.open(topath) { |o| o.read }
        assert_equal(from,to)
    end
    
    # Make sure a simple recursive copy works
    def test_simple_recursive_source
        source, dest, sourcefile, destfile = mk_sourcetree
        
        file = Puppet::Type.newfile :path => dest, :source => source, :recurse => true
        
        assert_events([:directory_created, :file_created], file)
        
        assert(FileTest.directory?(dest), "Dest dir was not created")
        assert(FileTest.file?(destfile), "dest file was not created")
        assert_equal("yay\n", File.read(destfile), "dest file was not copied correctly")
    end

    def recursive_source_test(fromdir, todir)
        initstorage
        tofile = nil
        trans = nil

        tofile = Puppet.type(:file).create(
            :path => todir,
            :recurse => true,
            :backup => false,
            :source => fromdir
        )
        catalog = mk_catalog(tofile)
        catalog.apply

        assert(FileTest.exists?(todir), "Created dir %s does not exist" % todir)
    end

    def run_complex_sources(networked = false)
        path = tempfile()

        # first create the source directory
        FileUtils.mkdir_p path

        # okay, let's create a directory structure
        fromdir = File.join(path,"fromdir")
        Dir.mkdir(fromdir)
        FileUtils.cd(fromdir) {
            File.open("one", "w") { |f| f.puts "onefile"}
            File.open("two", "w") { |f| f.puts "twofile"}
        }

        todir = File.join(path, "todir")
        source = fromdir
        if networked
            source = "puppet://localhost/%s%s" % [networked, fromdir]
        end
        recursive_source_test(source, todir)

        return [fromdir,todir, File.join(todir, "one"), File.join(todir, "two")]
    end

    def test_complex_sources_twice
        fromdir, todir, one, two = run_complex_sources
        assert_trees_equal(fromdir,todir)
        recursive_source_test(fromdir, todir)
        assert_trees_equal(fromdir,todir)
        # Now remove the whole tree and try it again.
        [one, two].each do |f| File.unlink(f) end
        Dir.rmdir(todir)
        recursive_source_test(fromdir, todir)
        assert_trees_equal(fromdir,todir)
    end

    def test_sources_with_deleted_destfiles
        fromdir, todir, one, two = run_complex_sources
        assert(FileTest.exists?(todir))

        # then delete a file
        File.unlink(two)

        # and run
        recursive_source_test(fromdir, todir)

        assert(FileTest.exists?(two), "Deleted file was not recopied")

        # and make sure they're still equal
        assert_trees_equal(fromdir,todir)
    end

    def test_sources_with_readonly_destfiles
        fromdir, todir, one, two = run_complex_sources
        assert(FileTest.exists?(todir))
        File.chmod(0600, one)
        recursive_source_test(fromdir, todir)

        # and make sure they're still equal
        assert_trees_equal(fromdir,todir)
        
        # Now try it with the directory being read-only
        File.chmod(0111, todir)
        recursive_source_test(fromdir, todir)

        # and make sure they're still equal
        assert_trees_equal(fromdir,todir)
    end

    def test_sources_with_modified_dest_files
        fromdir, todir, one, two = run_complex_sources

        assert(FileTest.exists?(todir))
        
        # Modify a dest file
        File.open(two, "w") { |f| f.puts "something else" }

        recursive_source_test(fromdir, todir)

        # and make sure they're still equal
        assert_trees_equal(fromdir,todir)
    end

    def test_sources_with_added_destfiles
        fromdir, todir = run_complex_sources
        assert(FileTest.exists?(todir))
        # and finally, add some new files
        add_random_files(todir)

        recursive_source_test(fromdir, todir)

        fromtree = file_list(fromdir)
        totree = file_list(todir)

        assert(fromtree != totree, "Trees are incorrectly equal")

        # then remove our new files
        FileUtils.cd(todir) {
            %x{find . 2>/dev/null}.chomp.split(/\n/).each { |file|
                if file =~ /file[0-9]+/
                    File.unlink(file)
                end
            }
        }

        # and make sure they're still equal
        assert_trees_equal(fromdir,todir)
    end

    # Make sure added files get correctly caught during recursion
    def test_RecursionWithAddedFiles
        basedir = tempfile()
        Dir.mkdir(basedir)
        @@tmpfiles << basedir
        file1 = File.join(basedir, "file1")
        file2 = File.join(basedir, "file2")
        subdir1 = File.join(basedir, "subdir1")
        file3 = File.join(subdir1, "file")
        File.open(file1, "w") { |f| f.puts "yay" }
        rootobj = nil
        assert_nothing_raised {
            rootobj = Puppet.type(:file).create(
                :name => basedir,
                :recurse => true,
                :check => %w{type owner},
                :mode => 0755
            )
        }
        
        assert_apply(rootobj)
        assert_equal(0755, filemode(file1))

        File.open(file2, "w") { |f| f.puts "rah" }
        assert_apply(rootobj)
        assert_equal(0755, filemode(file2))

        Dir.mkdir(subdir1)
        File.open(file3, "w") { |f| f.puts "foo" }
        assert_apply(rootobj)
        assert_equal(0755, filemode(file3))
    end

    def mkfileserverconf(mounts)
        file = tempfile()
        File.open(file, "w") { |f|
            mounts.each { |path, name|
                f.puts "[#{name}]\n\tpath #{path}\n\tallow *\n"
            }
        }

        @@tmpfiles << file
        return file
    end

    def test_NetworkSources
        server = nil
        mounts = {
            "/" => "root"
        }

        fileserverconf = mkfileserverconf(mounts)

        Puppet[:autosign] = true

        Puppet[:masterport] = 8762
        Puppet[:name] = "puppetmasterd"
        Puppet[:certdnsnames] = "localhost"

        serverpid = nil
        assert_nothing_raised() {
            server = Puppet::Network::HTTPServer::WEBrick.new(
                :Handlers => {
                    :CA => {}, # so that certs autogenerate
                    :FileServer => {
                        :Config => fileserverconf
                    }
                }
            )

        }
        serverpid = fork {
            assert_nothing_raised() {
                #trap(:INT) { server.shutdown; Kernel.exit! }
                trap(:INT) { server.shutdown }
                server.start
            }
        }
        @@tmppids << serverpid

        sleep(1)

        fromdir, todir = run_complex_sources("root")
        assert_trees_equal(fromdir,todir)
        recursive_source_test(fromdir, todir)
        assert_trees_equal(fromdir,todir)

        assert_nothing_raised {
            system("kill -INT %s" % serverpid)
        }
    end

    def test_unmountedNetworkSources
        server = nil
        mounts = {
            "/" => "root",
            "/noexistokay" => "noexist"
        }

        fileserverconf = mkfileserverconf(mounts)

        Puppet[:autosign] = true
        Puppet[:masterport] = @port
        Puppet[:certdnsnames] = "localhost"

        serverpid = nil
        assert_nothing_raised("Could not start on port %s" % @port) {
            server = Puppet::Network::HTTPServer::WEBrick.new(
                :Port => @port,
                :Handlers => {
                    :CA => {}, # so that certs autogenerate
                    :FileServer => {
                        :Config => fileserverconf
                    }
                }
            )

        }

        serverpid = fork {
            assert_nothing_raised() {
                #trap(:INT) { server.shutdown; Kernel.exit! }
                trap(:INT) { server.shutdown }
                server.start
            }
        }
        @@tmppids << serverpid

        sleep(1)

        name = File.join(tmpdir(), "nosourcefile")
        file = Puppet.type(:file).create(
            :source => "puppet://localhost/noexist/file",
            :name => name
        )

        assert_raise Puppet::Error do 
            file.retrieve
        end 

        comp = mk_catalog(file)
        comp.apply

        assert(!FileTest.exists?(name), "File with no source exists anyway")
    end

    def test_alwayschecksum
        from = tempfile()
        to = tempfile()

        File.open(from, "w") { |f| f.puts "yayness" }
        File.open(to, "w") { |f| f.puts "yayness" }

        file = nil

        # Now the files should be exactly the same, so we should not see attempts
        # at copying
        assert_nothing_raised {
            file = Puppet.type(:file).create(
                :path => to,
                :source => from
            )
        }

        currentvalue = file.retrieve

        assert(currentvalue[file.property(:checksum)], 
               "File does not have a checksum property")

        assert_equal(0, file.evaluate.length, "File produced changes")
    end

    def test_sourcepaths
        files = []
        3.times { 
            files << tempfile()
        }

        to = tempfile()

        File.open(files[-1], "w") { |f| f.puts "yee-haw" }

        file = nil
        assert_nothing_raised {
            file = Puppet.type(:file).create(
                :name => to,
                :source => files
            )
        }

        comp = mk_catalog(file)
        assert_events([:file_created], comp)

        assert(File.exists?(to), "File does not exist")

        txt = nil
        File.open(to) { |f| txt = f.read.chomp }

        assert_equal("yee-haw", txt, "Contents do not match")
    end

    # Make sure that source-copying updates the checksum on the same run
    def test_checksumchange
        source = tempfile()
        dest = tempfile()
        File.open(dest, "w") { |f| f.puts "boo" }
        File.open(source, "w") { |f| f.puts "yay" }

        file = nil
        assert_nothing_raised {
            file = Puppet.type(:file).create(
                :name => dest,
                :source => source
            )
        }

        file.retrieve

        assert_events([:file_changed], file)
        file.retrieve
        assert_events([], file)
    end

    # Make sure that source-copying updates the checksum on the same run
    def test_sourcebeatsensure
        source = tempfile()
        dest = tempfile()
        File.open(source, "w") { |f| f.puts "yay" }

        file = nil
        assert_nothing_raised {
            file = Puppet.type(:file).create(
                :name => dest,
                :ensure => "file",
                :source => source
            )
        }

        file.retrieve

        assert_events([:file_created], file)
        file.retrieve
        assert_events([], file)
        assert_events([], file)
    end

    def test_sourcewithlinks
        source = tempfile()
        link = tempfile()
        dest = tempfile()

        File.open(source, "w") { |f| f.puts "yay" }
        File.symlink(source, link)

        file = Puppet.type(:file).create(:name => dest, :source => link)

        catalog = mk_catalog(file)

        # Default to managing links
        catalog.apply
        assert(FileTest.symlink?(dest), "Did not create link")

        # Now follow the links
        file[:links] = :follow
        catalog.apply
        assert(FileTest.file?(dest), "Destination is not a file")
    end

    def test_changes
        source = tempfile()
        dest = tempfile()

        File.open(source, "w") { |f| f.puts "yay" }

        obj = nil
        assert_nothing_raised {
            obj = Puppet.type(:file).create(
                :name => dest,
                :source => source
            )
        }

        assert_events([:file_created], obj)
        assert_equal(File.read(source), File.read(dest), "Files are not equal")
        assert_events([], obj)

        File.open(source, "w") { |f| f.puts "boo" }

        assert_events([:file_changed], obj)
        assert_equal(File.read(source), File.read(dest), "Files are not equal")
        assert_events([], obj)

        File.open(dest, "w") { |f| f.puts "kaboom" }

        # There are two changes, because first the checksum is noticed, and
        # then the source causes a change
        assert_events([:file_changed, :file_changed], obj)
        assert_equal(File.read(source), File.read(dest), "Files are not equal")
        assert_events([], obj)
    end

    def test_file_source_with_space
        dir = tempfile()
        source = File.join(dir, "file with spaces")
        Dir.mkdir(dir)
        File.open(source, "w") { |f| f.puts "yayness" }

        newdir = tempfile()
        newpath = File.join(newdir, "file with spaces")

        file = Puppet::Type.newfile(
            :path => newdir,
            :source => dir,
            :recurse => true
        )


        assert_apply(file)

        assert(FileTest.exists?(newpath), "Did not create file")
        assert_equal("yayness\n", File.read(newpath))
    end

    # Make sure files aren't replaced when replace is false, but otherwise
    # are.
    def test_replace
        source = tempfile()
        File.open(source, "w") { |f| f.puts "yayness" }

        dest = tempfile()
        file = Puppet::Type.newfile(
            :path => dest,
            :source => source,
            :recurse => true
        )


        assert_apply(file)

        assert(FileTest.exists?(dest), "Did not create file")
        assert_equal("yayness\n", File.read(dest))

        # Now set :replace
        assert_nothing_raised {
            file[:replace] = false
        }

        File.open(source, "w") { |f| f.puts "funtest" }
        assert_apply(file)

        # Make sure it doesn't change.
        assert_equal("yayness\n", File.read(dest),
            "File got replaced when :replace was false")

        # Now set it to true and make sure it does change.
        assert_nothing_raised {
            file[:replace] = true
        }
        assert_apply(file)

        # Make sure it doesn't change.
        assert_equal("funtest\n", File.read(dest),
            "File was not replaced when :replace was true")
    end

    # Testing #285.  This just makes sure that URI parsing works correctly.
    def test_fileswithpoundsigns
        dir = tstdir()
        subdir = File.join(dir, "#dir")
        Dir.mkdir(subdir)
        file = File.join(subdir, "file")
        File.open(file, "w") { |f| f.puts "yayness" }

        dest = tempfile()
        source = "file://localhost#{dir}"
        obj = Puppet::Type.newfile(
            :path => dest,
            :source => source,
            :recurse => true
        )

        newfile = File.join(dest, "#dir", "file")

        poundsource = "file://localhost#{subdir}"

        sourceobj = path = nil
        assert_nothing_raised {
            sourceobj, path = obj.uri2obj(poundsource)
        }

        assert_equal("/localhost" + URI.escape(subdir), path)

        assert_apply(obj)

        assert(FileTest.exists?(newfile), "File did not get created")
        assert_equal("yayness\n", File.read(newfile))
    end

    def test_sourceselect
        dest = tempfile()
        sources = []
        2.times { |i|
            i = i + 1
            source = tempfile()
            sources << source
            file = File.join(source, "file%s" % i)
            Dir.mkdir(source)
            File.open(file, "w") { |f| f.print "yay" }
        }
        file1 = File.join(dest, "file1")
        file2 = File.join(dest, "file2")
        file3 = File.join(dest, "file3")

        # Now make different files with the same name in each source dir
        sources.each_with_index do |source, i|
            File.open(File.join(source, "file3"), "w") { |f|
                f.print i.to_s
            }
        end

        obj = Puppet::Type.newfile(:path => dest, :recurse => true,
            :source => sources)

        assert_equal(:first, obj[:sourceselect], "sourceselect has the wrong default")
        # First, make sure we default to just copying file1
        assert_apply(obj)

        assert(FileTest.exists?(file1), "File from source 1 was not copied")
        assert(! FileTest.exists?(file2), "File from source 2 was copied")
        assert(FileTest.exists?(file3), "File from source 1 was not copied")
        assert_equal("0", File.read(file3), "file3 got wrong contents")

        # Now reset sourceselect
        assert_nothing_raised do
            obj[:sourceselect] = :all
        end
        File.unlink(file1)
        File.unlink(file3)
        Puppet.err :yay
        assert_apply(obj)

        assert(FileTest.exists?(file1), "File from source 1 was not copied")
        assert(FileTest.exists?(file2), "File from source 2 was copied")
        assert(FileTest.exists?(file3), "File from source 1 was not copied")
        assert_equal("0", File.read(file3), "file3 got wrong contents")
    end
    
    def test_recursive_sourceselect
        dest = tempfile()
        source1 = tempfile()
        source2 = tempfile()
        files = []
        [source1, source2, File.join(source1, "subdir"), File.join(source2, "subdir")].each_with_index do |dir, i|
            Dir.mkdir(dir)
            # Make a single file in each directory
            file = File.join(dir, "file%s" % i)
            File.open(file, "w") { |f| f.puts "yay%s" % i}

            # Now make a second one in each directory
            file = File.join(dir, "second-file%s" % i)
            File.open(file, "w") { |f| f.puts "yaysecond-%s" % i}
            files << file
        end
        
        obj = Puppet::Type.newfile(:path => dest, :source => [source1, source2], :sourceselect => :all, :recurse => true)
        
        assert_apply(obj)
        
        ["file0", "file1", "second-file0", "second-file1", "subdir/file2", "subdir/second-file2", "subdir/file3", "subdir/second-file3"].each do |file|
            path = File.join(dest, file)
            assert(FileTest.exists?(path), "did not create %s" % file)
            
            assert_equal("yay%s\n" % File.basename(file).sub("file", ''), File.read(path), "file was not copied correctly")
        end
    end

    # #594
    def test_purging_missing_remote_files
        source = tempfile()
        dest = tempfile()
        s1 = File.join(source, "file1")
        s2 = File.join(source, "file2")
        d1 = File.join(dest, "file1")
        d2 = File.join(dest, "file2")
        Dir.mkdir(source)
        [s1, s2].each { |name| File.open(name, "w") { |file| file.puts "something" } }

        # We have to add a second parameter, because that's the only way to expose the "bug".
        file = Puppet::Type.newfile(:path => dest, :source => source, :recurse => true, :purge => true, :mode => "755")

        assert_apply(file)

        assert(FileTest.exists?(d1), "File1 was not copied")
        assert(FileTest.exists?(d2), "File2 was not copied")

        File.unlink(s2)

        assert_apply(file)

        assert(FileTest.exists?(d1), "File1 was not kept")
        assert(! FileTest.exists?(d2), "File2 was not purged")
    end
end