~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to test/server/fileserver.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
4
 
 
5
 
require 'puppet'
6
 
require 'puppet/server/fileserver'
7
 
require 'puppettest'
8
 
 
9
 
class TestFileServer < Test::Unit::TestCase
10
 
        include PuppetTest
11
 
 
12
 
    def mkmount(path = nil)
13
 
        mount = nil
14
 
        name = "yaytest"
15
 
        base = path || tempfile()
16
 
        unless FileTest.exists?(base)
17
 
            Dir.mkdir(base)
18
 
        end
19
 
        # Create a test file
20
 
        File.open(File.join(base, "file"), "w") { |f| f.puts "bazoo" }
21
 
        assert_nothing_raised {
22
 
            mount = Puppet::Server::FileServer::Mount.new(name, base)
23
 
        }
24
 
 
25
 
        return mount
26
 
    end
27
 
    # make a simple file source
28
 
    def mktestdir
29
 
        testdir = File.join(tmpdir(), "remotefilecopytesting")
30
 
        @@tmpfiles << testdir
31
 
 
32
 
        # create a tmpfile
33
 
        pattern = "tmpfile"
34
 
        tmpfile = File.join(testdir, pattern)
35
 
        assert_nothing_raised {
36
 
            Dir.mkdir(testdir)
37
 
            File.open(tmpfile, "w") { |f|
38
 
                3.times { f.puts rand(100) }
39
 
            }
40
 
        }
41
 
 
42
 
        return [testdir, %r{#{pattern}}, tmpfile]
43
 
    end
44
 
 
45
 
    # make a bunch of random test files
46
 
    def mktestfiles(testdir)
47
 
        @@tmpfiles << testdir
48
 
        assert_nothing_raised {
49
 
            files = %w{a b c d e}.collect { |l|
50
 
                name = File.join(testdir, "file%s" % l)
51
 
                File.open(name, "w") { |f|
52
 
                    f.puts rand(100)
53
 
                }
54
 
                
55
 
                name
56
 
            }
57
 
 
58
 
            return files
59
 
        }
60
 
    end
61
 
 
62
 
    def assert_describe(base, file, server)
63
 
        file = File.basename(file)
64
 
        assert_nothing_raised {
65
 
            desc = server.describe(base + file)
66
 
            assert(desc, "Got no description for %s" % file)
67
 
            assert(desc != "", "Got no description for %s" % file)
68
 
            assert_match(/^\d+/, desc, "Got invalid description %s" % desc)
69
 
        }
70
 
    end
71
 
 
72
 
    # test for invalid names
73
 
    def test_namefailures
74
 
        server = nil
75
 
        assert_nothing_raised {
76
 
            server = Puppet::Server::FileServer.new(
77
 
                :Local => true,
78
 
                :Config => false
79
 
            )
80
 
        }
81
 
 
82
 
        assert_raise(Puppet::FileServerError) {
83
 
            server.mount("/tmp", "invalid+name")
84
 
        }
85
 
 
86
 
        assert_raise(Puppet::FileServerError) {
87
 
            server.mount("/tmp", "invalid-name")
88
 
        }
89
 
 
90
 
        assert_raise(Puppet::FileServerError) {
91
 
            server.mount("/tmp", "invalid name")
92
 
        }
93
 
 
94
 
        assert_raise(Puppet::FileServerError) {
95
 
            server.mount("/tmp", "")
96
 
        }
97
 
    end
98
 
 
99
 
    # verify that listing the root behaves as expected
100
 
    def test_listroot
101
 
        server = nil
102
 
        testdir, pattern, tmpfile = mktestdir()
103
 
 
104
 
        file = nil
105
 
        checks = Puppet::Server::FileServer::CHECKPARAMS
106
 
 
107
 
        # and make our fileserver
108
 
        assert_nothing_raised {
109
 
            server = Puppet::Server::FileServer.new(
110
 
                :Local => true,
111
 
                :Config => false
112
 
            )
113
 
        }
114
 
 
115
 
        # mount the testdir
116
 
        assert_nothing_raised {
117
 
            server.mount(testdir, "test")
118
 
        }
119
 
 
120
 
        # and verify different iterations of 'root' return the same value
121
 
        list = nil
122
 
        assert_nothing_raised {
123
 
            list = server.list("/test/", :ignore, true, false)
124
 
        }
125
 
 
126
 
        assert(list =~ pattern)
127
 
 
128
 
        assert_nothing_raised {
129
 
            list = server.list("/test", :ignore, true, false)
130
 
        }
131
 
        assert(list =~ pattern)
132
 
 
133
 
    end
134
 
 
135
 
    # test listing individual files
136
 
    def test_getfilelist
137
 
        server = nil
138
 
        testdir, pattern, tmpfile = mktestdir()
139
 
 
140
 
        file = nil
141
 
 
142
 
        assert_nothing_raised {
143
 
            server = Puppet::Server::FileServer.new(
144
 
                :Local => true,
145
 
                :Config => false
146
 
            )
147
 
        }
148
 
 
149
 
        assert_nothing_raised {
150
 
            server.mount(testdir, "test")
151
 
        }
152
 
 
153
 
        # get our listing
154
 
        list = nil
155
 
        sfile = "/test/tmpfile"
156
 
        assert_nothing_raised {
157
 
            list = server.list(sfile, :ignore, true, false)
158
 
        }
159
 
 
160
 
        assert_nothing_raised {
161
 
            file = Puppet.type(:file)[tmpfile]
162
 
        }
163
 
 
164
 
        output = "/\tfile"
165
 
 
166
 
        # verify it got listed as a file
167
 
        assert_equal(output, list)
168
 
 
169
 
        # verify we got all fields
170
 
        assert(list !~ /\t\t/)
171
 
 
172
 
        # verify that we didn't get the directory itself
173
 
        list.split("\n").each { |line|
174
 
            assert(line !~ %r{remotefile})
175
 
        }
176
 
 
177
 
        # and then verify that the contents match
178
 
        contents = File.read(tmpfile)
179
 
 
180
 
        ret = nil
181
 
        assert_nothing_raised {
182
 
            ret = server.retrieve(sfile)
183
 
        }
184
 
 
185
 
        assert_equal(contents, ret)
186
 
    end
187
 
 
188
 
    # check that the fileserver is seeing newly created files
189
 
    def test_seenewfiles
190
 
        server = nil
191
 
        testdir, pattern, tmpfile = mktestdir()
192
 
 
193
 
 
194
 
        newfile = File.join(testdir, "newfile")
195
 
 
196
 
        # go through the whole schtick again...
197
 
        file = nil
198
 
        checks = Puppet::Server::FileServer::CHECKPARAMS
199
 
 
200
 
        assert_nothing_raised {
201
 
            server = Puppet::Server::FileServer.new(
202
 
                :Local => true,
203
 
                :Config => false
204
 
            )
205
 
        }
206
 
 
207
 
        assert_nothing_raised {
208
 
            server.mount(testdir, "test")
209
 
        }
210
 
 
211
 
        list = nil
212
 
        sfile = "/test/"
213
 
        assert_nothing_raised {
214
 
            list = server.list(sfile, :ignore, true, false)
215
 
        }
216
 
 
217
 
        # create the new file
218
 
        File.open(newfile, "w") { |f|
219
 
            3.times { f.puts rand(100) }
220
 
        }
221
 
 
222
 
        newlist = nil
223
 
        assert_nothing_raised {
224
 
            newlist = server.list(sfile, :ignore, true, false)
225
 
        }
226
 
 
227
 
        # verify the list has changed
228
 
        assert(list != newlist)
229
 
 
230
 
        # and verify that we are specifically seeing the new file
231
 
        assert(newlist =~ /newfile/)
232
 
    end
233
 
 
234
 
    # verify we can mount /, which is what local file servers will
235
 
    # normally do
236
 
    def test_zmountroot
237
 
        server = nil
238
 
        assert_nothing_raised {
239
 
            server = Puppet::Server::FileServer.new(
240
 
                :Local => true,
241
 
                :Config => false
242
 
            )
243
 
        }
244
 
 
245
 
        assert_nothing_raised {
246
 
            server.mount("/", "root")
247
 
        }
248
 
 
249
 
        testdir, pattern, tmpfile = mktestdir()
250
 
 
251
 
        list = nil
252
 
        assert_nothing_raised {
253
 
            list = server.list("/root/" + testdir, :ignore, true, false)
254
 
        }
255
 
 
256
 
        assert(list =~ pattern)
257
 
        assert_nothing_raised {
258
 
            list = server.list("/root" + testdir, :ignore, true, false)
259
 
        }
260
 
 
261
 
        assert(list =~ pattern)
262
 
    end
263
 
 
264
 
    # verify that we're correctly recursing the right number of levels
265
 
    def test_recursionlevels
266
 
        server = nil
267
 
        assert_nothing_raised {
268
 
            server = Puppet::Server::FileServer.new(
269
 
                :Local => true,
270
 
                :Config => false
271
 
            )
272
 
        }
273
 
 
274
 
        # make our deep recursion
275
 
        basedir = File.join(tmpdir(), "recurseremotetesting")
276
 
        testdir = "%s/with/some/sub/directories/for/the/purposes/of/testing" % basedir
277
 
        oldfile = File.join(testdir, "oldfile")
278
 
        assert_nothing_raised {
279
 
            system("mkdir -p %s" % testdir)
280
 
            File.open(oldfile, "w") { |f|
281
 
                3.times { f.puts rand(100) }
282
 
            }
283
 
            @@tmpfiles << basedir
284
 
        }
285
 
 
286
 
        assert_nothing_raised {
287
 
            server.mount(basedir, "test")
288
 
        }
289
 
 
290
 
        # get our list
291
 
        list = nil
292
 
        assert_nothing_raised {
293
 
            list = server.list("/test/with", :ignore, false, false)
294
 
        }
295
 
 
296
 
        # make sure we only got one line, since we're not recursing
297
 
        assert(list !~ /\n/)
298
 
 
299
 
        # for each level of recursion, make sure we get the right list
300
 
        [0, 1, 2].each { |num|
301
 
            assert_nothing_raised {
302
 
                list = server.list("/test/with", :ignore, num, false)
303
 
            }
304
 
 
305
 
            count = 0
306
 
            while list =~ /\n/
307
 
                list.sub!(/\n/, '')
308
 
                count += 1
309
 
            end
310
 
            assert_equal(num, count)
311
 
        }
312
 
    end
313
 
 
314
 
    # verify that we're not seeing the dir we ask for; i.e., that our
315
 
    # list is relative to that dir, not it's parent dir
316
 
    def test_listedpath
317
 
        server = nil
318
 
        assert_nothing_raised {
319
 
            server = Puppet::Server::FileServer.new(
320
 
                :Local => true,
321
 
                :Config => false
322
 
            )
323
 
        }
324
 
 
325
 
 
326
 
        # create a deep dir
327
 
        basedir = tempfile()
328
 
        testdir = "%s/with/some/sub/directories/for/testing" % basedir
329
 
        oldfile = File.join(testdir, "oldfile")
330
 
        assert_nothing_raised {
331
 
            system("mkdir -p %s" % testdir)
332
 
            File.open(oldfile, "w") { |f|
333
 
                3.times { f.puts rand(100) }
334
 
            }
335
 
            @@tmpfiles << basedir
336
 
        }
337
 
 
338
 
        # mounty mounty
339
 
        assert_nothing_raised {
340
 
            server.mount(basedir, "localhost")
341
 
        }
342
 
 
343
 
        list = nil
344
 
        # and then check a few dirs
345
 
        assert_nothing_raised {
346
 
            list = server.list("/localhost/with", :ignore, false, false)
347
 
        }
348
 
 
349
 
        assert(list !~ /with/)
350
 
 
351
 
        assert_nothing_raised {
352
 
            list = server.list("/localhost/with/some/sub", :ignore, true, false)
353
 
        }
354
 
 
355
 
        assert(list !~ /sub/)
356
 
    end
357
 
 
358
 
    # test many dirs, not necessarily very deep
359
 
    def test_widelists
360
 
        server = nil
361
 
        assert_nothing_raised {
362
 
            server = Puppet::Server::FileServer.new(
363
 
                :Local => true,
364
 
                :Config => false
365
 
            )
366
 
        }
367
 
 
368
 
        basedir = tempfile()
369
 
        dirs = %w{a set of directories}
370
 
        assert_nothing_raised {
371
 
            Dir.mkdir(basedir)
372
 
            dirs.each { |dir|
373
 
                Dir.mkdir(File.join(basedir, dir))
374
 
            }
375
 
            @@tmpfiles << basedir
376
 
        }
377
 
 
378
 
        assert_nothing_raised {
379
 
            server.mount(basedir, "localhost")
380
 
        }
381
 
 
382
 
        list = nil
383
 
        assert_nothing_raised {
384
 
            list = server.list("/localhost/", :ignore, 1, false)
385
 
        }
386
 
        assert_instance_of(String, list, "Server returned %s instead of string")
387
 
        list = list.split("\n")
388
 
 
389
 
        assert_equal(dirs.length + 1, list.length)
390
 
    end
391
 
 
392
 
    # verify that 'describe' works as advertised
393
 
    def test_describe
394
 
        server = nil
395
 
        testdir = tstdir()
396
 
        files = mktestfiles(testdir)
397
 
 
398
 
        file = nil
399
 
        checks = Puppet::Server::FileServer::CHECKPARAMS
400
 
 
401
 
        assert_nothing_raised {
402
 
            server = Puppet::Server::FileServer.new(
403
 
                :Local => true,
404
 
                :Config => false
405
 
            )
406
 
        }
407
 
 
408
 
        assert_nothing_raised {
409
 
            server.mount(testdir, "test")
410
 
        }
411
 
 
412
 
        # get our list
413
 
        list = nil
414
 
        sfile = "/test/"
415
 
        assert_nothing_raised {
416
 
            list = server.list(sfile, :ignore, true, false)
417
 
        }
418
 
 
419
 
        # and describe each file in the list
420
 
        assert_nothing_raised {
421
 
            list.split("\n").each { |line|
422
 
                file, type = line.split("\t")
423
 
 
424
 
                desc = server.describe(sfile + file)
425
 
            }
426
 
        }
427
 
 
428
 
        # and then make sure we can describe everything that we know is there
429
 
        files.each { |file|
430
 
            assert_describe(sfile, file, server)
431
 
        }
432
 
 
433
 
        # And then describe some files that we know aren't there
434
 
        retval = nil
435
 
        assert_nothing_raised("Describing non-existent files raised an error") {
436
 
            retval = server.describe(sfile + "noexisties")
437
 
        }
438
 
 
439
 
        assert_equal("", retval, "Description of non-existent files returned a value")
440
 
 
441
 
        # Now try to describe some sources that don't even exist
442
 
        retval = nil
443
 
        assert_raise(Puppet::FileServerError,
444
 
            "Describing non-existent mount did not raise an error") {
445
 
            retval = server.describe("/notmounted/" + "noexisties")
446
 
        }
447
 
 
448
 
        assert_nil(retval, "Description of non-existent mounts returned a value")
449
 
    end
450
 
 
451
 
    # test that our config file is parsing and working as planned
452
 
    def test_configfile
453
 
        server = nil
454
 
        basedir = File.join(tmpdir, "fileserverconfigfiletesting")
455
 
        @@tmpfiles << basedir
456
 
 
457
 
        # make some dirs for mounting
458
 
        Dir.mkdir(basedir)
459
 
        mounts = {}
460
 
        %w{thing thus these those}.each { |dir|
461
 
            path = File.join(basedir, dir)
462
 
            Dir.mkdir(path)
463
 
            mounts[dir] = mktestfiles(path)
464
 
 
465
 
        }
466
 
 
467
 
        # create an example file with each of them
468
 
        conffile = tempfile
469
 
        @@tmpfiles << conffile
470
 
 
471
 
        File.open(conffile, "w") { |f|
472
 
            f.print "# a test config file
473
 
 
474
 
[thing]
475
 
    path #{basedir}/thing
476
 
    allow 192.168.0.*
477
 
 
478
 
[thus]
479
 
    path #{basedir}/thus
480
 
    allow *.madstop.com, *.kanies.com
481
 
    deny *.sub.madstop.com
482
 
 
483
 
[these]
484
 
    path #{basedir}/these
485
 
 
486
 
[those]
487
 
    path #{basedir}/those
488
 
 
489
 
"
490
 
        }
491
 
        
492
 
 
493
 
        # create a server with the file
494
 
        assert_nothing_raised {
495
 
            server = Puppet::Server::FileServer.new(
496
 
                :Local => true,
497
 
                :Config => conffile
498
 
            )
499
 
        }
500
 
 
501
 
        list = nil
502
 
        # run through once with no host/ip info, to verify everything is working
503
 
        mounts.each { |mount, files|
504
 
            mount = "/#{mount}/"
505
 
            assert_nothing_raised {
506
 
                list = server.list(mount, :ignore, true, false)
507
 
            }
508
 
 
509
 
            assert_nothing_raised {
510
 
                list.split("\n").each { |line|
511
 
                    file, type = line.split("\t")
512
 
 
513
 
                    desc = server.describe(mount + file)
514
 
                }
515
 
            }
516
 
 
517
 
            files.each { |f|
518
 
                assert_describe(mount, f, server)
519
 
            }
520
 
        }
521
 
 
522
 
        # now let's check that things are being correctly forbidden
523
 
        # this is just a map of names and expected results
524
 
        {
525
 
            "thing" => {
526
 
                :deny => [
527
 
                    ["hostname.com", "192.168.1.0"],
528
 
                    ["hostname.com", "192.158.0.0"]
529
 
                ],
530
 
                :allow => [
531
 
                    ["hostname.com", "192.168.0.0"],
532
 
                    ["hostname.com", "192.168.0.245"],
533
 
                ]
534
 
            },
535
 
            "thus" => {
536
 
                :deny => [
537
 
                    ["hostname.com", "192.168.1.0"],
538
 
                    ["name.sub.madstop.com", "192.158.0.0"]
539
 
                ],
540
 
                :allow => [
541
 
                    ["luke.kanies.com", "192.168.0.0"],
542
 
                    ["luke.madstop.com", "192.168.0.245"],
543
 
                ]
544
 
            }
545
 
        }.each { |mount, hash|
546
 
            mount = "/#{mount}/"
547
 
 
548
 
            # run through the map
549
 
            hash.each { |type, ary|
550
 
                ary.each { |sub|
551
 
                    host, ip = sub
552
 
 
553
 
                    case type
554
 
                    when :deny:
555
 
                        assert_raise(Puppet::Server::AuthorizationError,
556
 
                            "Host %s, ip %s, allowed %s" %
557
 
                            [host, ip, mount]) {
558
 
                                list = server.list(mount, :ignore, true, false, host, ip)
559
 
                        }
560
 
                    when :allow:
561
 
                        assert_nothing_raised("Host %s, ip %s, denied %s" %
562
 
                            [host, ip, mount]) {
563
 
                                list = server.list(mount, :ignore, true, false, host, ip)
564
 
                        }
565
 
                    end
566
 
                }
567
 
            }
568
 
        }
569
 
 
570
 
    end
571
 
 
572
 
    # Test that we smoothly handle invalid config files
573
 
    def test_configfailures
574
 
        # create an example file with each of them
575
 
        conffile = tempfile()
576
 
 
577
 
        invalidmounts = {
578
 
            "noexist" => "[noexist]
579
 
    path /this/path/does/not/exist
580
 
    allow 192.168.0.*
581
 
"
582
 
}
583
 
 
584
 
        invalidconfigs = [
585
 
"[not valid]
586
 
    path /this/path/does/not/exist
587
 
    allow 192.168.0.*
588
 
",
589
 
"[valid]
590
 
    invalidstatement
591
 
    path /etc
592
 
    allow 192.168.0.*
593
 
",
594
 
"[valid]
595
 
    allow 192.168.0.*
596
 
"
597
 
]
598
 
 
599
 
        invalidmounts.each { |mount, text|
600
 
            File.open(conffile, "w") { |f|
601
 
                f.print text
602
 
            }
603
 
            
604
 
 
605
 
            # create a server with the file
606
 
            server = nil
607
 
            assert_nothing_raised {
608
 
                server = Puppet::Server::FileServer.new(
609
 
                    :Local => true,
610
 
                    :Config => conffile
611
 
                )
612
 
            }
613
 
 
614
 
            assert_raise(Puppet::FileServerError,
615
 
                "Invalid mount was mounted") {
616
 
                    server.list(mount, :ignore)
617
 
            }
618
 
        }
619
 
 
620
 
        invalidconfigs.each_with_index { |text, i|
621
 
            File.open(conffile, "w") { |f|
622
 
                f.print text
623
 
            }
624
 
            
625
 
 
626
 
            # create a server with the file
627
 
            server = nil
628
 
            assert_raise(Puppet::FileServerError,
629
 
                "Invalid config %s did not raise error" % i) {
630
 
                server = Puppet::Server::FileServer.new(
631
 
                    :Local => true,
632
 
                    :Config => conffile
633
 
                )
634
 
            }
635
 
        }
636
 
    end
637
 
 
638
 
    # verify we reread the config file when it changes
639
 
    def test_filereread
640
 
        server = nil
641
 
 
642
 
        conffile = tempfile()
643
 
        dir = tstdir()
644
 
 
645
 
        files = mktestfiles(dir)
646
 
        File.open(conffile, "w") { |f|
647
 
            f.print "# a test config file
648
 
 
649
 
[thing]
650
 
    path #{dir}
651
 
    allow test1.domain.com
652
 
"
653
 
        }
654
 
        
655
 
        # Reset the timeout, so we reload faster
656
 
        Puppet[:filetimeout] = 0.5
657
 
 
658
 
        # start our server with a fast timeout
659
 
        assert_nothing_raised {
660
 
            server = Puppet::Server::FileServer.new(
661
 
                :Local => true,
662
 
                :Config => conffile
663
 
            )
664
 
        }
665
 
 
666
 
        list = nil
667
 
        assert_nothing_raised {
668
 
            list = server.list("/thing/", :ignore, false, false,
669
 
                "test1.domain.com", "127.0.0.1")
670
 
        }
671
 
        assert(list != "", "List returned nothing in rereard test")
672
 
 
673
 
        assert_raise(Puppet::Server::AuthorizationError, "List allowed invalid host") {
674
 
            list = server.list("/thing/", :ignore, false, false,
675
 
                "test2.domain.com", "127.0.0.1")
676
 
        }
677
 
 
678
 
        sleep 1
679
 
        File.open(conffile, "w") { |f|
680
 
            f.print "# a test config file
681
 
 
682
 
[thing]
683
 
    path #{dir}
684
 
    allow test2.domain.com
685
 
"
686
 
        }
687
 
        
688
 
        assert_raise(Puppet::Server::AuthorizationError, "List allowed invalid host") {
689
 
            list = server.list("/thing/", :ignore, false, false,
690
 
                "test1.domain.com", "127.0.0.1")
691
 
        }
692
 
 
693
 
        assert_nothing_raised {
694
 
            list = server.list("/thing/", :ignore, false, false,
695
 
                "test2.domain.com", "127.0.0.1")
696
 
        }
697
 
 
698
 
        assert(list != "", "List returned nothing in rereard test")
699
 
 
700
 
        list = nil
701
 
    end
702
 
 
703
 
    # Verify that we get converted to the right kind of string
704
 
    def test_mountstring
705
 
        mount = nil
706
 
        name = "yaytest"
707
 
        path = tmpdir()
708
 
        assert_nothing_raised {
709
 
            mount = Puppet::Server::FileServer::Mount.new(name, path)
710
 
        }
711
 
 
712
 
        assert_equal("mount[#{name}]", mount.to_s)
713
 
    end
714
 
 
715
 
    def test_servinglinks
716
 
        server = nil
717
 
        source = tempfile()
718
 
        file = File.join(source, "file")
719
 
        link = File.join(source, "link")
720
 
        Dir.mkdir(source)
721
 
        File.open(file, "w") { |f| f.puts "yay" }
722
 
        File.symlink(file, link)
723
 
        assert_nothing_raised {
724
 
            server = Puppet::Server::FileServer.new(
725
 
                :Local => true,
726
 
                :Config => false
727
 
            )
728
 
        }
729
 
 
730
 
        assert_nothing_raised {
731
 
            server.mount(source, "mount")
732
 
        }
733
 
 
734
 
        # First describe the link when following
735
 
        results = {}
736
 
        assert_nothing_raised {
737
 
            server.describe("/mount/link", :follow).split("\t").zip(
738
 
                Puppet::Server::FileServer::CHECKPARAMS
739
 
            ).each { |v,p| results[p] = v }
740
 
        }
741
 
 
742
 
        assert_equal("file", results[:type])
743
 
 
744
 
        # Then not
745
 
        results = {}
746
 
        assert_nothing_raised {
747
 
            server.describe("/mount/link", :ignore).split("\t").zip(
748
 
                Puppet::Server::FileServer::CHECKPARAMS
749
 
            ).each { |v,p| results[p] = v }
750
 
        }
751
 
 
752
 
        assert_equal("link", results[:type])
753
 
 
754
 
        results.each { |p,v|
755
 
            assert(v, "%s has no value" % p)
756
 
            assert(v != "", "%s has no value" % p)
757
 
        }
758
 
    end
759
 
 
760
 
    # Test that substitution patterns in the path are exapanded
761
 
    # properly.  Disabled, because it was testing too much of the process
762
 
    # and in a non-portable way.  This is a thorough enough test that it should
763
 
    # be kept, but it should be done in a way that is clearly portable (e.g.,
764
 
    # no md5 sums of file paths).
765
 
    def test_host_specific
766
 
        client1 = "client1.example.com"
767
 
        client2 = "client2.example.com"
768
 
        ip = "127.0.0.1"
769
 
 
770
 
        # Setup a directory hierarchy for the tests
771
 
        fsdir = File.join(tmpdir(), "host-specific")
772
 
        @@tmpfiles << fsdir
773
 
        hostdir = File.join(fsdir, "host")
774
 
        fqdndir = File.join(fsdir, "fqdn")
775
 
        client1_hostdir = File.join(hostdir, "client1")
776
 
        client2_fqdndir = File.join(fqdndir, client2)
777
 
        contents = {
778
 
            client1_hostdir => "client1\n",
779
 
            client2_fqdndir => client2 + "\n"
780
 
        }
781
 
        [fsdir, hostdir, fqdndir, 
782
 
         client1_hostdir, client2_fqdndir].each { |d|  Dir.mkdir(d) }
783
 
        
784
 
        [client1_hostdir, client2_fqdndir].each do |d|
785
 
            File.open(File.join(d, "file.txt"), "w") do |f| 
786
 
                f.print contents[d] 
787
 
            end
788
 
        end
789
 
        conffile = tempfile()
790
 
        File.open(conffile, "w") do |f|
791
 
            f.print("
792
 
[host]
793
 
path #{hostdir}/%h
794
 
allow *
795
 
[fqdn]
796
 
path #{fqdndir}/%H
797
 
allow *
798
 
")
799
 
        end
800
 
 
801
 
        server = nil
802
 
        assert_nothing_raised {
803
 
            server = Puppet::Server::FileServer.new(
804
 
                :Local => true,
805
 
                :Config => conffile
806
 
            )
807
 
        }
808
 
 
809
 
        # check that list returns the correct thing for the two clients
810
 
        list = nil
811
 
        sfile = "/host/file.txt"
812
 
        assert_nothing_raised {
813
 
            list = server.list(sfile, :ignore, true, false, client1, ip)
814
 
        }
815
 
        assert_equal("/\tfile", list)
816
 
        assert_nothing_raised {
817
 
            list = server.list(sfile, :ignore, true, false, client2, ip)
818
 
        }
819
 
        assert_equal("", list)
820
 
 
821
 
        sfile = "/fqdn/file.txt"
822
 
        assert_nothing_raised {
823
 
            list = server.list(sfile, :ignore, true, false, client1, ip)
824
 
        }
825
 
        assert_equal("", list)
826
 
        assert_nothing_raised {
827
 
            list = server.list(sfile, :ignore, true, false, client2, ip)
828
 
        }
829
 
        assert_equal("/\tfile", list)
830
 
 
831
 
        # check describe
832
 
        sfile = "/host/file.txt"
833
 
        assert_nothing_raised {
834
 
            list = server.describe(sfile, :ignore, client1, ip).split("\t")
835
 
        }
836
 
        assert_equal(5, list.size)
837
 
        assert_equal("file", list[1])
838
 
        md5 = Digest::MD5.hexdigest(contents[client1_hostdir])
839
 
        assert_equal("{md5}#{md5}", list[4])
840
 
 
841
 
        assert_nothing_raised {
842
 
            list = server.describe(sfile, :ignore, client2, ip).split("\t")
843
 
        }
844
 
        assert_equal([], list)
845
 
 
846
 
        sfile = "/fqdn/file.txt"
847
 
        assert_nothing_raised {
848
 
            list = server.describe(sfile, :ignore, client1, ip).split("\t")
849
 
        }
850
 
        assert_equal([], list)
851
 
 
852
 
        assert_nothing_raised {
853
 
            list = server.describe(sfile, :ignore, client2, ip).split("\t")
854
 
        }
855
 
        assert_equal(5, list.size)
856
 
        assert_equal("file", list[1])
857
 
        md5 = Digest::MD5.hexdigest(contents[client2_fqdndir])
858
 
        assert_equal("{md5}#{md5}", list[4])
859
 
 
860
 
        # Check retrieve
861
 
        sfile = "/host/file.txt"
862
 
        assert_nothing_raised {
863
 
            list = server.retrieve(sfile, :ignore, client1, ip).chomp
864
 
        }
865
 
        assert_equal(contents[client1_hostdir].chomp, list)
866
 
 
867
 
        assert_nothing_raised {
868
 
            list = server.retrieve(sfile, :ignore, client2, ip).chomp
869
 
        }
870
 
        assert_equal("", list)
871
 
 
872
 
        sfile = "/fqdn/file.txt"
873
 
        assert_nothing_raised {
874
 
            list = server.retrieve(sfile, :ignore, client1, ip).chomp
875
 
        }
876
 
        assert_equal("", list)
877
 
 
878
 
        assert_nothing_raised {
879
 
            list = server.retrieve(sfile, :ignore, client2, ip).chomp
880
 
        }
881
 
        assert_equal(contents[client2_fqdndir].chomp, list)
882
 
    end
883
 
 
884
 
    # Make sure the 'subdir' method in Mount works.
885
 
    def test_mount_subdir
886
 
        mount = nil
887
 
        base = tempfile()
888
 
        Dir.mkdir(base)
889
 
        subdir = File.join(base, "subdir")
890
 
        Dir.mkdir(subdir)
891
 
        [base, subdir].each do |d|
892
 
            File.open(File.join(d, "file"), "w") { |f| f.puts "bazoo" }
893
 
        end
894
 
        mount = mkmount(base)
895
 
 
896
 
        assert_equal(base, mount.subdir(), "Did not default to base path")
897
 
        assert_equal(subdir, mount.subdir("subdir"), "Did not default to base path")
898
 
    end
899
 
 
900
 
    # Make sure mounts get correctly marked expandable or not, depending on
901
 
    # the path.
902
 
    def test_expandable
903
 
        name = "yaytest"
904
 
        dir = tempfile()
905
 
        Dir.mkdir(dir)
906
 
 
907
 
        mount = mkmount()
908
 
        assert_nothing_raised {
909
 
            mount.path = dir
910
 
        }
911
 
 
912
 
        assert(! mount.expandable?, "Mount incorrectly called expandable")
913
 
 
914
 
        assert_nothing_raised {
915
 
            mount.path = "/dir/a%a"
916
 
        }
917
 
        assert(mount.expandable?, "Mount not called expandable")
918
 
 
919
 
        # This isn't a valid replacement pattern, so it should throw an error
920
 
        # because the dir doesn't exist
921
 
        assert_raise(Puppet::FileServerError) {
922
 
            mount.path = "/dir/a%"
923
 
        }
924
 
 
925
 
        # Now send it back to a normal path
926
 
        assert_nothing_raised {
927
 
            mount.path = dir
928
 
        }
929
 
        # Make sure it got reverted
930
 
        assert(! mount.expandable?, "Mount incorrectly called expandable")
931
 
 
932
 
 
933
 
    end
934
 
 
935
 
    def test_mount_expand
936
 
        mount = mkmount()
937
 
 
938
 
        check = proc do |client, pattern, repl|
939
 
            path = "/my/#{pattern}/file"
940
 
            assert_equal("/my/#{repl}/file", mount.expand(path, client))
941
 
        end
942
 
 
943
 
        # Do a round of checks with a fake client
944
 
        client = "host.domain.com"
945
 
        {"%h" => "host", # Short name
946
 
         "%H" => client, # Full name
947
 
         "%d" => "domain.com", # domain
948
 
         "%%" => "%", # escape
949
 
         "%o" => "%o" # other
950
 
         }.each do |pat, repl|
951
 
             result = check.call(client, pat, repl)
952
 
         end
953
 
 
954
 
        # Now, check that they use Facter info 
955
 
        Puppet.notice "The following messages are normal"
956
 
        client = nil
957
 
        local = Facter["hostname"].value
958
 
        domain = Facter["domain"].value
959
 
        fqdn = [local, domain].join(".")
960
 
        {"%h" => local, # Short name
961
 
         "%H" => fqdn, # Full name
962
 
         "%d" => domain, # domain
963
 
         "%%" => "%", # escape
964
 
         "%o" => "%o" # other
965
 
         }.each do |pat, repl|
966
 
             check.call(client, pat, repl)
967
 
         end
968
 
 
969
 
    end
970
 
 
971
 
    def test_fileserver_expansion
972
 
        server = nil
973
 
        assert_nothing_raised {
974
 
            server = Puppet::Server::FileServer.new(
975
 
                :Local => true,
976
 
                :Config => false
977
 
            )
978
 
        }
979
 
 
980
 
        dir = tempfile()
981
 
        ip = Facter.value(:ipaddress)
982
 
 
983
 
        Dir.mkdir(dir)
984
 
        host = "host.domain.com"
985
 
        {
986
 
            "%H" => "host.domain.com", "%h" => "host", "%d" => "domain.com"
987
 
        }.each do |pattern, string|
988
 
            file = File.join(dir, string)
989
 
            mount = File.join(dir, pattern)
990
 
            File.open(file, "w") do |f| f.puts "yayness: %s" % string end
991
 
            name = "name"
992
 
            obj = nil
993
 
            assert_nothing_raised {
994
 
                obj = server.mount(mount, name)
995
 
            }
996
 
            obj.allow "*"
997
 
 
998
 
            ret = nil
999
 
            assert_nothing_raised do
1000
 
                ret = server.list("/name", :ignore, false, false, host, ip)
1001
 
            end
1002
 
 
1003
 
            assert_equal("/\tfile", ret)
1004
 
 
1005
 
            assert_nothing_raised do
1006
 
                ret = server.describe("/name", :ignore, host, ip)
1007
 
            end
1008
 
            assert(ret =~ /\tfile\t/, "Did not get valid a description")
1009
 
 
1010
 
            assert_nothing_raised do
1011
 
                ret = server.retrieve("/name", :ignore, host, ip)
1012
 
            end
1013
 
 
1014
 
            assert_equal(ret, File.read(file))
1015
 
 
1016
 
            server.umount(name)
1017
 
 
1018
 
            File.unlink(file)
1019
 
        end
1020
 
    end
1021
 
end
1022
 
 
1023
 
# $Id: fileserver.rb 1793 2006-10-16 22:01:40Z luke $
1024